From 60563ce0cef1470431507d0d0f6c31b95611fd93 Mon Sep 17 00:00:00 2001 From: PB Date: Fri, 23 Jun 2017 17:06:56 +0200 Subject: [PATCH] Modify wxBasicString so it is better suited to work as an RAII wrapper for BSTR. --- include/wx/msw/ole/oleutils.h | 45 ++++++++++++++++++++++++------ src/msw/ole/oleutils.cpp | 52 +++++++++++++++++++++++++++++------ 2 files changed, 80 insertions(+), 17 deletions(-) diff --git a/include/wx/msw/ole/oleutils.h b/include/wx/msw/ole/oleutils.h index 1dab81b18a..2477c25b95 100644 --- a/include/wx/msw/ole/oleutils.h +++ b/include/wx/msw/ole/oleutils.h @@ -66,18 +66,45 @@ inline void wxOleUninitialize() class WXDLLIMPEXP_CORE wxBasicString { 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 - // just get the string + // 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(); + + /// Returns the owned BSTR while keeping its ownership 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: // actual string diff --git a/src/msw/ole/oleutils.cpp b/src/msw/ole/oleutils.cpp index 67c1477753..b56a1bbf07 100644 --- a/src/msw/ole/oleutils.cpp +++ b/src/msw/ole/oleutils.cpp @@ -71,20 +71,19 @@ WXDLLEXPORT wxString wxConvertStringFromOle(BSTR bStr) // wxBasicString // ---------------------------------------------------------------------------- +wxBasicString::wxBasicString(BSTR bstr) +{ + m_bstrBuf = bstr; +} + wxBasicString::wxBasicString(const wxString& str) { m_bstrBuf = SysAllocString(str.wc_str(*wxConvCurrent)); } -wxBasicString::wxBasicString(const wxBasicString& src) +wxBasicString::wxBasicString(const wxBasicString& bstr) { - m_bstrBuf = src.Get(); -} - -wxBasicString& wxBasicString::operator=(const wxBasicString& src) -{ - SysReAllocString(&m_bstrBuf, src); - return *this; + m_bstrBuf = bstr.Copy(); } wxBasicString::~wxBasicString() @@ -92,6 +91,43 @@ wxBasicString::~wxBasicString() 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