Code::Blocks  SVN r11506
cbfunctor.h
Go to the documentation of this file.
1 /*
2  * This file is part of the Code::Blocks IDE and licensed under the GNU Lesser General Public License, version 3
3  * http://www.gnu.org/licenses/lgpl-3.0.html
4  */
5 
6 #ifndef CB_FUNCTOR_H
7 #define CB_FUNCTOR_H
8 
9 //uncomment the below line if you want to do the performance measure of the event processing
10 //#define PPRCESS_EVENT_PERFORMANCE_MEASURE
11 
12 #ifdef PPRCESS_EVENT_PERFORMANCE_MEASURE
13 #include <typeinfo> // typeid()
14 #endif // PPRCESS_EVENT_PERFORMANCE_MEASURE
15 
18 {
19  public:
20  virtual ~IFunctorBase(){}
21  virtual void* GetThis() = 0;
22 
23 #ifdef PPRCESS_EVENT_PERFORMANCE_MEASURE
24  // return the function name, this is used for Functor performance measure
25  virtual const char* GetTypeName() = 0;
26 #endif // PPRCESS_EVENT_PERFORMANCE_MEASURE
27 };
28 
30 template<typename EventType> class IEventFunctorBase : public IFunctorBase
31 {
32  public:
33  virtual void Call(EventType& event) = 0;
34 };
35 
37 template<class ClassType, typename EventType> class cbEventFunctor : public IEventFunctorBase<EventType>
38 {
39  private:
40  typedef void (ClassType::*Member)(EventType&);
41  ClassType* m_pThis;
42  Member m_pMember;
43  public:
44  cbEventFunctor(ClassType* this_, Member member) : m_pThis(this_), m_pMember(member) {}
45  cbEventFunctor(const cbEventFunctor<ClassType, EventType>& rhs) : m_pThis(rhs.m_pThis), m_pMember(rhs.m_pMember) {}
46  void* GetThis() override { return m_pThis; }
47  // usually the m_pThis is a pointer the instance of a specified class of ClassType
48  // the m_pMember is a member function of the ClassType, so just call this member function
49  void Call(EventType& event) override { if (m_pThis) (m_pThis->*m_pMember)(event); }
50 
51 #ifdef PPRCESS_EVENT_PERFORMANCE_MEASURE
52  // show the name by typeid operator
53  virtual const char* GetTypeName(){return typeid(m_pMember).name();}
54 #endif // PPRCESS_EVENT_PERFORMANCE_MEASURE
55 };
56 
57 #endif // CB_FUNCTOR_H
Member m_pMember
Definition: cbfunctor.h:42
Base abstract functor class.
Definition: cbfunctor.h:17
cbEventFunctor(ClassType *this_, Member member)
Definition: cbfunctor.h:44
void Call(EventType &event) override
Definition: cbfunctor.h:49
const SQChar * GetTypeName(const SQObjectPtr &obj1)
Definition: sqobject.cpp:43
void * GetThis() override
Definition: cbfunctor.h:46
Event functor class.
Definition: cbfunctor.h:37
Base abstract event functor class.
Definition: cbfunctor.h:30
virtual void * GetThis()=0
virtual ~IFunctorBase()
Definition: cbfunctor.h:20
cbEventFunctor(const cbEventFunctor< ClassType, EventType > &rhs)
Definition: cbfunctor.h:45
ClassType * m_pThis
Definition: cbfunctor.h:41