From 37418eed8e9b8f91c802f8c1bbb45b630f190cd8 Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Fri, 28 Oct 2022 19:37:05 +0100 Subject: [PATCH] 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. --- include/wx/buffer.h | 32 ++++++++++++++++---------------- include/wx/string.h | 5 +++-- 2 files changed, 19 insertions(+), 18 deletions(-) diff --git a/include/wx/buffer.h b/include/wx/buffer.h index 619fd32d55..2224c5bd3a 100644 --- a/include/wx/buffer.h +++ b/include/wx/buffer.h @@ -388,27 +388,27 @@ public: wxWCharBuffer(const wxCStrData& cstr); }; -// wxCharTypeBuffer implicitly convertible to T* -template -class wxWritableCharTypeBuffer : public wxCharTypeBuffer +// wxCharTypeBuffer 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 { public: - typedef typename wxScopedCharTypeBuffer::CharType CharType; + explicit wxWritableCharBuffer(const wxScopedCharTypeBuffer& src) + : wxScopedCharTypeBuffer(src) {} - wxWritableCharTypeBuffer(const wxScopedCharTypeBuffer& src) - : wxCharTypeBuffer(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(str) {} - - operator CharType*() { return this->data(); } + operator char*() { return data(); } }; -typedef wxWritableCharTypeBuffer wxWritableCharBuffer; -typedef wxWritableCharTypeBuffer wxWritableWCharBuffer; +class wxWritableWCharBuffer : public wxCharTypeBuffer +{ +public: + explicit wxWritableWCharBuffer(const CharType *str) + : wxCharTypeBuffer(str) {} + + operator wchar_t*() { return data(); } +}; // Compatibility defines, don't use them in the new code. diff --git a/include/wx/string.h b/include/wx/string.h index e10b95f285..02bcde0a10 100644 --- a/include/wx/string.h +++ b/include/wx/string.h @@ -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