Call Wrapper Class

G

GCRhoads

I have some templated functions and I want to write a call wrapper
class for it. I
also want to pass the function object from this class to some
functions that will then call the function (forwarded through my class
object to the real function). I've looked around for sources on how
to do this but I can't find anything. How do you do this? I would
also appreciate any references to good sources about this topic if you
have any.

Thanks for your time.
 
W

werasm

I have some templated functions and I want to write a call wrapper
class for it. I
also want to pass the function object from this class to some
functions that will then call the function (forwarded through my class
object to the real function). I've looked around for sources on how
to do this but I can't find anything. How do you do this? I would
also appreciate any references to good sources about this topic if you
have any.

Thanks for your time.

Sounds like what you want to do already exists. boost::function
allows the wrapping of various types of functions, and as far
as I know it is part of tr1 (as I recall). I'm not sure what
you want though (but that is certainly "Call wrappers").

Your question is quite vague. Perhaps add some sourcecode as
starting point.

Werner
 
G

GCRhoads

Sounds like what you want to do already exists. boost::function
allows the wrapping of various types of functions, and as far
as I know it is part of tr1 (as I recall). I'm not sure what
you want though (but that is certainly "Call wrappers").

Your question is quite vague. Perhaps add some sourcecode as
starting point.

Werner

I have several types of templated function objects (the function call
has the same signature)
and I want a wrapper class to be able to pass any of them to other
functions.

For example, the templated function objects are like the following.

template<typename TreeType>
struct Eval1
{
double operator() (const TreeType &tree, ...) {....}
};


template<typename TreeType>
struct Eval2
{
double operator() (const TreeType &tree, ...) {....}
};

The number of function objects like this will grow in the future. Now
I want a wrapper class that can hold any of these function objects and
pass them to other functions.

So I want to complete the following class

template<typename TreeType>
class FunctObj
{
};

so that I can do the following.

Eval1<HierarchyTree> funct1;
Eval2<EnhancedHierarchyTree> funct2;

FunctObj FO; // object of the class I am trying to
write.

Now I want to be able to assign either funct1 or funct2 to FO and pass
FO to a function.
How do I write the class FunctObj so that I can do this?

The code fragment would look like the following.

switch( user_option )
{
case 1:
FO = funct1;
break;

case 2:
FO = funct2;
break;

case 3:
//etc.
}

my_function( FO, .... );

-- Glenn
 
T

terminator

I have several types of templated function objects (the function call
has the same signature)
and I want a wrapper class to be able to pass any of them to other
functions.

For example, the templated function objects are like the following.

template<typename TreeType>
struct Eval1
{
double operator() (const TreeType &tree, ...) {....}
};

template<typename TreeType>
struct Eval2
{
double operator() (const TreeType &tree, ...) {....}
};

The number of function objects like this will grow in the future. Now
I want a wrapper class that can hold any of these function objects and
pass them to other functions.

So I want to complete the following class

template<typename TreeType>
class FunctObj
{
};

so that I can do the following.

Eval1<HierarchyTree> funct1;
Eval2<EnhancedHierarchyTree> funct2;

FunctObj FO; // object of the class I am trying to
write.

Now I want to be able to assign either funct1 or funct2 to FO and pass
FO to a function.
How do I write the class FunctObj so that I can do this?

The code fragment would look like the following.

switch( user_option )
{
case 1:
FO = funct1;
break;

case 2:
FO = funct2;
break;

case 3:
//etc.
}
make FuncObj the abstract base to all other templates and use a
pointer inside the switch block instead of an object.

regards,
FM.
 
G

GCRhoads

make FuncObj the abstract base to all other templates and use a
pointer inside the switch block instead of an object.

regards,
FM.

Thanks.

Since all the templated function objects have two arguments I was able
to get it to work using the STL.


#include <functional>

For the function I want to pass other functions into, I changed its
signature to

template<class BinaryFunc>
void MyFunct( BinaryFunc F, .... )
{
...
= F( ... );
...
}


inside the switch, I call this passing the various templated function
objects as the "BinaryFunc" object. There may be a prettier way but
it does work.
 
T

terminator

Thanks.

Since all the templated function objects have two arguments I was able
to get it to work using the STL.

#include <functional>

For the function I want to pass other functions into, I changed its
signature to

template<class BinaryFunc>
void MyFunct( BinaryFunc F, .... )
{
...
= F( ... );
...
}

in the absence of the switch statement this works fine you can use
elseif construrct:

if (user_opt = opt1)
MyFunct(.....);
else if (user_opt = opt2)
MyFunct(.....);
else if (user_opt = opt3)
MyFunct(.....);
..
..
..
else
MyFunct(.....);

regards,
FM
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,996
Messages
2,570,238
Members
46,826
Latest member
robinsontor

Latest Threads

Top