Modify wxBasicString so it is better suited to work as an RAII wrapper for BSTR.

This commit is contained in:
PB
2017-06-23 17:06:56 +02:00
parent 684f0146f1
commit 60563ce0ce
2 changed files with 80 additions and 17 deletions

View File

@@ -66,18 +66,45 @@ inline void wxOleUninitialize()
class WXDLLIMPEXP_CORE wxBasicString class WXDLLIMPEXP_CORE wxBasicString
{ {
public: public:
// ctors & dtor
// Takes over the ownership of bstr
wxBasicString(BSTR bstr = NULL);
// Constructs the BSTR from wxString
wxBasicString(const wxString& str); wxBasicString(const wxString& str);
// Creates a copy of the BSTR owned by bstr
wxBasicString(const wxBasicString& bstr); wxBasicString(const wxBasicString& bstr);
// Frees the owned BSTR
~wxBasicString(); ~wxBasicString();
// Sets its BSTR to a copy of the BSTR owned by bstr
wxBasicString& operator=(const wxBasicString& bstr); wxBasicString& operator=(const wxBasicString& bstr);
// accessors // Creates its BSTR from wxString
// just get the string 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; }
// 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! // retrieve a copy of our string - caller must SysFreeString() it later!
BSTR Get() const { return SysAllocString(m_bstrBuf); } wxDEPRECATED_MSG("use Copy() instead")
BSTR Get() const { return Copy(); }
private: private:
// actual string // actual string

View File

@@ -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