/******************************************************************** 文件名 : Res_Interface.h 创建人 : Clark/陈泽丹 创建时间 : 2012-4-15 描述: 资源 *********************************************************************/ #pragma once #include <vector> #include "CMD_POLICY.h" using namespace std; using namespace POLICY; template<class T_RES> class Res_Interface { public: static vector< T_RES* > m_vRes; static void G_AddResItem( T_RES* _pItem) { m_vRes.push_back(_pItem); } static void G_DelResItem(const T_RES* _pItem) { for(int i=0; i<m_vRes.size(); i++) { if( m_vRes[i] == _pItem) { m_vRes[i] = m_vRes[m_vRes.size()-1]; m_vRes.pop_back(); return; } } } static T_RES* GetResItem(const T_RES* _pItem) { for(int i=0; i<m_vRes.size(); i++) { if( m_vRes[i] == _pItem) { return m_vRes[i]; } } return NULL; } }; template<class T_RES> vector< T_RES* > Res_Interface< T_RES >::m_vRes;
/******************************************************************** 文件名 : Event_Interface.h 创建人 : Clark/陈泽丹 创建时间 : 2012-4-15 描述: 触发器 *********************************************************************/ #pragma once #include <iostream> #include <vector> #include "Res_Interface.h" using namespace POLICY; using namespace std; template<class T_DATA, class T_CALLBACK> class Event_Interface: public Res_Interface< Event_Interface< T_DATA, T_CALLBACK > > { public: class Trigger { public: virtual ~Trigger() { Event_Interface< T_DATA, T_CALLBACK >* pRes = Event_Interface< T_DATA, T_CALLBACK >::GetResItem(m_pEvt); if( NULL != pRes) { pRes->DelTgr(m_pCallBack); } else { #if _DEBUG cout<<"监控对象已不存在! 无需消除关系"<<endl; #endif } if( NULL != m_pCallBack) { delete m_pCallBack; m_pCallBack = NULL; } } private: template <class PtrObj, class MemFn> Trigger(Event_Interface< T_DATA, T_CALLBACK >* _pEvt, const PtrObj& _p, MemFn _memFn): m_pEvt(_pEvt), m_pCallBack(NULL) { Event_Interface< T_DATA, T_CALLBACK >* pRes = Event_Interface< T_DATA, T_CALLBACK >::GetResItem(m_pEvt); if( NULL != pRes) { m_pCallBack = new T_CALLBACK(_p, _memFn); pRes->AddTgr(m_pCallBack); } } template <class Fun> Trigger(Event_Interface< T_DATA, T_CALLBACK >* _pEvt, Fun _fun): m_pEvt(_pEvt), m_pCallBack(NULL) { Event_Interface< T_DATA, T_CALLBACK >* pRes = Event_Interface< T_DATA, T_CALLBACK >::GetResItem(m_pEvt); if( NULL != pRes) { m_pCallBack = new T_CALLBACK(_fun); pRes->AddTgr(m_pCallBack); } } const Event_Interface< T_DATA, T_CALLBACK >* m_pEvt; T_CALLBACK* m_pCallBack; friend class Event_Interface< T_DATA, T_CALLBACK >; }; //-------------------------------------------------------------------- public: Event_Interface(T_DATA _tgrData):m_tgrData(_tgrData) { Event_Interface< T_DATA, T_CALLBACK >* _pEvt = this; Event_Interface< T_DATA, T_CALLBACK >::G_AddResItem(_pEvt); } virtual ~Event_Interface() { if( m_vTgrCallBack.size() > 0) { #if _DEBUG cout<<"事件器有触发实例未删除!"<<endl; #endif } Event_Interface< T_DATA, T_CALLBACK >::G_DelResItem(this); } virtual void SetData(T_DATA _tgrData) { m_tgrData = _tgrData; //由于删除事件是采用倒替的方案,所以遍历要倒数,以免遗漏 for(int i=m_vTgrCallBack.size()-1; i>=0; i--) { OnEvent(m_vTgrCallBack[i]); } } virtual T_DATA GetData(){ return m_tgrData;} //创建触发器 template <class PtrObj, class MemFn> Trigger* CreateTgr(const PtrObj& _p, MemFn _memFn) { Trigger* pTgr = new Trigger(this, _p, _memFn); return pTgr; } template <class Fun> Trigger* CreateTgr(Fun _fun) { Trigger* pTgr = new Trigger(this, _fun); return pTgr; } private: //触发事件 virtual void OnEvent(T_CALLBACK* _pCallBack) = 0; //添加触发器 bool AddTgr(T_CALLBACK* _pCallBack) { if( NULL != _pCallBack) { m_vTgrCallBack.push_back(_pCallBack); return true; } return false; } //删除触发器 void DelTgr(const T_CALLBACK* _pCallBack) { for(int i=m_vTgrCallBack.size()-1; i>=0; i--) { if( _pCallBack == m_vTgrCallBack[i]) { m_vTgrCallBack[i] = m_vTgrCallBack[m_vTgrCallBack.size()-1]; m_vTgrCallBack.pop_back(); return; } } } //触发值 T_DATA m_tgrData; //事件队列 vector< T_CALLBACK* > m_vTgrCallBack; };
/******************************************************************** 文件名 : TimeEvt.h 创建人 : Clark/陈泽丹 创建时间 : 2012-4-15 描述: 定时触发器 *********************************************************************/ #pragma once #include "Event_Interface.h" class TimeEvt:public Event_Interface< int, Functor< void, TYPELIST_3(int, int, int) > > { public: TimeEvt(const int _iCurTime, const int _iTimeStep): EVT_START_TIME(_iCurTime), Event_Interface< int, Functor< void, TYPELIST_3(int, int, int) > >(_iCurTime), EVT_STEP_TIME(_iTimeStep){} virtual ~TimeEvt(){} private: //触发事件 virtual void OnEvent(Functor< void, TYPELIST_3(int, int, int) >* _pCallBack) { int iCurTime = GetData(); int iTime = (iCurTime - EVT_START_TIME) % EVT_STEP_TIME; if( 0 == iTime) (*_pCallBack)(EVT_START_TIME,EVT_STEP_TIME,iCurTime); } const int EVT_START_TIME; const int EVT_STEP_TIME; };
/******************************************************************** 文件名 : NpcParEvt.h 创建人 : Clark/陈泽丹 创建时间 : 2012-4-15 描述: NPC属性触发器 *********************************************************************/ #pragma once #include "Event_Interface.h" class NpcParEvt:public Event_Interface< int, Functor< void, TYPELIST_1(int) > > { public: NpcParEvt():Event_Interface< int, Functor< void, TYPELIST_1(int) > >(0){} virtual ~NpcParEvt(){} private: //触发事件 virtual void OnEvent(Functor< void, TYPELIST_1(int) >* _pCallBack) { int iCurTime = GetData(); if( iCurTime <= 0) (*_pCallBack)(iCurTime); } };
/******************************************************************** file name : CMD_POLICY.h author : Clark/陈泽丹 created : 2011-11-30 工具包: *********************************************************************/ #ifndef __CLK_POLICY_H__ #define __CLK_POLICY_H__ #define NULL 0 #define TYPELIST_1(T1) \ POLICY::Typelist<T1, ::POLICY::NullType> #define TYPELIST_2(T1, T2) \ POLICY::Typelist<T1, TYPELIST_1(T2) > #define TYPELIST_3(T1, T2, T3) \ POLICY::Typelist<T1, TYPELIST_2(T2, T3) > #define TYPELIST_4(T1, T2, T3, T4) \ POLICY::Typelist<T1, TYPELIST_3(T2, T3, T4) > #define TYPELIST_5(T1, T2, T3, T4, T5) \ POLICY::Typelist<T1, TYPELIST_4(T2, T3, T4, T5) > #define TYPELIST_6(T1, T2, T3, T4, T5, T6) \ POLICY::Typelist<T1, TYPELIST_5(T2, T3, T4, T5, T6) > #define TYPELIST_7(T1, T2, T3, T4, T5, T6, T7) \ POLICY::Typelist<T1, TYPELIST_6(T2, T3, T4, T5, T6, T7) > #define TYPELIST_8(T1, T2, T3, T4, T5, T6, T7, T8) \ POLICY::Typelist<T1, TYPELIST_7(T2, T3, T4, T5, T6, T7, T8) > #define TYPELIST_9(T1, T2, T3, T4, T5, T6, T7, T8, T9) \ POLICY::Typelist<T1, TYPELIST_8(T2, T3, T4, T5, T6, T7, T8, T9) > #define TYPELIST_10(T1, T2, T3, T4, T5, T6, T7, T8, T9, T10) \ POLICY::Typelist<T1, TYPELIST_9(T2, T3, T4, T5, T6, T7, T8, T9, T10) > #define TYPELIST_11(T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11) \ POLICY::Typelist<T1, TYPELIST_10(T2, T3, T4, T5, T6, T7, T8, T9, T10, T11) > #define TYPELIST_12(T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12) \ POLICY::Typelist<T1, TYPELIST_11(T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12) > #define TYPELIST_13(T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13) \ POLICY::Typelist<T1, TYPELIST_12(T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13) > #define TYPELIST_14(T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14) \ POLICY::Typelist<T1, TYPELIST_13(T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14) > #define TYPELIST_15(T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15) \ POLICY::Typelist<T1, TYPELIST_14(T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15) > namespace POLICY { class NullType {}; template <class T, class U> struct Typelist { typedef T Head; typedef U Tail; }; //线程类_为多线程情况的扩展做准备 class DEFAULT_THREADING{}; //内存分配器_子类通过继承获得其new和delete功能 template< class ThreadingModel = DEFAULT_THREADING> class SmallObject: public ThreadingModel { public: static void* operator new(unsigned int size) { ++m_iCount; return ::operator new(size); } static void operator delete(void* p, unsigned int size) { --m_iCount; delete p; } static int GetCount(){ return m_iCount; } virtual ~SmallObject(){} private: static int m_iCount; }; template<class ThreadingModel> int SmallObject<ThreadingModel>::m_iCount = 0; namespace Private { //指令器 template <class R> struct FunctorImplBase: public SmallObject<DEFAULT_THREADING> { typedef R ResultType; typedef NullType Parm1; typedef NullType Parm2; typedef NullType Parm3; typedef NullType Parm4; typedef NullType Parm5; typedef NullType Parm6; typedef NullType Parm7; typedef NullType Parm8; typedef NullType Parm9; typedef NullType Parm10; typedef NullType Parm11; typedef NullType Parm12; typedef NullType Parm13; typedef NullType Parm14; typedef NullType Parm15; virtual ~FunctorImplBase(){} }; template <class R, class TList> class FunctorImpl; template <class R> class FunctorImpl<R, NullType>: public FunctorImplBase<R> { public: typedef R ResultType; virtual R operator()() = 0; }; template <class R, class P1> class FunctorImpl<R, TYPELIST_1(P1)>: public FunctorImplBase<R> { public: typedef R ResultType; typedef P1 Parm1; virtual R operator()(Parm1) = 0; }; template <class R, class P1, class P2> class FunctorImpl<R, TYPELIST_2(P1, P2)>: public FunctorImplBase<R> { public: typedef R ResultType; typedef P1 Parm1; typedef P2 Parm2; virtual R operator()(Parm1, Parm2) = 0; }; template <class R, class P1, class P2, class P3> class FunctorImpl<R, TYPELIST_3(P1, P2, P3)>: public FunctorImplBase<R> { public: typedef R ResultType; typedef P1 Parm1; typedef P2 Parm2; typedef P3 Parm3; virtual R operator()(Parm1, Parm2, Parm3) = 0; }; template <class R, class P1, class P2, class P3, class P4> class FunctorImpl<R, TYPELIST_4(P1, P2, P3, P4) >: public FunctorImplBase<R> { public: typedef R ResultType; typedef P1 Parm1; typedef P2 Parm2; typedef P3 Parm3; typedef P4 Parm4; virtual R operator()(Parm1, Parm2, Parm3, Parm4) = 0; }; template <class R, class P1, class P2, class P3, class P4, class P5> class FunctorImpl<R, TYPELIST_5(P1, P2, P3, P4, P5)>: public FunctorImplBase<R> { public: typedef R ResultType; typedef P1 Parm1; typedef P2 Parm2; typedef P3 Parm3; typedef P4 Parm4; typedef P5 Parm5; virtual R operator()(Parm1, Parm2, Parm3, Parm4, Parm5) = 0; }; template <class R, class P1, class P2, class P3, class P4,class P5, class P6> class FunctorImpl<R, TYPELIST_6(P1, P2, P3, P4, P5, P6)>: public FunctorImplBase<R> { public: typedef R ResultType; typedef P1 Parm1; typedef P2 Parm2; typedef P3 Parm3; typedef P4 Parm4; typedef P5 Parm5; typedef P6 Parm6; virtual R operator()(Parm1, Parm2, Parm3, Parm4, Parm5, Parm6) = 0; }; template <class R, class P1, class P2, class P3, class P4, class P5, class P6, class P7> class FunctorImpl<R, TYPELIST_7(P1, P2, P3, P4, P5, P6, P7)>: public FunctorImplBase<R> { public: typedef R ResultType; typedef P1 Parm1; typedef P2 Parm2; typedef P3 Parm3; typedef P4 Parm4; typedef P5 Parm5; typedef P6 Parm6; typedef P7 Parm7; virtual R operator()(Parm1, Parm2, Parm3, Parm4, Parm5, Parm6, Parm7) = 0; }; template <class R, class P1, class P2, class P3, class P4, class P5, class P6, class P7, class P8> class FunctorImpl<R, TYPELIST_8(P1, P2, P3, P4, P5, P6, P7, P8)>: public FunctorImplBase<R> { public: typedef R ResultType; typedef P1 Parm1; typedef P2 Parm2; typedef P3 Parm3; typedef P4 Parm4; typedef P5 Parm5; typedef P6 Parm6; typedef P7 Parm7; typedef P8 Parm8; virtual R operator()(Parm1, Parm2, Parm3, Parm4, Parm5, Parm6, Parm7, Parm8) = 0; }; template <class R, class P1, class P2, class P3, class P4, class P5, class P6, class P7, class P8, class P9> class FunctorImpl<R, TYPELIST_9(P1, P2, P3, P4, P5, P6, P7, P8, P9)>: public FunctorImplBase<R> { public: typedef R ResultType; typedef P1 Parm1; typedef P2 Parm2; typedef P3 Parm3; typedef P4 Parm4; typedef P5 Parm5; typedef P6 Parm6; typedef P7 Parm7; typedef P8 Parm8; typedef P9 Parm9; virtual R operator()(Parm1, Parm2, Parm3, Parm4, Parm5, Parm6, Parm7, Parm8, Parm9) = 0; }; template <class R, class P1, class P2, class P3, class P4, class P5, class P6, class P7, class P8, class P9, class P10> class FunctorImpl<R, TYPELIST_10(P1, P2, P3, P4, P5, P6, P7, P8, P9, P10)>: public FunctorImplBase<R> { public: typedef R ResultType; typedef P1 Parm1; typedef P2 Parm2; typedef P3 Parm3; typedef P4 Parm4; typedef P5 Parm5; typedef P6 Parm6; typedef P7 Parm7; typedef P8 Parm8; typedef P9 Parm9; typedef P10 Parm10; virtual R operator()(Parm1, Parm2, Parm3, Parm4, Parm5, Parm6, Parm7, Parm8, Parm9, Parm10) = 0; }; template <class R, class P1, class P2, class P3, class P4, class P5, class P6, class P7, class P8, class P9, class P10, class P11> class FunctorImpl<R, TYPELIST_11(P1, P2, P3, P4, P5, P6, P7, P8, P9, P10, P11)>: public FunctorImplBase<R> { public: typedef R ResultType; typedef P1 Parm1; typedef P2 Parm2; typedef P3 Parm3; typedef P4 Parm4; typedef P5 Parm5; typedef P6 Parm6; typedef P7 Parm7; typedef P8 Parm8; typedef P9 Parm9; typedef P10 Parm10; typedef P11 Parm11; virtual R operator()(Parm1, Parm2, Parm3, Parm4, Parm5, Parm6, Parm7, Parm8, Parm9, Parm10, Parm11) = 0; }; template <class R, class P1, class P2, class P3, class P4, class P5, class P6, class P7, class P8, class P9, class P10, class P11, class P12> class FunctorImpl<R, TYPELIST_12(P1, P2, P3, P4, P5, P6, P7, P8, P9, P10, P11, P12)>: public FunctorImplBase<R> { public: typedef R ResultType; typedef P1 Parm1; typedef P2 Parm2; typedef P3 Parm3; typedef P4 Parm4; typedef P5 Parm5; typedef P6 Parm6; typedef P7 Parm7; typedef P8 Parm8; typedef P9 Parm9; typedef P10 Parm10; typedef P11 Parm11; typedef P12 Parm12; virtual R operator()(Parm1, Parm2, Parm3, Parm4, Parm5, Parm6, Parm7, Parm8, Parm9, Parm10, Parm11, Parm12) = 0; }; template <class R, class P1, class P2, class P3, class P4, class P5, class P6, class P7, class P8, class P9, class P10, class P11, class P12, class P13> class FunctorImpl<R, TYPELIST_13(P1, P2, P3, P4, P5, P6, P7, P8, P9, P10, P11, P12, P13)>: public FunctorImplBase<R> { public: typedef R ResultType; typedef P1 Parm1; typedef P2 Parm2; typedef P3 Parm3; typedef P4 Parm4; typedef P5 Parm5; typedef P6 Parm6; typedef P7 Parm7; typedef P8 Parm8; typedef P9 Parm9; typedef P10 Parm10; typedef P11 Parm11; typedef P12 Parm12; typedef P13 Parm13; virtual R operator()(Parm1, Parm2, Parm3, Parm4, Parm5, Parm6, Parm7, Parm8, Parm9, Parm10, Parm11, Parm12, Parm13) = 0; }; template <class R, class P1, class P2, class P3, class P4, class P5, class P6, class P7, class P8, class P9, class P10, class P11, class P12, class P13, class P14> class FunctorImpl<R, TYPELIST_14(P1, P2, P3, P4, P5, P6, P7, P8, P9, P10, P11, P12, P13, P14)> : public FunctorImplBase<R> { public: typedef R ResultType; typedef P1 Parm1; typedef P2 Parm2; typedef P3 Parm3; typedef P4 Parm4; typedef P5 Parm5; typedef P6 Parm6; typedef P7 Parm7; typedef P8 Parm8; typedef P9 Parm9; typedef P10 Parm10; typedef P11 Parm11; typedef P12 Parm12; typedef P13 Parm13; typedef P14 Parm14; virtual R operator()(Parm1, Parm2, Parm3, Parm4, Parm5, Parm6, Parm7, Parm8, Parm9, Parm10, Parm11, Parm12, Parm13, Parm14) = 0; }; template <class R, class P1, class P2, class P3, class P4, class P5, class P6, class P7, class P8, class P9, class P10, class P11, class P12, class P13, class P14, class P15> class FunctorImpl<R, TYPELIST_15(P1, P2, P3, P4, P5, P6, P7, P8, P9, P10, P11, P12, P13, P14, P15)> : public FunctorImplBase<R> { public: typedef R ResultType; typedef P1 Parm1; typedef P2 Parm2; typedef P3 Parm3; typedef P4 Parm4; typedef P5 Parm5; typedef P6 Parm6; typedef P7 Parm7; typedef P8 Parm8; typedef P9 Parm9; typedef P10 Parm10; typedef P11 Parm11; typedef P12 Parm12; typedef P13 Parm13; typedef P14 Parm14; typedef P15 Parm15; virtual R operator()(Parm1, Parm2, Parm3, Parm4, Parm5, Parm6, Parm7, Parm8, Parm9, Parm10, Parm11, Parm12, Parm13, Parm14, Parm15) = 0; }; //普通函数和仿函数的对外接口 template <class Impl, class Fun> class FunctorHandler: public Impl { private: typedef typename Impl Base; typedef typename Base::ResultType ResultType; typedef typename Base::Parm1 Parm1; typedef typename Base::Parm2 Parm2; typedef typename Base::Parm3 Parm3; typedef typename Base::Parm4 Parm4; typedef typename Base::Parm5 Parm5; typedef typename Base::Parm6 Parm6; typedef typename Base::Parm7 Parm7; typedef typename Base::Parm8 Parm8; typedef typename Base::Parm9 Parm9; typedef typename Base::Parm10 Parm10; typedef typename Base::Parm11 Parm11; typedef typename Base::Parm12 Parm12; typedef typename Base::Parm13 Parm13; typedef typename Base::Parm14 Parm14; typedef typename Base::Parm15 Parm15; Fun m_fun; public: FunctorHandler(const Fun& fun) : m_fun(fun) {} ResultType operator()(){ return m_fun(); } ResultType operator()(Parm1 p1){ return m_fun(p1); } ResultType operator()(Parm1 p1, Parm2 p2){ return m_fun(p1, p2); } ResultType operator()(Parm1 p1, Parm2 p2, Parm3 p3){ return m_fun(p1, p2, p3); } ResultType operator()(Parm1 p1, Parm2 p2, Parm3 p3, Parm4 p4){ return m_fun(p1, p2, p3, p4); } ResultType operator()(Parm1 p1, Parm2 p2, Parm3 p3, Parm4 p4, Parm5 p5){ return m_fun(p1, p2, p3, p4, p5); } ResultType operator()(Parm1 p1, Parm2 p2, Parm3 p3, Parm4 p4, Parm5 p5, Parm6 p6){ return m_fun(p1, p2, p3, p4, p5, p6); } ResultType operator()(Parm1 p1, Parm2 p2, Parm3 p3, Parm4 p4, Parm5 p5, Parm6 p6, Parm7 p7){ return m_fun(p1, p2, p3, p4, p5, p6, p7); } ResultType operator()(Parm1 p1, Parm2 p2, Parm3 p3, Parm4 p4, Parm5 p5, Parm6 p6, Parm7 p7, Parm8 p8){ return m_fun(p1, p2, p3, p4, p5, p6, p7, p8); } ResultType operator()(Parm1 p1, Parm2 p2, Parm3 p3, Parm4 p4, Parm5 p5, Parm6 p6, Parm7 p7, Parm8 p8, Parm9 p9){ return m_fun(p1, p2, p3, p4, p5, p6, p7, p8, p9); } ResultType operator()(Parm1 p1, Parm2 p2, Parm3 p3, Parm4 p4, Parm5 p5, Parm6 p6, Parm7 p7, Parm8 p8, Parm9 p9, Parm10 p10){ return m_fun(p1, p2, p3, p4, p5, p6, p7, p8, p9, p10); } ResultType operator()(Parm1 p1, Parm2 p2, Parm3 p3, Parm4 p4, Parm5 p5, Parm6 p6, Parm7 p7, Parm8 p8, Parm9 p9, Parm10 p10, Parm11 p11){ return m_fun(p1, p2, p3, p4, p5, p6, p7, p8, p9, p10, p11); } ResultType operator()(Parm1 p1, Parm2 p2, Parm3 p3, Parm4 p4, Parm5 p5, Parm6 p6, Parm7 p7, Parm8 p8, Parm9 p9, Parm10 p10, Parm11 p11,Parm12 p12){ return m_fun(p1, p2, p3, p4, p5, p6, p7, p8, p9, p10, p11, p12); } ResultType operator()(Parm1 p1, Parm2 p2, Parm3 p3, Parm4 p4, Parm5 p5, Parm6 p6, Parm7 p7, Parm8 p8, Parm9 p9, Parm10 p10, Parm11 p11,Parm12 p12, Parm13 p13){ return m_fun(p1, p2, p3, p4, p5, p6, p7, p8, p9, p10, p11, p12, p13); } ResultType operator()(Parm1 p1, Parm2 p2, Parm3 p3, Parm4 p4, Parm5 p5, Parm6 p6, Parm7 p7, Parm8 p8, Parm9 p9, Parm10 p10, Parm11 p11,Parm12 p12, Parm13 p13, Parm14 p14){return m_fun(p1, p2, p3, p4, p5, p6, p7, p8, p9, p10, p11, p12, p13, p14);} ResultType operator()(Parm1 p1, Parm2 p2, Parm3 p3, Parm4 p4, Parm5 p5, Parm6 p6, Parm7 p7, Parm8 p8, Parm9 p9, Parm10 p10, Parm11 p11,Parm12 p12, Parm13 p13, Parm14 p14, Parm15 p15){ return m_fun(p1, p2, p3, p4, p5, p6, p7, p8, p9, p10, p11, p12, p13, p14, p15);} }; //成员函数的对外接口 template <class Impl, class PointerToObj, class PointerToMemFn> class MemFunHandler : public Impl { private: typedef typename Impl Base; typedef typename Base::ResultType ResultType; typedef typename Base::Parm1 Parm1; typedef typename Base::Parm2 Parm2; typedef typename Base::Parm3 Parm3; typedef typename Base::Parm4 Parm4; typedef typename Base::Parm5 Parm5; typedef typename Base::Parm6 Parm6; typedef typename Base::Parm7 Parm7; typedef typename Base::Parm8 Parm8; typedef typename Base::Parm9 Parm9; typedef typename Base::Parm10 Parm10; typedef typename Base::Parm11 Parm11; typedef typename Base::Parm12 Parm12; typedef typename Base::Parm13 Parm13; typedef typename Base::Parm14 Parm14; typedef typename Base::Parm15 Parm15; PointerToObj m_pObj; PointerToMemFn m_pMemFn; public: MemFunHandler(const PointerToObj& pObj, PointerToMemFn pMemFn): m_pObj(pObj), m_pMemFn(pMemFn){} ResultType operator()(){ return ((*m_pObj).*m_pMemFn)(); } ResultType operator()(Parm1 p1){ return ((*m_pObj).*m_pMemFn)(p1); } ResultType operator()(Parm1 p1, Parm2 p2){ return ((*m_pObj).*m_pMemFn)(p1, p2); } ResultType operator()(Parm1 p1, Parm2 p2, Parm3 p3){ return ((*m_pObj).*m_pMemFn)(p1, p2, p3); } ResultType operator()(Parm1 p1, Parm2 p2, Parm3 p3, Parm4 p4){ return ((*m_pObj).*m_pMemFn)(p1, p2, p3, p4); } ResultType operator()(Parm1 p1, Parm2 p2, Parm3 p3, Parm4 p4, Parm5 p5){ return ((*m_pObj).*m_pMemFn)(p1, p2, p3, p4, p5); } ResultType operator()(Parm1 p1, Parm2 p2, Parm3 p3, Parm4 p4, Parm5 p5, Parm6 p6){ return ((*m_pObj).*m_pMemFn)(p1, p2, p3, p4, p5, p6); } ResultType operator()(Parm1 p1, Parm2 p2, Parm3 p3, Parm4 p4, Parm5 p5, Parm6 p6, Parm7 p7){ return ((*m_pObj).*m_pMemFn)(p1, p2, p3, p4, p5, p6, p7); } ResultType operator()(Parm1 p1, Parm2 p2, Parm3 p3, Parm4 p4, Parm5 p5, Parm6 p6, Parm7 p7, Parm8 p8){ return ((*m_pObj).*m_pMemFn)(p1, p2, p3, p4, p5, p6, p7, p8); } ResultType operator()(Parm1 p1, Parm2 p2, Parm3 p3, Parm4 p4, Parm5 p5, Parm6 p6, Parm7 p7, Parm8 p8, Parm9 p9){ return ((*m_pObj).*m_pMemFn)(p1, p2, p3, p4, p5, p6, p7, p8, p9); } ResultType operator()(Parm1 p1, Parm2 p2, Parm3 p3, Parm4 p4, Parm5 p5, Parm6 p6, Parm7 p7, Parm8 p8, Parm9 p9, Parm10 p10){ return ((*m_pObj).*m_pMemFn)(p1, p2, p3, p4, p5, p6, p7, p8, p9, p10); } ResultType operator()(Parm1 p1, Parm2 p2, Parm3 p3, Parm4 p4, Parm5 p5, Parm6 p6, Parm7 p7, Parm8 p8, Parm9 p9, Parm10 p10, Parm11 p11){return ((*m_pObj).*m_pMemFn)(p1, p2, p3, p4, p5, p6, p7, p8, p9, p10, p11);} ResultType operator()(Parm1 p1, Parm2 p2, Parm3 p3, Parm4 p4, Parm5 p5, Parm6 p6, Parm7 p7, Parm8 p8, Parm9 p9, Parm10 p10, Parm11 p11,Parm12 p12){ return ((*m_pObj).*m_pMemFn)(p1, p2, p3, p4, p5, p6, p7, p8, p9, p10, p11, p12);} ResultType operator()(Parm1 p1, Parm2 p2, Parm3 p3, Parm4 p4, Parm5 p5, Parm6 p6, Parm7 p7, Parm8 p8, Parm9 p9, Parm10 p10, Parm11 p11,Parm12 p12, Parm13 p13){return ((*m_pObj).*m_pMemFn)(p1, p2, p3, p4, p5, p6, p7, p8, p9, p10, p11, p12, p13);} ResultType operator()(Parm1 p1, Parm2 p2, Parm3 p3, Parm4 p4, Parm5 p5, Parm6 p6, Parm7 p7, Parm8 p8, Parm9 p9, Parm10 p10, Parm11 p11,Parm12 p12, Parm13 p13, Parm14 p14){return ((*m_pObj).*m_pMemFn)(p1, p2, p3, p4, p5, p6, p7, p8, p9, p10, p11, p12, p13, p14);} ResultType operator()(Parm1 p1, Parm2 p2, Parm3 p3, Parm4 p4, Parm5 p5, Parm6 p6, Parm7 p7, Parm8 p8, Parm9 p9, Parm10 p10, Parm11 p11,Parm12 p12, Parm13 p13, Parm14 p14, Parm15 p15){return ((*m_pObj).*m_pMemFn)(p1, p2, p3, p4, p5, p6, p7, p8, p9, p10, p11, p12, p13, p14, p15);} }; } //指令封装 //将普通函数,仿函数和成员函数的对外接口整合在一起 template <class R, class TList = NullType> class Functor { private: typedef Private::FunctorImpl<R, TList> Impl; typedef R ResultType; typedef TList ParmList; typedef typename Impl::Parm1 Parm1; typedef typename Impl::Parm2 Parm2; typedef typename Impl::Parm3 Parm3; typedef typename Impl::Parm4 Parm4; typedef typename Impl::Parm5 Parm5; typedef typename Impl::Parm6 Parm6; typedef typename Impl::Parm7 Parm7; typedef typename Impl::Parm8 Parm8; typedef typename Impl::Parm9 Parm9; typedef typename Impl::Parm10 Parm10; typedef typename Impl::Parm11 Parm11; typedef typename Impl::Parm12 Parm12; typedef typename Impl::Parm13 Parm13; typedef typename Impl::Parm14 Parm14; typedef typename Impl::Parm15 Parm15; Impl* m_spImpl; //不提供拷贝构造 Functor(const Functor& other){} //不提供赋值语句 Functor& operator=(const Functor& other){} public: template <class Fun> Functor(Fun fun):m_spImpl(NULL) { m_spImpl = new Private::FunctorHandler<Impl, Fun>(fun); if( NULL == m_spImpl){ /*Log语句*/ } } template <class PtrObj, class MemFn> Functor(const PtrObj& p, MemFn memFn):m_spImpl(NULL) { m_spImpl = new Private::MemFunHandler<Impl, PtrObj, MemFn>(p, memFn); if( NULL == m_spImpl){ /*Log语句*/ } } virtual ~Functor() { if( NULL != m_spImpl) { delete m_spImpl; m_spImpl = NULL; } } ResultType operator()(){ return (*m_spImpl)(); } ResultType operator()(Parm1 p1){ return (*m_spImpl)(p1); } ResultType operator()(Parm1 p1, Parm2 p2){ return (*m_spImpl)(p1, p2); } ResultType operator()(Parm1 p1, Parm2 p2, Parm3 p3){ return (*m_spImpl)(p1, p2, p3); } ResultType operator()(Parm1 p1, Parm2 p2, Parm3 p3, Parm4 p4){ return (*m_spImpl)(p1, p2, p3, p4); } ResultType operator()(Parm1 p1, Parm2 p2, Parm3 p3, Parm4 p4, Parm5 p5){ return (*m_spImpl)(p1, p2, p3, p4, p5); } ResultType operator()(Parm1 p1, Parm2 p2, Parm3 p3, Parm4 p4, Parm5 p5, Parm6 p6){ return (*m_spImpl)(p1, p2, p3, p4, p5, p6); } ResultType operator()(Parm1 p1, Parm2 p2, Parm3 p3, Parm4 p4, Parm5 p5, Parm6 p6, Parm7 p7){ return (*m_spImpl)(p1, p2, p3, p4, p5, p6, p7); } ResultType operator()(Parm1 p1, Parm2 p2, Parm3 p3, Parm4 p4, Parm5 p5, Parm6 p6, Parm7 p7, Parm8 p8){ return (*m_spImpl)(p1, p2, p3, p4, p5, p6, p7, p8); } ResultType operator()(Parm1 p1, Parm2 p2, Parm3 p3, Parm4 p4, Parm5 p5, Parm6 p6, Parm7 p7, Parm8 p8, Parm9 p9){ return (*m_spImpl)(p1, p2, p3, p4, p5, p6, p7, p8, p9); } ResultType operator()(Parm1 p1, Parm2 p2, Parm3 p3, Parm4 p4, Parm5 p5, Parm6 p6, Parm7 p7, Parm8 p8, Parm9 p9, Parm10 p10){ return (*m_spImpl)(p1, p2, p3, p4, p5, p6, p7, p8, p9, p10); } ResultType operator()(Parm1 p1, Parm2 p2, Parm3 p3, Parm4 p4, Parm5 p5, Parm6 p6, Parm7 p7, Parm8 p8, Parm9 p9, Parm10 p10, Parm11 p11){ return (*m_spImpl)(p1, p2, p3, p4, p5, p6, p7, p8, p9, p10, p11); } ResultType operator()(Parm1 p1, Parm2 p2, Parm3 p3, Parm4 p4, Parm5 p5, Parm6 p6, Parm7 p7, Parm8 p8, Parm9 p9, Parm10 p10, Parm11 p11, Parm12 p12){ return (*m_spImpl)(p1, p2, p3, p4, p5, p6, p7, p8, p9, p10, p11, p12);} ResultType operator()(Parm1 p1, Parm2 p2, Parm3 p3, Parm4 p4, Parm5 p5, Parm6 p6, Parm7 p7, Parm8 p8, Parm9 p9, Parm10 p10, Parm11 p11, Parm12 p12, Parm13 p13){ return (*m_spImpl)(p1, p2, p3, p4, p5, p6, p7, p8, p9, p10, p11,p12, p13);} ResultType operator()(Parm1 p1, Parm2 p2, Parm3 p3, Parm4 p4, Parm5 p5, Parm6 p6, Parm7 p7, Parm8 p8, Parm9 p9, Parm10 p10, Parm11 p11, Parm12 p12, Parm13 p13, Parm14 p14){ return (*m_spImpl)(p1, p2, p3, p4, p5, p6, p7, p8, p9, p10, p11, p12, p13, p14);} ResultType operator()(Parm1 p1, Parm2 p2, Parm3 p3, Parm4 p4, Parm5 p5, Parm6 p6, Parm7 p7, Parm8 p8, Parm9 p9, Parm10 p10, Parm11 p11, Parm12 p12, Parm13 p13, Parm14 p14, Parm15 p15){ return (*m_spImpl)(p1, p2, p3, p4, p5, p6, p7, p8, p9, p10, p11, p12, p13, p14, p15);} }; //回滚操作 //回滚的触发条件:本个体操作已成功执行,但群体操作未成功,导致本个体操作回滚 /* 使用: TOOL::Rall<void> r2; DataBase db; if( db.doDB(true)) r2.SetRall(&db,&DataBase::UndoDB); else return; TOOL::Rall<void> r1; if( doFun(true)) r1.SetRall(UndoFun); else return; r1.OK(); r2.OK(); */ template <class R> class Rall { public: Rall():cmd(NULL),m_bIsOK(false){} ~Rall() { if( NULL != cmd) { if( !m_bIsOK ) { (*cmd)(); } delete cmd; cmd = NULL; } } template <class Fun> void SetRall(Fun fun) { if( NULL != cmd){ delete cmd; cmd = NULL; } cmd = new Functor<R>(fun); } template <class PtrObj, class MemFn> void SetRall(const PtrObj& p, MemFn memFn) { if( NULL != cmd){ delete cmd; cmd = NULL; } cmd = new Functor<R>(p, memFn); } void OK(){ m_bIsOK = true; } private: bool m_bIsOK; Functor<R>* cmd; }; } #endif //__CLK_POLICY_H__
#include <iostream> #include "TimeEvt.h" #include "NpcParEvt.h" using namespace std; class NPC { public: NPC(TimeEvt* _pTimeEvt, const char* _pName): m_pHeartbeatTgr(NULL), m_pWatch(NULL) { m_pHeartbeatTgr = _pTimeEvt->CreateTgr(this, &NPC::Heartbeat); strcpy(m_szName, _pName); } virtual ~NPC() { if( NULL != m_pWatch) { delete m_pWatch; m_pWatch = NULL; } delete m_pHeartbeatTgr; m_pHeartbeatTgr = NULL; } void Watch(NPC* _pNpc) { m_pWatch = _pNpc->m_HP.CreateTgr(this,&NPC::Angry); } NpcParEvt m_HP; private: void Heartbeat(int _tmStart, int _tmStep, int _tmCur) { cout<<m_szName<<", NPC心跳,Time(start:"<<_tmStart<<", step:"<<_tmStep<<", cur:"<<_tmCur<<")"<<endl; } void Angry(int _HP) { cout<<m_szName<<", 见到士兵被杀, 爆发了!"<<endl; } char m_szName[64]; TimeEvt::Trigger* m_pHeartbeatTgr; NpcParEvt::Trigger* m_pWatch; }; void GameUpdata(int _tmStart, int _tmStep, int _tmCur) { cout<<"游戏刷新,Time(start:"<<_tmStart<<", step:"<<_tmStep<<", cur:"<<_tmCur<<")"<<endl; } void main() { TimeEvt* pTimeEvt = new TimeEvt(0, 7); TimeEvt::Trigger* pGameTgr = pTimeEvt->CreateTgr(&GameUpdata); NPC* pGeneral = new NPC(pTimeEvt, "上将: 阿尔德夫"); NPC* pSoldier = new NPC(pTimeEvt, "士兵: 切西尔"); pGeneral->Watch(pSoldier); //delete pGeneral; //delete pSoldier; pTimeEvt->SetData(0); pSoldier->m_HP.SetData(0); delete pGeneral; delete pSoldier; delete pGameTgr; delete pTimeEvt; system("pause"); }