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"
48 template <NotSameAs<AnyStrand> StrandType>
50 : pimpl_{std::make_shared<Model<StrandType>>(std::forward<StrandType>(strand))}
67 using RetType = std::decay_t<std::invoke_result_t<
decltype(fn)>>;
68 static_assert(not std::is_same_v<RetType, std::any>);
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));
76 return std::make_any<RetType>(std::invoke(std::forward<decltype(fn)>(fn)));
91 using RetType = std::decay_t<std::invoke_result_t<
decltype(fn),
AnyStopToken>>;
92 static_assert(not std::is_same_v<RetType, std::any>);
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));
101 return std::make_any<RetType>(
102 std::invoke(std::forward<decltype(fn)>(fn), std::move(stopToken))
120 using RetType = std::decay_t<std::invoke_result_t<
decltype(fn),
AnyStopToken>>;
121 static_assert(not std::is_same_v<RetType, std::any>);
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));
130 return std::make_any<RetType>(
131 std::invoke(std::forward<decltype(fn)>(fn), std::move(stopToken))
135 std::chrono::duration_cast<std::chrono::milliseconds>(timeout)
150 using RetType = std::decay_t<std::invoke_result_t<
decltype(fn)>>;
151 static_assert(not std::is_same_v<RetType, std::any>);
153 auto const millis = std::chrono::duration_cast<std::chrono::milliseconds>(interval);
155 pimpl_->executeRepeatedly(
156 millis, [fn = std::forward<
decltype(fn)>(fn)] mutable -> std::any {
157 std::invoke(std::forward<decltype(fn)>(fn));
173 pimpl_->submit(std::forward<
decltype(fn)>(fn));
178 virtual ~Concept() =
default;
183 std::optional<std::chrono::milliseconds> timeout = std::nullopt
187 executeRepeatedly(std::chrono::milliseconds, std::function<std::any()>) = 0;
188 virtual void submit(std::function<
void()>) = 0;
191 template <
typename StrandType>
192 struct Model : Concept {
195 template <
typename SType>
196 requires std::is_same_v<SType, StrandType>
197 Model(SType&& strand) : strand{std::forward<SType>(strand)}
201 [[nodiscard]] impl::ErasedOperation
203 std::function<std::any(AnyStopToken)> fn,
204 std::optional<std::chrono::milliseconds> timeout
207 return strand.execute(std::move(fn), timeout);
210 [[nodiscard]] impl::ErasedOperation
211 execute(std::function<std::any()> fn)
override
213 return strand.execute(std::move(fn));
216 impl::ErasedOperation
217 executeRepeatedly(std::chrono::milliseconds interval, std::function<std::any()> fn)
override
219 return strand.executeRepeatedly(interval, std::move(fn));
223 submit(std::function<
void()> fn)
override
225 return strand.submit(std::move(fn));
230 std::shared_ptr<Concept> pimpl_;
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