本文是上一篇文章中实现代码的具体使用实例。
- #include "MultiCB.h"
- using namespace std;
- using namespace MultiCB;
- class EventArgs
- {
- public:
- EventArgs()
- {
- Name = "EventArgs";
- }
- string Name;
- void TestMemFunc(EventArgs* arg)
- {
- cout << arg->Name.c_str() << endl;
- }
- };
- class EvtArgs2 : public EventArgs
- {
- public:
- EvtArgs2()
- {
- Name = "EvtArgs2";
- }
- };
- void TestGlbFunc1(EventArgs* arg)
- {
- cout << "TestGlbFunc1: " << arg->Name.c_str() << endl;
- }
- void TestGlbFunc2(EventArgs* arg)
- {
- cout << "TestGlbFunc2: " << arg->Name.c_str() << endl;
- }
- class StaticClass
- {
- public:
- static void Func(EventArgs* arg)
- {
- cout << "StaticClass::Func: " << arg->Name.c_str() << endl;
- }
- };
- typedef void(*GlbFuncType)(EventArgs*);
- void TestStaticCallBack()
- {
- cout << "Test the StaticCallBack" << endl;
- StaticCallBack<GlbFuncType, EventArgs> stEvent;
- EventArgs evtArg;
- stEvent.Raise(&evtArg);
- cout << endl;
- stEvent.Register(&TestGlbFunc1);
- stEvent.Raise(&evtArg);
- cout << endl;
- stEvent.Register(&TestGlbFunc2);
- stEvent.Raise(&evtArg);
- cout << endl;
- stEvent.UnRegister(&TestGlbFunc1);
- stEvent.Raise(&evtArg);
- cout << endl;
- stEvent.Register(&TestGlbFunc1);
- stEvent.Raise(&evtArg);
- cout << endl;
- stEvent.Register(&TestGlbFunc1);
- stEvent.Raise(&evtArg);
- cout << endl;
- EvtArgs2 arg2;
- stEvent -= &TestGlbFunc1;
- stEvent.Raise(&arg2);
- cout << endl;
- stEvent += &TestGlbFunc2;
- stEvent.Raise(&arg2);
- cout << endl;
- stEvent += &TestGlbFunc1;
- stEvent.Raise(&arg2);
- cout << endl;
- stEvent += &StaticClass::Func;
- stEvent.Raise(&arg2);
- cout << endl;
- stEvent.Clear();
- stEvent.Raise(&arg2);
- cout << endl;
- }
- class UserClass1
- {
- public:
- void Func1(EventArgs* arg)
- {
- cout << "UserClass1::Func1 " << arg->Name.c_str() << endl;
- }
- virtual void VirtualFunc(EventArgs* arg)
- {
- cout << "UserClass1::VirtualFunc " << arg->Name.c_str() << endl;
- }
- };
- class UserClass2 : public UserClass1
- {
- public:
- void VirtualFunc(EventArgs* arg)
- {
- cout << "UserClass2::VirtualFunc " << arg->Name.c_str() << endl;
- }
- };
- class UserClass3 : public UserClass2
- {
- public:
- void VirtualFunc(EventArgs* arg)
- {
- cout << "UserClass3::VirtualFunc " << arg->Name.c_str() << endl;
- }
- };
- typedef void (UserClass1::*MemFuncType)(EventArgs* arg);
- void TestClassCallBack()
- {
- typedef ClassCallBack<UserClass1,MemFuncType,EventArgs> Event;
- Event clsEvt;
- Event::Pair pair;
- UserClass1 cls1;
- UserClass2 cls2;
- UserClass3 cls3;
- EventArgs evtArg;
- EvtArgs2 evtArg2;
- pair.func = &UserClass1::Func1;
- pair.obj = &cls1;
- clsEvt.Register(pair);
- clsEvt.Raise(&evtArg);
- cout << endl;
- pair.func = &UserClass1::VirtualFunc;
- pair.obj = &cls1;
- clsEvt.Register(pair);
- clsEvt.Raise(&evtArg);
- cout << endl;
- //pair.func = &UserClass2::VirtualFunc;
- pair.func = &UserClass1::VirtualFunc;
- pair.obj = &cls2;
- clsEvt.Register(pair);
- clsEvt.Raise(&evtArg2);
- cout << endl;
- pair.func = &UserClass1::Func1;
- pair.obj = &cls2;
- clsEvt += pair;
- clsEvt.Raise(&evtArg2);
- cout << endl;
- pair.func = &UserClass1::VirtualFunc;
- pair.obj = &cls1;
- clsEvt -= pair;
- clsEvt.Raise(&evtArg2);
- cout << endl;
- pair.func = &UserClass1::VirtualFunc;
- pair.obj = &cls3;
- clsEvt += pair;
- clsEvt.Raise(&evtArg2);
- cout << endl;
- }