Fix resplit | batch
[folly.git] / folly / Optional.h
index b168b59a768f825eb77c7e0334dc5fafdb99abea..f4bc9ff4ce7a36bb47eb82f7a5d49a4d5d7a7e10 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright 2013 Facebook, Inc.
+ * Copyright 2015 Facebook, Inc.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
  *    cout << *v << endl;
  *  }
  */
-#include <utility>
 #include <cassert>
 #include <cstddef>
 #include <type_traits>
+#include <utility>
 
 #include <boost/operators.hpp>
 
+#include <folly/Portability.h>
+
 namespace folly {
 
 namespace detail { struct NoneHelper {}; }
@@ -73,7 +75,7 @@ const None none = nullptr;
  * gcc-4.7 warns about use of uninitialized memory around the use of storage_
  * even though this is explicitly initialized at each point.
  */
-#ifdef __GNUC__
+#if defined(__GNUC__) && !defined(__clang__)
 # pragma GCC diagnostic push
 # pragma GCC diagnostic ignored "-Wuninitialized"
 # pragma GCC diagnostic ignored "-Wpragmas"
@@ -81,19 +83,20 @@ const None none = nullptr;
 #endif // __GNUC__
 
 template<class Value>
-class Optional : boost::totally_ordered<Optional<Value>,
-                 boost::totally_ordered<Optional<Value>, Value>> {
-  typedef void (Optional::*bool_type)() const;
-  void truthy() const {};
+class Optional {
  public:
   static_assert(!std::is_reference<Value>::value,
                 "Optional may not be used with reference types");
+  static_assert(!std::is_abstract<Value>::value,
+                "Optional may not be used with abstract types");
 
   Optional()
     : hasValue_(false) {
   }
 
-  Optional(const Optional& src) {
+  Optional(const Optional& src)
+    noexcept(std::is_nothrow_copy_constructible<Value>::value) {
+
     if (src.hasValue()) {
       construct(src.value());
     } else {
@@ -101,7 +104,9 @@ class Optional : boost::totally_ordered<Optional<Value>,
     }
   }
 
-  Optional(Optional&& src) {
+  Optional(Optional&& src)
+    noexcept(std::is_nothrow_move_constructible<Value>::value) {
+
     if (src.hasValue()) {
       construct(std::move(src.value()));
       src.clear();
@@ -110,19 +115,21 @@ class Optional : boost::totally_ordered<Optional<Value>,
     }
   }
 
-  /* implicit */ Optional(const None& empty)
+  /* 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);
   }
 
-  ~Optional() {
+  ~Optional() noexcept {
     clear();
   }
 
@@ -131,11 +138,13 @@ class Optional : boost::totally_ordered<Optional<Value>,
   }
 
   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();
+      }
     }
   }
 
@@ -169,30 +178,18 @@ class Optional : boost::totally_ordered<Optional<Value>,
     return *this;
   }
 
-  bool operator<(const Optional& other) const {
-    if (hasValue() != other.hasValue()) {
-      return hasValue() < other.hasValue();
-    }
-    if (hasValue()) {
-      return value() < other.value();
-    }
-    return false; // both empty
-  }
+  Optional& operator=(Optional &&other)
+    noexcept (std::is_nothrow_move_assignable<Value>::value) {
 
-  bool operator<(const Value& other) const {
-    return !hasValue() || value() < other;
+    assign(std::move(other));
+    return *this;
   }
 
-  bool operator==(const Optional& other) const {
-    if (hasValue()) {
-      return other.hasValue() && value() == other.value();
-    } else {
-      return !other.hasValue();
-    }
-  }
+  Optional& operator=(const Optional &other)
+    noexcept (std::is_nothrow_copy_assignable<Value>::value) {
 
-  bool operator==(const Value& other) const {
-    return hasValue() && value() == other;
+    assign(other);
+    return *this;
   }
 
   template<class... Args>
@@ -220,9 +217,8 @@ class Optional : boost::totally_ordered<Optional<Value>,
 
   bool hasValue() const { return hasValue_; }
 
-  /* safe bool idiom */
-  operator bool_type() const {
-    return hasValue() ? &Optional::truthy : nullptr;
+  explicit operator bool() const {
+    return hasValue();
   }
 
   const Value& operator*() const { return value(); }
@@ -231,6 +227,17 @@ class Optional : boost::totally_ordered<Optional<Value>,
   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) {
@@ -245,7 +252,9 @@ class Optional : boost::totally_ordered<Optional<Value>,
   bool hasValue_;
 };
 
+#if defined(__GNUC__) && !defined(__clang__)
 #pragma GCC diagnostic pop
+#endif
 
 template<class T>
 const T* get_pointer(const Optional<T>& opt) {
@@ -274,6 +283,75 @@ Opt make_optional(T&& v) {
   return Opt(std::forward<T>(v));
 }
 
+///////////////////////////////////////////////////////////////////////////////
+// Comparisons.
+
+template<class V>
+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>
+bool operator==(const Optional<V>& a, const Optional<V>& b) {
+  if (a.hasValue() != b.hasValue()) { return false; }
+  if (a.hasValue())                 { return a.value() == b.value(); }
+  return true;
+}
+
+template<class V>
+bool operator!=(const Optional<V>& a, const Optional<V>& b) {
+  return !(a == b);
+}
+
+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;
+}
+
+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);
+}
+
+template<class V>
+bool operator>=(const Optional<V>& a, const Optional<V>& b) {
+  return !(a < b);
+}
+
+// Suppress 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 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_