18#ifndef INCLUDED_FUTURE
19#define INCLUDED_FUTURE
24#include <condition_variable>
31template<
typename Callback>
45using ResultHolder = std::conditional_t<std::is_void_v<T>, std::nullopt_t, std::optional<T>>;
50template<
typename ResultType>
53 static constexpr bool VoidResult = std::is_same_v<ResultType, void>;
75 std::unique_lock<std::mutex> lock(
m_Mutex);
106 if constexpr (!std::is_void_v<ResultType>)
108 std::get<std::exception_ptr>(
m_Outcome));
112 if (std::get<std::exception_ptr>(
m_Outcome))
113 std::rethrow_exception(std::get<std::exception_ptr>(
m_Outcome));
115 if constexpr (std::is_void_v<ResultType>)
120 std::get<ResultHolder<ResultType>>(
m_Outcome).reset();
130 std::tuple<ResultHolder<ResultType>, std::exception_ptr>
m_Outcome{std::nullopt,
131 std::exception_ptr{}};
137template<
typename Callback>
163template<
typename ResultType>
169 static constexpr bool VoidResult = std::is_same_v<ResultType, void>;
190 template<
typename Callback>
245 std::shared_ptr<FutureSharedStateDetail::Receiver<ResultType>>
m_Receiver;
254template<
typename Callback>
266 if (!
m_SharedState->receiver.m_Status.compare_exchange_strong(expected,
274 using ResultType = std::invoke_result_t<Callback>;
275 if constexpr (std::is_void_v<ResultType>)
278 std::get<FutureSharedStateDetail::ResultHolder<ResultType>>(
283 std::get<std::exception_ptr>(
m_SharedState->receiver.m_Outcome) =
284 std::current_exception();
292 std::lock_guard<std::mutex> lock(
m_SharedState->receiver.m_Mutex);
309 std::shared_ptr<FutureSharedStateDetail::SharedState<Callback>>
m_SharedState;
312template<
typename ResultType>
313template<
typename Callback>
316 static_assert(std::is_same_v<std::invoke_result_t<Callback>, ResultType>,
317 "The return type of the wrapped function is not the same as the type the Future expects.");
319 auto temp = std::make_shared<FutureSharedStateDetail::SharedState<Callback>>(std::move(callback));
320 m_Receiver = {temp, &temp->receiver};
Responsible for syncronization between the task and the receiving thread.
Definition: Future.h:52
std::condition_variable m_ConditionVariable
Definition: Future.h:127
static constexpr bool VoidResult
Definition: Future.h:53
Receiver(Receiver &&)=delete
~Receiver()
Definition: Future.h:56
bool Cancel()
If the task is pending, cancel it: the status becomes CANCELED and if the task was completed,...
Definition: Future.h:83
std::mutex m_Mutex
Definition: Future.h:126
Receiver(const Receiver &)=delete
std::atomic< Status > m_Status
Definition: Future.h:125
ResultType GetResult()
Move the result away from the shared state, mark the future invalid.
Definition: Future.h:103
bool IsDoneOrCanceled() const
Definition: Future.h:64
void Wait()
Definition: Future.h:69
std::tuple< ResultHolder< ResultType >, std::exception_ptr > m_Outcome
Definition: Future.h:130
Corresponds to std::future.
Definition: Future.h:165
static constexpr bool VoidResult
Definition: Future.h:169
bool Valid() const
Definition: Future.h:219
ResultType Get()
Move the result out of the future, and invalidate the future.
Definition: Future.h:198
~Future()
Definition: Future.h:182
PackagedTask< Callback > Wrap(Callback &&callback)
Make the future wait for the result of callback.
Definition: Future.h:314
Future(Future &&)=default
Future & operator=(Future &&other)
Definition: Future.h:176
void Wait()
Definition: Future.h:224
bool IsReady() const
Definition: Future.h:211
void CancelOrWait()
Cancels the task, waiting if the task is currently started.
Definition: Future.h:235
std::shared_ptr< FutureSharedStateDetail::Receiver< ResultType > > m_Receiver
Definition: Future.h:245
Future(const Future &o)=delete
Corresponds somewhat to std::packaged_task.
Definition: Future.h:256
std::shared_ptr< FutureSharedStateDetail::SharedState< Callback > > m_SharedState
Definition: Future.h:309
void operator()()
Definition: Future.h:263
PackagedTask(std::shared_ptr< FutureSharedStateDetail::SharedState< Callback > > ss)
Definition: Future.h:259
void Cancel()
Definition: Future.h:302
#define ENSURE(expr)
ensure the expression <expr> evaluates to non-zero.
Definition: debug.h:277
std::conditional_t< std::is_void_v< T >, std::nullopt_t, std::optional< T > > ResultHolder
Definition: Future.h:45
Status
Definition: Future.h:37
Definition: ShaderDefines.cpp:31
The shared state between futures and packaged state.
Definition: Future.h:139
Receiver< std::invoke_result_t< Callback > > receiver
Definition: Future.h:145
Callback callback
Definition: Future.h:144
SharedState(Callback &&callbackFunc)
Definition: Future.h:140