/********************************************************************
* 文件名: CLK_LIB.h
* 文件描述: Switch_Case对象
* 创建人: 陈泽丹 ,2012年8月27日
* 版本号: 1.0
* 修改记录:
********************************************************************/
#include <iostream>
#include <tchar.h>
#include <windows.h>
#include <map>
#include <algorithm>
using namespace std;
#ifdef _DEBUG
#define TIME_GUARD( _x ) Time_Guard( _x )
#else
#define TIME_GUARD( _x ) NULL
#endif // _DEBUG
//时间守卫 - 用于测试经历时间
class Time_Guard
{
public:
Time_Guard(const char* _p_caption)
{
size_t len = strlen(_p_caption);
if( len < MAX_PATH )
{
sprintf(m_caption,"%s", _p_caption);
}
else
{
sprintf(m_caption, "%s", "Error: caption is over stack! ");
cout<<"Error: "<<_p_caption<<"is over stack!"<<endl;
}
m_time = GetTickCount();
}
~Time_Guard()
{
ULONG tm = GetTickCount() - m_time;
cout<<m_caption<<": "<<tm<<" (ms)"<<endl;
}
private:
char m_caption[256];
ULONG m_time;
};
//Switch_Case对象
template< template< int v > class _PAR_TYPE, int _start, int _end>
class CLK_Switch
{
public:
template <class _PtrObj>
static bool switch_case(const _PtrObj& _p, const int _msg_type_ID, void* buf)
{
if( _msg_type_ID < _start || _msg_type_ID > _end || _start > _end)
return false;
const int POS = (_start + _end)/2;
if( POS == _msg_type_ID)
{
_PAR_TYPE< POS >* p_msg = ( _PAR_TYPE< POS >* ) buf;
_p->dispatch( p_msg );
return true;
}
else if( _msg_type_ID < POS )
{
return CLK_Switch<_PAR_TYPE, _start, POS>::switch_case(_p, _msg_type_ID, buf);
}
else
{
return CLK_Switch<_PAR_TYPE, POS+1, _end>::switch_case(_p, _msg_type_ID, buf);
}
}
};
template< class _Local_Type>
class CLK_Base_Case
{
public:
CLK_Base_Case(_Local_Type* _p_local):m_p_local(_p_local){}
virtual ~CLK_Base_Case(){ m_p_local = NULL; }
virtual void operator()(const int _msg_type_ID, void* buf) = 0;
protected:
_Local_Type* m_p_local;
};
时间守卫
最新推荐文章于 2022-06-29 20:08:18 发布