mardi 5 juillet 2016

EventDispatcher with extra function arguments

Trying to make a EventDispatcher that lets me add any function with any arguments and pass the arguments to the function I had it working when i was just trying to call a function with a set arguments of Base* I hope you just can understand what im trying to do here, im not the best at explaining

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

My class calling the EventDispatcher

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

    eCallbacks&                     RenderEvent();

private:
    eCallbacks                      m_eRenderEvent;
};

call it like this

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

Aucun commentaire:

Enregistrer un commentaire