X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/5c1de526d2e02655cf42069cc35eaf07249414d5..08e827d82daf916a08d0eb0f12cb91d9061fa921:/include/wx/buffer.h diff --git a/include/wx/buffer.h b/include/wx/buffer.h index 1144aa420d..2f8bc64866 100644 --- a/include/wx/buffer.h +++ b/include/wx/buffer.h @@ -27,7 +27,7 @@ class WXDLLIMPEXP_FWD_BASE wxCStrData; // ---------------------------------------------------------------------------- template -class WXDLLIMPEXP_BASE wxCharTypeBuffer +class wxCharTypeBuffer { public: typedef T CharType; @@ -37,7 +37,7 @@ public: if ( str ) m_data = new Data(wxStrdup(str)); else - m_data = &NullData; + m_data = GetNullData(); } wxCharTypeBuffer(size_t len) @@ -59,17 +59,23 @@ public: DecRef(); } - CharType *release() + // NB: this method is only const for backward compatibility. It used to + // be needed for auto_ptr-like semantics of the copy ctor, but now + // that ref-counting is used, it's not really needed. + CharType *release() const { - if ( m_data == &NullData ) + if ( m_data == GetNullData() ) return NULL; wxASSERT_MSG( m_data->m_owned, _T("can't release non-owned buffer") ); wxASSERT_MSG( m_data->m_ref == 1, _T("can't release shared buffer") ); CharType *p = m_data->m_str; - m_data->m_str = NULL; - DecRef(); + + wxCharTypeBuffer *self = wx_const_cast(wxCharTypeBuffer*, this); + self->m_data->m_str = NULL; + self->DecRef(); + return p; } @@ -115,7 +121,7 @@ public: if ( !str ) return false; - if ( m_data == &NullData ) + if ( m_data == GetNullData() ) { m_data = new Data(str); } @@ -161,23 +167,27 @@ private: }; // placeholder for NULL string, to simplify this code - // NB: this is defined in string.cpp, not (non-existent) buffer.cpp - static Data NullData; + static Data *GetNullData() + { + static Data s_nullData(NULL); + + return &s_nullData; + } void IncRef() { - if ( m_data == &NullData ) // exception, not ref-counted + if ( m_data == GetNullData() ) // exception, not ref-counted return; m_data->m_ref++; } void DecRef() { - if ( m_data == &NullData ) // exception, not ref-counted + if ( m_data == GetNullData() ) // exception, not ref-counted return; if ( --m_data->m_ref == 0 ) delete m_data; - m_data = &NullData; + m_data = GetNullData(); } private: @@ -186,7 +196,7 @@ private: WXDLLIMPEXP_TEMPLATE_INSTANCE_BASE( wxCharTypeBuffer ) -class WXDLLIMPEXP_BASE wxCharBuffer : public wxCharTypeBuffer +class wxCharBuffer : public wxCharTypeBuffer { public: typedef wxCharTypeBuffer wxCharTypeBufferBase; @@ -203,7 +213,7 @@ public: #if wxUSE_WCHAR_T WXDLLIMPEXP_TEMPLATE_INSTANCE_BASE( wxCharTypeBuffer ) -class WXDLLIMPEXP_BASE wxWCharBuffer : public wxCharTypeBuffer +class wxWCharBuffer : public wxCharTypeBuffer { public: typedef wxCharTypeBuffer wxCharTypeBufferBase; @@ -329,7 +339,7 @@ private: }; -class WXDLLIMPEXP_BASE wxMemoryBuffer +class wxMemoryBuffer { public: // ctor and dtor