游戏开发论坛

 找回密码
 立即注册
搜索
查看: 9906|回复: 9

请问两个LPCWSTR字符串怎么才能连接在一起?

[复制链接]

83

主题

169

帖子

202

积分

中级会员

Rank: 3Rank: 3

积分
202
发表于 2008-2-5 20:01:00 | 显示全部楼层 |阅读模式
如题

3

主题

121

帖子

121

积分

注册会员

Rank: 2

积分
121
QQ
发表于 2008-2-6 14:46:00 | 显示全部楼层

Re:请问两个LPCWSTR字符串怎么才能连接在一起?

StringCchCat

97

主题

590

帖子

590

积分

高级会员

Rank: 4

积分
590
QQ
发表于 2008-2-6 20:54:00 | 显示全部楼层

Re:请问两个LPCWSTR字符串怎么才能连接在一起?

boost::lexical_cast<LPWCSTR>(str)

5

主题

68

帖子

75

积分

注册会员

Rank: 2

积分
75
QQ
发表于 2008-2-8 22:05:00 | 显示全部楼层

Re:请问两个LPCWSTR字符串怎么才能连接在一起?

wcscat_s

32

主题

1583

帖子

1589

积分

金牌会员

Rank: 6Rank: 6

积分
1589
发表于 2008-2-10 02:55:00 | 显示全部楼层

Re:请问两个LPCWSTR字符串怎么才能连接在一起?

同楼上。

83

主题

169

帖子

202

积分

中级会员

Rank: 3Rank: 3

积分
202
 楼主| 发表于 2008-2-12 22:36:00 | 显示全部楼层

Re:请问两个LPCWSTR字符串怎么才能连接在一起?

好象不行

如果有
LPCWSTR pStr1;
LPCWSTR pStr2;
LPCWSTR pStr3;
我想实现的是
pStr3=pStr1+pStr2;(实际上这样做不行)

用什么方法可以实现

362

主题

3023

帖子

3553

积分

论坛元老

Rank: 8Rank: 8

积分
3553
发表于 2008-2-13 10:39:00 | 显示全部楼层

Re:请问两个LPCWSTR字符串怎么才能连接在一起?

//instStr.h
#ifndef INST_STR_H
#define INST_STR_H


#include <instTypes.h>
#include <instDefines.h>


namespace inst
{


typedef UInt32 CODEPAGE;
//win32获取某Locale对应的CP的方法:::GetLocaleInfo(locale, 11 ,...)
class CStr
{
private:
        WChar *m_pBuf;
        UInt32 m_Len; //当len==0时,m_pBuf[0]=='0',size==len+1
public:
        static CODEPAGE GetSystemCodePage();
private:
        static CODEPAGE CP(CODEPAGE cp){ return        cp?cp:GetSystemCodePage(); }
private:
        CStr(UInt32 len,Int32 unused); //不为数组每个元素初始化
public:

        //ADT运算操作

        CStr();
        CStr(const CStr &str);
        CStr(const Char *chars); //非常慢!建议不要用这个来初始化!
        CStr(const Char *chars,CODEPAGE cp); //非常慢!建议不要用这个来初始化!
        CStr(const WChar *wchars);

        CStr(Int32 n);
        CStr(UInt32 n);
        CStr(Float f);
        CStr(Double d);
        CStr(Bool b);
        ~CStr();

        Int32 GetLen()const{ return m_Len; }

        CStr &operator =(const CStr &str);
//        CStr &operator =(const Char *chars); //非常慢!建议不要用这个!
//        CStr &operator =(const WChar *wchars);

        Bool operator ==(const CStr &str)const;
//        Bool operator ==(const Char *chars)const; //非常慢!建议不要用这个!
//        Bool operator ==(const WChar *wchars)const;

        Bool operator !=(const CStr &str)const;
//        Bool operator !=(const Char *chars)const; //非常慢!建议不要用这个!
//        Bool operator !=(const WChar *wchars)const;

