Expose the time remaining in HHWheelTimer::Callback
[folly.git] / folly / futures / Promise.h
index a223db0b4a771fe7bd5e028c089e7b3123d327dc..7bb58d1e8abfaa61c9ff4f9d303848472013fa8d 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright 2014 Facebook, Inc.
+ * Copyright 2017 Facebook, Inc.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
 
 #pragma once
 
-#include <folly/futures/Deprecated.h>
-#include <folly/futures/Try.h>
+#include <folly/Portability.h>
+#include <folly/Try.h>
 #include <functional>
 
 namespace folly {
 
 // forward declaration
+template <class T>
+class SemiFuture;
 template <class T> class Future;
 
+namespace futures {
+namespace detail {
+template <class T>
+class FutureBase;
+struct EmptyConstruct {};
+template <typename T, typename F>
+class CoreCallbackState;
+} // namespace detail
+} // namespace futures
+
 template <class T>
 class Promise {
-public:
+ public:
+  static Promise<T> makeEmpty() noexcept; // equivalent to moved-from
+
   Promise();
   ~Promise();
 
@@ -43,19 +57,20 @@ public:
     once, thereafter Future already retrieved exception will be raised. */
   Future<T> getFuture();
 
-  /** Fulfil the Promise with an exception_wrapper */
+  /** Fulfill the Promise with an exception_wrapper */
   void setException(exception_wrapper ew);
 
-  /** Fulfil the Promise with an exception_ptr, e.g.
+  /** Fulfill the Promise with an exception_ptr, e.g.
     try {
       ...
     } catch (...) {
       p.setException(std::current_exception());
     }
     */
-  void setException(std::exception_ptr const&) DEPRECATED;
+  FOLLY_DEPRECATED("use setException(exception_wrapper)")
+  void setException(std::exception_ptr const&);
 
-  /** Fulfil the Promise with an exception type E, which can be passed to
+  /** Fulfill the Promise with an exception type E, which can be passed to
     std::make_exception_ptr(). Useful for originating exceptions. If you
     caught an exception the exception_wrapper form is more appropriate.
     */
@@ -65,31 +80,45 @@ public:
 
   /// Set an interrupt handler to handle interrupts. See the documentation for
   /// Future::raise(). Your handler can do whatever it wants, but if you
-  /// bother to set one then you probably will want to fulfil the promise with
+  /// bother to set one then you probably will want to fulfill the promise with
   /// an exception (or special value) indicating how the interrupt was
   /// handled.
   void setInterruptHandler(std::function<void(exception_wrapper const&)>);
 
-  /** Fulfil this Promise (only for Promise<void>) */
-  void setValue();
+  /// Sugar to fulfill this Promise<Unit>
+  template <class B = T>
+  typename std::enable_if<std::is_same<Unit, B>::value, void>::type
+  setValue() {
+    setTry(Try<T>(T()));
+  }
 
   /** Set the value (use perfect forwarding for both move and copy) */
   template <class M>
   void setValue(M&& value);
 
-  void fulfilTry(Try<T> t);
+  void setTry(Try<T>&& t);
 
-  /** Fulfil this Promise with the result of a function that takes no
+  /** Fulfill this Promise with the result of a function that takes no
     arguments and returns something implicitly convertible to T.
     Captures exceptions. e.g.
 
-    p.fulfil([] { do something that may throw; return a T; });
+    p.setWith([] { do something that may throw; return a T; });
   */
   template <class F>
-  void fulfil(F&& func);
+  void setWith(F&& func);
+
+  bool isFulfilled() const noexcept;
 
-private:
+ private:
   typedef typename Future<T>::corePtr corePtr;
+  template <class>
+  friend class futures::detail::FutureBase;
+  template <class>
+  friend class SemiFuture;
+  template <class>
+  friend class Future;
+  template <class, class>
+  friend class futures::detail::CoreCallbackState;
 
   // Whether the Future has been retrieved (a one-time operation).
   bool retrieved_;
@@ -97,12 +126,14 @@ private:
   // shared core state object
   corePtr core_;
 
+  explicit Promise(futures::detail::EmptyConstruct) noexcept;
+
   void throwIfFulfilled();
   void throwIfRetrieved();
   void detach();
 };
 
-}
+} // namespace folly
 
 #include <folly/futures/Future.h>
 #include <folly/futures/Promise-inl.h>