Replace wxWritableCharBuffer template with 2 simpler classes

This addresses a FIXME comment in this template, it was written in a
generic form to compile in both Unicode and ANSI builds of the library,
but now it can be written more simply and efficiently (in the "char"
case).

No real changes.
This commit is contained in:
Vadim Zeitlin 2022-10-28 19:37:05 +01:00
parent 3bc0d1ed92
commit 37418eed8e
2 changed files with 19 additions and 18 deletions

View file

@ -388,27 +388,27 @@ public:
wxWCharBuffer(const wxCStrData& cstr);
};
// wxCharTypeBuffer<T> implicitly convertible to T*
template <typename T>
class wxWritableCharTypeBuffer : public wxCharTypeBuffer<T>
// wxCharTypeBuffer<T> implicitly convertible to T*, for char and wchar_t,
// implemented slightly differently because in one case we must be
// constructible from a buffer and in another -- from a raw pointer.
class wxWritableCharBuffer : public wxScopedCharTypeBuffer<char>
{
public:
typedef typename wxScopedCharTypeBuffer<T>::CharType CharType;
explicit wxWritableCharBuffer(const wxScopedCharTypeBuffer<char>& src)
: wxScopedCharTypeBuffer<char>(src) {}
wxWritableCharTypeBuffer(const wxScopedCharTypeBuffer<T>& src)
: wxCharTypeBuffer<T>(src) {}
// FIXME-UTF8: this won't be needed after converting mb_str()/wc_str() to
// always return a buffer
// + we should derive this class from wxScopedCharTypeBuffer
// then
wxWritableCharTypeBuffer(const CharType *str = nullptr)
: wxCharTypeBuffer<T>(str) {}
operator CharType*() { return this->data(); }
operator char*() { return data(); }
};
typedef wxWritableCharTypeBuffer<char> wxWritableCharBuffer;
typedef wxWritableCharTypeBuffer<wchar_t> wxWritableWCharBuffer;
class wxWritableWCharBuffer : public wxCharTypeBuffer<wchar_t>
{
public:
explicit wxWritableWCharBuffer(const CharType *str)
: wxCharTypeBuffer<wchar_t>(str) {}
operator wchar_t*() { return data(); }
};
// Compatibility defines, don't use them in the new code.

View file

@ -1572,8 +1572,9 @@ public:
// returned buffer won't affect the string, these methods are only useful
// for passing values to const-incorrect functions
wxWritableCharBuffer char_str(const wxMBConv& conv wxSTRING_DEFAULT_CONV_ARG) const
{ return mb_str(conv); }
wxWritableWCharBuffer wchar_str() const { return wc_str(); }
{ return wxWritableCharBuffer(mb_str(conv)); }
wxWritableWCharBuffer wchar_str() const
{ return wxWritableWCharBuffer(wc_str()); }
// conversion to the buffer of the given type T (= char or wchar_t) and
// also optionally return the buffer length