From: Vadim Zeitlin Date: Thu, 11 Sep 2008 13:35:32 +0000 (+0000) Subject: added wxArrayString::swap() X-Git-Url: https://git.saurik.com/wxWidgets.git/commitdiff_plain/9127686895aa155a4c5b3c5f4feaeaed15626936 added wxArrayString::swap() git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@55549 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- diff --git a/docs/changes.txt b/docs/changes.txt index bb79146572..38529b36d6 100644 --- a/docs/changes.txt +++ b/docs/changes.txt @@ -291,6 +291,7 @@ All: - Corrected bug in wxTimeSpan::IsShorterThan() for equal time spans. - Use std::unordered_{map,set} for wxHashMap/Set if available (Jan van Dijk). - Added wxString::Capitalize() and MakeCapitalized(). +- Added wxArrayString::swap(). - Added wxSHUTDOWN_LOGOFF and wxSHUTDOWN_FORCE wxShutdown() flags (troelsk). All (Unix): diff --git a/include/wx/arrstr.h b/include/wx/arrstr.h index 4c099c36b2..ddb11c0a31 100644 --- a/include/wx/arrstr.h +++ b/include/wx/arrstr.h @@ -301,6 +301,25 @@ public: void reserve(size_type n) /* base::reserve*/; void resize(size_type n, value_type v = value_type()); size_type size() const { return GetCount(); } + void swap(wxArrayString& other) + { + // not sure if we can rely on having std::swap() everywhere so do it + // manually + const size_t savedSize = m_nSize; + const size_t savedCount = m_nCount; + wxString * const savedItems = m_pItems; + const bool savedAutoSort = m_autoSort; + + m_nSize = other.m_nSize; + m_nCount = other.m_nCount; + m_pItems = other.m_pItems; + m_autoSort = other.m_autoSort; + + other.m_nSize = savedSize; + other.m_nCount = savedCount; + other.m_pItems = savedItems; + other.m_autoSort = savedAutoSort; + } protected: void Init(bool autoSort); // common part of all ctors diff --git a/tests/arrays/arrays.cpp b/tests/arrays/arrays.cpp index 42cacef17c..f97572dc99 100644 --- a/tests/arrays/arrays.cpp +++ b/tests/arrays/arrays.cpp @@ -170,6 +170,7 @@ private: CPPUNIT_TEST( wxArrayCharTest ); CPPUNIT_TEST( TestSTL ); CPPUNIT_TEST( Alloc ); + CPPUNIT_TEST( Swap ); CPPUNIT_TEST_SUITE_END(); void wxStringArrayTest(); @@ -182,6 +183,7 @@ private: void wxArrayCharTest(); void TestSTL(); void Alloc(); + void Swap(); DECLARE_NO_COPY_CLASS(ArraysTestCase) }; @@ -555,6 +557,28 @@ void ArraysTestCase::Alloc() CPPUNIT_ASSERT_EQUAL( 9, a[1] ); } +void ArraysTestCase::Swap() +{ + wxArrayString a1, a2; + a1.swap(a2); + CPPUNIT_ASSERT( a1.empty() && a2.empty() ); + + a1.push_back("Foo"); + a1.swap(a2); + CPPUNIT_ASSERT( a1.empty() ); + CPPUNIT_ASSERT_EQUAL( 1, a2.size() ); + + a1.push_back("Bar"); + a1.push_back("Baz"); + a2.swap(a1); + CPPUNIT_ASSERT_EQUAL( 1, a1.size() ); + CPPUNIT_ASSERT_EQUAL( 2, a2.size() ); + + a1.swap(a2); + CPPUNIT_ASSERT_EQUAL( 2, a1.size() ); + CPPUNIT_ASSERT_EQUAL( 1, a2.size() ); +} + void ArraysTestCase::TestSTL() { wxArrayInt list1;