Add a move constructor and move assignment operator to Deque
authorandersca@apple.com <andersca@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Fri, 13 Feb 2015 19:52:56 +0000 (19:52 +0000)
committerandersca@apple.com <andersca@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Fri, 13 Feb 2015 19:52:56 +0000 (19:52 +0000)
https://bugs.webkit.org/show_bug.cgi?id=141571

Reviewed by Andreas Kling.

Source/WTF:

* wtf/Deque.h:
(WTF::inlineCapacity>::Deque):
(WTF::=): Deleted.

Tools:

* TestWebKitAPI/Tests/WTF/Deque.cpp:
(TestWebKitAPI::TEST):

git-svn-id: https://svn.webkit.org/repository/webkit/trunk@180066 268f45cc-cd09-0410-ab3c-d52691b4dbfc

Source/WTF/ChangeLog
Source/WTF/wtf/Deque.h
Tools/ChangeLog
Tools/TestWebKitAPI/Tests/WTF/Deque.cpp

index 9b65543..1b68f6c 100644 (file)
@@ -1,5 +1,16 @@
 2015-02-13  Anders Carlsson  <andersca@apple.com>
 
+        Add a move constructor and move assignment operator to Deque
+        https://bugs.webkit.org/show_bug.cgi?id=141571
+
+        Reviewed by Andreas Kling.
+
+        * wtf/Deque.h:
+        (WTF::inlineCapacity>::Deque):
+        (WTF::=): Deleted.
+
+2015-02-13  Anders Carlsson  <andersca@apple.com>
+
         Add an initializer list constructor to Deque
         https://bugs.webkit.org/show_bug.cgi?id=141565
 
index a62048e..8f7ae4f 100644 (file)
@@ -53,11 +53,14 @@ public:
 
     Deque();
     Deque(std::initializer_list<T>);
-    Deque(const Deque<T, inlineCapacity>&);
-    Deque& operator=(const Deque<T, inlineCapacity>&);
+    Deque(const Deque&);
+    Deque(Deque&&);
     ~Deque();
 
-    void swap(Deque<T, inlineCapacity>&);
+    Deque& operator=(const Deque&);
+    Deque& operator=(Deque&&);
+
+    void swap(Deque&);
 
     size_t size() const { return m_start <= m_end ? m_end - m_start : m_end + m_buffer.capacity() - m_start; }
     bool isEmpty() const { return m_start == m_end; }
@@ -282,7 +285,7 @@ inline Deque<T, inlineCapacity>::Deque(std::initializer_list<T> initializerList)
 }
 
 template<typename T, size_t inlineCapacity>
-inline Deque<T, inlineCapacity>::Deque(const Deque<T, inlineCapacity>& other)
+inline Deque<T, inlineCapacity>::Deque(const Deque& other)
     : m_start(other.m_start)
     , m_end(other.m_end)
     , m_buffer(other.m_buffer.capacity())
@@ -300,7 +303,14 @@ inline Deque<T, inlineCapacity>::Deque(const Deque<T, inlineCapacity>& other)
 }
 
 template<typename T, size_t inlineCapacity>
-inline Deque<T, inlineCapacity>& Deque<T, inlineCapacity>::operator=(const Deque<T, inlineCapacity>& other)
+inline Deque<T, inlineCapacity>::Deque(Deque&& other)
+    : Deque()
+{
+    swap(other);
+}
+
+template<typename T, size_t inlineCapacity>
+inline auto Deque<T, inlineCapacity>::operator=(const Deque& other) -> Deque&
 {
     // FIXME: This is inefficient if we're using an inline buffer and T is
     // expensive to copy since it will copy the buffer twice instead of once.
@@ -310,6 +320,13 @@ inline Deque<T, inlineCapacity>& Deque<T, inlineCapacity>::operator=(const Deque
 }
 
 template<typename T, size_t inlineCapacity>
+inline auto Deque<T, inlineCapacity>::operator=(Deque&& other) -> Deque&
+{
+    swap(other);
+    return *this;
+}
+
+template<typename T, size_t inlineCapacity>
 inline void Deque<T, inlineCapacity>::destroyAll()
 {
     if (m_start <= m_end)
index 441b5bb..941f311 100644 (file)
@@ -1,3 +1,13 @@
+2015-02-13  Anders Carlsson  <andersca@apple.com>
+
+        Add a move constructor and move assignment operator to Deque
+        https://bugs.webkit.org/show_bug.cgi?id=141571
+
+        Reviewed by Andreas Kling.
+
+        * TestWebKitAPI/Tests/WTF/Deque.cpp:
+        (TestWebKitAPI::TEST):
+
 2015-02-13  Csaba Osztrogon√°c  <ossy@webkit.org>
 
         run-jsc-stress-tests --remote should skip profiler tests
index 874f5c4..db9b0ac 100644 (file)
@@ -144,4 +144,48 @@ TEST(WTF_Deque, MoveOnly)
     EXPECT_EQ(1U, last.value());
 }
 
+TEST(WTF_Deque, MoveConstructor)
+{
+    Deque<MoveOnly, 4> deque;
+
+    for (unsigned i = 0; i < 10; ++i)
+        deque.append(MoveOnly(i));
+
+    EXPECT_EQ(10u, deque.size());
+
+    Deque<MoveOnly, 4> deque2 = WTF::move(deque);
+
+    EXPECT_EQ(10u, deque2.size());
+
+    unsigned i = 0;
+    for (auto& element : deque2) {
+        EXPECT_EQ(i, element.value());
+        ++i;
+    }
+}
+
+TEST(WTF_Deque, MoveAssignmentOperator)
+{
+    Deque<MoveOnly, 4> deque1;
+
+    for (unsigned i = 0; i < 10; ++i)
+        deque1.append(MoveOnly(i));
+
+    EXPECT_EQ(10u, deque1.size());
+
+    Deque<MoveOnly, 4> deque2;
+    for (unsigned i = 0; i < 10; ++i)
+        deque2.append(MoveOnly(i * 2));
+
+    deque1 = WTF::move(deque2);
+
+    EXPECT_EQ(10u, deque2.size());
+
+    unsigned i = 0;
+    for (auto& element : deque1) {
+        EXPECT_EQ(i * 2, element.value());
+        ++i;
+    }
+}
+
 } // namespace TestWebKitAPI