]> git.saurik.com Git - wxWidgets.git/commitdiff
Fix return value of wxList::insert() in non-STL builds.
authorVadim Zeitlin <vadim@wxwidgets.org>
Mon, 12 Apr 2010 00:36:36 +0000 (00:36 +0000)
committerVadim Zeitlin <vadim@wxwidgets.org>
Mon, 12 Apr 2010 00:36:36 +0000 (00:36 +0000)
The returned value was the same as the iterator that was passed in which could
even be invalid when appending.

Fix the wrong use of postfix decrement instead of prefix one and handle the
case of appending correctly.

Closes #11808.

git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@63943 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775

include/wx/list.h
tests/lists/lists.cpp

index a84ee041bb9ed63357e60220e46eb01d05f6d4db..78e86ee6944990edcc805327bd6d7e249209a40f 100644 (file)
@@ -1010,11 +1010,22 @@ private:
         iterator insert(const iterator& it, const_reference v)              \
         {                                                                   \
             if ( it == end() )                                              \
+            {                                                               \
                 Append((const_base_reference)v);                            \
+                /*                                                          \
+                    note that this is the new end(), the old one was        \
+                    invalidated by the Append() call, and this is why we    \
+                    can't use the same code as in the normal case below     \
+                 */                                                         \
+                iterator itins(end());                                      \
+                return --itins;                                             \
+            }                                                               \
             else                                                            \
+            {                                                               \
                 Insert(it.m_node, (const_base_reference)v);                 \
-            iterator itprev(it);                                            \
-            return itprev--;                                                \
+                iterator itins(it);                                         \
+                return --itins;                                             \
+            }                                                               \
         }                                                                   \
         void insert(const iterator& it, size_type n, const_reference v)     \
         {                                                                   \
index fb37655b8d47ae42c5de3aa86351a30f8a1e03e5..64b1f2b2aedf9c3a1d7122ac72ccf07cf6c5968f 100644 (file)
@@ -152,11 +152,20 @@ void ListsTestCase::wxStdListTest()
     list1.clear();
     CPPUNIT_ASSERT( list1.empty() );
 
-    list1.insert(list1.end(), (int *)1);
-    list1.insert(list1.end(), (int *)2);
+    it = list1.insert(list1.end(), (int *)1);
+    CPPUNIT_ASSERT_EQUAL( (int *)1, *it );
+    CPPUNIT_ASSERT( it == list1.begin() );
     CPPUNIT_ASSERT_EQUAL( (int *)1, list1.front() );
+
+    it = list1.insert(list1.end(), (int *)2);
+    CPPUNIT_ASSERT_EQUAL( (int *)2, *it );
+    CPPUNIT_ASSERT( ++it == list1.end() );
     CPPUNIT_ASSERT_EQUAL( (int *)2, list1.back() );
 
+    it = list1.begin();
+    wxListInt::iterator it2 = list1.insert(++it, (int *)3);
+    CPPUNIT_ASSERT_EQUAL( (int *)3, *it2 );
+
     it = list1.begin();
     it = list1.erase(++it, list1.end());
     CPPUNIT_ASSERT_EQUAL( 1, list1.size() );