(wangle) s/continuation_/callback/ (missed some)
[folly.git] / folly / Optional.h
index 5b574f5b509dd557189a9aa3eb5f2836b9d4d37a..701d744e9ad4ff16928afc2aea6c8962e71ef60d 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright 2013 Facebook, Inc.
+ * Copyright 2014 Facebook, Inc.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -61,6 +61,8 @@
 
 #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,10 +83,7 @@ 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");
@@ -93,7 +92,9 @@ class Optional : boost::totally_ordered<Optional<Value>,
     : 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 +102,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 +113,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();
   }
 
@@ -169,30 +174,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 +213,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(); }
@@ -245,7 +237,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) {
@@ -268,6 +262,60 @@ void swap(Optional<T>& a, Optional<T>& b) {
   }
 }
 
-}// namespace folly
+template<class T,
+         class Opt = Optional<typename std::decay<T>::type>>
+Opt make_optional(T&& v) {
+  return Opt(std::forward<T>(v));
+}
+
+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) {
+  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 !(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);
+}
+
+template<class V>
+bool operator> (const Optional<V>& a, const Optional<V>& b) {
+  return b < a;
+}
+
+// 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_