Struct CallbackAwaiter

Synopsis

#include <lib/inc/drogon/utils/coroutine.h>

template <typename T = void>
struct CallbackAwaiter

Description

Helper class that provides the infrastructure for turning callback into coroutines

Methods

await_ready
await_resume
setException
setValue overload

Source

Lines 419-459 in lib/inc/drogon/utils/coroutine.h.

template <typename T = void>
struct CallbackAwaiter
{
    bool await_ready() noexcept
    {
        return false;
    }

    const T &await_resume() const noexcept(false)
    {
        // await_resume() should always be called after co_await
        // (await_suspend()) is called. Therefore the value should always be set
        // (or there's an exception)
        assert(result_.has_value() == true || exception_ != nullptr);

        if (exception_)
            std::rethrow_exception(exception_);
        return result_.value();
    }

  private:
    // HACK: Not all desired types are default constructable. But we need the
    // entire struct to be constructed for awaiting. std::optional takes care of
    // that.
    optional<T> result_;
    std::exception_ptr exception_{nullptr};

  protected:
    void setException(const std::exception_ptr &e)
    {
        exception_ = e;
    }
    void setValue(const T &v)
    {
        result_.emplace(v);
    }
    void setValue(T &&v)
    {
        result_.emplace(std::move(v));
    }
};





Add Discussion as Guest

Log in