simple_future


A function can mark itself as a coroutine by having a return type of simple_future<T, Promise>. The purpose of a simple_future is to provide a method for asynchronous functions to have a return type. Since the simple_future<T> may exhibit asynchronous behaviour the return type must be accessed through co_await. A simple_future<T> will wake up the waiting coroutine and return a value to it be using the co_return keyword.

The promise type for an simple_future<T> must satisfy the Returnable concept. The default promise type is the simple_promise.


Example
template <typename T = void>
simple_future<T>
future_value() noexcept
{
    /* async behaviour goes here... */
    co_return T{};
}

/*
    template <typename T>
    using guaranteed_future = simple_future<promise_always_resolves<T>,  simple_promise<promise_always_resolves<T>>>;
*/
template <typename T = void>
guaranteed_future<T>
guaranteed_future_value() noexcept
{
    /* async behaviour goes here... */
    co_return T{};
}

async_function<>
foo() noexcept
{
    /* no return type (just awaitable)- void */
    co_await future_value<>();

    /* Simple return type - bool */
    bool predicate = co_await future_value<bool>();

    /* All others return an std::optional<T> - std::size_t */
    std::optional<std::size_t> value = co_await future_value<std::size_t>();

    /* Turn off optional by guaranteeing it will resolve - std::size_t */
    std::size_t value = co_await guaranteed_future_value<std::size_t>();
}

The return type is taken from Promise::ReturnType.

Note: A simple_future may return to you in a different thread than was entered (if the application explitelty does the swap in the body). proxy can be used for simple thread coordination.


template<typename T = void, details::Returnable Promise = simple_promise<T>>
class zab::simple_future

Represents the future value of a simple promise.

Template Parameters

T – The type of the promised value.

Public Types

using promise_type = Promise
using return_value = typename deduce_type<typename promise_type::returns>::type

Public Functions

inline simple_future(std::coroutine_handle<promise_type> _handle)

Construct with the future with a handle to its coroutine.

Parameters

_coroutine[in] The coroutine handle.

inline ~simple_future()

Destroys the future and cleans up the coroutine handle.

We destroy the coroutine handle here as the the final_suspend in the simple_promise does not resume.

simple_future(const simple_future &_other) = delete

Cannot be coppied.

Parameters

_other[in] The simple_future to copy.

inline simple_future(simple_future &&_other)

Moving makes the moved simple_future lose ownership of the handle.

Parameters

_other – The simple_future to move.

inline simple_future &operator=(simple_future &&_other)

Move Assignment operator.

Parameters

_other – The simple_future to move.

Returns

*this.

inline details::simple_awaitable<promise_type> operator co_await() noexcept

wait for the simple_promise to be fulfilled or fail.

Then co_await returns an std::optional<T>&& which represents if the simple_promise was fulfilled or failed.

Returns

A co_await’able struct.

inline void inline_co_await(tagged_event _event) noexcept
inline decltype(auto) get_inline_result() noexcept

Public Members

std::coroutine_handle<promise_type> handle_