Assume that I have a boost::function of with an arbitrary signature called type CallbackType.
- Is it possible to use
boost::bindto compose a function that takes the same arguments as the CallbackType but calls the two functors in succession?
I’m open to any potential solution, but here’s a…
…Hypothetical example using some magic template:
Template<typename CallbackType>
class MyClass
{
public:
CallbackType doBoth;
MyClass( CallbackType callback )
{
doBoth = bind( magic<CallbackType>,
protect( bind(&MyClass::alert, this) ),
protect( callback ) );
}
void alert()
{
cout << "It has been called\n";
}
};
void doIt( int a, int b, int c)
{
cout << "Doing it!" << a << b << c << "\n";
}
int main()
{
typedef boost::function<void (int, int, int)> CallbackType;
MyClass<CallbackType> object( boost::bind(doIt) );
object.doBoth();
return 0;
}
Boost already provides a way to create a sequence of bound functions. Use Lambda’s comma operator.
That will create a new functor,
object. When you invoke that functor with three arguments, it will in turn callmc.alert()before passing those arguments todoIt. The parentheses are important.For my example above to work, you’d need
alertto be aconstfunction. If it needs to be non-const, then either pass a pointer tomc, or wrap it withboost::ref(mc). And you’ll need to use Boost.Lambda’sbindrather than Boost.Bind’s; the latter isn’t compatible with Lambda’s function-combining operators (comma, in particular).