        CStr &operator +=(const CStr &str);
//        CStr &operator +=(const Char *chars); //非常慢!建议不要用这个!
//        CStr &operator +=(const WChar *wchars);

        CStr operator +(const CStr &str)const;
//        CStr operator +(const Char *chars)const; //非常慢!建议不要用这个!
//        CStr operator +(const WChar *wchars)const;

        //缓冲区操作

        static CStr &CreateUninitializedStr(Int32 len){ return *(new CStr(len,0)); }

        //必须要分开搞两个函数,否则会出现const错误
        const WChar *ReadW()const{ return m_pBuf; } //这个很常用
        WChar *ReadWriteW(){ return m_pBuf; }

        //用户需要自己delete[],否则内存泄漏!
        //大多情况下,ansi字符串尾的多余的\0是不要紧的
        Char *ToCharArrayA()const{ return ToCharArrayA(0); } //很慢!建议不要用!
        Char *ToCharArrayA(CODEPAGE cp)const;  //很慢!建议不要用!
        WChar *ToCharArrayW()const; //建议尽量不要用!为了更快,大多场合用ReadW()即可。

#ifdef TmpCharArrayA
#undef TmpCharArrayA
#endif
#define TmpCharArrayA(str) (AutoArrayPtr<Char>((str).ToCharArrayA()).GetPtr())

#ifdef TmpCharArrayAEx
#undef TmpCharArrayAEx
#endif
#define TmpCharArrayAEx(str,cp) (AutoArrayPtr<Char>((str).ToCharArrayA(cp)).GetPtr())

#ifdef TmpCharArrayW
#undef TmpCharArrayW
#endif
#define TmpCharArrayW(str) (AutoArrayPtr<WChar>((str).ToCharArrayW()).GetPtr())

        //pos均从0开始!(改了~

        //这里为了速度!!不可以检查边界!!
        Char GetCharA(UInt32 pos);
        WChar GetCharW(UInt32 pos){ return m_pBuf[pos]; }
        void SetCharA(UInt32 pos,Char ch);
        void SetCharW(UInt32 pos,WChar wch){ m_pBuf[pos]=wch; }

        static void BltA(CStr &dest,UInt32 posdest,UInt32 lendest,const Char *src,UInt32 possrc,UInt32 lensrc);
        static void BltA(Char *dest,UInt32 posdest,UInt32 lendest,const CStr &src,UInt32 possrc,UInt32 lensrc);
        static void BltA(Char *dest,UInt32 posdest,UInt32 lendest,const Char *src,UInt32 possrc,UInt32 lensrc);

        static void BltExA(CStr &dest,UInt32 posdest,UInt32 lendest,CODEPAGE cpdest,const Char *src,UInt32 possrc,UInt32 lensrc,CODEPAGE cpsrc);
        static void BltExA(Char *dest,UInt32 posdest,UInt32 lendest,CODEPAGE cpdest,const CStr &src,UInt32 possrc,UInt32 lensrc,CODEPAGE cpsrc);
        static void BltExA(Char *dest,UInt32 posdest,UInt32 lendest,CODEPAGE cpdest,const Char *src,UInt32 possrc,UInt32 lensrc,CODEPAGE cpsrc);

        static void BltFastW(CStr &dest,UInt32 posdest,const CStr &src,UInt32 possrc,UInt32 len);
        static void BltFastW(CStr &dest,UInt32 posdest,const WChar *src,UInt32 possrc,UInt32 len);
        static void BltFastW(WChar *dest,UInt32 posdest,const CStr &src,UInt32 possrc,UInt32 len);
        static void BltFastW(WChar *dest,UInt32 posdest,const WChar *src,UInt32 possrc,UInt32 len);

        static Int32 GetCharArrayBinaryLenA(Char *chars);
        static Int32 GetCharArrayBinaryLenW(WChar *wchars);
        static Int32 GetCharArrayLenA(Char *chars);
        static Int32 GetCharArrayLenW(WChar *wchars);

        //高级操作

        CStr &ToLowerCase();
        CStr &ToUpperCase();
        CStr &ToHiragana();
        CStr &ToKatakana();

};

}// end of namespace inst


#endif


//instStr.cpp
#ifndef INST_STR_CPP
#define INST_STR_CPP


#include <instDefines.h>
#include <instStr.h>

#include <windows.h>


namespace inst
{


__forceinline CODEPAGE CStr::GetSystemCodePage()
{
        return ::GetACP();
}

CStr::CStr()
{
        m_pBuf=new WChar[1+(m_Len=0)];
}

CStr::CStr(UInt32 len,Int32 unused)
{
        m_pBuf=new WChar[1+(m_Len=len)];
}

CStr::CStr(const CStr &str)
{
        m_pBuf=new WChar[1+(m_Len=str.m_Len)];
        for(UInt32 i=0;i<m_Len;i++) //不 包括字符串尾
                m_pBuf=str.m_pBuf;
        m_pBuf[m_Len]=0;
}

CStr::CStr(const Char *chars) //非常慢!建议不要用这个来初始化!
{
        UInt32 i=0;
        while(chars!=0)
                ++i;
        UInt32 len=i;
        WChar *tmp=new WChar[len+1];
        for(i=0;i<len;i++)  //不 包括字符串尾 //其实只需要填充后半部分!
                tmp=0;
        tmp[len]=0;
        ::MultiByteToWideChar(GetSystemCodePage(),0,chars,len,tmp,len);
        i=0;
        while(tmp!=0) //其实如果倒过来搜索可以加速
                ++i;
        m_Len=i;
        //或者把下列代码改为m_pBuf=tmp;
        m_pBuf=new WChar[m_Len+1];
        for(i=0;i<m_Len;i++) //不 包括字符串尾
                m_pBuf=tmp;
        m_pBuf[m_Len]=0;
}

CStr::CStr(const Char *chars,CODEPAGE cp) //非常慢!建议不要用这个来初始化!
{
        UInt32 i=0;
        while(chars!=0)
                ++i;
        UInt32 len=i;
        WChar *tmp=new WChar[len+1];
        for(i=0;i<len;i++)  //不 包括字符串尾 //其实只需要填充后半部分!
                tmp=0;
        tmp[len]=0;
        ::MultiByteToWideChar(CP(cp),0,chars,len,tmp,len);
        i=0;
        while(tmp!=0) //其实如果倒过来搜索可以加速
                ++i;
        m_Len=i;
        //或者把下列代码改为m_pBuf=tmp;
        m_pBuf=new WChar[m_Len+1];
        for(i=0;i<m_Len;i++) //不 包括字符串尾
                m_pBuf=tmp;
        m_pBuf[m_Len]=0;
}

CStr::CStr(const WChar *wchars)
{
        UInt32 i=0;                        //UInt32 pos=1;
        while(wchars!=0) //while(wchars[pos-1]!=0)
                ++i;                        //        ++pos;
        m_Len=i;                        //m_Len=pos-1;
        m_pBuf=new WChar[m_Len+1];
        for(i=0;i<m_Len;i++) //不 包括字符串尾
                m_pBuf=wchars;
        m_pBuf[m_Len]=0;
}

CStr::~CStr()
{
        SafeDelArray(m_pBuf);
}

//建议使用CStr来相互运算,否则慢

CStr &CStr:perator =(const CStr &str)
{
        if(&str==this)return *this;
        SafeDelArray(m_pBuf);
        m_pBuf=new WChar[1+(m_Len=str.m_Len)];
        for(UInt32 i=0;i<m_Len;i++) //不 包括字符串尾
                m_pBuf=str.m_pBuf;
        m_pBuf[m_Len]=0;
        return *this;
}

Bool CStr::operator ==(const CStr &str)const
{
        if(m_Len!=str.m_Len)return False;
        for(UInt32 i=0;i<m_Len;i++) //不 包括字符串尾
                if(m_pBuf!=str.m_pBuf)return False;
        return True;
}

Bool CStr::operator !=(const CStr &str)const
{
        if(m_Len!=str.m_Len)return False;
        for(UInt32 i=0;i<m_Len;i++) //不 包括字符串尾
                if(m_pBuf!=str.m_pBuf)return True;
        return False;
}

CStr &CStr::operator +=(const CStr &str)
{
        WChar *tmp=new WChar[1+m_Len+str.m_Len];
        for(UInt32 i=0;i<m_Len;i++) //不 包括字符串尾
                tmp=m_pBuf;
        for(UInt32 j=0;j<str.m_Len;j++) //不 包括字符串尾
                tmp[m_Len+j]=str.m_pBuf[j];
        tmp[m_Len+str.m_Len]=0;
        SafeDelArray(m_pBuf);
        m_pBuf=tmp;
        m_Len=m_Len+str.m_Len;
        return *this;
}

CStr CStr::operator +(const CStr &str)const
{
        CStr ret=CStr(m_Len+str.m_Len,0); //不为数组每个元素初始化
        for(UInt32 i=0;i<m_Len;i++) //不 包括字符串尾
                ret.m_pBuf=m_pBuf;
        for(UInt32 j=0;j<str.m_Len;j++) //不 包括字符串尾
                ret.m_pBuf[m_Len+j]=str.m_pBuf[j];
        ret.m_pBuf[m_Len+str.m_Len]=0;
        return ret; //拷贝过去!不能传递引用!否则内存泄漏!看看MFC的STRCORE.CPP
}

//大多情况下,ansi字符串尾的多余的\0是不要紧的
Char *CStr::ToCharArrayA(CODEPAGE cp)const
{
        Char *tmp=new Char[m_Len*2+1];
        for(UInt32 i=0;i<m_Len*2;i++) //不 包括字符串尾 //其实只需要填充后半部分!
                tmp=0;
        tmp[m_Len*2]=0;
        ::WideCharToMultiByte(CP(cp),0,m_pBuf,m_Len,tmp,m_Len*2,0,False);
        return tmp;
}

WChar *CStr::ToCharArrayW()const
{
        WChar *ret=new WChar[m_Len+1];
        for(UInt32 i=0;i<m_Len;i++) //不 包括字符串尾
                ret=m_pBuf;
        ret[m_Len]=0;
        return ret;
}

void CStr::BltFastW(WChar *dest,UInt32 posdest,const CStr &src,UInt32 possrc,UInt32 len)
{
        if(!dest)return;
        if(src.m_Len<=0)return;
       
        if(possrc<0)possrc=0;
        if(possrc+len > src.m_Len)len=src.m_Len-possrc;
        if(posdest<0)posdest=0;

        for(UInt32 i=0;i<len;i++)
        {
                dest[posdest+i]=src.m_pBuf[possrc+i];
        }       
}


}// end of ns inst


#endif

7

主题

438

帖子

438

积分

中级会员

Rank: 3Rank: 3

积分
438
发表于 2008-2-13 10:48:00 | 显示全部楼层

Re:请问两个LPCWSTR字符串怎么才能连接在一起?

std::wstring result = std::wstring(str1)+std::wstring(str2);
应该可以。

362

主题

3023

帖子

3553

积分

论坛元老

Rank: 8Rank: 8

积分
3553
发表于 2008-2-13 10:58:00 | 显示全部楼层

Re:请问两个LPCWSTR字符串怎么才能连接在一起?

例子:

CStr str1 = "hello"; //比较慢
CStr str2 = L"world"; //比较快

CStr str3 = CStr( "HELLO" );  //比较慢
CStr str4 = CStr( L"WORLD" ); //比较快

CStr str5 = CStr( "?f?[?^" , 932 ); //932 - Japanese
//这样做就没有乱码了。因为可以手工设置代码页。

CStr str6 = str1;
str1=str2+str3;
str3+=str4;
str4 = L"wchar abc" + "char kkk" + str5;
str5+=L"wchar qqqq";
str5+="char123456"
------------------------------------------
const wchar_t *unicode1 = str1.ReadW(); //读取缓冲区
wchar_t *unicode2 = str1.ReadWrite(); //读写缓冲区
//以上这种方式,只能用于Unicode。

wchar_t *unicode3 = str1.ToCharArrayW(); //创建一个数组
delete[] unicode3; //删除数组

char *ansi = str1.ToCharArrayA(); //创建一个数组
delete [] ansi; //删除数组
char *japanese = str5.ToCharArrayA( 932 );  //创建一个数组
delete[] japanese; //删除数组


尽量不要输出 Ansi.因为比较慢,也比较麻烦。
不过。。。D3DX9里面的CompileShader,AssembleShader,不支持Unicode...

使用的时候比较麻烦,还要手工删除数组。

推荐用AutoArrayPtr<char>模版:

D3DXCompileShader(  AutoArrayPtr<char>( ShaderString.ToCharArrayA() ).GetPtr() ...  );

如果还是嫌麻烦,用这个宏:
#define TmpCharArrayA(str)  (AutoArrayPtr<char>((str).ToCharArrayA()).GetPtr())

D3DXCompileShader(  TmpCharArrayA( ShaderString ) ...  );


362

主题

3023

帖子

3553

积分

论坛元老

Rank: 8Rank: 8

积分
3553
发表于 2008-2-13 11:05:00 | 显示全部楼层

Re:请问两个LPCWSTR字符串怎么才能连接在一起?

要想编译成功,需要以下2个头文件:
注意你要 using namespace inst;

//instTypes.h
#ifndef INST_TYPES_H
#define INST_TYPES_H


namespace inst
{
        //为了输入方便,基本数据类型只是首字母大写
        //但inst命名空间中其他的typedef一律全部大写!
        //在inst命名空间里,基本数据类型一律用这些typedef。

        //这些定义在win32下是完全可用的,
        //但是其他平台则应该使用其他头文件

        typedef char                        Byte;
        typedef int                                Int;
        typedef short                        Int16;
        typedef long                        Int32;
        typedef bool                        Bool;
        typedef float                        Float;
        typedef double                        Double;

        typedef unsigned char        UByte;
        typedef unsigned int        UInt;
        typedef unsigned short        UInt16;
        typedef unsigned long        UInt32;

//有的编译器对wchat_t没有定义
//但是,不能擅自地直接使用unsigned short来定义WChar,
//因为在有的编译器里,wchar_t不等于unsigned short
#ifndef _WCHAR_T_DEFINED
typedef unsigned short wchar_t;
//#define _WCHAR_T_DEFINED
#endif

        typedef char                        Char;        //在win32平台,等同于CHAR而无需类型转换
        typedef wchar_t                        WChar;        //在win32平台,等同于WCHAR而无需类型转换

        const Bool True                =        true;
        const Bool False        =        false;

}


#endif

//instDefines.h
#ifndef INST_DEFINES_H
#define INST_DEFINES_H


#ifdef NULL
#undef NULL
#endif
#define NULL 0

#ifdef null
#undef null
#endif
#define null 0

#ifdef Null
#undef Null
#endif
#define Null 0


#ifdef SAFEDEL
#undef SAFEDEL
#endif
#define SAFEDEL(x) if(x) delete x

#ifdef safedel
#undef safedel
#endif
#define safedel(x) if(x) delete x

#ifdef SafeDel
#undef SafeDel
#endif
#define SafeDel(x) if(x) delete x


#ifdef SAFEDELARRAY
#undef SAFEDELARRAY       
#endif
#define SAFEDELARRAY(x) if(x) delete[] x

#ifdef safedelarray
#undef safedelarray
#endif
#define safedelarray(x) if(x) delete[] x

#ifdef SafeDelArray
#undef SafeDelArray
#endif
#define SafeDelArray(x) if(x) delete[] x


#ifdef SAFERELEASE
#undef SAFERELEASE
#endif
#define SAFERELEASE(x) if(x) x->Release()

#ifdef saferelease
#undef saferelease
#endif
#define saferelease(x) if(x) x->Release()

