From d8a4b666cf4a596ad76e9ce2a3808624f9f95480 Mon Sep 17 00:00:00 2001 From: =?utf8?q?V=C3=A1clav=20Slav=C3=ADk?= Date: Wed, 7 Mar 2007 20:01:52 +0000 Subject: [PATCH] deprecated wxString::GetWriteBuf() and friends in favour of wxStringBuffer git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@44637 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- include/wx/string.h | 33 +++++++++++++++++++++++---------- src/common/string.cpp | 28 ++++++++++++++++++++++++---- tests/strings/strings.cpp | 21 +++++++++++---------- 3 files changed, 58 insertions(+), 24 deletions(-) diff --git a/include/wx/string.h b/include/wx/string.h index 72419d7e4c..6733c24b2a 100644 --- a/include/wx/string.h +++ b/include/wx/string.h @@ -942,7 +942,7 @@ public: // string += string wxString& operator<<(const wxString& s) { -#if !wxUSE_STL +#if WXWIN_COMPATIBILITY_2_8 && !wxUSE_STL wxASSERT_MSG( s.GetStringData()->IsValid(), _T("did you forget to call UngetWriteBuf()?") ); #endif @@ -1175,14 +1175,16 @@ public: // minimize the string's memory // only works if the data of this string is not shared bool Shrink(); -#if !wxUSE_STL +#if WXWIN_COMPATIBILITY_2_8 && !wxUSE_STL + // These are deprecated, use wxStringBuffer or wxStringBufferLength instead + // // get writable buffer of at least nLen bytes. Unget() *must* be called // a.s.a.p. to put string back in a reasonable state! - wxChar *GetWriteBuf(size_t nLen); + wxDEPRECATED( wxChar *GetWriteBuf(size_t nLen) ); // call this immediately after GetWriteBuf() has been used - void UngetWriteBuf(); - void UngetWriteBuf(size_t nLen); -#endif + wxDEPRECATED( void UngetWriteBuf() ); + wxDEPRECATED( void UngetWriteBuf(size_t nLen) ); +#endif // WXWIN_COMPATIBILITY_2_8 && !wxUSE_STL // wxWidgets version 1 compatibility functions @@ -1371,6 +1373,17 @@ public: // string += char wxString& operator+=(wxChar ch) { return (wxString&)wxStringBase::operator+=(ch); } + +private: +#if !wxUSE_STL + // helpers for wxStringBuffer and wxStringBufferLength + wxChar *DoGetWriteBuf(size_t nLen); + void DoUngetWriteBuf(); + void DoUngetWriteBuf(size_t nLen); + + friend class WXDLLIMPEXP_BASE wxStringBuffer; + friend class WXDLLIMPEXP_BASE wxStringBufferLength; +#endif }; // notice that even though for many compilers the friend declarations above are @@ -1459,9 +1472,9 @@ class WXDLLIMPEXP_BASE wxStringBuffer public: wxStringBuffer(wxString& str, size_t lenWanted = 1024) : m_str(str), m_buf(NULL) - { m_buf = m_str.GetWriteBuf(lenWanted); } + { m_buf = m_str.DoGetWriteBuf(lenWanted); } - ~wxStringBuffer() { m_str.UngetWriteBuf(); } + ~wxStringBuffer() { m_str.DoUngetWriteBuf(); } operator wxChar*() const { return m_buf; } @@ -1478,14 +1491,14 @@ public: wxStringBufferLength(wxString& str, size_t lenWanted = 1024) : m_str(str), m_buf(NULL), m_len(0), m_lenSet(false) { - m_buf = m_str.GetWriteBuf(lenWanted); + m_buf = m_str.DoGetWriteBuf(lenWanted); wxASSERT(m_buf != NULL); } ~wxStringBufferLength() { wxASSERT(m_lenSet); - m_str.UngetWriteBuf(m_len); + m_str.DoUngetWriteBuf(m_len); } operator wxChar*() const { return m_buf; } diff --git a/src/common/string.cpp b/src/common/string.cpp index 2dfd8cc229..208a77c020 100644 --- a/src/common/string.cpp +++ b/src/common/string.cpp @@ -1061,7 +1061,7 @@ bool wxString::Shrink() #if !wxUSE_STL // get the pointer to writable buffer of (at least) nLen bytes -wxChar *wxString::GetWriteBuf(size_t nLen) +wxChar *wxString::DoGetWriteBuf(size_t nLen) { if ( !AllocBeforeWrite(nLen) ) { // allocation failure handled by caller @@ -1075,12 +1075,12 @@ wxChar *wxString::GetWriteBuf(size_t nLen) } // put string back in a reasonable state after GetWriteBuf -void wxString::UngetWriteBuf() +void wxString::DoUngetWriteBuf() { - UngetWriteBuf(wxStrlen(m_pchData)); + DoUngetWriteBuf(wxStrlen(m_pchData)); } -void wxString::UngetWriteBuf(size_t nLen) +void wxString::DoUngetWriteBuf(size_t nLen) { wxStringData * const pData = GetStringData(); @@ -1091,8 +1091,28 @@ void wxString::UngetWriteBuf(size_t nLen) pData->nDataLength = nLen; pData->Validate(true); } + +// deprecated compatibility code: +#if WXWIN_COMPATIBILITY_2_8 +wxChar *wxString::GetWriteBuf(size_t nLen) +{ + return DoGetWriteBuf(nLen); +} + +void wxString::UngetWriteBuf() +{ + DoUngetWriteBuf(); +} + +void wxString::UngetWriteBuf(size_t nLen) +{ + DoUngetWriteBuf(nLen); +} +#endif // WXWIN_COMPATIBILITY_2_8 + #endif // !wxUSE_STL + // --------------------------------------------------------------------------- // data access // --------------------------------------------------------------------------- diff --git a/tests/strings/strings.cpp b/tests/strings/strings.cpp index 03a0a50177..fac4253275 100644 --- a/tests/strings/strings.cpp +++ b/tests/strings/strings.cpp @@ -635,18 +635,19 @@ void StringTestCase::WriteBuf() CPPUNIT_ASSERT_EQUAL(_T('o'), s[2]); CPPUNIT_ASSERT_EQUAL((size_t)3, s.length()); + { - wxChar *p = s.GetWriteBuf(10); - wxStrcpy(p, _T("barrbaz")); - s.UngetWriteBuf(4); + wxStringBufferLength buf(s, 10); + wxStrcpy(buf, _T("barrbaz")); + buf.SetLength(4); + } - CPPUNIT_ASSERT_EQUAL(_T('b'), s[0u]); - CPPUNIT_ASSERT_EQUAL(_T('a'), s[1]); - CPPUNIT_ASSERT_EQUAL(_T('r'), s[2]); - CPPUNIT_ASSERT_EQUAL(_T('r'), s[3]); - CPPUNIT_ASSERT_EQUAL((size_t)4, s.length()); + CPPUNIT_ASSERT_EQUAL(_T('b'), s[0u]); + CPPUNIT_ASSERT_EQUAL(_T('a'), s[1]); + CPPUNIT_ASSERT_EQUAL(_T('r'), s[2]); + CPPUNIT_ASSERT_EQUAL(_T('r'), s[3]); + CPPUNIT_ASSERT_EQUAL((size_t)4, s.length()); - CPPUNIT_ASSERT_EQUAL( 0, wxStrcmp(_T("barr"), s) ); - } + CPPUNIT_ASSERT_EQUAL( 0, wxStrcmp(_T("barr"), s) ); } -- 2.45.2