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 
16 /** Base abstract functor class. All functors must extend this interface. */
17 class IFunctorBase
18 {
19 	public:
~IFunctorBase()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 
29 /** Base abstract event functor class. All event functors must extend this interface.*/
30 template<typename EventType> class IEventFunctorBase : public IFunctorBase
31 {
32 	public:
33 		virtual void Call(EventType& event) = 0;
34 };
35 
36 /** Event functor class. */
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:
cbEventFunctor(ClassType * this_,Member member)44 		cbEventFunctor(ClassType* this_, Member member) : m_pThis(this_), m_pMember(member) {}
cbEventFunctor(const cbEventFunctor<ClassType,EventType> & rhs)45 		cbEventFunctor(const cbEventFunctor<ClassType, EventType>& rhs) : m_pThis(rhs.m_pThis), m_pMember(rhs.m_pMember) {}
GetThis()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
Call(EventType & event)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
GetTypeName()53 		virtual const char* GetTypeName(){return typeid(m_pMember).name();}
54 #endif // PPRCESS_EVENT_PERFORMANCE_MEASURE
55 };
56 
57 #endif // CB_FUNCTOR_H
58