Go to the documentation of this file.
9 #ifndef EAGINE_MESSAGE_BUS_FUTURE_HPP
10 #define EAGINE_MESSAGE_BUS_FUTURE_HPP
12 #include "../flat_map.hpp"
13 #include "../nothing.hpp"
14 #include "../timeout.hpp"
22 timeout too_late{std::chrono::seconds{1}};
23 std::function<void(T)> success_handler{};
24 std::function<void()> timeout_handler{};
37 promise(std::shared_ptr<future_state<T>>& state) noexcept
40 auto should_be_removed() ->
bool {
41 if(
auto state{_state.lock()}) {
43 if(state->timeout_handler) {
44 state->timeout_handler();
56 if(
auto state{_state.lock()}) {
59 if(state->timeout_handler) {
60 state->timeout_handler();
63 if(state->success_handler) {
64 state->success_handler(std::move(value));
71 std::weak_ptr<future_state<T>> _state{};
89 explicit operator bool() const noexcept {
94 template <
typename R,
typename P>
97 _state->too_late.reset(dur);
107 _state->success_handler = handler;
116 _state->timeout_handler = handler;
126 typename = std::enable_if_t<std::is_invocable_v<Handler, T>>>
129 _state->success_handler = std::function<void(T)>(
130 [state{_state}, handler{std::move(handler)}](T value) {
142 typename = std::enable_if_t<std::is_invocable_v<Handler>>>
145 _state->timeout_handler = std::function<void()>(
146 [state{_state}, handler{std::move(handler)}]() { handler(); });
157 std::shared_ptr<future_state<T>> _state{
158 std::make_shared<future_state<T>>()};
165 template <
typename T>
171 auto make() -> std::tuple<message_sequence_t, future<T>> {
173 const auto id{++_id_seq};
174 _promises[id] = result.get_promise();
180 auto pos = _promises.find(
id);
181 if(pos != _promises.end()) {
182 pos->second.fulfill(std::move(value));
183 _promises.erase(pos);
190 return _promises.erase_if(
191 [](
auto& p) {
return p.second.should_be_removed(); }) > 0;
197 return !_promises.empty();
203 return _promises.empty();
208 flat_map<message_sequence_t, promise<T>> _promises{};
213 #endif // EAGINE_MESSAGE_BUS_FUTURE_HPP
Message bus promise class.
Definition: future.hpp:32
auto set_timeout(std::chrono::duration< R, P > dur) -> future< T > &
Sets the timeout for this future if there is shared state.
Definition: future.hpp:95
Class representing "none" / "nothing" values.
Definition: nothing.hpp:17
auto then(Handler handler) -> future< T > &
Wraps the given handler object and sets it as the on-success handler.
Definition: future.hpp:127
auto make() -> std::tuple< message_sequence_t, future< T >>
Constructs and returns a new message bus future and its unique id.
Definition: future.hpp:171
void fulfill(message_sequence_t id, T value)
Fulfills the promise/future pair idenified by id with the given value.
Definition: future.hpp:179
auto on_timeout(const std::function< void()> &handler) -> future< T > &
Sets the on-timeout handler.
Definition: future.hpp:114
auto has_none() const noexcept -> bool
Indicates if there are no pending promises.
Definition: future.hpp:202
auto otherwise(Handler handler) -> future< T > &
Wraps the given handler object and sets it as the on-timeout handler.
Definition: future.hpp:143
std::uint32_t message_sequence_t
Alias for message sequence number type.
Definition: types.hpp:22
auto update() -> bool
Update the internal state of this promise/future tracker.
Definition: future.hpp:189
void fulfill(T value)
Fulfills the promise and the corresponding future.
Definition: future.hpp:55
Message bus code is placed in this namespace.
Definition: eagine.hpp:58
promise() noexcept=default
Default constructor.
auto on_success(const std::function< void(T)> &handler) -> future< T > &
Sets the on-success handler.
Definition: future.hpp:105
auto has_some() const noexcept -> bool
Indicates if there are any unfulfilled pending promises.
Definition: future.hpp:196
future()=default
Default constructor.
Message bus future class.
Definition: future.hpp:79
Class that makes new and tracks existing pending message bus promises.
Definition: future.hpp:166
auto get_promise() -> promise< T >
Returns the associated promise it there is shared state.
Definition: future.hpp:152
future(nothing_t) noexcept
Constructs empty stateless future.
Definition: future.hpp:85