(wangle) Fix a couple compilation issues
[folly.git] / folly / Optional.h
index f85fbf6b600a2e949d985292848cb824727d16e7..dcc184aa585eafb0022dc0715580cb44696e59c6 100644 (file)
  *    cout << *v << endl;
  *  }
  */
-#include <utility>
 #include <cassert>
 #include <cstddef>
 #include <type_traits>
+#include <utility>
 
 #include <boost/operators.hpp>
 
+#include <folly/Portability.h>
 
 namespace folly {
 
@@ -112,15 +113,17 @@ class Optional {
     }
   }
 
-  /* implicit */ Optional(const None&)
+  /* implicit */ Optional(const None&) noexcept
     : hasValue_(false) {
   }
 
-  /* implicit */ Optional(Value&& newValue) {
+  /* implicit */ Optional(Value&& newValue)
+    noexcept(std::is_nothrow_move_constructible<Value>::value) {
     construct(std::move(newValue));
   }
 
-  /* implicit */ Optional(const Value& newValue) {
+  /* implicit */ Optional(const Value& newValue)
+    noexcept(std::is_nothrow_copy_constructible<Value>::value) {
     construct(newValue);
   }
 
@@ -133,11 +136,13 @@ class Optional {
   }
 
   void assign(Optional&& src) {
-    if (src.hasValue()) {
-      assign(std::move(src.value()));
-      src.clear();
-    } else {
-      clear();
+    if (this != &src) {
+      if (src.hasValue()) {
+        assign(std::move(src.value()));
+        src.clear();
+      } else {
+        clear();
+      }
     }
   }
 
@@ -220,6 +225,17 @@ class Optional {
   const Value* operator->() const { return &value(); }
         Value* operator->()       { return &value(); }
 
+  // Return a copy of the value if set, or a given default if not.
+  template <class U>
+  Value value_or(U&& dflt) const& {
+    return hasValue_ ? value_ : std::forward<U>(dflt);
+  }
+
+  template <class U>
+  Value value_or(U&& dflt) && {
+    return hasValue_ ? std::move(value_) : std::forward<U>(dflt);
+  }
+
  private:
   template<class... Args>
   void construct(Args&&... args) {
@@ -265,11 +281,27 @@ Opt make_optional(T&& v) {
   return Opt(std::forward<T>(v));
 }
 
+///////////////////////////////////////////////////////////////////////////////
+// Comparisons.
+
 template<class V>
-bool operator< (const Optional<V>& a, const Optional<V>& b) {
-  if (a.hasValue() != b.hasValue()) { return a.hasValue() < b.hasValue(); }
-  if (a.hasValue())                 { return a.value()    < b.value(); }
-  return false;
+bool operator==(const Optional<V>& a, const V& b) {
+  return a.hasValue() && a.value() == b;
+}
+
+template<class V>
+bool operator!=(const Optional<V>& a, const V& b) {
+  return !(a == b);
+}
+
+template<class V>
+bool operator==(const V& a, const Optional<V>& b) {
+  return b.hasValue() && b.value() == a;
+}
+
+template<class V>
+bool operator!=(const V& a, const Optional<V>& b) {
+  return !(a == b);
 }
 
 template<class V>
@@ -279,19 +311,16 @@ bool operator==(const Optional<V>& a, const Optional<V>& b) {
   return true;
 }
 
-template<class V>
-bool operator<=(const Optional<V>& a, const Optional<V>& b) {
-  return !(b < a);
-}
-
 template<class V>
 bool operator!=(const Optional<V>& a, const Optional<V>& b) {
-  return !(b == a);
+  return !(a == b);
 }
 
 template<class V>
-bool operator>=(const Optional<V>& a, const Optional<V>& b) {
-  return !(a < b);
+bool operator< (const Optional<V>& a, const Optional<V>& b) {
+  if (a.hasValue() != b.hasValue()) { return a.hasValue() < b.hasValue(); }
+  if (a.hasValue())                 { return a.value()    < b.value(); }
+  return false;
 }
 
 template<class V>
@@ -299,20 +328,28 @@ bool operator> (const Optional<V>& a, const Optional<V>& b) {
   return b < a;
 }
 
+template<class V>
+bool operator<=(const Optional<V>& a, const Optional<V>& b) {
+  return !(b < a);
+}
+
+template<class V>
+bool operator>=(const Optional<V>& a, const Optional<V>& b) {
+  return !(a < b);
+}
+
 // To supress comparability of Optional<T> with T, despite implicit conversion.
 template<class V> bool operator< (const Optional<V>&, const V& other) = delete;
 template<class V> bool operator<=(const Optional<V>&, const V& other) = delete;
-template<class V> bool operator==(const Optional<V>&, const V& other) = delete;
-template<class V> bool operator!=(const Optional<V>&, const V& other) = delete;
 template<class V> bool operator>=(const Optional<V>&, const V& other) = delete;
 template<class V> bool operator> (const Optional<V>&, const V& other) = delete;
 template<class V> bool operator< (const V& other, const Optional<V>&) = delete;
 template<class V> bool operator<=(const V& other, const Optional<V>&) = delete;
-template<class V> bool operator==(const V& other, const Optional<V>&) = delete;
-template<class V> bool operator!=(const V& other, const Optional<V>&) = delete;
 template<class V> bool operator>=(const V& other, const Optional<V>&) = delete;
 template<class V> bool operator> (const V& other, const Optional<V>&) = delete;
 
+///////////////////////////////////////////////////////////////////////////////
+
 } // namespace folly
 
-#endif//FOLLY_OPTIONAL_H_
+#endif // FOLLY_OPTIONAL_H_