Bug 1129559 - Implement move construction for mozilla::MaybeOneOf; r=jandem
authorNick Fitzgerald <fitzgen@gmail.com>
Wed, 04 Feb 2015 13:34:18 -0800
changeset 227501 cd19700f93819787fb6ce461f30b03541c5c0fcd
parent 227500 3adfa33cef51a556e57656087e2b2fdd380f5979
child 227502 fdc1b8923d34846c4b02d173226f2c6ff71c658e
push id55140
push usernfitzgerald@mozilla.com
push dateWed, 04 Feb 2015 21:34:31 +0000
treeherdermozilla-inbound@cd19700f9381 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersjandem
bugs1129559
milestone38.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1129559 - Implement move construction for mozilla::MaybeOneOf; r=jandem
mfbt/MaybeOneOf.h
--- a/mfbt/MaybeOneOf.h
+++ b/mfbt/MaybeOneOf.h
@@ -46,16 +46,39 @@ class MaybeOneOf
     MOZ_ASSERT(state == Type2State<T>::result);
     return *(T*)storage.addr();
   }
 
 public:
   MaybeOneOf() : state(None) {}
   ~MaybeOneOf() { destroyIfConstructed(); }
 
+  MaybeOneOf(MaybeOneOf&& rhs)
+    : state(None)
+  {
+    if (!rhs.empty()) {
+      if (rhs.constructed<T1>()) {
+        construct<T1>(Move(rhs.as<T1>()));
+        rhs.as<T1>().~T1();
+      } else {
+        construct<T2>(Move(rhs.as<T2>()));
+        rhs.as<T2>().~T2();
+      }
+      rhs.state = None;
+    }
+  }
+
+  MaybeOneOf &operator=(MaybeOneOf&& rhs)
+  {
+    MOZ_ASSERT(this != &rhs, "Self-move is prohibited");
+    this->~MaybeOneOf();
+    new(this) MaybeOneOf(Move(rhs));
+    return *this;
+  }
+
   bool empty() const { return state == None; }
 
   template <class T>
   bool constructed() const { return state == Type2State<T>::result; }
 
   template <class T, class... Args>
   void construct(Args&&... aArgs)
   {