Clio
develop
The XRP Ledger API server.
Theme:
Default
Round
Robot
Toggle main menu visibility
Loading...
Searching...
No Matches
Utils.hpp
1
#pragma once
2
3
#include "util/async/Concepts.hpp"
4
#include "util/async/Error.hpp"
5
#include "util/async/context/impl/Timer.hpp"
6
7
#include <boost/asio/spawn.hpp>
8
#include <boost/asio/strand.hpp>
9
#include <boost/asio/thread_pool.hpp>
10
11
#include <expected>
12
#include <optional>
13
#include <type_traits>
14
15
namespace
util::async::impl {
16
17
inline
constexpr
struct
AssociatedExecutorExtractor
{
18
template
<
typename
CtxType>
19
[[nodiscard]] CtxType::ExecutorType&
20
operator()(CtxType& ctx)
const
noexcept
21
{
22
return
ctx.context_.getExecutor();
23
}
24
} extractAssociatedExecutor;
// NOLINT(readability-identifier-naming)
25
26
template
<
typename
CtxType>
27
[[nodiscard]]
constexpr
auto
28
getTimeoutHandleIfNeeded(
29
CtxType& ctx,
30
SomeOptStdDuration
auto
timeout,
31
SomeStopSource
auto
& stopSource
32
)
33
{
34
using
TimerType = CtxType::Timer;
35
std::optional<TimerType> timer;
36
if
(timeout) {
37
timer.emplace(extractAssociatedExecutor(ctx), *timeout, [&stopSource](
auto
cancelled) {
38
if
(not cancelled)
39
stopSource.requestStop();
40
});
41
}
42
return
timer;
43
}
44
45
template
<SomeStopSource StopSourceType>
46
[[nodiscard]]
constexpr
auto
47
outcomeForHandler(
auto
&& fn)
48
{
49
if
constexpr
(SomeHandlerWith<
decltype
(fn),
typename
StopSourceType::Token>) {
50
using
FnRetType =
51
std::decay_t<std::invoke_result_t<
decltype
(fn),
typename
StopSourceType::Token>>;
52
using
RetType = std::expected<FnRetType, ExecutionError>;
53
54
return
StoppableOutcome<RetType, StopSourceType>();
55
}
else
{
56
using
FnRetType = std::decay_t<std::invoke_result_t<
decltype
(fn)>>;
57
using
RetType = std::expected<FnRetType, ExecutionError>;
58
59
return
Outcome<RetType>();
60
}
61
}
62
63
struct
SelfContextProvider
{
64
template
<
typename
CtxType>
65
[[nodiscard]]
static
constexpr
auto
&
66
getContext(CtxType& self)
noexcept
67
{
68
return
self;
69
}
70
};
71
72
}
// namespace util::async::impl
util::async::SomeOptStdDuration
Specifies that the type must be some std::duration wrapped in an optional.
Definition
Concepts.hpp:240
util::async::SomeStopSource
Specifies the interface for a stop source.
Definition
Concepts.hpp:166
util::async::impl::AssociatedExecutorExtractor
Definition
Utils.hpp:17
util::async::impl::SelfContextProvider
Definition
Utils.hpp:63
src
util
async
context
impl
Utils.hpp
Generated by
1.17.0