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<std::invoke_result_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)] mutable -> std::any {
72 if constexpr (std::is_void_v<RetType>) {
73 std::invoke(std::forward<decltype(fn)>(fn));
74 return {};
75 } else {
76 return std::make_any<RetType>(std::invoke(std::forward<decltype(fn)>(fn)));
77 }
78 })
79 );
80 }
81
88 [[nodiscard]] auto
90 {
91 using RetType = std::decay_t<std::invoke_result_t<decltype(fn), AnyStopToken>>;
92 static_assert(not std::is_same_v<RetType, std::any>);
93
94 return AnyOperation<RetType>( //
95 pimpl_->execute(
96 [fn = std::forward<decltype(fn)>(fn)](auto stopToken) mutable -> std::any {
97 if constexpr (std::is_void_v<RetType>) {
98 std::invoke(std::forward<decltype(fn)>(fn), std::move(stopToken));
99 return {};
100 } else {
101 return std::make_any<RetType>(
102 std::invoke(std::forward<decltype(fn)>(fn), std::move(stopToken))
103 );
104 }
105 }
106 )
107 );
108 }
109
117 [[nodiscard]] auto
119 {
120 using RetType = std::decay_t<std::invoke_result_t<decltype(fn), AnyStopToken>>;
121 static_assert(not std::is_same_v<RetType, std::any>);
122
123 return AnyOperation<RetType>( //
124 pimpl_->execute(
125 [fn = std::forward<decltype(fn)>(fn)](auto stopToken) mutable -> std::any {
126 if constexpr (std::is_void_v<RetType>) {
127 std::invoke(std::forward<decltype(fn)>(fn), std::move(stopToken));
128 return {};
129 } else {
130 return std::make_any<RetType>(
131 std::invoke(std::forward<decltype(fn)>(fn), std::move(stopToken))
132 );
133 }
134 },
135 std::chrono::duration_cast<std::chrono::milliseconds>(timeout)
136 )
137 );
138 }
139
147 [[nodiscard]] auto
149 {
150 using RetType = std::decay_t<std::invoke_result_t<decltype(fn)>>;
151 static_assert(not std::is_same_v<RetType, std::any>);
152
153 auto const millis = std::chrono::duration_cast<std::chrono::milliseconds>(interval);
154 return AnyOperation<RetType>( //
155 pimpl_->executeRepeatedly(
156 millis, [fn = std::forward<decltype(fn)>(fn)] mutable -> std::any {
157 std::invoke(std::forward<decltype(fn)>(fn));
158 return {};
159 }
160 )
161 );
162 }
163
170 void
172 {
173 pimpl_->submit(std::forward<decltype(fn)>(fn));
174 }
175
176private:
177 struct Concept {
178 virtual ~Concept() = default;
179
180 [[nodiscard]] virtual impl::ErasedOperation
181 execute(
182 std::function<std::any(AnyStopToken)>,
183 std::optional<std::chrono::milliseconds> timeout = std::nullopt
184 ) = 0;
185 [[nodiscard]] virtual impl::ErasedOperation execute(std::function<std::any()>) = 0;
186 [[nodiscard]] virtual impl::ErasedOperation
187 executeRepeatedly(std::chrono::milliseconds, std::function<std::any()>) = 0;
188 virtual void submit(std::function<void()>) = 0;
189 };
190
191 template <typename StrandType>
192 struct Model : Concept {
193 StrandType strand;
194
195 template <typename SType>
196 requires std::is_same_v<SType, StrandType>
197 Model(SType&& strand) : strand{std::forward<SType>(strand)}
198 {
199 }
200
201 [[nodiscard]] impl::ErasedOperation
202 execute(
203 std::function<std::any(AnyStopToken)> fn,
204 std::optional<std::chrono::milliseconds> timeout
205 ) override
206 {
207 return strand.execute(std::move(fn), timeout);
208 }
209
210 [[nodiscard]] impl::ErasedOperation
211 execute(std::function<std::any()> fn) override
212 {
213 return strand.execute(std::move(fn));
214 }
215
216 impl::ErasedOperation
217 executeRepeatedly(std::chrono::milliseconds interval, std::function<std::any()> fn) override
218 {
219 return strand.executeRepeatedly(interval, std::move(fn));
220 }
221
222 void
223 submit(std::function<void()> fn) override
224 {
225 return strand.submit(std::move(fn));
226 }
227 };
228
229private:
230 std::shared_ptr<Concept> pimpl_;
231};
232
233} // namespace util::async
A type-erased operation that can be executed via AnyExecutionContext.
Definition AnyOperation.hpp:44
A type-erased stop token.
Definition AnyStopToken.hpp:37
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:148
void submit(SomeHandlerWithoutStopToken auto &&fn)
Schedule an operation on the execution context without expectations of a result.
Definition AnyStrand.hpp:171
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:118
Definition ErasedOperation.hpp:34
Specifies the interface for a handler that can be invoked with the specified args.
Definition Concepts.hpp:184
Specifies the interface for a handler that can be stopped.
Definition Concepts.hpp:176
Specifies that the type must be some std::duration.
Definition Concepts.hpp:192
This namespace implements an async framework built on top of execution contexts.
Definition AnyExecutionContext.hpp:36