proxygen
Promise.h
Go to the documentation of this file.
1 /*
2  * Copyright 2014-present Facebook, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #pragma once
18 
19 #include <functional>
20 
21 #include <folly/Portability.h>
22 #include <folly/Try.h>
24 #include <folly/lang/Exception.h>
25 
26 namespace folly {
27 
28 class FOLLY_EXPORT PromiseException : public std::logic_error {
29  public:
30  using std::logic_error::logic_error;
31 };
32 
34  public:
35  PromiseInvalid() : PromiseException("Promise invalid") {}
36 };
37 
39  public:
40  PromiseAlreadySatisfied() : PromiseException("Promise already satisfied") {}
41 };
42 
44  public:
45  FutureAlreadyRetrieved() : PromiseException("Future already retrieved") {}
46 };
47 
49  public:
50  explicit BrokenPromise(const std::string& type)
51  : PromiseException("Broken promise for type name `" + type + '`') {}
52 
53  explicit BrokenPromise(const char* type) : BrokenPromise(std::string(type)) {}
54 };
55 
56 // forward declaration
57 template <class T>
58 class SemiFuture;
59 template <class T>
60 class Future;
61 
62 namespace futures {
63 namespace detail {
64 template <class T>
65 class FutureBase;
66 struct EmptyConstruct {};
67 template <typename T, typename F>
68 class CoreCallbackState;
69 } // namespace detail
70 } // namespace futures
71 
142 template <class T>
143 class Promise {
144  public:
151  static Promise<T> makeEmpty() noexcept;
152 
159  Promise();
160 
167  ~Promise();
168 
169  // not copyable
170  Promise(Promise const&) = delete;
171  Promise& operator=(Promise const&) = delete;
172 
180  Promise(Promise<T>&& other) noexcept;
181 
193  Promise& operator=(Promise<T>&& other) noexcept;
194 
210  SemiFuture<T> getSemiFuture();
211 
229  Future<T> getFuture();
230 
253  void setException(exception_wrapper ew);
254 
259  template <class E>
261  setException(E const& e);
262 
310  template <typename F>
311  void setInterruptHandler(F&& fn);
312 
325  template <class B = T>
327  setTry(Try<T>(T()));
328  }
329 
344  template <class M>
345  void setValue(M&& value);
346 
359  void setTry(Try<T>&& t);
360 
385  template <class F>
386  void setWith(F&& func);
387 
390  bool valid() const noexcept {
391  return core_ != nullptr;
392  }
393 
399  bool isFulfilled() const noexcept;
400 
401  private:
402  template <class>
404  template <class>
405  friend class SemiFuture;
406  template <class>
407  friend class Future;
408  template <class, class>
410 
411  // Whether the Future has been retrieved (a one-time operation).
413 
415 
416  // Throws PromiseInvalid if there is no shared state object; else returns it
417  // by ref.
418  //
419  // Implementation methods should usually use this instead of `this->core_`.
420  // The latter should be used only when you need the possibly-null pointer.
422  return getCoreImpl(core_);
423  }
424  Core const& getCore() const {
425  return getCoreImpl(core_);
426  }
427 
428  template <typename CoreT>
429  static CoreT& getCoreImpl(CoreT* core) {
430  if (!core) {
431  throw_exception<PromiseInvalid>();
432  }
433  return *core;
434  }
435 
436  // shared core state object
437  // usually you should use `getCore()` instead of directly accessing `core_`.
439 
440  explicit Promise(futures::detail::EmptyConstruct) noexcept;
441 
442  void throwIfFulfilled() const;
443  void detach();
444 };
445 
446 } // namespace folly
447 
448 #include <folly/futures/Future.h>
Core const & getCore() const
Definition: Promise.h:424
BrokenPromise(const std::string &type)
Definition: Promise.h:50
PskType type
STL namespace.
folly::std T
—— Concurrent Priority Queue Implementation ——
Definition: AtomicBitSet.h:29
#define FOLLY_EXPORT
Definition: CPortability.h:133
requires E e noexcept(noexcept(s.error(std::move(e))))
static CoreT & getCoreImpl(CoreT *core)
Definition: Promise.h:429
Core * core_
Definition: Promise.h:438
static const char *const value
Definition: Conv.cpp:50
Definition: Try.h:51
**Optimized Holders **The template hazptr_array< M > provides most of the functionality *of M hazptr_holder s but with faster construction destruction *for M
Definition: Hazptr.h:104
std::enable_if< std::is_same< Unit, B >::value, void >::type setValue()
Definition: Promise.h:326
const char * string
Definition: Conv.cpp:212
const
Definition: upload.py:398
BrokenPromise(const char *type)
Definition: Promise.h:53
uint64_t value(const typename LockFreeRingBuffer< T, Atom >::Cursor &rbcursor)
Core & getCore()
Definition: Promise.h:421
bool retrieved_
Definition: Promise.h:412
bool valid() const noexcept
Definition: Promise.h:390