Use nullptr rather than 0 for a null pointer
[folly.git] / folly / Optional.h
index f8d39cb8cc39e0ce9b4407041e5a9d329be7e825..2bb2d658eb897467cb7786f8343b51f3851196bd 100644 (file)
@@ -67,6 +67,9 @@
 
 namespace folly {
 
+template <class Value>
+class Optional;
+
 namespace detail {
 struct NoneHelper {};
 
@@ -74,7 +77,10 @@ struct NoneHelper {};
 // If exceptions are disabled, std::terminate() will be called instead of
 // throwing OptionalEmptyException when the condition fails.
 [[noreturn]] void throw_optional_empty_exception();
-}
+
+template <class Value>
+struct OptionalPromiseReturn;
+} // namespace detail
 
 typedef int detail::NoneHelper::*None;
 
@@ -98,7 +104,7 @@ class Optional {
       !std::is_abstract<Value>::value,
       "Optional may not be used with abstract types");
 
-  Optional() noexcept {}
+  FOLLY_CPP14_CONSTEXPR Optional() noexcept {}
 
   Optional(const Optional& src) noexcept(
       std::is_nothrow_copy_constructible<Value>::value) {
@@ -115,24 +121,30 @@ class Optional {
     }
   }
 
-  /* implicit */ Optional(const None&) noexcept {}
+  FOLLY_CPP14_CONSTEXPR /* implicit */ Optional(const None&) noexcept {}
 
