I am looking everywhere (Modern C++ design & co) but I can't find a nice way to store a set of callbacks that accept different arguments and operate on different classes. I need this because I would like every object of my application to have the possibility to defer the execution of one of its methods to a master Clock
object that, keeping track of the current time, can call this methods in the right moment. The code I am aiming for is something along the lines of:
In the void executeAction1Deferred(int time, int arg1, bool arg2)
method of class1
, where time is the execution time wanted in the future, there should be something like this:
Clock::defer(time, Class1::action1, this, arg1, arg2);
In Clock::defer(??? what signature ????)
an object that represents this Task is stored in a priority queue where the time is the Key. For every Clock
quantum the list of Tasks is then traversed and the tasks that need to be run in this quantum will be executed. Note that I have used "defer" as a static function because I intend the Clock
object of a singleton, but it could also be a member function, it's just matter of choice.
I have thought of using void*
to keep a variable number of the arguments, but having my action1()
method accepting a void*
is pretty terrible, also because I would need to craft a struct for the argument every time I use this function directly without deferring it.
I have been facing this problems various times in the past, and I have never found a really decent solution. Please note that being this a small multi-platform project where the simplicity of building for the inexperienced programmers that could extend it is essential, I don't want to use boost. But, every compiler for the platforms we address have std::tr1
bind. The question is: how to define a container of generic functions, each of these accepting a variable number of parameters (up to N ~ 5), and being a different member method of objects that do not derive from a common virtual class? Thanks