CatapultServer  v0.5.0.1 (Elephant)
Future.h
Go to the documentation of this file.
1 
21 #pragma once
24 
25 namespace catapult { namespace thread {
26 
28  template<typename T>
29  class future : public utils::MoveOnly {
30  public:
32  future() = default;
33 
35  explicit future(const std::shared_ptr<detail::shared_state<T>>& pState) : m_pState(pState)
36  {}
37 
38  public:
40  bool valid() {
41  return !!m_pState;
42  }
43 
45  bool is_ready() const {
46  return m_pState->is_ready();
47  }
48 
50  T get() {
51  return m_pState->get();
52  }
53 
55  template<
56  typename TContinuation,
57  typename TResultType = std::invoke_result_t<TContinuation, future<T>&&>
58  >
59  auto then(TContinuation continuation) {
60  if constexpr (!std::is_same_v<TResultType, void>) {
61  auto pResultState = std::make_shared<detail::shared_state<TResultType>>();
62  m_pState->set_continuation([pResultState, continuation](const auto& pState) {
63  try {
64  pResultState->set_value(continuation(future<T>(pState)));
65  } catch (...) {
66  pResultState->set_exception(std::current_exception());
67  }
68  });
69 
70  return future<TResultType>(pResultState);
71  } else {
72  return then([continuation](auto&& future) {
73  continuation(std::move(future));
74  return true;
75  });
76  }
77  }
78 
79  private:
80  std::shared_ptr<detail::shared_state<T>> m_pState;
81  };
82 
84  template<typename T>
85  class promise : public utils::MoveOnly {
86  public:
89  : m_pState(std::make_shared<detail::shared_state<T>>())
90  , m_pIsFutureCreated(std::make_unique<std::atomic_flag>()) {
91  m_pIsFutureCreated->clear(std::memory_order_release);
92  }
93 
94  public:
96  bool valid() {
97  return !!m_pState;
98  }
99 
102  if (m_pIsFutureCreated->test_and_set(std::memory_order_acquire))
103  throw std::future_error(std::future_errc::future_already_retrieved);
104 
105  return future<T>(m_pState);
106  }
107 
108  public:
110  void set_value(T&& value) {
111  m_pState->set_value(std::move(value));
112  }
113 
115  void set_exception(std::exception_ptr pException) {
116  m_pState->set_exception(pException);
117  }
118 
119  private:
120  std::shared_ptr<detail::shared_state<T>> m_pState;
121  std::unique_ptr<std::atomic_flag> m_pIsFutureCreated;
122  };
123 
125  template<typename T>
127  auto pState = std::make_shared<detail::shared_state<T>>();
128  pState->set_value(std::move(value));
129  return future<T>(pState);
130  }
131 
133  template<typename T, typename E>
135  auto pState = std::make_shared<detail::shared_state<T>>();
136  pState->set_exception(std::make_exception_ptr(ex));
137  return future<T>(pState);
138  }
139 }}
catapult::thread::detail::shared_state
Shared state that is shared between a promise and a future.
Definition: FutureSharedState.h:32
catapult::thread::future::m_pState
std::shared_ptr< detail::shared_state< T > > m_pState
Definition: Future.h:80
catapult::thread::future::get
T get()
Returns the result of this future and blocks until the result is available.
Definition: Future.h:50
catapult::thread::promise::m_pState
std::shared_ptr< detail::shared_state< T > > m_pState
Definition: Future.h:120
catapult::thread::future::future
future(const std::shared_ptr< detail::shared_state< T >> &pState)
Constructs a future around a shared state (pState).
Definition: Future.h:35
catapult::thread::future::then
auto then(TContinuation continuation)
Configures continuation to run at the completion of this future.
Definition: Future.h:59
catapult::thread::promise::valid
bool valid()
Returns true if this promise is valid.
Definition: Future.h:96
catapult::thread::make_exceptional_future
future< T > make_exceptional_future(E ex)
Produces a future that is ready immediately and holds the given exception (ex).
Definition: Future.h:134
catapult::thread::future::future
future()=default
Creates a default future.
catapult::thread::promise::set_exception
void set_exception(std::exception_ptr pException)
Sets the result of this promise to pException.
Definition: Future.h:115
catapult::thread::future
Provides a way to access the result of an asynchronous operation.
Definition: Future.h:29
catapult::thread::promise::get_future
future< T > get_future()
Returns a future associated with this promise.
Definition: Future.h:101
catapult::utils::MoveOnly
A class that can be moved but not copied.
Definition: NonCopyable.h:43
catapult::thread::make_ready_future
future< T > make_ready_future(T &&value)
Produces a future that is ready immediately and holds the given value.
Definition: Future.h:126
catapult::thread::promise::promise
promise()
Constructs a promise.
Definition: Future.h:88
FutureSharedState.h
catapult::thread::promise::set_value
void set_value(T &&value)
Sets the result of this promise to value.
Definition: Future.h:110
catapult
Definition: AddressExtractionExtension.cpp:28
catapult::thread::future::valid
bool valid()
Returns true if this future is valid.
Definition: Future.h:40
catapult::thread::promise
Stores the result of an asynchronous operation.
Definition: Future.h:85
NonCopyable.h
catapult::thread::promise::m_pIsFutureCreated
std::unique_ptr< std::atomic_flag > m_pIsFutureCreated
Definition: Future.h:121
catapult::thread::future::is_ready
bool is_ready() const
Returns true if this future has completed and get will not block.
Definition: Future.h:45