Skip to main content
Pigweed's logo Pigweed
code_xml
  1. Home
  2. Reference
  3. C/C++
  4. pw_async2
  5. Tasks
  6. pw::async2::FutureTask< T, policy > Class Template Reference
Loading...
Searching...
No Matches
pw::async2::FutureTask< T, policy > Class Template Referencefinal

Overview

template<typename T, ReturnValuePolicy policy = std::is_void_v<typename std::remove_reference_t<T>::value_type> ? ReturnValuePolicy::kDiscard : ReturnValuePolicy::kKeep>
class pw::async2::FutureTask< T, policy >

Creates a task that pends a future until it completes.

FutureTask can be initialized in a few ways:

  • Default construct: FutureTask<MyFuture> task
  • Move a future: FutureTask task(std::move(future))
  • Construct a future in place: FutureTask<MyFuture> task(arg1, arg2)
  • Refer to an existing future: FutureTask<MyFuture&> task(future)

A FutureTask that owns its future can be assigned a new future when the task is not registered with a Dispatcher:

  • Assign a future: task = std::move(future)
  • Construct a future in place: task.emplace_future(arg1, arg2)
  • Reset to an empty future: task.reset()

The future's return value is stored and can be accessed through value() after joining, or by calling Wait(). For futures returning void, the return value is discarded by default (ReturnValuePolicy::kDiscard). To discard the return value futures returning non-void, specify ReturnValuePolicy::kDiscard.

Warning
FutureTask is intended for test and occasional production use. A FutureTask does not contain logic, and relying too much on FutureTasks could push logic out of async tasks, which nullifies the benefits of pw_async2. Creating a task for each future is also less efficient than having one task work with multiple futures.
Inheritance diagram for pw::async2::FutureTask< T, policy >:
pw::async2::internal::FutureTaskBase pw::async2::Task pw::IntrusiveQueue< T >::Item pw::async2::Context

Public Types

using future_type = std::remove_reference_t< T >
 The type of the future that is pended by this task.
 
using value_type = typename future_type::value_type
 The type produced by this tasks's future when it completes.
 

Public Member Functions

template<typename U = T, typename = std::enable_if_t<!std::is_reference_v<U>>>
constexpr FutureTask ()
 
template<typename Arg = T, typename... Args, typename = std::enable_if_t< !std::is_same_v<std::remove_cv_t<std::remove_reference_t<Arg>>, FutureTask> && std::is_constructible_v<T, Arg, Args...>>>
constexpr FutureTask (Arg &&arg, Args &&... args)
 
 FutureTask (const FutureTask &)=delete
 
FutureTask & operator= (const FutureTask &)=delete
 
 FutureTask (FutureTask &&)=delete
 
FutureTask & operator= (FutureTask &&)=delete
 
template<typename U = T, typename = std::enable_if_t< !std::is_same_v<std::remove_cv_t<std::remove_reference_t<U>>, FutureTask> && !std::is_reference_v<T> && std::is_assignable_v<T&, U>>>
FutureTask & operator= (U &&future)
 
template<typename... Args, typename = std::enable_if_t<!std::is_reference_v<T> && std::is_constructible_v<T, Args...>>>
void emplace_future (Args &&... args)
 
template<typename U = T, typename = std::enable_if_t<!std::is_reference_v<U>>>
void reset ()
 
constexpr bool is_pendable () const
 
constexpr bool is_complete () const
 Returns whether the inner future has completed.
 
constexpr bool has_value () const
 
Poll< value_type > TakePoll ()
 
FutureValue< future_type > & value () &
 
const FutureValue< future_type > & value () const &
 
FutureValue< future_type > && value () &&
 
const FutureValue< future_type > && value () const &&
 
FutureValue< future_type > & Wait ()
 
- Public Member Functions inherited from pw::async2::Task
constexpr Task (log::Token name=kDefaultName)
 
 Task (const Task &)=delete
 
 Task (Task &&)=delete
 
Task & operator= (const Task &)=delete
 
