DebugPrint和字符串转换

#pragma once

#pragma warning (disable:4996)

#define MAXLEN 256

#include <iostream>

namespace _Liuxb
{
	class Conver
	{
	public:
		static CString PChar2CString(const char* pParam)
		{
			return CString(pParam);
		}

		static CString PTCHAR2CString(const TCHAR* pParam)
		{
			return CString(pParam);
		}

		static TCHAR* CString2PTCHAR(CString &str,TCHAR* pTCHAR,int len)
		{
			TCHAR* pTemp = pTCHAR;
			if(len>=str.GetLength()&&pTemp!=0)
			{
				lstrcpy(pTemp,str.GetBuffer(str.GetLength()));
				str.ReleaseBuffer();
			}
			else
			{
				pTemp = 0;
			}
			return pTemp;
		}

		static char* TCHAR2Char(const TCHAR* pTCHAR,char* pCHAR,int charlen)
		{
			char* pTemp = pCHAR;
			int tcharLen = 2*wcslen(pTCHAR);
			if(tcharLen+1>charlen)
			{
				pTemp = 0;
			}
			else
			{
				wcstombs(pTemp,pTCHAR,tcharLen+1);
			}
			return pTemp;
		}

		static char* CString2PChar(CString& str,char* pChar,int len)
		{
			int size = 2*str.GetLength();
			char* pTemp = pChar;
			if(size+1>len)
			{
				pTemp = 0;
			}
			else
			{
				//wcstombs(pTemp,str,size+1);
				USES_CONVERSION; 
				strcpy((LPSTR)pTemp,OLE2A(str.LockBuffer()));
			}
			return pTemp;
		}

		static int CStringToInt(const CString& srv)
		{
			return _tstoi(LPCTSTR(srv));
		}

		static CString IntToCString(int val)
		{
			CString text;
			text.Format(_T("%d"),val);
			return text;
		}
	};
	
	template<BOOL ErrorMsg>
	class MsgPrint
	{
	public:
		MsgPrint(CString& msg):_msg(msg)
		{

		}

		MsgPrint(LPCTSTR msg):_msg(msg)
		{

		}

	public:
		void operator ()()
		{
			::MessageBox(NULL,(LPCTSTR)_msg,_T("Error"),MB_OK);
		}
	private:
		MsgPrint(MsgPrint& error);
	private:
		CString _msg;
	};

	template<>
	class MsgPrint<FALSE>
	{
	public:
		MsgPrint(CString& msg):_msg(msg)
		{

		}

		MsgPrint(LPCTSTR msg):_msg(msg)
		{

		}

	public:
		void operator ()()
		{
			::MessageBox(NULL,(LPCTSTR)_msg,_T("Message"),MB_OK);
		}
	private:
		MsgPrint(MsgPrint& error);
	private:
		CString _msg;
	};


	///用于debug跟踪测试内容
	template<BOOL IsDebug>
	class DebugPrint
	{
	public:
		DebugPrint(CString printInfo)
		{
			_printInfo = CString(printInfo);
		}
		DebugPrint(TCHAR* pPrintInfo)
		{
			_printInfo = CString(pPrintInfo);
		}

		void operator ()()
		{
			char pTemp[MAXLEN];
			memset(pTemp,0,MAXLEN);
			Conver::CString2PChar(_printInfo,pTemp,MAXLEN);
			std::cout<<pTemp<<std::endl;
		}

		inline DebugPrint operator+(DebugPrint& print)
		{
			_printInfo = _printInfo + print._printInfo;
			return *this;
		}

		inline DebugPrint operator+(CString& print)
		{
			_printInfo = _printInfo + print;
			return *this;
		}

		inline DebugPrint operator+(const TCHAR* pPrint)
		{
			CString print(pPrint);
			*this = *this+print;
			return *this;
		}

		~DebugPrint(void)
		{}

	private:
		CString _printInfo;
	};

	///Release版本调试信息
	template<>
	class DebugPrint<FALSE>
	{
	public:
		DebugPrint(CString printInfo)
		{
		}
		DebugPrint(TCHAR* pPrintInfo)
		{
		}

		void operator ()()
		{

		}

		inline DebugPrint operator+(DebugPrint& print)
		{
			return *this;
		}

		inline DebugPrint operator+(CString& print)
		{
			return *this;
		}

		inline DebugPrint operator+(const TCHAR* pPrint)
		{
			return *this;
		}

		~DebugPrint(void)
		{}

	private:
		CString _printInfo;
	};
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值