Main Page | Class Hierarchy | Class List | File List | Class Members | File Members

OleStr.h

Go to the documentation of this file.
00001 /*  OleStr.h - OLESTR wrapper
00002     Copyright (C) 2001-2004 Mark Weaver
00003     Written by Mark Weaver <mark@npsl.co.uk>
00004 
00005     Part of the Open-Win32 library.
00006     This library is free software; you can redistribute it and/or
00007     modify it under the terms of the GNU Library General Public
00008     License as published by the Free Software Foundation; either
00009     version 2 of the License, or (at your option) any later version.
00010 
00011     This library is distributed in the hope that it will be useful,
00012     but WITHOUT ANY WARRANTY; without even the implied warranty of
00013     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014     Library General Public License for more details.
00015 
00016     You should have received a copy of the GNU Library General Public
00017     License along with this library; if not, write to the
00018     Free Software Foundation, Inc., 59 Temple Place - Suite 330,
00019     Boston, MA  02111-1307, USA.
00020 */
00021 
00026 #ifndef OW32_OleStr_h
00027 #define OW32_OleStr_h
00028 
00029 namespace OW32
00030 {
00031 
00033 class OW32_LIB_EXPORT COleStr
00034 {
00035 private:
00036     LPOLESTR m_data;
00037 
00038     // copy wchar_t*
00039     void _Copy(LPCWSTR str)
00040     {
00041         ::CoTaskMemFree(m_data);
00042         int size = (wcslen(str)+1)*sizeof(OLECHAR);
00043         m_data = (LPOLESTR)::CoTaskMemAlloc(size);
00044         memcpy(m_data, str, size);
00045     }
00046 
00047     // copy char*
00048     void _Copy(LPCSTR str)
00049     {
00050         USES_CONVERSION;
00051         _Copy(A2CW(str));
00052     }
00053 
00054 public:
00056     COleStr() : m_data(0) { }
00057 
00061     COleStr(LPCWSTR str) : m_data(0) { _Copy(str); }
00065     COleStr(LPCSTR str) : m_data(0) { _Copy(str); }
00069     COleStr(const COleStr &str) : m_data(0) { _Copy(str.m_data); }
00070 
00072     ~COleStr() { ::CoTaskMemFree(m_data); }
00073 
00075     unsigned int Length() const { return m_data ? wcslen(m_data) : 0; }
00076     
00078     void Attach(LPOLESTR lpOle) 
00079     { 
00080         if (m_data) ::CoTaskMemFree(m_data);
00081         m_data = lpOle; 
00082     }
00083 
00085     LPOLESTR Detach() 
00086     { 
00087         LPOLESTR data = m_data; 
00088         m_data = NULL; 
00089         return data; 
00090     }
00091 
00093     LPOLESTR Copy() const
00094     {
00095         LPOLESTR data;
00096         int size = (wcslen(m_data)+1)*sizeof(OLECHAR);
00097         data = (LPOLESTR)::CoTaskMemAlloc(size);
00098         if (data)
00099         {
00100             memcpy(data, m_data, size);
00101         }
00102         return data;
00103     }
00104 
00108     COleStr& operator=(const COleStr &str)
00109     {
00110         _Copy(str.m_data);
00111         return *this;
00112     }
00113     
00117     COleStr& operator=(const LPCWSTR &str)
00118     {
00119         _Copy(str);
00120         return *this;
00121     }
00122 
00126     COleStr& operator=(const LPCSTR &str)
00127     {
00128         _Copy(str);
00129         return *this;
00130     }
00131 
00133     LPOLESTR* operator&()
00134     {
00135         _ASSERT(!m_data);
00136         return &m_data;
00137     }
00138 
00140     operator LPOLESTR() const { return m_data; }
00141 
00143     bool operator!() const { return m_data == NULL; }
00145     operator bool() const { return m_data != NULL; }
00146 };
00147 
00148 } // namespace OW32
00149 
00150 #endif // OW32_OleStr_h

Generated on Sun Jun 5 01:29:17 2005 for OW32 by  doxygen 1.3.9.1