Clio develop
The XRP Ledger API server.
Loading...
Searching...
No Matches
Utils.hpp
1//------------------------------------------------------------------------------
2/*
3 This file is part of clio: https://github.com/XRPLF/clio
4 Copyright (c) 2024, the clio developers.
5
6 Permission to use, copy, modify, and distribute this software for any
7 purpose with or without fee is hereby granted, provided that the above
8 copyright notice and this permission notice appear in all copies.
9
10 THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17*/
18//==============================================================================
19
20#pragma once
21
22#include "util/async/Concepts.hpp"
23#include "util/async/Error.hpp"
24#include "util/async/context/impl/Timer.hpp"
25
26#include <boost/asio/spawn.hpp>
27#include <boost/asio/strand.hpp>
28#include <boost/asio/thread_pool.hpp>
29
30#include <expected>
31#include <optional>
32
33namespace util::async::impl {
34
35inline constexpr struct AssociatedExecutorExtractor {
36 template <typename CtxType>
37 [[nodiscard]] typename CtxType::ExecutorType&
38 operator()(CtxType& ctx) const noexcept
39 {
40 return ctx.context_.getExecutor();
41 }
42} extractAssociatedExecutor; // NOLINT(readability-identifier-naming)
43
44template <typename CtxType>
45[[nodiscard]] constexpr auto
46getTimeoutHandleIfNeeded(CtxType& ctx, SomeOptStdDuration auto timeout, SomeStopSource auto& stopSource)
47{
48 using TimerType = typename CtxType::Timer;
49 std::optional<TimerType> timer;
50 if (timeout) {
51 timer.emplace(extractAssociatedExecutor(ctx), *timeout, [&stopSource](auto cancelled) {
52 if (not cancelled)
53 stopSource.requestStop();
54 });
55 }
56 return timer;
57}
58
59template <SomeStopSource StopSourceType>
60[[nodiscard]] constexpr auto
61outcomeForHandler(auto&& fn)
62{
63 if constexpr (SomeHandlerWith<decltype(fn), typename StopSourceType::Token>) {
64 using FnRetType = decltype(fn(std::declval<typename StopSourceType::Token>()));
65 using RetType = std::expected<FnRetType, ExecutionError>;
66
67 return StoppableOutcome<RetType, StopSourceType>();
68 } else {
69 using FnRetType = decltype(fn());
70 using RetType = std::expected<FnRetType, ExecutionError>;
71
72 return Outcome<RetType>();
73 }
74}
75
77 template <typename CtxType>
78 [[nodiscard]] static constexpr auto&
79 getContext(CtxType& self) noexcept
80 {
81 return self;
82 }
83};
84
85} // namespace util::async::impl
Specifies that the type must be some std::duration wrapped in an optional.
Definition Concepts.hpp:184
Specifies the interface for a stop source.
Definition Concepts.hpp:126