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:rintB);
objA.Event_Update.Register(&C:rintC);
objA.Event_Update.Register(&D:rintD);
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.
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:rintB);
objA.Event_Update.Register(&C:rintC);
objA.Event_Update.Register(&D:rintD);
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.