实现定时触发器

/******************************************************************** 
 文件名 :     TimeEvtCenter.h 
 创建人  :    Clark/陈泽丹 
 创建时间 :   2012-4-11
 描述:		  定时触发器
 *********************************************************************/ 


#pragma once
#include "CMD_POLICY.h"
#include <vector>

using namespace POLICY;
using namespace std;


class TimeEvtCenter
{
public:
	class TimeEvent:public Functor<void, TYPELIST_3(int, int, int) >
	{
	private:
		template <class Fun>
		TimeEvent(const int _iEventStep, Fun _fun):
		Functor<void, TYPELIST_3(int, int, int) >(_fun), EVT_START_TIME(0),EVT_STEP_TIME(_iEventStep){}

		template <class PtrObj, class MemFn>
		TimeEvent(const int _iEventStep, const PtrObj& _p, MemFn _memFn):
		Functor<void, TYPELIST_3(int, int, int) >(_p, _memFn), EVT_START_TIME(0),EVT_STEP_TIME(_iEventStep){}

		const int EVT_START_TIME;
		const int EVT_STEP_TIME;

		friend class TimeEvtCenter;
		friend class TimeEvtCallBack;
	};

	TimeEvtCenter(void);
	virtual ~TimeEvtCenter(void);

	//刷新时间
	void UpdataCurTime();

	//获得时间
	int GetCurTime();

	//触发事件
	void OnEvent();

private:
	//添加事件
	template <class PtrObj, class MemFn>
	TimeEvent* AddEvent(const int _iEventStep, const PtrObj& _p, MemFn _memFn)
	{
		TimeEvent* pEvt = new TimeEvent(_iEventStep, _p, _memFn);
		( NULL != pEvt) ? (m_vTimeEvt.push_back(pEvt)) : 0;
		return pEvt;
	}

	//删除事件
	void DelEvent(const TimeEvent* _pEvt);

	//触发事件
	void OnEvent(TimeEvent* _pEvt);

	//时间
	int m_iCenterTime;
	//事件队列
	vector< TimeEvent* > m_vTimeEvt;
	friend class TimeEvtCallBack;
};
extern TimeEvtCenter* g_pTimeCallBackCenter;








//事件接口
class TimeEvtCallBack
{
public:
	//添加事件
	template <class PtrObj, class MemFn>
	TimeEvtCallBack(const int _iEventStep, const PtrObj& _p, MemFn _memFn)
	{
		m_pTEvt = g_pTimeCallBackCenter->AddEvent(_iEventStep, _p, _memFn);
	}

	virtual ~TimeEvtCallBack(void)
	{
		g_pTimeCallBackCenter->DelEvent(m_pTEvt);
	}

	//触发事件
	void OnEvent()
	{
		g_pTimeCallBackCenter->OnEvent(m_pTEvt);
	}

private:
	TimeEvtCenter::TimeEvent* m_pTEvt;
};


 

#include "TimeEvtCenter.h"
#include <iostream>


using namespace std;


class TimeEvent:public Functor<void, TYPELIST_3(int, int, int) >
{
private:
	template <class Fun>
	TimeEvent(const int _iEventStep, Fun _fun):
	Functor<void, TYPELIST_3(int, int, int) >(_fun), EVT_START_TIME(0),EVT_STEP_TIME(_iEventStep){}

	template <class PtrObj, class MemFn>
	TimeEvent(const int _iEventStep, const PtrObj& _p, MemFn _memFn):
	Functor<void, TYPELIST_3(int, int, int) >(_p, _memFn), EVT_START_TIME(0),EVT_STEP_TIME(_iEventStep){}

	const int EVT_START_TIME;
	const int EVT_STEP_TIME;

	friend class TimeEvtCenter;
	friend class TimeEvtCallBack;
};



TimeEvtCenter* g_pTimeCallBackCenter = new TimeEvtCenter();



void TimeEvtCenter::UpdataCurTime()
{
	m_iCenterTime = m_iCenterTime + 1;
}

int TimeEvtCenter::GetCurTime()
{
	return m_iCenterTime;
}

TimeEvtCenter::TimeEvtCenter(void)
{
	m_iCenterTime = 0;
}

TimeEvtCenter::~TimeEvtCenter(void)
{
	if (m_vTimeEvt.size() > 0)
	{
		cout<<"时间触发器有事件未删除!"<<endl;
	}
}


//删除事件
void TimeEvtCenter::DelEvent(const TimeEvent* _pEvt)
{
	for(int i=m_vTimeEvt.size()-1; i>=0; i--)
	{
		if( _pEvt == m_vTimeEvt[i])
		{
			delete m_vTimeEvt[i];
			m_vTimeEvt[i] = m_vTimeEvt[m_vTimeEvt.size()-1];
			m_vTimeEvt.pop_back();
			return;
		}
	}
}

//触发事件
void TimeEvtCenter::OnEvent()
{
	//由于删除事件是采用倒替的方案,所以遍历要倒数,以免遗漏
	for(int i=m_vTimeEvt.size()-1; i>=0; i--)
	{
		if( GetCurTime() >= m_vTimeEvt[i]->EVT_START_TIME)
		{
			int iTime = (GetCurTime() - m_vTimeEvt[i]->EVT_START_TIME) % (m_vTimeEvt[i]->EVT_STEP_TIME);
			if( 0 == iTime)
				(*m_vTimeEvt[i])(m_vTimeEvt[i]->EVT_START_TIME, m_vTimeEvt[i]->EVT_STEP_TIME, GetCurTime());
		}
	}
}

//触发事件
void TimeEvtCenter::OnEvent(TimeEvent* _pEvt)
{
	//由于删除事件是采用倒替的方案,所以遍历要倒数,以免遗漏
	for(int i=m_vTimeEvt.size()-1; i>=0; i--)
	{
		if( _pEvt == m_vTimeEvt[i])
		{
			(*m_vTimeEvt[i])(m_vTimeEvt[i]->EVT_START_TIME, m_vTimeEvt[i]->EVT_STEP_TIME, GetCurTime());
			return;
		}
	}
}



 

/******************************************************************** 
 file name : 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 "TimeEvtCenter.h"

using namespace std;

class NPC
{
public:
	NPC(){ m_pTimeEvtCallBack = new TimeEvtCallBack(7, this, &NPC::test); }
	void test(int _iStartTime, int _iStep, int _iCurTime)
	{
		cout<<"NPC心跳事件发生"<<endl;
		if( _iCurTime+_iStep > 50)
			delete m_pTimeEvtCallBack;
	}

private:
	TimeEvtCallBack* m_pTimeEvtCallBack;
};
void main()
{
	NPC npc;
	for(int i=0; i<=100; i++)
	{
		cout<<"现在时间: "<<g_pTimeCallBackCenter->GetCurTime()<<endl;
		g_pTimeCallBackCenter->OnEvent();
		g_pTimeCallBackCenter->UpdataCurTime();
	}
	delete g_pTimeCallBackCenter;
	system("pause");  
}


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值