简体   繁体   中英

Container for pointers to member functions with different arguments

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

Use std::function<void()> to store the calls and then use a variadic template argument to forward and bind the function parameters:

class Clock
{
    vector<function<void()>> tasks;

    template<class F, class... Args>
    void defer(F f, Args&&... args)
    {
        tasks.push_back(bind(f, forward<Args>(args)...);
    }

}

void f(A a, B b);

int main()
{
    A a;
    B b;

    Clock c;
    c.push_back(f, a, b);
}

see also std::bind and std::mem_fun

In C++11, store std::function<void()> . You can use std::bind to create the function from one of a different signature, for example:

std::vector<std::function<void()>> deferred;
deferred.push_back(std::bind(&Class1::action1, this, arg1, arg2));

// later...
for (auto f : deferred) f();

If C++11 isn't an option, then Boost has very similar function and bind templates. I think they might also exist in TR1, although I don't have any historical references to check.

If Boost really isn't an option (and TR1 doesn't provide these), then I strongly recommend that you make it an option; otherwise, use Boost as an example of how to implement this. Without variadic templates, it gets very hairy.

(And since you mention Modern C++ Design, read the section on type lists; that's how you would do it without variadic templates).

Since your callbacks get deferred including their provided arguments, the real signature will be void() , ie the Clock object won't provide arguments on its own and has no need to evaluate the results. So generally you will want to bind member function pointers (or other function pointers) together with the needed arguments and pass the result (a function object) to the clock. This is where boost::bind / std::tr1::bind and boost::function / std::function<void()> come in - or C++11 lambdas:

Clock::defer(time, boost::bind(&Class1::action1, this, arg1, arg2));
//C++11 lambda:
Clock::defer(time, [=](){action1(arg1, arg2);} );

But what you are doing is already done - take a look at Boost.Asio timers:

boost::asio::basic_deadline_timer timer(ioService, expiryTime);
timer.async_wait([=](){action1(arg1, arg2);} //perform action1 when the timer is done

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM