/*
class CComTSTR

	by: Shawn A. Van Ness
	rev: 1999.10.06

Usage:  CComTSTR is not meant to replace the full functionality of CComBSTR.  
Rather, its purpose for existance is only to perform conversions from W2T, so 
that strings from COM method parameters can be sent to Win32 API functions.

	HRESULT Foo( BSTR bstr)
	{
		ATLX::CComTSTR tstr = bstr;
		::SetWindowText( m_hWnd, tstr);
	}

Motivation:  The USES_CONVERSION stuff in ATL3 is both ugly and extremely prone 
to misuse.  ATL7 contains a set of classes, similar to this one, which are much 
less evil.

Note that on NT platforms, you're well advised to build against the wide-char 
Win32 API anyway (ie: #define _UNICODE), so this class is really only useful 
for building modules that must run on both NT and 9X platforms (ie: #define 
_MBCS).  This class is pretty much a NOP when _UNICODE is #define'd.

However, even on NT, there are a handful of Win32 functions that accept only 
narrow strings, and have no wide-string counterparts (eg: GetProcAddressA).  
And sometimes it's desirable to convert wide strings to narrow, for file I/O 
purposes.

OTOH, if you'd like to build portable modules that leverage the wide-char APIs 
on NT, and go through a wide-narrow-wide conversion layer on 9X, be sure to 
check out the excellent Microsoft Layer for Unicode (MSLU) available here: 
	http://www.microsoft.com/globaldev/articles/mslu_announce.asp
*/

#pragma once 

namespace ATLX {

template<typename TDummy>
struct _CComTSTR
{
    TCHAR* m_str;
    
    _CComTSTR()
    { 
        m_str = 0; 
    }
    
    ~_CComTSTR()
    { 
        Empty(); 
    }
    
    _CComTSTR(const _CComTSTR& t) // fwd to operator=
    { 
        m_str = 0; 
        (*this) = t; 
    }
    
    _CComTSTR& operator=(const _CComTSTR& t)
    {
        Empty();
        
        if (t.m_str)
        {
            m_str = new TCHAR[lstrlen(t.m_str)+1];
            lstrcpy(m_str,t.m_str);
        }
        
        return (*this);
    }
    
    _CComTSTR(const OLECHAR* wsz) // fwd to operator=
    { 
        m_str = 0;
        (*this) = wsz; 
    }
    
    _CComTSTR& operator=(const OLECHAR* wsz)
    {
        Empty();
        
        if (wsz)
        {
#if (defined(_UNICODE) || defined(UNICODE)) 
            m_str = new TCHAR[lstrlenW(wsz)+1];
            lstrcpyW(m_str,wsz);
#else 
            m_str = new TCHAR[(lstrlenW(wsz)+1)*2]; // allow space for 16-bit chars (for mbcs)
            AtlW2AHelper(m_str,wsz,(lstrlenW(wsz)+1)*2);
#endif 
        }
        
        return (*this);
    }
    
    operator TCHAR*() const
    {
        return m_str;
    }
    
    void Empty()
    {
        if (m_str) delete [] m_str;
        m_str = 0;
    }
    
    int Length() const
    {
        return (m_str == 0) ? (0) : (lstrlen(m_str));
    }
    
}

typedef _CComTSTR<void> CComTSTR;

} // namespace




w e b c p p
web c plus plus