指向具有不同参数的成员函数的指针的容器 [英] Container for pointers to member functions with different arguments

查看:250
本文介绍了指向具有不同参数的成员函数的指针的容器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我到处都是(现代C ++设计和合作),但是我找不到一种好方法来存储一组接受不同参数并在不同类上运行的回调。我需要这样做是因为我希望应用程序的每个对象都可以将其方法之一的执行推迟到主 Clock 对象执行,以跟踪当前对象。时间,可以在适当的时候调用此方法。我想要的代码类似于以下内容:

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:

void executeAction1Deferred(int time,int arg1,bool arg2) class1 的code>方法,其中时间是将来所需的执行时间,应该是这样的:

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);

Clock :: defer(???什么签名???? )代表此任务的对象存储在优先级队列中,其中时间是键。然后,对于每个 Clock 量表,将遍历任务列表,并执行需要在该量表中运行的任务。请注意,我已经将 defer用作静态函数,因为我打算使用单例对象的 Clock 对象,但是它也可以是成员函数,这只是选择的问题。

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.

我曾经考虑过使用 void * 来保留可变数量的参数,但是让我的<$ c接受 void * 的$ c> action1()方法非常糟糕,这也是因为每次我都需要为参数构造一个结构

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.

过去,我多次遇到过这个问题,但是我从未找到过一个不错的解决方案。请注意,由于这是一个小型的多平台项目,因此对于经验不足的程序员来说,简化构建过程以使其扩展很重要,所以我不想使用boost。但是,我们处理的平台的每个编译器都具有 std :: tr1 绑定。问题是:如何定义一个泛型函数的容器,每个泛型函数接受可变数量的参数(最多N〜5个),并且是不是从公共虚类派生的对象的不同成员方法?谢谢

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

推荐答案

使用 std :: function< void()> 存储调用,然后使用可变参数模板参数转发和绑定函数参数:

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);
}

另请参见 std :: bind std :: mem_fun

这篇关于指向具有不同参数的成员函数的指针的容器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