X-Git-Url: http://plrg.eecs.uci.edu/git/?a=blobdiff_plain;f=folly%2Ffutures%2FPromise.h;h=ccc74c6e2047bce606179cd9c4a5cec41d12ff8b;hb=7da4ef82aee382777bb50aadd4af14a482739d10;hp=58b7323065162ca571c566c382aad21d7950e041;hpb=bc374dcbb0e552826e4bcbed1d8cbd479ce062c0;p=folly.git diff --git a/folly/futures/Promise.h b/folly/futures/Promise.h index 58b73230..ccc74c6e 100644 --- a/folly/futures/Promise.h +++ b/folly/futures/Promise.h @@ -1,5 +1,5 @@ /* - * Copyright 2014 Facebook, Inc. + * Copyright 2014-present Facebook, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -16,18 +16,32 @@ #pragma once -#include -#include +#include +#include #include -namespace folly { namespace wangle { +namespace folly { // forward declaration +template +class SemiFuture; template class Future; +namespace futures { +namespace detail { +template +class FutureBase; +struct EmptyConstruct {}; +template +class CoreCallbackState; +} // namespace detail +} // namespace futures + template class Promise { -public: + public: + static Promise makeEmpty() noexcept; // equivalent to moved-from + Promise(); ~Promise(); @@ -36,26 +50,34 @@ public: Promise& operator=(Promise const&) = delete; // movable - Promise(Promise&&); - Promise& operator=(Promise&&); + Promise(Promise&&) noexcept; + Promise& operator=(Promise&&) noexcept; + + /** Return a SemiFuture tied to the shared core state. This can be called only + once, thereafter FutureAlreadyRetrieved exception will be raised. */ + SemiFuture getSemiFuture(); /** Return a Future tied to the shared core state. This can be called only - once, thereafter Future already retrieved exception will be raised. */ + once, thereafter FutureAlreadyRetrieved exception will be raised. + NOTE: This function is deprecated. Please use getSemiFuture and pass the + appropriate executor to .via on the returned SemiFuture to get a + valid Future where necessary. */ Future 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 +87,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); - /** Fulfil this Promise (only for Promise) */ - void setValue(); + /// Sugar to fulfill this Promise + template + typename std::enable_if::value, void>::type + setValue() { + setTry(Try(T())); + } /** Set the value (use perfect forwarding for both move and copy) */ template void setValue(M&& value); - void fulfilTry(Try&& t); + void setTry(Try&& 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 - void fulfil(F&& func); + void setWith(F&& func); + + bool isFulfilled() const noexcept; -private: + private: typedef typename Future::corePtr corePtr; + template + friend class futures::detail::FutureBase; + template + friend class SemiFuture; + template + friend class Future; + template + friend class futures::detail::CoreCallbackState; // Whether the Future has been retrieved (a one-time operation). bool retrieved_; @@ -97,12 +133,14 @@ private: // shared core state object corePtr core_; + explicit Promise(futures::detail::EmptyConstruct) noexcept; + void throwIfFulfilled(); void throwIfRetrieved(); void detach(); }; -}} +} // namespace folly #include #include