Modify wxBasicString so it is better suited to work as an RAII wrapper for BSTR.
This commit is contained in:
@@ -66,18 +66,45 @@ inline void wxOleUninitialize()
|
|||||||
class WXDLLIMPEXP_CORE wxBasicString
|
class WXDLLIMPEXP_CORE wxBasicString
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
// ctors & dtor
|
|
||||||
wxBasicString(const wxString& str);
|
|
||||||
wxBasicString(const wxBasicString& bstr);
|
|
||||||
~wxBasicString();
|
|
||||||
|
|
||||||
wxBasicString& operator=(const wxBasicString& bstr);
|
// Takes over the ownership of bstr
|
||||||
|
wxBasicString(BSTR bstr = NULL);
|
||||||
|
|
||||||
|
// Constructs the BSTR from wxString
|
||||||
|
wxBasicString(const wxString& str);
|
||||||
|
|
||||||
|
// Creates a copy of the BSTR owned by bstr
|
||||||
|
wxBasicString(const wxBasicString& bstr);
|
||||||
|
|
||||||
|
// Frees the owned BSTR
|
||||||
|
~wxBasicString();
|
||||||
|
|
||||||
|
|
||||||
// accessors
|
// Sets its BSTR to a copy of the BSTR owned by bstr
|
||||||
// just get the string
|
wxBasicString& operator=(const wxBasicString& bstr);
|
||||||
|
|
||||||
|
// Creates its BSTR from wxString
|
||||||
|
wxBasicString& operator=(const wxString& str);
|
||||||
|
|
||||||
|
// Takes over the ownership of bstr
|
||||||
|
wxBasicString& operator=(BSTR bstr);
|
||||||
|
|
||||||
|
// Returns the owned BSTR and gives up its ownership
|
||||||
|
BSTR Detach();
|
||||||
|
|
||||||
|
/// Returns the owned BSTR while keeping its ownership
|
||||||
operator BSTR() const { return m_bstrBuf; }
|
operator BSTR() const { return m_bstrBuf; }
|
||||||
// retrieve a copy of our string - caller must SysFreeString() it later!
|
|
||||||
BSTR Get() const { return SysAllocString(m_bstrBuf); }
|
// Returns the address of the owned BSTR
|
||||||
|
operator BSTR*() { return &m_bstrBuf; }
|
||||||
|
|
||||||
|
// Returns a copy of the owned BSTR
|
||||||
|
BSTR Copy() const { return SysAllocString(m_bstrBuf); }
|
||||||
|
|
||||||
|
|
||||||
|
// retrieve a copy of our string - caller must SysFreeString() it later!
|
||||||
|
wxDEPRECATED_MSG("use Copy() instead")
|
||||||
|
BSTR Get() const { return Copy(); }
|
||||||
|
|
||||||
private:
|
private:
|
||||||
// actual string
|
// actual string
|
||||||
|
@@ -71,20 +71,19 @@ WXDLLEXPORT wxString wxConvertStringFromOle(BSTR bStr)
|
|||||||
// wxBasicString
|
// wxBasicString
|
||||||
// ----------------------------------------------------------------------------
|
// ----------------------------------------------------------------------------
|
||||||
|
|
||||||
|
wxBasicString::wxBasicString(BSTR bstr)
|
||||||
|
{
|
||||||
|
m_bstrBuf = bstr;
|
||||||
|
}
|
||||||
|
|
||||||
wxBasicString::wxBasicString(const wxString& str)
|
wxBasicString::wxBasicString(const wxString& str)
|
||||||
{
|
{
|
||||||
m_bstrBuf = SysAllocString(str.wc_str(*wxConvCurrent));
|
m_bstrBuf = SysAllocString(str.wc_str(*wxConvCurrent));
|
||||||
}
|
}
|
||||||
|
|
||||||
wxBasicString::wxBasicString(const wxBasicString& src)
|
wxBasicString::wxBasicString(const wxBasicString& bstr)
|
||||||
{
|
{
|
||||||
m_bstrBuf = src.Get();
|
m_bstrBuf = bstr.Copy();
|
||||||
}
|
|
||||||
|
|
||||||
wxBasicString& wxBasicString::operator=(const wxBasicString& src)
|
|
||||||
{
|
|
||||||
SysReAllocString(&m_bstrBuf, src);
|
|
||||||
return *this;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
wxBasicString::~wxBasicString()
|
wxBasicString::~wxBasicString()
|
||||||
@@ -92,6 +91,43 @@ wxBasicString::~wxBasicString()
|
|||||||
SysFreeString(m_bstrBuf);
|
SysFreeString(m_bstrBuf);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
wxBasicString& wxBasicString::operator=(const wxBasicString& src)
|
||||||
|
{
|
||||||
|
if ( this != &src )
|
||||||
|
{
|
||||||
|
SysFreeString(m_bstrBuf);
|
||||||
|
m_bstrBuf = src.Copy();
|
||||||
|
}
|
||||||
|
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
wxBasicString& wxBasicString::operator=(const wxString& str)
|
||||||
|
{
|
||||||
|
SysFreeString(m_bstrBuf);
|
||||||
|
m_bstrBuf = ::SysAllocString(str.wc_str(*wxConvCurrent));
|
||||||
|
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
wxBasicString& wxBasicString::operator=(BSTR bstr)
|
||||||
|
{
|
||||||
|
wxCHECK_MSG(m_bstrBuf != bstr, *this, wxS("Attempting to attach already attached BSTR!"));
|
||||||
|
|
||||||
|
SysFreeString(m_bstrBuf);
|
||||||
|
m_bstrBuf = bstr;
|
||||||
|
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
BSTR wxBasicString::Detach()
|
||||||
|
{
|
||||||
|
BSTR bstr = m_bstrBuf;
|
||||||
|
|
||||||
|
m_bstrBuf = NULL;
|
||||||
|
|
||||||
|
return bstr;
|
||||||
|
}
|
||||||
|
|
||||||
// ----------------------------------------------------------------------------
|
// ----------------------------------------------------------------------------
|
||||||
// Convert variants
|
// Convert variants
|
||||||
|
Reference in New Issue
Block a user