/******************************************************************** 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; }; } template< class T> void DeleteObj(T*& _pObj) { if( NULL != _pObj) { delete _pObj; _pObj = NULL; } } #endif //__CLK_POLICY_H__
/******************************************************************** 文件名 : Event_Interface.h 创建人 : Clark/陈泽丹 创建时间 : 2012-4-15 描述: 触发器 *********************************************************************/ #pragma once #include <iostream> #include <vector> #include "Res_Interface.h" 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 >::G_AddResItem(this); } 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; };
/****************************** * 文件名: Lighthouse_Interface.h * 创建人: 陈泽丹/Clark * 创建时间: 20120330 * 文件描述: ******************************/ #pragma once #include <iostream> #include <map> #include "Res_Interface.h" using namespace std; template<class T_TYPE> class Lighthouse_Interface: public Res_Interface< Lighthouse_Interface< T_TYPE > > { public: Lighthouse_Interface(const int _RES_ID):RES_ID(_RES_ID){ Lighthouse_Interface< T_TYPE >::G_AddResItem(this); } virtual ~Lighthouse_Interface(){ Lighthouse_Interface< T_TYPE >::G_DelResItem(this); } bool Login(int _UID, T_TYPE* _pEntity) { if( 0 == m_vpEntitys[_UID]) { m_vpEntitys[_UID] = _pEntity; return true; } return false; } void Logout(int _UID) { m_vpEntitys[_UID] = 0; } int GetSize() { return m_vpEntitys.size(); } const int RES_ID; protected: map< int, T_TYPE* > m_vpEntitys; };
/******************************************************************** 文件名 : 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 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; } static int Get_GResSize() { return m_vRes.size(); } protected: static vector< T_RES* > m_vRes; }; template<class T_RES> vector< T_RES* > Res_Interface< T_RES >::m_vRes;
/****************************** * 文件名: BaseEntity.h * 创建人: 陈泽丹/Clark * 创建时间: 20120330 * 文件描述: ******************************/ #pragma once #include "../Interface/Lighthouse_Interface.h" class BaseEntity: public Res_Interface< BaseEntity > { public: virtual ~BaseEntity(); void ToRes( Lighthouse_Interface< BaseEntity >* _pDestRes, const int _iPosX = 0, const int _iPosY = 0 ); const int UID; protected: int m_iPosX; int m_iPosY; protected: BaseEntity(Lighthouse_Interface< BaseEntity >* _pRes, int _UID); Lighthouse_Interface< BaseEntity >* m_pRes; };
#include <iostream> #include "BaseEntity.h" using namespace std; BaseEntity::BaseEntity(Lighthouse_Interface< BaseEntity >* _pRes, int _UID): m_pRes(_pRes), UID(_UID), m_iPosX(0), m_iPosY(0) { BaseEntity::G_AddResItem(this); Lighthouse_Interface< BaseEntity >* pRes = Lighthouse_Interface< BaseEntity >::GetResItem(m_pRes); if( NULL != pRes) { pRes->Login( UID, this ); } else { cout<<"实体创建时找不到相关灯塔"<<endl; } } BaseEntity::~BaseEntity() { BaseEntity::G_DelResItem(this); Lighthouse_Interface< BaseEntity >* pRes = Lighthouse_Interface< BaseEntity >::GetResItem(m_pRes); if( NULL != pRes) { pRes->Logout( UID ); } else { cout<<"实体删除时找不到相关灯塔"<<endl; } }; void BaseEntity::ToRes(Lighthouse_Interface< BaseEntity >* _pDestRes, const int _iPosX, const int _iPosY) { if( m_pRes != _pDestRes && NULL != Lighthouse_Interface< BaseEntity >::GetResItem(m_pRes) && NULL != Lighthouse_Interface< BaseEntity >::GetResItem(_pDestRes) ) { m_pRes->Logout( UID ); m_pRes = _pDestRes; m_pRes->Login( UID, this ); } m_iPosX = _iPosX; m_iPosY = _iPosY; }
/******************************************************************** 文件名 : ParEvt.h 创建人 : Clark/陈泽丹 创建时间 : 2012-4-15 描述: NPC属性触发器 *********************************************************************/ #pragma once #include "./Interface/Event_Interface.h" class ParEvt:public Event_Interface< int, Functor< void, TYPELIST_1(int) > > { public: ParEvt():Event_Interface< int, Functor< void, TYPELIST_1(int) > >(0){} virtual ~ParEvt(){} private: //触发事件 virtual void OnEvent(Functor< void, TYPELIST_1(int) >* _pCallBack) { int iCurData = GetData(); (*_pCallBack)(iCurData); } };
/******************************************************************** 文件名 : TimeEvt.h 创建人 : Clark/陈泽丹 创建时间 : 2012-4-15 描述: 定时触发器 *********************************************************************/ #pragma once #include "../Interface/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; }; extern TimeEvt* g_pTimeCenter;
#include "TimeEvt.h" extern TimeEvt* g_pTimeCenter = NULL;
#pragma once #include "Interface/Res_Interface.h" //生成NPC,法术,效果伤害等的指令 //AI带N种状态 //让逻辑服生成, 然后逻辑服再同步到客户端 class LogicJudgment: public Res_Interface< LogicJudgment > { public: LogicJudgment(){LogicJudgment::G_AddResItem(this);} virtual ~LogicJudgment(){LogicJudgment::G_DelResItem(this);} virtual void Run() = 0; static void Exe() { for(int i=0; m_vRes.size(); i++) { m_vRes[i]->Run(); delete m_vRes[i]; } } }; template<int v> class LogicJudgmentCmd;
#pragma once #include "LogicJudgment.h" #include "LogicTrap.h" class LogicTrap; struct LOGIC_JUDGMENT_CMD_TRAP { enum TYPE { KILL_TRAP = 0, CREATE_FIRE_BALL }; }; template<> class LogicJudgmentCmd<LOGIC_JUDGMENT_CMD_TRAP::KILL_TRAP>: public LogicJudgment { public: LogicJudgmentCmd<LOGIC_JUDGMENT_CMD_TRAP::KILL_TRAP>(LogicTrap* _pRes):RES_ID(_pRes){} virtual void Run(); private: const LogicTrap* RES_ID; }; template<> class LogicJudgmentCmd<LOGIC_JUDGMENT_CMD_TRAP::CREATE_FIRE_BALL>: public LogicJudgment { public: LogicJudgmentCmd<LOGIC_JUDGMENT_CMD_TRAP::CREATE_FIRE_BALL>(Lighthouse_Interface< BaseEntity >* _pRes):RES_ID(_pRes){} virtual void Run(); private: const Lighthouse_Interface< BaseEntity >* RES_ID; };
#include "LogicJudgment_Trap.h" void LogicJudgmentCmd<LOGIC_JUDGMENT_CMD_TRAP::KILL_TRAP>::Run() { BaseEntity* pRes = LogicTrap::GetResItem(RES_ID); if( NULL != pRes) { delete pRes; } } void LogicJudgmentCmd<LOGIC_JUDGMENT_CMD_TRAP::CREATE_FIRE_BALL>::Run() { Lighthouse_Interface< BaseEntity >* pRes = Lighthouse_Interface< BaseEntity >::GetResItem(RES_ID); if( NULL != pRes) { LogicTrap* pLogicTrap = new LogicTrap(pRes, 0, "火焰球"); pLogicTrap->m_pTrapAI = new TrapAI(); } }
#pragma once #include "LogicTrap.h" #include "LogicJudgment.h" #include "LogicJudgment_Trap.h" class LogicTrap; class LogicState { public: virtual ~LogicState(){} virtual void Run(LogicTrap* _pTrap) = 0; }; class MoveState: public LogicState { public: void Run(LogicTrap* _pTrap); }; class KillSelfState: public LogicState { public: void Run(LogicTrap* _pTrap); }; class TrapAI { public: TrapAI(){ m_pCurState = &m_moveState; m_iCount = 0;} virtual ~TrapAI(){} void Run(LogicTrap* _pTrap); private: int m_iCount; MoveState m_moveState; KillSelfState m_killSelfState; LogicState* m_pCurState; };
#include "LogicState.h" void MoveState::Run(LogicTrap* _pTrap) { cout<<_pTrap->TRAP_FACE<<"向目标移动"<<endl; } void KillSelfState::Run(LogicTrap* _pTrap) { new LogicJudgmentCmd<LOGIC_JUDGMENT_CMD_TRAP::KILL_TRAP>(_pTrap); } void TrapAI::Run(LogicTrap* _pTrap) { if(m_iCount > 5) { cout<<_pTrap->TRAP_FACE<<"限定时间未能追求目标,执行自爆"<<endl; m_pCurState = &m_killSelfState; } m_pCurState->Run(_pTrap); ++m_iCount; }
#pragma once #include "./GameCommon/BaseEntity.h" #include "./GameCommon/TimeEvt.h" #include "LogicState.h" class TrapAI; class LogicTrap: public BaseEntity { public: virtual ~LogicTrap(); //protected: LogicTrap( Lighthouse_Interface< BaseEntity >* _pRes, int _UID, const char* _TRAP_FACE ); TrapAI* m_pTrapAI; char TRAP_FACE[64]; private: TimeEvt::Trigger* m_pHeartBeat; void HeartBeat(int iStart, int iStep, int iCur); };
#include "LogicTrap.h" #include <iostream> using namespace std; LogicTrap::LogicTrap( Lighthouse_Interface< BaseEntity >* _pRes, int _UID, const char* _TRAP_FACE ): BaseEntity(_pRes, _UID) { strcpy(TRAP_FACE, _TRAP_FACE); m_pHeartBeat = g_pTimeCenter->CreateTgr(this, &LogicTrap::HeartBeat); } LogicTrap::~LogicTrap() { cout<<"技能效果自爆了"<<endl; DeleteObj(m_pHeartBeat); delete m_pTrapAI; } void LogicTrap::HeartBeat(int iStart, int iStep, int iCur) { m_pTrapAI->Run(this); }
#include <iostream> #include <vld.h> #include "./GameCommon/TimeEvt.h" #include "./Interface/Lighthouse_Interface.h" #include "./GameCommon/BaseEntity.h" #include "LogicJudgment_Trap.h" using namespace std; void main() { g_pTimeCenter = new TimeEvt(0,1); Lighthouse_Interface< BaseEntity >* pLighthouse = new Lighthouse_Interface< BaseEntity >(0); new LogicJudgmentCmd<LOGIC_JUDGMENT_CMD_TRAP::CREATE_FIRE_BALL>(pLighthouse); for(int i=0; i<10; i++) { g_pTimeCenter->SetData(g_pTimeCenter->GetData() + 1); LogicJudgment::Exe(); } delete pLighthouse; delete g_pTimeCenter; //释放内存需要时间,否则vld的内存检测会出误诊。<a href="http://vld.codeplex.com">http://vld.codeplex.com</a> system("pause"); Sleep(100); }