#ifdef SafeRelease
#undef SafeRelease
#endif
#define SafeRelease(x) if(x) x->Release()


#ifdef MIN
#undef MIN
#endif
#define MIN(x,y)        ((x)<(y)?(x)y))

#ifdef min
#undef min
#endif
#define min(x,y)        ((x)<(y)?(x):(y))

#ifdef Min
#undef Min
#endif
#define Min(x,y)        ((x)<(y)?(x):(y))


#ifdef MAX
#undef MAX
#endif
#define MAX(x,y)        ((x)>(y)?(x):(y))

#ifdef max
#undef max
#endif
#define max(x,y)        ((x)>(y)?(x):(y))

#ifdef Max
#undef Max
#endif
#define Max(x,y)        ((x)>(y)?(x):(y))


#ifdef GET_SET
#undef GET_SET
#endif

#define GET_SET(p1,p2,p3,type,x,g,s)p1: type x;p2: type g()const{return x;}p3: void s(type val){x=val;}

#ifdef Get_Set
#undef Get_Set
#endif
#define Get_Set GET_SET

#ifdef get_set
#undef get_set
#endif       
#define Get_Set GET_SET


#ifdef GET_SET_Q
#undef GET_SET_Q
#endif
#define GET_SET_Q(type,x,g,s) GET_SET(private,public,public,type,x,g,s)

#ifdef Get_Set_Q
#undef Get_Set_Q
#endif
#define Get_Set_Q GET_SET_Q

#ifdef get_set_q
#undef get_set_q
#endif
#define get_set_q GET_SET_Q


#ifdef GET_SET_FUNC
#undef GET_SET_FUNC
#endif

#define GET_SET_FUNC(p2,p3,type,x,g,s)p2: type g()const{return (x);}p3: void s(type val){(x)=val;}

#ifdef Get_Set_Func
#undef Get_Set_Func
#endif
#define Get_Set_Func GET_SET_FUNC

#ifdef get_set_func
#undef get_set_func
#endif       
#define Get_Set_func GET_SET_FUNC


#ifdef GET_SET_FUNC_Q
#undef GET_SET_FUNC_Q
#endif
#define GET_SET_FUNC_Q(type,x,g,s) GET_SET_FUNC(public,public,type,x,g,s)

#ifdef Get_Set_Func_Q
#undef Get_Set_Func_Q
#endif
#define Get_Set_Func_Q GET_SET_FUNC

#ifdef get_set_func_q
#undef get_set_func_q
#endif
#define get_set_func_q GET_SET_FUNC
       

template <class dest, class src> struct _UNIONCAST
{
        union
        {
                dest d;
                src s;
        };
};

template <class dest, class src> dest union_cast(src s)
{
        _UNIONCAST<dest,src> tmp;
        tmp.s=s;
        return tmp.d;
}


#include <instTypes.h>


