folly: add bser encode/decode for dynamic
[folly.git] / folly / Optional.h
index 82070889e32d17a812af444b8b474532bb626e6c..38f198fdc5f8fc0cb02cd95d4c8880769beac1e0 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 <stdexcept>
 #include <type_traits>
-
-#include <boost/operators.hpp>
+#include <utility>
 
 namespace folly {
 
@@ -80,20 +78,29 @@ const None none = nullptr;
 # pragma GCC diagnostic ignored "-Wmaybe-uninitialized"
 #endif // __GNUC__
 
+class OptionalEmptyException : public std::runtime_error {
+ public:
+  OptionalEmptyException()
+      : std::runtime_error("Empty Optional cannot be unwrapped") {}
+};
+
 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:
+  typedef Value value_type;
+
   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()
+  Optional() noexcept
     : 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 +108,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 +119,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 +142,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 +182,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>
@@ -208,30 +209,56 @@ class Optional : boost::totally_ordered<Optional<Value>,
     }
   }
 
-  const Value& value() const {
-    assert(hasValue());
+  const Value& value() const& {
+    require_value();
     return value_;
   }
 
-  Value& value() {
-    assert(hasValue());
+  Value& value() {
+    require_value();
     return value_;
   }
 
+  Value value() && {
+    require_value();
+    return std::move(value_);
+  }
+
+  const Value* get_pointer() const&  { return hasValue_ ? &value_ : nullptr; }
+        Value* get_pointer()      &  { return hasValue_ ? &value_ : nullptr; }
+        Value* get_pointer()      && = delete;
+
   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(); }
-        Value& operator*()       { return value(); }
+  const Value& operator*() const&  { return value(); }
+        Value& operator*()      &  { return value(); }
+        Value  operator*()      && { return std::move(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:
+  void require_value() const {
+    if (!hasValue_) {
+      throw OptionalEmptyException();
+    }
+  }
+
   template<class... Args>
   void construct(Args&&... args) {
     const void* ptr = &value_;
@@ -251,12 +278,12 @@ class Optional : boost::totally_ordered<Optional<Value>,
 
 template<class T>
 const T* get_pointer(const Optional<T>& opt) {
-  return opt ? &opt.value() : nullptr;
+  return opt.get_pointer();
 }
 
 template<class T>
 T* get_pointer(Optional<T>& opt) {
-  return opt ? &opt.value() : nullptr;
+  return opt.get_pointer();
 }
 
 template<class T>
@@ -276,6 +303,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_