Make wxBasicString safer and easier to use as a BSTR RAII wrapper.

This commit is contained in:
PB
2017-07-01 12:50:24 +02:00
parent 564c0aafa5
commit ca3f919da9
3 changed files with 88 additions and 70 deletions

View File

@@ -65,47 +65,48 @@ inline void wxOleUninitialize()
class WXDLLIMPEXP_CORE wxBasicString
{
public:
public:
// Takes over the ownership of bstr
explicit wxBasicString(BSTR bstr = NULL);
// 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();
// Constructs the BSTR from wxString
wxBasicString(const wxString& str);
// Sets its BSTR to a copy of the BSTR owned by bstr
wxBasicString& operator=(const wxBasicString& bstr);
// Creates a copy of the BSTR owned by bstr
wxBasicString(const wxBasicString& bstr);
// Creates its BSTR from wxString
wxBasicString& operator=(const wxString& str);
// Frees the owned BSTR
~wxBasicString();
// Returns the owned BSTR and gives up its ownership
BSTR Detach();
// Frees the owned BSTR
void Free();
// Returns a copy of the owned BSTR,
// the caller is responsible for freeing it
BSTR Copy() const { return SysAllocString(m_bstrBuf); }
// Returns the address of the owned BSTR, not to be called
// when wxBasicString already contains a non-NULL BSTR
BSTR* ByRef();
// Sets its BSTR to a copy of the BSTR owned by bstr
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();
// Takes over the ownership of bstr
wxBasicString& operator=(BSTR bstr);
/// Returns the owned BSTR while keeping its ownership
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!
wxDEPRECATED_MSG("use Copy() instead")
BSTR Get() const { return Copy(); }
// retrieve a copy of our string - caller must SysFreeString() it later!
wxDEPRECATED_MSG("use Copy() instead")
BSTR Get() const { return Copy(); }
private:
// actual string
BSTR m_bstrBuf;