namespace inst
{


class __obj{};

typedef Int32 POS;
typedef UInt32 TIME;


//对于COLOR不需要使用引用传递(因为COLOR本身才4字节==1个指针)
typedef UInt32 COLOR;

__forceinline COLOR ARgb(UByte a,UByte r,UByte g,UByte b)
{
        return a<<24 | r<<16 | g<<16 | b;
}

__forceinline COLOR Rgb(UByte r,UByte g,UByte b)
{
        return ARgb(0xff,r,g,b);
}

__forceinline UByte GetA(COLOR color){ return (UByte)( (color&0xff000000) >> 24 ); }
__forceinline UByte GetR(COLOR color){ return (UByte)( (color&0x00ff0000) >> 16 ); }
__forceinline UByte GetG(COLOR color){ return (UByte)( (color&0x0000ff00) >> 8 ); }
__forceinline UByte GetB(COLOR color){ return (UByte)( (color&0x000000ff) ); }

typedef struct _FLOATCOLOR
{
    Float r;
    Float g;
    Float b;
    Float a;
        _FLOATCOLOR()
        {
                r=0.0f;
                g=0.0f;
                b=0.0f;
                a=0.0f;
        }
        _FLOATCOLOR(const _FLOATCOLOR &fc)
        {
                r=fc.r;
                g=fc.g;
                b=fc.b;
                a=fc.a;
        }
        _FLOATCOLOR(Float fr,Float fg,Float fb,Float fa=1.0f)
        {
                r=fr;
                g=fg;
                b=fb;
                a=fa;
        }
        _FLOATCOLOR(COLOR color)
        {
                r=(Float)GetR(color)/255.0f;
                g=(Float)GetG(color)/255.0f;
                b=(Float)GetB(color)/255.0f;
                a=(Float)GetA(color)/255.0f;
        }
        _FLOATCOLOR(UByte r,UByte g,UByte b,UByte a=0xff)
        {
                _FLOATCOLOR::r=(Float)r/255.0f;
                _FLOATCOLOR::g=(Float)g/255.0f;
                _FLOATCOLOR::b=(Float)b/255.0f;
                _FLOATCOLOR::a=(Float)a/255.0f;
        }
}FLOATCOLOR;


__forceinline Bool CompareMemory(void *a,void *b,UInt32 size)
{
        for(UInt32 i=0;i<size;i++)
                if(((UByte *)a)!=((UByte *)b))return False;
        return True;
}

template <class T> class Optional
{
private:
        Bool m_bInited;
        T m_Val;
public:
        Optional(){ m_bInited=False; }
        void Reset(){ m_bInited=False; }
        Bool IsInited()const{ return m_bInited; }
        const T &GetValue()const{ return m_Val; }
        const T &operator =(const T & val){ m_bInited=True; m_Val=val; return m_Val; }
        Bool operator ==(const T & val)const{ return m_bInited & CompareMemory((void *)&m_Val,(void *)&val,sizeof(T)); }
        Bool operator !=(const T & val)const{ return (!m_bInited) || (!CompareMemory((void *)&m_Val,(void *)&val,sizeof(T))); }
};

template <class T> class AutoPtrBase
{
protected:
        T *m_p;
        virtual void SafeDestroy()=0;
public:
        virtual ~AutoPtrBase(){}
       
        T *GetPtr(){ return m_p; }

        T *SetPtr(T *p){ m_p=p; }

        T *operator =(const T *p){ SafeDestroy(m_p); return SetPtr(p); }

        T *operator ->()const{ return GetPtr(); }

        Bool operator !(){ return null==m_p; }

        Bool operator ==(const T *p){ return m_p==p; }
        Bool operator !=(const T *p){ return m_p!=p; }

};

template <class T> class AutoPtr:public AutoPtrBase<T>
{
protected:
        virtual void SafeDestroy()
        {
                SafeDel(m_p);
        }
public:
        AutoPtr(){ m_p=null; }
        AutoPtr(T *p){ m_p=p; }
        AutoPtr(const T *p){ m_p=const_cast<T *>(p); }
        virtual ~AutoPtr(){ SafeDestroy(); }
};

template <class T> class AutoArrayPtr:public AutoPtrBase<T>
{
protected:
        virtual void SafeDestroy()
        {
                SafeDelArray(m_p);
        }
public:
        AutoArrayPtr(){ m_p=null; }
        AutoArrayPtr(T *p){ m_p=p; }
        AutoArrayPtr(const T *p){ m_p=const_cast<T *>(p); }
        virtual ~AutoArrayPtr(){ SafeDestroy(); }
};

template <class T> class AutoComPtr:public AutoPtrBase<T>
{
protected:
        virtual void SafeDestroy()
        {
                SafeRelease(m_p);
        }
public:
        AutoComPtr(){ m_p=null; }
        AutoComPtr(T *p){ m_p=p; }
        AutoComPtr(const T *p){ m_p=const_cast<T *>(p); }
        virtual ~AutoComPtr(){ SafeDestroy(); }
};


}// end of namespace inst


#endif
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

作品发布|文章投稿|广告合作|关于本站|游戏开发论坛 ( 闽ICP备17032699号-3 )

GMT+8, 2025-12-20 01:15

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表