Reputation:
I'd like to obtain the pointer to the object and an indication of which method the functor will call from a functor constructed using boost::function and boost::bind. This will allow me to automatically determine the order in a which bunch of functors must be executed.
The following (pseudo) code (see POINTER_OF & METHOD_OF) shows what I'm trying to do:
class myClassA
{
public:
DoIt(int i) { return i+i; }
};
class myClassB
{
public:
DoItToo(double d) { return d*d; }
};
typedef boost::function0<int> Functor;
myClassA classA;
myClassB classB;
Functor funcA = boost::bind( &myClassA::DoIt, &classA, 10 );
Functor funcB = boost::bind( &myClassB::DoItToo, &classB, 12.34 );
// Create a vector containing some functors and try to determine the objects
// they are called upon and the methods they invoke
std::vector<Functor> vec;
vec.push_back( funcA );
vec.push_back( funcB );
for (int i = 0; i < vec.size();i++)
{
if (POINTER_OF(vec[i]) == &classA)
{
// This functor acts on classA
if (METHOD_OF(vec[i]) == &myClassA::DoIt)
{
// This functor calls the 'DoIt' method.
}
else if (METHOD_OF(vec[i]) == &myClassB::DoItToo)
{
// This functor calls the 'DoItToo' method.
}
}
// etc...
}
Thanks in advance!
Upvotes: 2
Views: 1136
Reputation: 24892
boost::function
objects are equality comparable, so you should be able to do stuff like
if (vec[i] == boost::bind( &myClassA::DoIt, &classA, 10 ))
{
// ... this Functor calls DoIt on classA with argument 10
}
I suspect you're looking for something more general though. Digging into the implementation details of boost/function_equal.hpp
(ie boost::bind
's function_equal_impl
) might give you some idea how to selectively test a subset of the boost::bind
arguments.
But I really think youd be better off with a polymorphism-based solution, or just aggregating the function objects with some metadata.
Upvotes: 0
Reputation: 6981
No, I don't think you can get the target of a boost function that is set to a boost bind (ie METHOD_OF). According to this post on the boost mailing list this is because the return type of bind is not formally specified.
Edit: Link to an earlier question that is some-what related to this question: demote-boostfunction-to-a-plain-function-pointer
Upvotes: 0
Reputation: 59912
I know that the following is not a strict answer to your question but.
Don't do this. Use polymorphism instead. It is one of the strangest things I saw in my current project code: if function pointer points to "someFunction" - do some extra acctions.
You can add extra behavior without changing your classes much with Decorator design pattern. That will extend your myClassA::DoIt with Decorator::DoIt.
http://en.wikipedia.org/wiki/Decorator_pattern
Upvotes: 4