时间守卫

本文介绍了一个名为CLK_Switch的模板类,该类用于实现一种高效的消息分发机制。CLK_Switch通过递归地使用模板特化来模拟Switch Case语句的功能,支持在特定消息类型到达时调用相应对象的dispatch方法。此外,还提供了一个辅助类Time_Guard,用于测量代码段的执行时间。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

/********************************************************************
*  文件名:   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;
};

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值