带有额外函数参数的EventDispatcher

时间:2016-07-06 04:33:28

标签: c++ windows c++11

尝试创建一个EventDispatcher,它允许我添加任何带有任何参数的函数,并将参数传递给函数 当我试图用Base*的设置参数调用一个函数时,我有它的工作 我希望你能理解我在这里尝试做什么,我不是最好的解释

EventDispatcher.h

template<typename T>

    class EventDispatcher
    {
    public:
        typedef std::function<T> Event;

        template<typename... Args>
        void Invoke(Args&& ...Params)
        {
            for (auto&& event : m_Events)
            {
                event(std::forward<Args>(Params)...);
            }
        }

        template <typename ...Args>
        void operator+=(Event& event, Args&&... args)
        {
            m_Events.push_back(std::function<void()>(std::bind(std::forward<Event>(event), this, std::forward<Args>(args)...)));
        }

    private:
        std::vector<Event> m_Events;
    };

我的班级调用EventDispatcher

class Base
{
public:
    typedef EventDispatcher<void(Base*)> eCallbacks;

    eCallbacks&                     RenderEvent();

private:
    eCallbacks                      m_eRenderEvent;
};

像这样称呼它

void Event1(Overlay* Base)
{

}
void Event2(Base* Base, int Someint)
{

}

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
    Base base();

    int IntToPass = 12345;

    base.RenderEvent() += Event1;
    base.RenderEvent() += Event2(IntToPass);

    while (base.Render())
    {
    }

    return 1;
}

0 个答案:

没有答案