18#ifndef INCLUDED_FUTURE
19#define INCLUDED_FUTURE
24#include <condition_variable>
30template<
typename Callback>
44using ResultHolder = std::conditional_t<std::is_void_v<T>, std::nullopt_t, std::optional<T>>;
49template<
typename ResultType>
52 static constexpr bool VoidResult = std::is_same_v<ResultType, void>;
76 std::unique_lock<std::mutex> lock(
m_Mutex);
104 template<
typename _ResultType = ResultType>
105 std::enable_if_t<!std::is_same_v<_ResultType, void>, ResultType>
GetResult()
108 ENSURE(this->has_value());
110 ResultType ret = std::move(**
this);
123template<
typename Callback>
149template<
typename ResultType>
155 static constexpr bool VoidResult = std::is_same_v<ResultType, void>;
176 template<
typename Callback>
184 template<
typename SfinaeType = ResultType>
185 std::enable_if_t<!std::is_same_v<SfinaeType, void>, ResultType>
Get()
238 std::shared_ptr<FutureSharedStateDetail::Receiver<ResultType>>
m_Receiver;
247template<
typename Callback>
259 if (!
m_SharedState->receiver.m_Status.compare_exchange_strong(expected,
265 if constexpr (std::is_void_v<std::invoke_result_t<Callback>>)
275 std::lock_guard<std::mutex> lock(
m_SharedState->receiver.m_Mutex);
292 std::shared_ptr<FutureSharedStateDetail::SharedState<Callback>>
m_SharedState;
295template<
typename ResultType>
296template<
typename Callback>
299 static_assert(std::is_same_v<std::invoke_result_t<Callback>, ResultType>,
300 "The return type of the wrapped function is not the same as the type the Future expects.");
302 auto temp = std::make_shared<FutureSharedStateDetail::SharedState<Callback>>(std::move(callback));
303 m_Receiver = {temp, &temp->receiver};
Responsible for syncronization between the task and the receiving thread.
Definition: Future.h:51
std::condition_variable m_ConditionVariable
Definition: Future.h:117
Receiver()
Definition: Future.h:54
static constexpr bool VoidResult
Definition: Future.h:52
Receiver(Receiver &&)=delete
~Receiver()
Definition: Future.h:57
bool Cancel()
If the task is pending, cancel it: the status becomes CANCELED and if the task was completed,...
Definition: Future.h:84
std::mutex m_Mutex
Definition: Future.h:116
std::enable_if_t<!std::is_same_v< _ResultType, void >, ResultType > GetResult()
Move the result away from the shared state, mark the future invalid.
Definition: Future.h:105
Receiver(const Receiver &)=delete
std::atomic< Status > m_Status
Definition: Future.h:115
bool IsDoneOrCanceled() const
Definition: Future.h:65
void Wait()
Definition: Future.h:70
Corresponds to std::future.
Definition: Future.h:151
static constexpr bool VoidResult
Definition: Future.h:155
bool Valid() const
Definition: Future.h:212
~Future()
Definition: Future.h:168
PackagedTask< Callback > Wrap(Callback &&callback)
Make the future wait for the result of callback.
Definition: Future.h:297
Future(Future &&)=default
Future & operator=(Future &&other)
Definition: Future.h:162
void Wait()
Definition: Future.h:217
std::enable_if_t<!std::is_same_v< SfinaeType, void >, ResultType > Get()
Move the result out of the future, and invalidate the future.
Definition: Future.h:185
bool IsReady() const
Definition: Future.h:204
void CancelOrWait()
Cancels the task, waiting if the task is currently started.
Definition: Future.h:228
std::shared_ptr< FutureSharedStateDetail::Receiver< ResultType > > m_Receiver
Definition: Future.h:238
Future(const Future &o)=delete
Corresponds somewhat to std::packaged_task.
Definition: Future.h:249
std::shared_ptr< FutureSharedStateDetail::SharedState< Callback > > m_SharedState
Definition: Future.h:292
void operator()()
Definition: Future.h:256
PackagedTask(std::shared_ptr< FutureSharedStateDetail::SharedState< Callback > > ss)
Definition: Future.h:252
void Cancel()
Definition: Future.h:285
#define ENSURE(expr)
ensure the expression <expr> evaluates to non-zero.
Definition: debug.h:290
std::conditional_t< std::is_void_v< T >, std::nullopt_t, std::optional< T > > ResultHolder
Definition: Future.h:44
Status
Definition: Future.h:36
Definition: ShaderDefines.cpp:31
The shared state between futures and packaged state.
Definition: Future.h:125
Receiver< std::invoke_result_t< Callback > > receiver
Definition: Future.h:131
Callback callback
Definition: Future.h:130
SharedState(Callback &&callbackFunc)
Definition: Future.h:126