18 #ifndef INCLUDED_FUTURE 19 #define INCLUDED_FUTURE 24 #include <condition_variable> 28 #include <type_traits> 30 template<
typename ResultType>
44 using ResultHolder = std::conditional_t<std::is_void_v<T>, std::nullopt_t, std::optional<T>>;
50 template<
typename ResultType>
53 static constexpr
bool VoidResult = std::is_same_v<ResultType, void>;
80 if (IsDoneOrCanceled())
83 std::unique_lock<std::mutex> lock(m_Mutex);
84 m_ConditionVariable.wait(lock, [
this]() ->
bool {
return IsDoneOrCanceled(); });
94 bool cancelled = m_Status.compare_exchange_strong(expected,
Status::CANCELED);
100 if constexpr (!VoidResult)
102 m_ConditionVariable.notify_all();
111 template<
typename _ResultType = ResultType>
112 std::enable_if_t<!std::is_same_v<_ResultType, void>, ResultType>
GetResult()
115 ENSURE(this->has_value());
117 ResultType ret = std::move(**
this);
144 template<
typename ResultType>
150 static constexpr
bool VoidResult = std::is_same_v<ResultType, void>;
172 template<
typename SfinaeType = ResultType>
173 std::enable_if_t<!std::is_same_v<SfinaeType, void>, ResultType>
Get()
178 if constexpr (VoidResult)
235 template<
typename ResultType>
238 static constexpr
bool VoidResult = std::is_same_v<ResultType, void>;
249 if constexpr (VoidResult)
250 m_SharedState->m_Func();
252 m_SharedState->emplace(m_SharedState->m_Func());
259 std::lock_guard<std::mutex> lock(m_SharedState->m_Mutex);
263 m_SharedState->m_ConditionVariable.notify_all();
266 m_SharedState.reset();
271 m_SharedState->Cancel();
272 m_SharedState.reset();
279 template<
typename ResultType>
283 static_assert(std::is_convertible_v<std::invoke_result_t<T>, ResultType>,
"The return type of the wrapped function cannot be converted to the type of the Future.");
284 m_SharedState = std::make_shared<SharedState>(std::move(func));
288 #endif // INCLUDED_FUTURE std::mutex m_Mutex
Definition: Future.h:123
PackagedTask< ResultType > Wrap(T &&func)
Make the future wait for the result of func.
Definition: Future.h:281
Status
Definition: Future.h:35
void CancelOrWait()
Cancels the task, waiting if the task is currently started.
Definition: Future.h:216
std::shared_ptr< SharedState > m_SharedState
Definition: Future.h:226
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:173
void Cancel()
Definition: Future.h:269
Definition: ShaderDefines.cpp:30
std::function< ResultType()> m_Func
Definition: Future.h:126
void Wait()
Definition: Future.h:77
void operator()()
Definition: Future.h:243
PackagedTask(std::shared_ptr< typename Future< ResultType >::SharedState > ss)
Definition: Future.h:241
#define ENSURE(expr)
ensure the expression <expr> evaluates to non-zero.
Definition: debug.h:290
m_Func(std::move(func))
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:91
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:112
bool IsDoneOrCanceled() const
Definition: Future.h:72
Corresponds to std::future.
Definition: Future.h:145
#define T(string_literal)
Definition: secure_crt.cpp:77
std::shared_ptr< typename Future< ResultType >::SharedState > m_SharedState
Definition: Future.h:276
~SharedState()
Definition: Future.h:59
SharedState(std::function< ResultType()> &&func)
Definition: Future.h:55
bool Valid() const
Definition: Future.h:200
std::conditional_t< std::is_void_v< T >, std::nullopt_t, std::optional< T > > ResultHolder
Definition: Future.h:44
bool IsReady() const
Definition: Future.h:192
std::condition_variable m_ConditionVariable
Definition: Future.h:124
The shared state between futures and packaged state.
Definition: Future.h:51
void Wait()
Definition: Future.h:205
Corresponds somewhat to std::packaged_task.
Definition: Future.h:31