Clio  develop
The XRP Ledger API server.
Loading...
Searching...
No Matches
Spawn.hpp
1#pragma once
2
3#include <boost/asio/spawn.hpp>
4#include <boost/asio/strand.hpp>
5
6#include <concepts>
7#include <exception>
8#include <type_traits>
9
10namespace util {
11namespace impl {
12
13template <typename T>
14concept IsStrand = std::
15 same_as<std::decay_t<T>, boost::asio::strand<typename std::decay_t<T>::inner_executor_type>>;
16
23inline constexpr struct PropagatingCompletionHandler {
28 void
29 operator()(std::exception_ptr ePtr)
30 {
31 if (ePtr)
32 std::rethrow_exception(ePtr);
33 }
34} kPROPAGATE_EXCEPTIONS;
35
36} // namespace impl
37
50template <typename Ctx, typename F>
51 requires std::is_invocable_r_v<void, F, boost::asio::yield_context>
52void
53spawn(Ctx&& ctx, F&& func)
54{
55 if constexpr (impl::IsStrand<Ctx>) {
56 boost::asio::spawn(
57 std::forward<Ctx>(ctx), std::forward<F>(func), impl::kPROPAGATE_EXCEPTIONS
58 );
59 } else {
60 boost::asio::spawn(
61 boost::asio::make_strand(std::forward<Ctx>(ctx).get_executor()),
62 std::forward<F>(func),
63 impl::kPROPAGATE_EXCEPTIONS
64 );
65 }
66}
67
68} // namespace util
Definition Spawn.hpp:14
This namespace contains various utilities.
Definition AccountUtils.hpp:11
void spawn(Ctx &&ctx, F &&func)
Spawns a coroutine using boost::asio::spawn.
Definition Spawn.hpp:53
A completion handler that restores boost::asio::spawn's behaviour from Boost 1.83.
Definition Spawn.hpp:23
void operator()(std::exception_ptr ePtr)
The completion handler.
Definition Spawn.hpp:29