Pyrogenesis HEAD
Pyrogenesis, a RTS Engine
Future< ResultType > Class Template Reference

Corresponds to std::future. More...

#include <Future.h>

Public Member Functions

 Future ()=default
 
 Future (const Future &o)=delete
 
 Future (Future &&)=default
 
Futureoperator= (Future &&other)
 
 ~Future ()
 
template<typename Callback >
PackagedTask< Callback > Wrap (Callback &&callback)
 Make the future wait for the result of callback. More...
 
ResultType Get ()
 Move the result out of the future, and invalidate the future. More...
 
bool IsReady () const
 
bool Valid () const
 
void Wait ()
 
void CancelOrWait ()
 Cancels the task, waiting if the task is currently started. More...
 

Protected Attributes

std::shared_ptr< FutureSharedStateDetail::Receiver< ResultType > > m_Receiver
 

Private Types

using Status = FutureSharedStateDetail::Status
 

Static Private Attributes

static constexpr bool VoidResult = std::is_same_v<ResultType, void>
 

Friends

template<typename T >
class PackagedTask
 

Detailed Description

template<typename ResultType>
class Future< ResultType >

Corresponds to std::future.

Lightweight header / forward declarations for Future.

Unlike std::future, Future can request the cancellation of the task that would produce the result. This makes it more similar to Java's CancellableTask or C#'s Task. The name Future was kept over Task so it would be more familiar to C++ users, but this all should be revised once Concurrency TS wraps up.

Future is not thread-safe. Call it from a single thread or ensure synchronization externally.

The callback never runs after the Future is destroyed. TODO:

  • Handle exceptions.

Include this in your header files where possible.

Member Typedef Documentation

◆ Status

template<typename ResultType >
using Future< ResultType >::Status = FutureSharedStateDetail::Status
private

Constructor & Destructor Documentation

◆ Future() [1/3]

template<typename ResultType >
Future< ResultType >::Future ( )
default

◆ Future() [2/3]

template<typename ResultType >
Future< ResultType >::Future ( const Future< ResultType > &  o)
delete

◆ Future() [3/3]

template<typename ResultType >
Future< ResultType >::Future ( Future< ResultType > &&  )
default

◆ ~Future()

template<typename ResultType >
Future< ResultType >::~Future ( )
inline

Member Function Documentation

◆ CancelOrWait()

template<typename ResultType >
void Future< ResultType >::CancelOrWait ( )
inline

Cancels the task, waiting if the task is currently started.

Use this function over Cancel() if you need to ensure determinism (i.e. in the simulation).

See also
Cancel.

◆ Get()

template<typename ResultType >
ResultType Future< ResultType >::Get ( )
inline

Move the result out of the future, and invalidate the future.

If the future is not complete, calls Wait(). If the future is canceled, asserts.

◆ IsReady()

template<typename ResultType >
bool Future< ResultType >::IsReady ( ) const
inline
Returns
true if the shared state is valid and has a result (i.e. Get can be called).

◆ operator=()

template<typename ResultType >
Future & Future< ResultType >::operator= ( Future< ResultType > &&  other)
inline

◆ Valid()

template<typename ResultType >
bool Future< ResultType >::Valid ( ) const
inline
Returns
true if the future has a shared state and it's not been invalidated, ie. pending, started or done.

◆ Wait()

template<typename ResultType >
void Future< ResultType >::Wait ( )
inline

◆ Wrap()

template<typename ResultType >
template<typename Callback >
PackagedTask< Callback > Future< ResultType >::Wrap ( Callback &&  callback)

Make the future wait for the result of callback.

Friends And Related Function Documentation

◆ PackagedTask

template<typename ResultType >
template<typename T >
friend class PackagedTask
friend

Member Data Documentation

◆ m_Receiver

template<typename ResultType >
std::shared_ptr<FutureSharedStateDetail::Receiver<ResultType> > Future< ResultType >::m_Receiver
protected

◆ VoidResult

template<typename ResultType >
constexpr bool Future< ResultType >::VoidResult = std::is_same_v<ResultType, void>
staticconstexprprivate

The documentation for this class was generated from the following file: