Make wxBasicString as simple and safe to use as possible

Don't try to write a general purpose class, but provide just the methods
that we need in our code.

This fixes the bug added in 294436c8bb
which resulted in a crash because a literal string, not a BSTR, was
passed to wxBasicString ctor.
This commit is contained in:
PB
2017-07-16 15:39:26 +02:00
committed by Vadim Zeitlin
parent 2d8657d37c
commit cfb3ef98fc
3 changed files with 29 additions and 73 deletions

View File

@@ -66,24 +66,26 @@ inline void wxOleUninitialize()
class WXDLLIMPEXP_CORE wxBasicString
{
public:
// Takes over the ownership of bstr
explicit wxBasicString(BSTR bstr = NULL);
// Constructs with the owned BSTR set to NULL
wxBasicString() : m_bstrBuf(NULL) {}
// Constructs the BSTR from wxString
wxBasicString(const wxString& str);
// Constructs with the owned BSTR created from a wxString
wxBasicString(const wxString& str)
: m_bstrBuf(SysAllocString(str.wc_str(*wxConvCurrent))) {}
// Creates a copy of the BSTR owned by bstr
wxBasicString(const wxBasicString& bstr);
// Constructs with the owned BSTR as a copy of the BSTR owned by bstr
wxBasicString(const wxBasicString& bstr) : m_bstrBuf(bstr.Copy()) {}
// Frees the owned BSTR
~wxBasicString();
~wxBasicString() { SysFreeString(m_bstrBuf); }
// Returns the owned BSTR and gives up its ownership
// Creates the owned BSTR from a wxString
void AssignFromString(const wxString& str);
// Returns the owned BSTR and gives up its ownership,
// the caller is responsible for freeing it
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); }
@@ -95,12 +97,6 @@ public:
// 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 while keeping its ownership
operator BSTR() const { return m_bstrBuf; }

View File

@@ -130,8 +130,8 @@ bool wxAutomationObject::Invoke(const wxString& member, int action,
}
int namedArgStringCount = namedArgCount + 1;
wxVector<wxBasicString> argNames(namedArgStringCount, wxString());
argNames[0] = member;
wxVector<wxBasicString> argNames(namedArgStringCount);
argNames[0].AssignFromString(member);
// Note that arguments are specified in reverse order
// (all totally logical; hey, we're dealing with OLE here.)
@@ -141,7 +141,7 @@ bool wxAutomationObject::Invoke(const wxString& member, int action,
{
if ( !INVOKEARG(i).GetName().empty() )
{
argNames[(namedArgCount-j)] = INVOKEARG(i).GetName();
argNames[(namedArgCount-j)].AssignFromString(INVOKEARG(i).GetName());
j ++;
}
}

View File

@@ -70,25 +70,10 @@ 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& bstr)
{
m_bstrBuf = bstr.Copy();
}
wxBasicString::~wxBasicString()
void wxBasicString::AssignFromString(const wxString& str)
{
SysFreeString(m_bstrBuf);
m_bstrBuf = SysAllocString(str.wc_str(*wxConvCurrent));
}
BSTR wxBasicString::Detach()
@@ -100,12 +85,6 @@ BSTR wxBasicString::Detach()
return bstr;
}
void wxBasicString::Free()
{
SysFreeString(m_bstrBuf);
m_bstrBuf = NULL;
}
BSTR* wxBasicString::ByRef()
{
wxASSERT_MSG(!m_bstrBuf,
@@ -126,25 +105,6 @@ wxBasicString& wxBasicString::operator=(const wxBasicString& src)
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 == NULL || m_bstrBuf != bstr,
*this, wxS("Attempting to assign already owned BSTR"));
SysFreeString(m_bstrBuf);
m_bstrBuf = bstr;
return *this;
}
// ----------------------------------------------------------------------------
// Convert variants
// ----------------------------------------------------------------------------