Clio  develop
The XRP Ledger API server.
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
15namespace util::async::impl {
16
17inline constexpr struct AssociatedExecutorExtractor {
18 template <typename CtxType>
19 [[nodiscard]] typename CtxType::ExecutorType&
20 operator()(CtxType& ctx) const noexcept
21 {
22 return ctx.context_.getExecutor();
23 }
24} extractAssociatedExecutor; // NOLINT(readability-identifier-naming)
25
26template <typename CtxType>
27[[nodiscard]] constexpr auto
28getTimeoutHandleIfNeeded(
29 CtxType& ctx,
30 SomeOptStdDuration auto timeout,
31 SomeStopSource auto& stopSource
32)
33{
34 using TimerType = typename 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
45template <SomeStopSource StopSourceType>
46[[nodiscard]] constexpr auto
47outcomeForHandler(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
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
Specifies that the type must be some std::duration wrapped in an optional.
Definition Concepts.hpp:196
Specifies the interface for a stop source.
Definition Concepts.hpp:137