Clio develop
The XRP Ledger API server.
Loading...
Searching...
No Matches
AnyStrand.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/AnyOperation.hpp"
23#include "util/async/AnyStopToken.hpp"
24#include "util/async/Concepts.hpp"
25#include "util/async/impl/ErasedOperation.hpp"
26
27#include <any>
28#include <chrono>
29#include <functional>
30#include <memory>
31#include <optional>
32#include <type_traits>
33#include <utility>
34
35namespace util::async {
36
40class AnyStrand {
41public:
48 template <NotSameAs<AnyStrand> StrandType>
49 /* implicit */ AnyStrand(StrandType&& strand)
50 : pimpl_{std::make_shared<Model<StrandType>>(std::forward<StrandType>(strand))}
51 {
52 }
53
54 AnyStrand(AnyStrand const&) = default;
55 AnyStrand(AnyStrand&&) = default;
56 ~AnyStrand() = default;
57
64 [[nodiscard]] auto
66 {
67 using RetType = std::decay_t<decltype(fn())>;
68 static_assert(not std::is_same_v<RetType, std::any>);
69
70 return AnyOperation<RetType>( //
71 pimpl_->execute([fn = std::forward<decltype(fn)>(fn)]() -> std::any {
72 if constexpr (std::is_void_v<RetType>) {
73 fn();
74 return {};
75 } else {
76 return std::make_any<RetType>(fn());
77 }
78 })
79 );
80 }
81
88 [[nodiscard]] auto
90 {
91 using RetType = std::decay_t<decltype(fn(std::declval<AnyStopToken>()))>;
92 static_assert(not std::is_same_v<RetType, std::any>);
93
94 return AnyOperation<RetType>( //
95 pimpl_->execute([fn = std::forward<decltype(fn)>(fn)](auto stopToken) -> std::any {
96 if constexpr (std::is_void_v<RetType>) {
97 fn(std::move(stopToken));
98 return {};
99 } else {
100 return std::make_any<RetType>(fn(std::move(stopToken)));
101 }
102 })
103 );
104 }
105
113 [[nodiscard]] auto
115 {
116 using RetType = std::decay_t<decltype(fn(std::declval<AnyStopToken>()))>;
117 static_assert(not std::is_same_v<RetType, std::any>);
118
119 return AnyOperation<RetType>( //
120 pimpl_->execute(
121 [fn = std::forward<decltype(fn)>(fn)](auto stopToken) -> std::any {
122 if constexpr (std::is_void_v<RetType>) {
123 fn(std::move(stopToken));
124 return {};
125 } else {
126 return std::make_any<RetType>(fn(std::move(stopToken)));
127 }
128 },
129 std::chrono::duration_cast<std::chrono::milliseconds>(timeout)
130 )
131 );
132 }
133
141 [[nodiscard]] auto
143 {
144 using RetType = std::decay_t<decltype(fn())>;
145 static_assert(not std::is_same_v<RetType, std::any>);
146
147 auto const millis = std::chrono::duration_cast<std::chrono::milliseconds>(interval);
148 return AnyOperation<RetType>( //
149 pimpl_->executeRepeatedly(millis, [fn = std::forward<decltype(fn)>(fn)] -> std::any {
150 fn();
151 return {};
152 })
153 );
154 }
155
156private:
157 struct Concept {
158 virtual ~Concept() = default;
159
160 [[nodiscard]] virtual impl::ErasedOperation
161 execute(
162 std::function<std::any(AnyStopToken)>,
163 std::optional<std::chrono::milliseconds> timeout = std::nullopt
164 ) = 0;
165 [[nodiscard]] virtual impl::ErasedOperation execute(std::function<std::any()>) = 0;
166 [[nodiscard]] virtual impl::ErasedOperation
167 executeRepeatedly(std::chrono::milliseconds, std::function<std::any()>) = 0;
168 };
169
170 template <typename StrandType>
171 struct Model : Concept {
172 StrandType strand;
173
174 template <typename SType>
175 requires std::is_same_v<SType, StrandType>
176 Model(SType&& strand) : strand{std::forward<SType>(strand)}
177 {
178 }
179
180 [[nodiscard]] impl::ErasedOperation
181 execute(std::function<std::any(AnyStopToken)> fn, std::optional<std::chrono::milliseconds> timeout) override
182 {
183 return strand.execute(std::move(fn), timeout);
184 }
185
186 [[nodiscard]] impl::ErasedOperation
187 execute(std::function<std::any()> fn) override
188 {
189 return strand.execute(std::move(fn));
190 }
191
192 impl::ErasedOperation
193 executeRepeatedly(std::chrono::milliseconds interval, std::function<std::any()> fn) override
194 {
195 return strand.executeRepeatedly(interval, std::move(fn));
196 }
197 };
198
199private:
200 std::shared_ptr<Concept> pimpl_;
201};
202
203} // namespace util::async
A type-erased operation that can be executed via AnyExecutionContext.
Definition AnyOperation.hpp:44
A type-erased execution context.
Definition AnyStrand.hpp:40
auto executeRepeatedly(SomeStdDuration auto interval, SomeHandlerWithoutStopToken auto &&fn)
Schedule a repeating operation on the execution context.
Definition AnyStrand.hpp:142
AnyStrand(StrandType &&strand)
Construct a new Any Strand object.
Definition AnyStrand.hpp:49
auto execute(SomeHandlerWithoutStopToken auto &&fn)
Execute a function without a stop token on the strand.
Definition AnyStrand.hpp:65
auto execute(SomeHandlerWith< AnyStopToken > auto &&fn)
Execute a function taking a stop token on the strand.
Definition AnyStrand.hpp:89
auto execute(SomeHandlerWith< AnyStopToken > auto &&fn, SomeStdDuration auto timeout)
Execute a function taking a stop token on the strand with a timeout.
Definition AnyStrand.hpp:114
Specifies the interface for a handler that can be invoked with the specified args.
Definition Concepts.hpp:162
Specifies the interface for a handler that can be stopped.
Definition Concepts.hpp:154
Specifies that the type must be some std::duration.
Definition Concepts.hpp:170
This namespace implements an async framework built on top of execution contexts.
Definition AnyExecutionContext.hpp:36