Task & operator= (Task &&)=delete
 
virtual ~Task ()
 
Poll Pend (Context &cx)
 
bool IsRegistered () const
 
void Deregister ()
 
void BlockingJoin ()
 

Private Member Functions

Poll DoPend (Context &cx) override
 

Additional Inherited Members

- Protected Types inherited from pw::async2::Task
using Context = ::pw::async2::Context
 

Constructor & Destructor Documentation

◆ FutureTask() [1/2]

template<typename T , ReturnValuePolicy policy = std::is_void_v<typename std::remove_reference_t<T>::value_type> ? ReturnValuePolicy::kDiscard : ReturnValuePolicy::kKeep>
template<typename U = T, typename = std::enable_if_t<!std::is_reference_v<U>>>
constexpr pw::async2::FutureTask< T, policy >::FutureTask ( )
inlineconstexpr

Creates a FutureTask with a default-constructed future.

A default-constructed future is empty and cannot be pended (is_pendable() is false). A valid future must be assigned before this task is run by a Dispatcher, or Pend will crash.

◆ FutureTask() [2/2]

template<typename T , ReturnValuePolicy policy = std::is_void_v<typename std::remove_reference_t<T>::value_type> ? ReturnValuePolicy::kDiscard : ReturnValuePolicy::kKeep>
template<typename Arg = T, typename... Args, typename = std::enable_if_t< !std::is_same_v<std::remove_cv_t<std::remove_reference_t<Arg>>, FutureTask> && std::is_constructible_v<T, Arg, Args...>>>
constexpr pw::async2::FutureTask< T, policy >::FutureTask ( Arg &&  arg,
Args &&...  args 
)
inlineexplicitconstexpr

Constructs a FutureTask. Forwards arguments to the future's constructor for FutureTasks that own their future. Reference FutureTasks take a mutable reference to their future.

Member Function Documentation

◆ DoPend()

template<typename T , ReturnValuePolicy policy = std::is_void_v<typename std::remove_reference_t<T>::value_type> ? ReturnValuePolicy::kDiscard : ReturnValuePolicy::kKeep>
Poll pw::async2::FutureTask< T, policy >::DoPend ( Context &  )
inlineoverrideprivatevirtual

Attempts to advance this Task to completion.

This method should not perform synchronous waiting, as doing so may block the main Dispatcher loop and prevent other Task s from progressing. Because of this, Task s should not invoke blocking Dispatcher methods such as RunUntilComplete.

Tasks should also avoid invoking RunUntilStalled on their own Dispatcher.

Returns Ready if complete, or Pending if the Task was not yet able to complete.

If Pending is returned, the Task must ensure it is woken up when it is able to make progress. To do this, Task::Pend must arrange for Waker::Wake to be called, either by storing a copy of the Waker away to be awoken by another system (such as an interrupt handler).

Implements pw::async2::Task.

◆ emplace_future()

template<typename T , ReturnValuePolicy policy = std::is_void_v<typename std::remove_reference_t<T>::value_type> ? ReturnValuePolicy::kDiscard : ReturnValuePolicy::kKeep>
template<typename... Args, typename = std::enable_if_t<!std::is_reference_v<T> && std::is_constructible_v<T, Args...>>>
void pw::async2::FutureTask< T, policy >::emplace_future ( Args &&...  args)
inline

Constructs a new future in place to be pended by this task, resetting any previous Poll result to Pending.

Precondition
The task MUST NOT be registered with a Dispatcher (!IsRegistered()).

◆ has_value()

template<typename T , ReturnValuePolicy policy = std::is_void_v<typename std::remove_reference_t<T>::value_type> ? ReturnValuePolicy::kDiscard : ReturnValuePolicy::kKeep>
constexpr bool pw::async2::FutureTask< T, policy >::has_value ( ) const
inlineconstexpr

Returns whether the task ran and set value() to the future's return value.

◆ is_pendable()