-  /* implicit */ Optional(Value&& newValue) noexcept(
+  FOLLY_CPP14_CONSTEXPR /* implicit */ Optional(Value&& newValue) noexcept(
       std::is_nothrow_move_constructible<Value>::value) {
     storage_.construct(std::move(newValue));
   }
 
-  /* implicit */ Optional(const Value& newValue) noexcept(
+  FOLLY_CPP14_CONSTEXPR /* implicit */ Optional(const Value& newValue) noexcept(
       std::is_nothrow_copy_constructible<Value>::value) {
     storage_.construct(newValue);
   }
 
   template <typename... Args>
-  explicit Optional(in_place_t, Args&&... args) noexcept(
+  FOLLY_CPP14_CONSTEXPR explicit Optional(in_place_t, Args&&... args) noexcept(
       std::is_nothrow_constructible<Value, Args...>::value) {
     storage_.construct(std::forward<Args>(args)...);
   }
 
+  // Used only when an Optional is used with coroutines on MSVC
+  /* implicit */ Optional(const detail::OptionalPromiseReturn<Value>& p)
+      : Optional{} {
+    p.promise_->value_ = this;
+  }
+
   void assign(const None&) {
     clear();
   }
@@ -200,22 +212,22 @@ class Optional {
     storage_.clear();
   }
 
-  const Value& value() const & {
+  FOLLY_CPP14_CONSTEXPR const Value& value() const & {
     require_value();
     return *storage_.value_pointer();
   }
 
-  Value& value() & {
+  FOLLY_CPP14_CONSTEXPR Value& value() & {
     require_value();
     return *storage_.value_pointer();
   }
 
-  Value&& value() && {
+  FOLLY_CPP14_CONSTEXPR Value&& value() && {
     require_value();
     return std::move(*storage_.value_pointer());
   }
 
-  const Value&& value() const && {
+  FOLLY_CPP14_CONSTEXPR const Value&& value() const && {
     require_value();
     return std::move(*storage_.value_pointer());
   }
@@ -228,37 +240,37 @@ class Optional {
   }
   Value* get_pointer() && = delete;
 
-  bool hasValue() const {
+  FOLLY_CPP14_CONSTEXPR bool hasValue() const noexcept {
     return storage_.hasValue();
   }
 
-  explicit operator bool() const {
+  FOLLY_CPP14_CONSTEXPR explicit operator bool() const noexcept {
     return hasValue();
   }
 
-  const Value& operator*() const & {
+  FOLLY_CPP14_CONSTEXPR const Value& operator*() const & {
     return value();
   }
-  Value& operator*() & {
+  FOLLY_CPP14_CONSTEXPR Value& operator*() & {
     return value();
   }
-  const Value&& operator*() const && {
+  FOLLY_CPP14_CONSTEXPR const Value&& operator*() const && {
     return std::move(value());
   }
-  Value&& operator*() && {
+  FOLLY_CPP14_CONSTEXPR Value&& operator*() && {
     return std::move(value());
   }
 
-  const Value* operator->() const {
+  FOLLY_CPP14_CONSTEXPR const Value* operator->() const {
     return &value();
   }
-  Value* operator->() {
+  FOLLY_CPP14_CONSTEXPR 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 & {
+  FOLLY_CPP14_CONSTEXPR Value value_or(U&& dflt) const & {
     if (storage_.hasValue()) {
       return *storage_.value_pointer();
     }
@@ -267,7 +279,7 @@ class Optional {
   }
 
   template <class U>
-  Value value_or(U&& dflt) && {
+  FOLLY_CPP14_CONSTEXPR Value value_or(U&& dflt) && {
     if (storage_.hasValue()) {
       return std::move(*storage_.value_pointer());
     }
@@ -319,7 +331,7 @@ class Optional {
                        std::is_trivially_destructible<Value>::value,
                        StorageTriviallyDestructible,
                        StorageNonTriviallyDestructible>::type {
-    bool hasValue() const {
+    bool hasValue() const noexcept {
       return this->hasValue_;
     }
 
@@ -374,7 +386,7 @@ void swap(Optional<T>& a, Optional<T>& b) {
 }
 
 template <class T, class Opt = Optional<typename std::decay<T>::type>>
-Opt make_optional(T&& v) {
+constexpr Opt make_optional(T&& v) {
   return Opt(std::forward<T>(v));
 }
 
@@ -382,27 +394,29 @@ Opt make_optional(T&& v) {
 // Comparisons.
 
 template <class U, class V>
-bool operator==(const Optional<U>& a, const V& b) {
+constexpr bool operator==(const Optional<U>& a, const V& b) {
   return a.hasValue() && a.value() == b;
 }
 
 template <class U, class V>
-bool operator!=(const Optional<U>& a, const V& b) {
+constexpr bool operator!=(const Optional<U>& a, const V& b) {
   return !(a == b);
 }
 
 template <class U, class V>
-bool operator==(const U& a, const Optional<V>& b) {
+constexpr bool operator==(const U& a, const Optional<V>& b) {
   return b.hasValue() && b.value() == a;
 }
 
 template <class U, class V>
-bool operator!=(const U& a, const Optional<V>& b) {
+constexpr bool operator!=(const U& a, const Optional<V>& b) {
   return !(a == b);
 }
 
 template <class U, class V>
-bool operator==(const Optional<U>& a, const Optional<V>& b) {
+FOLLY_CPP14_CONSTEXPR bool operator==(
+    const Optional<U>& a,
+    const Optional<V>& b) {
   if (a.hasValue() != b.hasValue()) {
     return false;
   }
@@ -413,12 +427,14 @@ bool operator==(const Optional<U>& a, const Optional<V>& b) {
 }
 
 template <class U, class V>
-bool operator!=(const Optional<U>& a, const Optional<V>& b) {
+constexpr bool operator!=(const Optional<U>& a, const Optional<V>& b) {
   return !(a == b);
 }
 
 template <class U, class V>
-bool operator<(const Optional<U>& a, const Optional<V>& b) {
+FOLLY_CPP14_CONSTEXPR bool operator<(
+    const Optional<U>& a,
+    const Optional<V>& b) {
   if (a.hasValue() != b.hasValue()) {
     return a.hasValue() < b.hasValue();
   }
@@ -429,17 +445,17 @@ bool operator<(const Optional<U>& a, const Optional<V>& b) {
 }
 
 template <class U, class V>
-bool operator>(const Optional<U>& a, const Optional<V>& b) {
+constexpr bool operator>(const Optional<U>& a, const Optional<V>& b) {
   return b < a;
 }
 
 template <class U, class V>
-bool operator<=(const Optional<U>& a, const Optional<V>& b) {
+constexpr bool operator<=(const Optional<U>& a, const Optional<V>& b) {
   return !(b < a);
 }
 
 template <class U, class V>
-bool operator>=(const Optional<U>& a, const Optional<V>& b) {
+constexpr bool operator>=(const Optional<U>& a, const Optional<V>& b) {
   return !(a < b);
 }
 
@@ -461,6 +477,48 @@ bool operator>=(const V& other, const Optional<V>&) = delete;
 template <class V>
 bool operator>(const V& other, const Optional<V>&) = delete;
 
+// Comparisons with none
+template <class V>
+constexpr bool operator==(const Optional<V>& a, None) noexcept {
+  return !a.hasValue();
+}
+template <class V>
+constexpr bool operator==(None, const Optional<V>& a) noexcept {
+  return !a.hasValue();
+}
+template <class V>
+constexpr bool operator<(const Optional<V>&, None) noexcept {
+  return false;
+}
+template <class V>
+constexpr bool operator<(None, const Optional<V>& a) noexcept {
+  return a.hasValue();
+}
+template <class V>
+constexpr bool operator>(const Optional<V>& a, None) noexcept {
+  return a.hasValue();
+}
+template <class V>
+constexpr bool operator>(None, const Optional<V>&) noexcept {
+  return false;
+}
+template <class V>
+constexpr bool operator<=(None, const Optional<V>&) noexcept {
+  return true;
+}
+template <class V>
+constexpr bool operator<=(const Optional<V>& a, None) noexcept {
+  return !a.hasValue();
+}
+template <class V>
+constexpr bool operator>=(const Optional<V>&, None) noexcept {
+  return true;
+}
+template <class V>
+constexpr bool operator>=(None, const Optional<V>& a) noexcept {
+  return !a.hasValue();
+}
+
 ///////////////////////////////////////////////////////////////////////////////
 
 } // namespace folly
@@ -477,3 +535,96 @@ struct hash<folly::Optional<T>> {
   }
 };
 FOLLY_NAMESPACE_STD_END
+
+// Enable the use of folly::Optional with `co_await`
+// Inspired by https://github.com/toby-allsopp/coroutine_monad
+#if FOLLY_HAS_COROUTINES
+#include <experimental/coroutine>
+
+namespace folly {
+namespace detail {
+template <typename Value>
+struct OptionalPromise;
+
+template <typename Value>
+struct OptionalPromiseReturn {
+  Optional<Value> storage_;
+  OptionalPromise<Value>* promise_;
+  /* implicit */ OptionalPromiseReturn(OptionalPromise<Value>& promise) noexcept
+      : promise_(&promise) {
+    promise.value_ = &storage_;
+  }
+  OptionalPromiseReturn(OptionalPromiseReturn&& that) noexcept
+      : OptionalPromiseReturn{*that.promise_} {}
+  ~OptionalPromiseReturn() {}
+  /* implicit */ operator Optional<Value>() & {
+    return std::move(storage_);
+  }
+};
+
+template <typename Value>
+struct OptionalPromise {
+  Optional<Value>* value_ = nullptr;
+  OptionalPromise() = default;
+  OptionalPromise(OptionalPromise const&) = delete;
+  // This should work regardless of whether the compiler generates:
+  //    folly::Optional<Value> retobj{ p.get_return_object(); } // MSVC
+  // or:
+  //    auto retobj = p.get_return_object(); // clang
+  OptionalPromiseReturn<Value> get_return_object() noexcept {
+    return *this;
+  }
+  std::experimental::suspend_never initial_suspend() const noexcept {
+    return {};
+  }
+  std::experimental::suspend_never final_suspend() const {
+    return {};
+  }
+  template <typename U>
+  void return_value(U&& u) {
+    *value_ = static_cast<U&&>(u);
+  }
+  void unhandled_exception() {
+    // Technically, throwing from unhandled_exception is underspecified:
+    // https://github.com/GorNishanov/CoroutineWording/issues/17
+    throw;
+  }
+};
+
+template <typename Value>
+struct OptionalAwaitable {
+  Optional<Value> o_;
+  bool await_ready() const noexcept {
+    return o_.hasValue();
+  }
+  Value await_resume() {
+    return std::move(o_.value());
+  }
+
+  // Explicitly only allow suspension into an OptionalPromise
+  template <typename U>
+  void await_suspend(
+      std::experimental::coroutine_handle<OptionalPromise<U>> h) const {
+    // Abort the rest of the coroutine. resume() is not going to be called
+    h.destroy();
+  }
+};
+} // namespace detail
+
+template <typename Value>
+detail::OptionalAwaitable<Value>
+/* implicit */ operator co_await(Optional<Value> o) {
+  return {std::move(o)};
+}
+} // namespace folly
+
+// This makes folly::Optional<Value> useable as a coroutine return type..
+FOLLY_NAMESPACE_STD_BEGIN
+namespace experimental {
+template <typename Value, typename... Args>
+struct coroutine_traits<folly::Optional<Value>, Args...> {
+  using promise_type = folly::detail::OptionalPromise<Value>;
+};
+} // experimental
+FOLLY_NAMESPACE_STD_END
+#endif // FOLLY_HAS_COROUTINES