[boost::function] Is there a way to assign a generic function, regardless number of parameters?

Hello list, My doubt is simple. We need to implement some callbacks, but we don't want to declare a boost::function to every different callback, we just want to declare one generic boost::function to receive any function, with any number of parameters (the return type may be one well defined, no problem with that in our case). Is there a way to do this? What we want to do is something like is done in the asynchronous methods of boost::asio when passing the callback to the async operation. TIA, -- Matheus Araújo Aguiar Computer Scientist matheus.pit@gmail.com

What we want to do is something like is done in the asynchronous methods of boost::asio when passing the callback to the async operation.
In asio the callback (i.e. completion handler) has very specific prototype, it does not accept "any number of parameters".

On Thu, Jan 28, 2010 at 12:48 PM, Igor R
What we want to do is something like is done in the asynchronous methods of boost::asio when passing the callback to the async operation.
In asio the callback (i.e. completion handler) has very specific prototype, it does not accept "any number of parameters".
True. What i was trying to exemplify was the behavior not the implementation itself. One case that exemplifies what I was trying to say, is that i can declare: *void handler1( ); void handler2(unsigned int i1, unsigned int i2, float f, char* c);* and pass both handlers to an async function of asio and have no problem. That's the behavior we would like to see: We pass different functions, with different prototypes, and without declaring a specific function to receive each different function. It would be as in: * * */* some code doing stuff ... */ * * schedule_callback(boost::bind(callback1, parameter1, parameter2)); * */* ... do some more things ... */ schedule_callback(boost::bind(&FooClass::method1, FooInstancePointer, param1)); /* more code ... */ schedule_callback(boost::bind(callback2, param1));* Would there be a way of implementing this kind of thing with boost::function, declaring only one generic function *schedule_callback *wich receives different functions with different arguments? Thank you for your attention,
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
-- Matheus Araújo Aguiar Computer Scientist matheus.pit@gmail.com

void handler1( ); void handler2(unsigned int i1, unsigned int i2, float f, char* c);
and pass both handlers to an async function of asio and have no problem.
How can you pass both handlers to an async function of asio? Maybe, you can create a "binder" based on these functions, but the resulting functor should have the number of arguments required by asio concepts.
/* some code doing stuff ... */ schedule_callback(boost::bind(callback1, parameter1, parameter2)); /* ... do some more things ... */ schedule_callback(boost::bind(&FooClass::method1, FooInstancePointer, param1)); /* more code ... */ schedule_callback(boost::bind(callback2, param1));
Actually, the question is: *who* will set parameter1, parameter2 etc?
If you know the values of these arguments in the place where you call
schedule_callback, and the callback invoker should not set pass any
arguments to your handler, then the prototype of your handler should
be: void(void). Then you can do something like this:
void shedule_callback(function

On Thu, Jan 28, 2010 at 2:22 PM, Igor R
void handler1( ); void handler2(unsigned int i1, unsigned int i2, float f, char* c);
and pass both handlers to an async function of asio and have no problem.
How can you pass both handlers to an async function of asio? Maybe, you can create a "binder" based on these functions, but the resulting functor should have the number of arguments required by asio concepts.
/* some code doing stuff ... */ schedule_callback(boost::bind(callback1, parameter1, parameter2)); /* ... do some more things ... */ schedule_callback(boost::bind(&FooClass::method1, FooInstancePointer, param1)); /* more code ... */ schedule_callback(boost::bind(callback2, param1));
Actually, the question is: *who* will set parameter1, parameter2 etc? If you know the values of these arguments in the place where you call schedule_callback, and the callback invoker should not set pass any arguments to your handler, then the prototype of your handler should be: void(void). Then you can do something like this:
void shedule_callback(function
); void handler1(int, int, std::string); //... schedule_callback(bind(&handler1, 5, 7, "hello")); But if the callback inoker should pass some parameters to your handler, the handler prototype should reflect this fact.
Now i understand it. I was messing up functions with binders. Thanks for your patience and good will Igor!
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
-- Matheus Araújo Aguiar Computer Scientist matheus.pit@gmail.com

Matheus Araújo Aguiar wrote:
We need to implement some callbacks, but we don't want to declare a boost::function to every different callback,
When you say "callback," I assume that some particular place in your code is calling the passed boost::function. That call is passing some particular set of arguments. Why not declare a boost::function with a signature matching the call, so the compiler can perform appropriate conversions?
participants (3)
-
Igor R
-
Matheus Araújo Aguiar
-
Nat Goodspeed