defer_block_promise


A defer_block_promise is an extension of execution_promise that allows deferred execution of some function/s. defer_block_promise satisfies the Execution concept.

The coroutine key words available are co_return (void), co_yield (defer_block), co_yeild (purge_block). The coroutine can also suspend using co_await (AwaitableProxy).

The defer_block_promise allows the user to save a/many callable/s of signature void(void) which will executed during the final suspend phase of the coroutine (after co_return is called). If multiple callable’s are saved, they will be executed in reverse order of deferment.

A code block can be deferred by using the co_yield (defer_block) operator. All deferred code blocks can be removed through the co_yeild (purge_block) operator. These operators do not actually suspend the coroutine.

Access to local variables in the functions scope is more or less undefined behavior. For example, capturing from the function scope by reference. The deconstuctors will have been called but the memory would have not been de-allocated. Accessing local variables with trivial / no de-constructors (ints) is not a memory violation. Experimentation indicates correct behavior although optimizations present or future could easier mess it up. Accessing local variables with non-trivial de-constructors is undefined behavior.


Example
async_function<defer_block_promise>
defer_example() noexcept
{
    co_yield defer_block(
        []
        () noexcept
            {
                std::cout << "This will not run :(\n";
            }
        );

    /* Remove all defer blocks... */
    co_yield purge_block{};

    co_yield defer_block(
        []
        () noexcept
            {
                std::cout << "This will run 2nd :)\n";
            }
        );

    co_yield defer_block(
        []
        () noexcept
            {
                std::cout << "This will run  1st :)\n";
            }
        );

    co_return;
    /* The defer blocks will run after this return... */
}

class zab::defer_block_promise

Public Functions

inline ~defer_block_promise()
inline auto get_return_object() noexcept

Gets the coroutine handle from this.

Returns

The coroutine handle.

inline auto initial_suspend() noexcept

All execution_promise’s’ begin execution on function call.

Returns

A std::suspend_never.

inline auto final_suspend() noexcept

When execution completes, the coroutine will clean itself up.

Returns

A std::suspend_never.

inline void return_void() noexcept

Returning is a no-op.

template<typename T> inline  requires (std::is_nothrow_invocable_v< T >) auto yield_value(defer_block< T > &&_block)
inline auto yield_value(purge_block&&)
inline void unhandled_exception()

Exceptions are currently not implermented.