Event Programming: Custom Callback function names !!

Discussion in 'C++' started by PSN, May 5, 2010.

  1. PSN

    PSN Guest

    Hi all,
    Here goes my code for events !!

    class CEventArgs;
    typedef void (*EventHandler)(CEventArgs& eventArgs);

    //#define CALLBACK(class_name, func)

    class CEventArgs
    {
    public:
    CEventArgs() {}
    ~CEventArgs() {}
    };


    class CEvent
    {
    private:
    uint32 uiEventID;
    const int8* pi8EventName;
    int8 i8NumRegistered;

    public:
    // Construction and destruction !!
    CEvent(uint32 id, const int8* _name);
    ~CEvent();

    public:
    int8 NumRegistered()const;
    void Register(EventHandler pEventHandler);
    void Unregister(EventHandler pEventHandler);
    void CallHandlers(CEventArgs &arg)
    {
    for(int i=0; i<vecEventHandlers.size(); i++)
    (*(vecEventHandlers.at(i)))(arg);
    }

    public:
    std::vector<EventHandler> vecEventHandlers;
    };


    class A
    {
    public:
    CEvent Event_Update;

    void Random_Func()
    {
    // Does something and calls all the registered
    functions !!!
    }
    };

    class B
    {
    public:
    static void printB (CEventArgs& arg);
    };

    class C
    {
    public:
    static void printC (CEventArgs& arg);
    };

    class D
    {
    public:
    static void printD (CEventArgs& arg);
    };


    int main()
    {
    A objA;
    B objB;
    C objC;
    D objD;

    objA.Event_Update.Register(&B::printB);
    objA.Event_Update.Register(&C::printC);
    objA.Event_Update.Register(&D::printD);

    objA.Random_Func();

    return 0;
    }

    In the above code, everything seems great, and everything works out
    very well, except that I have to make all the callback functions
    static. Is there any other way to implement it without having to
    declare them static, so that I can register any function name that
    sticks to the function definition !!

    Thanks for your time !!!

    Regards,
    P.
    PSN, May 5, 2010
    #1
    1. Advertising

  2. PSN

    Puppet_Sock Guest

    On May 5, 4:14 pm, PSN <> wrote:
    [snips]
    > In the above code, everything seems great, and everything works out
    > very well, except that I have to make all the callback functions
    > static. Is there any other way to implement it without having to
    > declare them static, so that I can register any function name that
    > sticks to the function definition !!


    This is an evergreen question. You should be looking
    in the FAQ.

    Recall that an object in C++ has a this pointer, and
    that non-static member functions have an implied this
    value that goes with them. If you want a specific
    instance of a class to do the work of a callback,
    you have to tell the calling code which instance.
    That means you need to do something about passing
    the information that identifies the specific instance
    to the calling routine.

    One pattern is to have the calling code get passed
    a pointer to an instance of a handler class, and
    have a defined interface for the handler class.
    This likely happens at a registration step.

    Another is to pass the calling code some kind of
    identifier. The calling code then includes this in
    its call to the callback, which is static. Then
    the callback looks up the specific instance in
    some kind of data set (an STL container hopefully)
    and calls the specific instance.

    There are several other possible patterns, but
    hopefully you get the idea from this. Which you
    choose will depend to some extent how much
    control you have over the code that calls the
    callback routine.
    Socks
    Puppet_Sock, May 5, 2010
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. prettysmurfed
    Replies:
    6
    Views:
    695
    prettysmurfed
    Jul 22, 2003
  2. Ares Lagae
    Replies:
    8
    Views:
    448
    Ares Lagae
    Sep 24, 2004
  3. News123
    Replies:
    2
    Views:
    463
    John Machin
    Nov 26, 2008
  4. Stephen Kellett
    Replies:
    2
    Views:
    186
    Stephen Kellett
    Dec 12, 2004
  5. Replies:
    3
    Views:
    118
Loading...

Share This Page