Orbit Framework 2.0.0
A high-performance asynchronous C++20 HTTP server framework.
Loading...
Searching...
No Matches
orbit::concurrency::Awaitable< T > Class Template Reference

A lazy coroutine that produces a T for whoever co_awaits it. More...

#include <Awaitable.hpp>

Classes

struct  promise_type
 

Public Member Functions

 Awaitable (Awaitable &&other) noexcept
 
Awaitable & operator= (Awaitable &&other) noexcept
 
 Awaitable (const Awaitable &)=delete
 
Awaitable & operator= (const Awaitable &)=delete
 
 ~Awaitable ()
 
bool await_ready () const noexcept
 
std::coroutine_handle await_suspend (std::coroutine_handle<> awaiting) noexcept
 
T await_resume ()
 

Private Member Functions

 Awaitable (std::coroutine_handle< promise_type > h) noexcept
 

Private Attributes

std::coroutine_handle< promise_type > handle_
 

Detailed Description

template<typename T>
class orbit::concurrency::Awaitable< T >

A lazy coroutine that produces a T for whoever co_awaits it.

For helpers called from a handler (or from another Awaitable), so a handler need not be one long coroutine:

Awaitable<std::optional<User>> load_user(std::shared_ptr<PostgresClient> db, int id) {
auto users = co_await query_User(db).where(Col("id") == id).get_async();
if (users.empty()) co_return std::nullopt;
co_return users.front();
}
Task show_user(HttpRequest& req, std::shared_ptr<ResponseWriter> w) {
auto user = co_await load_user(db, std::stoi(req.params.at("id")));
HttpResponse res;
if (user) res.json(*user); else res.status(HttpStatus::NotFound);
w->send(std::move(res));
}
A lazy coroutine that produces a T for whoever co_awaits it.
Definition Awaitable.hpp:77
Fire-and-forget coroutine type, used as the return type of coroutine route handlers.
Definition Task.hpp:36
  • Nothing runs until it is awaited; awaiting starts it on the awaiting thread, and the awaiting coroutine resumes, by symmetric transfer, on whichever thread the helper finishes on.
  • An exception thrown in the helper is rethrown at the co_await. In a handler that is not caught, it reaches on_error (else a 500), exactly as if the handler had thrown it.
  • Await it once. Destroying it without awaiting simply never runs it.
  • Handlers themselves keep returning Task: the router starts a Task, but would never await an Awaitable.

Constructor & Destructor Documentation

◆ Awaitable() [1/3]

template<typename T >
orbit::concurrency::Awaitable< T >::Awaitable ( Awaitable< T > &&  other)
inlinenoexcept

◆ Awaitable() [2/3]

template<typename T >
orbit::concurrency::Awaitable< T >::Awaitable ( const Awaitable< T > &  )
delete

◆ ~Awaitable()

template<typename T >
orbit::concurrency::Awaitable< T >::~Awaitable ( )
inline

◆ Awaitable() [3/3]

template<typename T >
orbit::concurrency::Awaitable< T >::Awaitable ( std::coroutine_handle< promise_type >  h)
inlineexplicitprivatenoexcept

Member Function Documentation

◆ await_ready()

template<typename T >
bool orbit::concurrency::Awaitable< T >::await_ready ( ) const
inlinenoexcept

◆ await_resume()

template<typename T >
T orbit::concurrency::Awaitable< T >::await_resume ( )
inline

◆ await_suspend()

template<typename T >
std::coroutine_handle orbit::concurrency::Awaitable< T >::await_suspend ( std::coroutine_handle<>  awaiting)
inlinenoexcept

◆ operator=() [1/2]

template<typename T >
Awaitable & orbit::concurrency::Awaitable< T >::operator= ( Awaitable< T > &&  other)
inlinenoexcept

◆ operator=() [2/2]

template<typename T >
Awaitable & orbit::concurrency::Awaitable< T >::operator= ( const Awaitable< T > &  )
delete

Member Data Documentation

◆ handle_

template<typename T >
std::coroutine_handle<promise_type> orbit::concurrency::Awaitable< T >::handle_
private

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