libcoro  1.0
Coroutine support library for C++20
Public Member Functions | Static Public Member Functions | Protected Member Functions | List of all members
coro::subscription< T, SubscribeFn > Class Template Reference

Awaiter for subscriptions. More...

#include <subscription.h>

+ Collaboration diagram for coro::subscription< T, SubscribeFn >:

Public Member Functions

 subscription ()=default
 default constructor More...
 
 subscription (SubscribeFn &&fn)
 construct and pass subscribe function More...
 
 subscription (const SubscribeFn &fn)
 construct and pass subscribe function More...
 
 subscription (subscription &&other)
 movable More...
 
subscriptionoperator= (subscription &&other)
 movable More...
 
decltype(auto) get ()
 retrieve last value More...
 
std::coroutine_handle await_suspend (std::coroutine_handle<> h)
 co_await support Subscribe and wait for a value More...
 
ret_value await_resume ()
 returns current value More...
 
canceled_awaiter operator! ()
 wait and determine whether operation is canceled More...
 
bool has_value () const
 Determine, whether has value. More...
 
template<std::invocable Fn>
prepared_coro then (Fn &&fn)
 define callback, which is called when value is ready More...
 
template<std::invocable Fn>
void operator>> (Fn &&fn)
 define callback, which is called when value is ready More...
 
void lock (lock_ptr &ptr)
 subscribe and lock value during processing More...
 

Static Public Member Functions

static constexpr bool await_ready () noexcept
 co_await support More...
 
static constexpr lock_ptr init_lock ()
 initialize lock_ptr object, which can be used to call lock() to wait on subscription and process result More...
 
static constexpr lock_ptr init_lock (std::atomic< bool > &unlock)
 initialize lock_ptr, set atomic variable, which receives signal once the thread starts waiting on a subscription More...
 

Protected Member Functions

void wait_internal ()
 

Detailed Description

template<typename T, typename SubscribeFn = function<prepared_coro(promise<T>)>>
class coro::subscription< T, SubscribeFn >

Awaiter for subscriptions.

Represents subscription. You can subscribe a anything (represented as lambda function), and if you need to monitor subscription, you need co_await on instance. If you need to receive all values, you need to repeat co_await in the same thread - without any other co_await in between (in case that you need asynchronous processing, you need to connect subscription with a queue)

Template Parameters
Ttype of object, it can be reference or const reference
SubscribeFnsubscription function. Default value also defines prototype of the function

Works similar as deferred_future, but it can be awaited repeatedly. Every co_await performs subscription on the source. Synchronous access is possible, but has different API. You need to use pointer access

Note
object is movable (unless it is locked)

,

Definition at line 29 of file subscription.h.


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