template<typename T , ReturnValuePolicy policy = std::is_void_v<typename std::remove_reference_t<T>::value_type> ? ReturnValuePolicy::kDiscard : ReturnValuePolicy::kKeep>
constexpr bool pw::async2::FutureTask< T, policy >::is_pendable ( ) const
inlineconstexpr

Returns whether the inner future is pendable.

A default-constructed future or a completed future is not pendable.

◆ operator=()

template<typename T , ReturnValuePolicy policy = std::is_void_v<typename std::remove_reference_t<T>::value_type> ? ReturnValuePolicy::kDiscard : ReturnValuePolicy::kKeep>
template<typename U = T, typename = std::enable_if_t< !std::is_same_v<std::remove_cv_t<std::remove_reference_t<U>>, FutureTask> && !std::is_reference_v<T> && std::is_assignable_v<T&, U>>>
FutureTask & pw::async2::FutureTask< T, policy >::operator= ( U &&  future)
inline

Assigns a new future to be pended by this task, resetting any previous Poll result to Pending.

Precondition
The task MUST NOT be registered with a Dispatcher (!IsRegistered()).

◆ reset()

template<typename T , ReturnValuePolicy policy = std::is_void_v<typename std::remove_reference_t<T>::value_type> ? ReturnValuePolicy::kDiscard : ReturnValuePolicy::kKeep>
template<typename U = T, typename = std::enable_if_t<!std::is_reference_v<U>>>
void pw::async2::FutureTask< T, policy >::reset ( )
inline

Resets the inner future to a default-constructed state and clears any previous Poll result.

Precondition
The task MUST NOT be registered with a Dispatcher (!IsRegistered()).

◆ TakePoll()

template<typename T , ReturnValuePolicy policy = std::is_void_v<typename std::remove_reference_t<T>::value_type> ? ReturnValuePolicy::kDiscard : ReturnValuePolicy::kKeep>
Poll< value_type > pw::async2::FutureTask< T, policy >::TakePoll ( )
inline

Takes the Poll result from the most recent task run. This function is NOT thread safe. It cannot be called when the task may run on another thread.

◆ value() [1/4]

template<typename T , ReturnValuePolicy policy = std::is_void_v<typename std::remove_reference_t<T>::value_type> ? ReturnValuePolicy::kDiscard : ReturnValuePolicy::kKeep>
FutureValue< future_type > & pw::async2::FutureTask< T, policy >::value ( ) &
inline

Returns the value produced by the future.

Precondition
The task MUST have been joined first.

◆ value() [2/4]

template<typename T , ReturnValuePolicy policy = std::is_void_v<typename std::remove_reference_t<T>::value_type> ? ReturnValuePolicy::kDiscard : ReturnValuePolicy::kKeep>
FutureValue< future_type > && pw::async2::FutureTask< T, policy >::value ( ) &&
inline

Returns the value produced by the future.

Precondition
The task MUST have been joined first.

◆ value() [3/4]

template<typename T , ReturnValuePolicy policy = std::is_void_v<typename std::remove_reference_t<T>::value_type> ? ReturnValuePolicy::kDiscard : ReturnValuePolicy::kKeep>
const FutureValue< future_type > & pw::async2::FutureTask< T, policy >::value ( ) const &
inline

Returns the value produced by the future.

Precondition
The task MUST have been joined first.

◆ value() [4/4]

template<typename T , ReturnValuePolicy policy = std::is_void_v<typename std::remove_reference_t<T>::value_type> ? ReturnValuePolicy::kDiscard : ReturnValuePolicy::kKeep>
const FutureValue< future_type > && pw::async2::FutureTask< T, policy >::value ( ) const &&
inline

Returns the value produced by the future.

Precondition
The task MUST have been joined first.

◆ Wait()

template<typename T , ReturnValuePolicy policy = std::is_void_v<typename std::remove_reference_t<T>::value_type> ? ReturnValuePolicy::kDiscard : ReturnValuePolicy::kKeep>
FutureValue< future_type > & pw::async2::FutureTask< T, policy >::Wait ( )
inline

Joins that task, blocking until the future completes, then returns a reference its value.


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