提供常量_转_变量的适配器
来源:互联网 发布:多多印 知乎 编辑:程序博客网 时间:2024/06/08 18:30
/******************************************************************** file name : const2buffer.h author : Clark/陈泽丹 created : 2011-11-9 purpose : 提供常量"转"变量的适配器*********************************************************************/#pragma once#include <tchar.h>namespace CLK{//常量转变量template<class T>class const2buffer{public:static const2buffer c2b(const T* _pConst, const int _iLen) throw(const TCHAR*){//结果用值传递比用局部,引用, 指针,静态传出结果的作法更具安全意义try{ return const2buffer(_pConst, _iLen); }catch(const TCHAR* pError){ throw pError; }}explicit const2buffer(const T* _pConst, const int _iLen) throw(const TCHAR*): m_pBuf(NULL) {init(_pConst, _iLen);}const2buffer(const const2buffer& other) throw(const TCHAR*):m_pBuf(NULL) {//有内存操作,传参时需注意class objThis = other时出现浅拷贝问题init(other.m_pBuf, other.m_iLen);}const2buffer& operator = (const const2buffer& other) throw(const TCHAR*){//有内存操作,传参时需注意objThis = other时出现浅拷贝问题init(other.m_pBuf, other.m_iLen);return *this;}~const2buffer(){uninit();}operator T*(){return m_pBuf;}operator T(){return m_pBuf[0];}private:void init(const T* _pConst, const int _iLen){uninit();m_iLen = _iLen;m_pBuf = new T[m_iLen+1]; if( NULL == m_pBuf){TCHAR _tcsError[64];_stprintf(_tcsError,_T("Failed in const2buffer::new[%d]\n"),m_iLen);throw _tcsError;} memset(m_pBuf,0,(m_iLen+1)*sizeof(T));memcpy(m_pBuf,_pConst,m_iLen*sizeof(T));}void uninit(){if( NULL != m_pBuf){delete[] m_pBuf;m_pBuf = NULL;}}T* m_pBuf;int m_iLen;};//单字节转双字节class a2w { public: explicit a2w():buffer(0){} explicit a2w(const char* str) throw(const TCHAR*):buffer(0) { try { init(str); } catch(const TCHAR* pError){ throw pError; } } a2w(a2w& other) throw(const TCHAR*):buffer(0) { try { *this = other; } catch(const TCHAR* pError){ throw pError; } } void init(const char* str) throw(const TCHAR*) { if( NULL != buffer) delete[] buffer; if(NULL != str) { int nLen = ::MultiByteToWideChar(CP_ACP,0,str,-1,NULL,0); buffer = new wchar_t[nLen+1];if( NULL == buffer) { buffer = NULL; TCHAR _tcsError[256]; _stprintf(_tcsError,_T("Failed in a2w -> init -> new wchar_t[%d]\n"),nLen+1); throw _tcsError; } memset(buffer,0,(nLen+1)*sizeof(wchar_t)); ::MultiByteToWideChar(CP_ACP,0,str,-1,buffer,nLen); buffer[nLen] = 0; } } ~a2w() { delete[] buffer; } a2w& operator=(a2w& other) throw(const TCHAR*) { if( NULL != buffer) delete[] buffer; if(NULL != other.buffer) { int nLen = wcslen(other.buffer); buffer = new wchar_t[nLen+1];if( NULL == buffer) { buffer = NULL; TCHAR _tcsError[256]; _stprintf(_tcsError,_T("Failed in a2w::init -> new wchar_t[%d]\n"),nLen+1); throw _tcsError; } memset(buffer,0,(nLen+1)*sizeof(wchar_t)); wcscpy(buffer,other.buffer); buffer[nLen] = 0; } return *this; } operator const wchar_t*() { return buffer; } private: wchar_t* buffer; }; //双字节转单字节class w2a { public: explicit w2a():buffer(0){} explicit w2a(const wchar_t* str) throw(const TCHAR*):buffer(0) { try { init(str); } catch(const TCHAR* pError){ throw pError; } } w2a(w2a& other) throw(const TCHAR*):buffer(0) { try { *this = other; } catch(const TCHAR* pError){ throw pError; } } void init(const wchar_t* str) throw(const TCHAR*) { if( NULL != buffer) delete[] buffer; if(NULL != str) { int nLen = WideCharToMultiByte(CP_ACP, 0, str, -1, NULL, 0, NULL, NULL); buffer = new char[nLen+1]; if( NULL == buffer) { buffer = NULL; TCHAR _tcsError[256]; _stprintf(_tcsError,_T("Failed in w2a::init -> new char[%d]\n"),nLen+1); throw _tcsError; } memset(buffer,0,(nLen+1)*sizeof(char)); ::WideCharToMultiByte (CP_ACP, 0,str, -1,buffer , nLen, NULL,NULL); buffer[nLen] = 0; } } ~w2a() { delete[] buffer; } w2a& operator=(w2a& other) throw(const TCHAR*) { if( NULL != buffer) delete[] buffer; if(NULL != other.buffer) { int nLen = strlen(other.buffer); buffer = new char[nLen+1]; if( NULL == buffer) { buffer = NULL; TCHAR _tcsError[256]; _stprintf(_tcsError,_T("Failed in w2a::init -> new char[%d]\n"),nLen+1); throw _tcsError; } memset(buffer,0,(nLen+1)*sizeof(char)); strcpy(buffer,other.buffer); buffer[nLen] = 0; } return *this; } operator const char*() { return buffer; } private: char *buffer; }; }//测试 ------------------------------CLK::const2buffer<TCHAR> Test(){TCHAR szTemp[100];_tcscpy(szTemp,L"123456789");return CLK::const2buffer<TCHAR>(szTemp, _tcslen(szTemp));}try{const int TEMP_SIZE = 100;TCHAR tcsTemp[TEMP_SIZE];_stprintf(tcsTemp,_T("char* = %s\n"), (TCHAR*)CLK::const2buffer<TCHAR>::c2b(L"123",_tcslen(L"123")));MessageBox(tcsTemp);_stprintf(tcsTemp,_T("int = %d\n"), (int)CLK::const2buffer<int>::c2b(&TEMP_SIZE,1));MessageBox(tcsTemp);MessageBox(Test());}catch (const TCHAR* e){MessageBox(e);}//测试结果: 功能正常,无内存漏露
- 提供常量_转_变量的适配器
- 关键字_标识符_常量_变量_数据类型
- PHP初学_变量常量
- 12_变量_成员变量和局部变量_常量_命名规范
- C#_基础(一):常量&变量
- 课后总结:1.3_变量和常量
- 20150527常量变量_练习_运算符表达式练习
- 黑马程序员_03_标示符_常量_进制_变量_类型转换
- perl的变量@_
- java学习之旅12--变量_成员变量和局部变量_常量_命名规范
- 黑马程序员_Java基础[2]_数据类型、常量、变量、进制间的换算
- Windows 常见数据类型及变量_常量命名规律
- 黑马程序员_JAVA语言基础组成_常量、变量
- Windows常见数据类型及变量_常量命名规律
- 黑马程序员_java语言基础_常量变量运算符
- Java基础学习2_变量与常量
- [C++][基础]1_变量、常量和基本类型
- 第01章 CORE C++_C++ 基本结构_注释_输出语句_输入语句_初始化_条件控制_ASCII码值_转义字符_常量_变量_类型长度_运算符
- sicily 1342 (0/1背包问题)
- iphone开发-SQLite数据库使用
- 找不到php_mbstring.dll或php_exif.dll的解决方法与mb_系列函数的用法 libmcrypt.dll时出错的处理
- iphone 开发开放库-ASIHTTPRequest类库简介和使用说明
- hibernate 主键自增
- 提供常量_转_变量的适配器
- Linux--常用命令--date
- Linux设备模型<二>底层模型
- 程序什么时候应该使用线程,什么时候单线程效率高
- iphone SQLite3使用总结
- Fedora 14下的yum源
- oracle中主键自增
- vmware tools ,/mnt/hgfs 下没有共享文件夹,解决办法
- MySQL5.5编译工具configure向cmake过渡指南(转载)