Title: [155152] trunk
Revision
155152
Author
[email protected]
Date
2013-09-05 14:52:25 -0700 (Thu, 05 Sep 2013)

Log Message

Make Vector::uncheckedAppend work with move-only types
https://bugs.webkit.org/show_bug.cgi?id=120799

Reviewed by Andreas Kling.

Source/WTF:

* wtf/Vector.h:
(WTF::::uncheckedAppend):
Use std::forward to invoke the move constructor when possible.

Tools:

* TestWebKitAPI/Tests/WTF/Vector.cpp:
(TestWebKitAPI::MoveOnly):
Add a move-only class.

(TestWebKitAPI::TEST):
Add a test for Vector<MoveOnly>::uncheckedAppend.

Modified Paths

Diff

Modified: trunk/Source/WTF/ChangeLog (155151 => 155152)


--- trunk/Source/WTF/ChangeLog	2013-09-05 21:34:17 UTC (rev 155151)
+++ trunk/Source/WTF/ChangeLog	2013-09-05 21:52:25 UTC (rev 155152)
@@ -1,5 +1,16 @@
 2013-09-05  Anders Carlsson  <[email protected]>
 
+        Make Vector::uncheckedAppend work with move-only types
+        https://bugs.webkit.org/show_bug.cgi?id=120799
+
+        Reviewed by Andreas Kling.
+
+        * wtf/Vector.h:
+        (WTF::::uncheckedAppend):
+        Use std::forward to invoke the move constructor when possible.
+
+2013-09-05  Anders Carlsson  <[email protected]>
+
         Add COMPILER_SUPPORTS(CXX_AUTO_TYPE) and #error if it's 0
         https://bugs.webkit.org/show_bug.cgi?id=120794
 

Modified: trunk/Source/WTF/wtf/Vector.h (155151 => 155152)


--- trunk/Source/WTF/wtf/Vector.h	2013-09-05 21:34:17 UTC (rev 155151)
+++ trunk/Source/WTF/wtf/Vector.h	2013-09-05 21:52:25 UTC (rev 155152)
@@ -643,7 +643,7 @@
 
     template<typename U> void append(const U*, size_t);
     template<typename U> void append(const U&);
-    template<typename U> void uncheckedAppend(const U& val);
+    template<typename U> void uncheckedAppend(U&& val);
     template<typename U, size_t otherCapacity> void appendVector(const Vector<U, otherCapacity>&);
     template<typename U> bool tryAppend(const U*, size_t);
 
@@ -1068,11 +1068,11 @@
 // vector's capacity is large enough for the append to succeed.
 
 template<typename T, size_t inlineCapacity, typename OverflowHandler> template<typename U>
-inline void Vector<T, inlineCapacity, OverflowHandler>::uncheckedAppend(const U& val)
+inline void Vector<T, inlineCapacity, OverflowHandler>::uncheckedAppend(U&& val)
 {
     ASSERT(size() < capacity());
-    const U* ptr = &val;
-    new (NotNull, end()) T(*ptr);
+    typename std::remove_reference<U>::type* ptr = &val;
+    new (NotNull, end()) T(std::forward<U>(*ptr));
     ++m_size;
 }
 

Modified: trunk/Tools/ChangeLog (155151 => 155152)


--- trunk/Tools/ChangeLog	2013-09-05 21:34:17 UTC (rev 155151)
+++ trunk/Tools/ChangeLog	2013-09-05 21:52:25 UTC (rev 155152)
@@ -1,5 +1,19 @@
 2013-09-05  Anders Carlsson  <[email protected]>
 
+        Make Vector::uncheckedAppend work with move-only types
+        https://bugs.webkit.org/show_bug.cgi?id=120799
+
+        Reviewed by Andreas Kling.
+
+        * TestWebKitAPI/Tests/WTF/Vector.cpp:
+        (TestWebKitAPI::MoveOnly):
+        Add a move-only class.
+
+        (TestWebKitAPI::TEST):
+        Add a test for Vector<MoveOnly>::uncheckedAppend.
+
+2013-09-05  Anders Carlsson  <[email protected]>
+
         Move all Vector tests into Vector.cpp
         https://bugs.webkit.org/show_bug.cgi?id=120797
 

Modified: trunk/Tools/TestWebKitAPI/Tests/WTF/Vector.cpp (155151 => 155152)


--- trunk/Tools/TestWebKitAPI/Tests/WTF/Vector.cpp	2013-09-05 21:34:17 UTC (rev 155151)
+++ trunk/Tools/TestWebKitAPI/Tests/WTF/Vector.cpp	2013-09-05 21:52:25 UTC (rev 155152)
@@ -108,4 +108,51 @@
     EXPECT_TRUE(end == it);
 }
 
+class MoveOnly {
+public:
+    MoveOnly(unsigned value)
+        : m_value(value)
+    {
+    }
+
+    unsigned value() const
+    {
+        return m_value;
+    }
+
+    MoveOnly(MoveOnly&& other)
+        : m_value(other.m_value)
+    {
+        other.m_value = 0;
+    }
+
+    MoveOnly& operator=(MoveOnly&& other)
+    {
+        if (this == &other)
+            return *this;
+
+        m_value = other.m_value;
+        other.m_value = 0;
+        return *this;
+    }
+
+private:
+    unsigned m_value;
+};
+
+TEST(WTF_Vector, MoveOnly_UncheckedAppend)
+{
+    Vector<MoveOnly> vector;
+
+    vector.reserveInitialCapacity(100);
+    for (size_t i = 0; i < 100; ++i) {
+        MoveOnly moveOnly(i);
+        vector.uncheckedAppend(std::move(moveOnly));
+        EXPECT_EQ(moveOnly.value(), 0U);
+    }
+
+    for (size_t i = 0; i < 100; ++i)
+        EXPECT_EQ(vector[i].value(), i);
+}
+
 } // namespace TestWebKitAPI
_______________________________________________
webkit-changes mailing list
[email protected]
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to