Event Programming: Custom Callback function names !!

P

PSN

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.
 
P

Puppet_Sock

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,954
Messages
2,570,116
Members
46,704
Latest member
BernadineF

Latest Threads

Top