22#include <boost/asio/any_io_executor.hpp>
23#include <boost/asio/bind_cancellation_slot.hpp>
24#include <boost/asio/cancellation_signal.hpp>
25#include <boost/asio/cancellation_type.hpp>
26#include <boost/asio/error.hpp>
27#include <boost/asio/spawn.hpp>
28#include <boost/signals2/connection.hpp>
29#include <boost/signals2/signal.hpp>
30#include <boost/signals2/variadic_signal.hpp>
67 boost::asio::cancellation_slot_binder<boost::asio::yield_context, boost::asio::cancellation_slot>;
70 boost::asio::yield_context yield_;
71 boost::system::error_code error_;
72 boost::asio::cancellation_signal cancellationSignal_;
74 std::atomic_bool isCancelled_{
false};
76 using FamilyCancellationSignal = boost::signals2::signal<void(boost::asio::cancellation_type_t)>;
77 std::shared_ptr<FamilyCancellationSignal> familySignal_;
78 boost::signals2::connection connection_;
86 boost::asio::yield_context&&
yield,
87 std::shared_ptr<FamilyCancellationSignal> signal = std::make_shared<FamilyCancellationSignal>()
113 template <
typename ExecutionContext, CoroutineFunction Fn>
117 boost::asio::spawn(ioContext, [fn = std::move(fn)](boost::asio::yield_context
yield) {
129 template <CoroutineFunction Fn>
138 [signal = familySignal_, fn = std::move(fn)](boost::asio::yield_context
yield)
mutable {
149 [[nodiscard]] boost::system::error_code
158 cancelAll(boost::asio::cancellation_type_t cancellationType = boost::asio::cancellation_type::terminal);
180 [[nodiscard]] boost::asio::any_io_executor
Manages a coroutine execution context, allowing for cooperative multitasking and cancellation.
Definition Coroutine.hpp:60
static void spawnNew(ExecutionContext &ioContext, Fn fn)
Spawns a new top-level coroutine.
Definition Coroutine.hpp:115
boost::asio::cancellation_slot_binder< boost::asio::yield_context, boost::asio::cancellation_slot > cancellable_yield_context_type
Type alias for a yield_context that is bound to a cancellation slot. This allows asynchronous operati...
Definition Coroutine.hpp:66
~Coroutine()
Destructor for the Coroutine. Handles cleanup, such as disconnecting from the cancellation signal.
Definition Coroutine.cpp:47
boost::system::error_code error() const
Returns the error code, if any, associated with the last operation in this coroutine.
Definition Coroutine.cpp:53
bool isCancelled() const
Checks if this coroutine has been cancelled.
Definition Coroutine.cpp:67
boost::asio::any_io_executor executor() const
Returns the executor associated with this coroutine's yield context.
Definition Coroutine.cpp:79
void cancelAll(boost::asio::cancellation_type_t cancellationType=boost::asio::cancellation_type::terminal)
Cancels all coroutines sharing the same root cancellation signal.
Definition Coroutine.cpp:59
void spawnChild(Fn fn)
Spawns a child coroutine from this coroutine. The child coroutine shares the same cancellation signal...
Definition Coroutine.hpp:131
cancellable_yield_context_type yieldContext() const
Returns the cancellable yield context associated with this coroutine. This context should be used for...
Definition Coroutine.cpp:73
void yield() const
Explicitly yields execution back to the scheduler. This can be used to allow other tasks to run.
Definition Coroutine.cpp:85
Concept for functions that can be used as coroutine bodies. Such functions must be invocable with a C...
Definition Coroutine.hpp:49
This namespace contains various utilities.
Definition AccountUtils.hpp:30