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>);
95 pimpl_->execute([fn = std::forward<
decltype(fn)>(fn)](
auto stopToken) mutable -> std::any {
96 if constexpr (std::is_void_v<RetType>) {
97 std::invoke(std::forward<decltype(fn)>(fn), std::move(stopToken));
100 return std::make_any<RetType>(std::invoke(std::forward<decltype(fn)>(fn), std::move(stopToken)));
116 using RetType = std::decay_t<std::invoke_result_t<
decltype(fn),
AnyStopToken>>;
117 static_assert(not std::is_same_v<RetType, std::any>);
121 [fn = std::forward<
decltype(fn)>(fn)](
auto stopToken) mutable -> std::any {
122 if constexpr (std::is_void_v<RetType>) {
123 std::invoke(std::forward<decltype(fn)>(fn), std::move(stopToken));
126 return std::make_any<RetType>(
127 std::invoke(std::forward<decltype(fn)>(fn), std::move(stopToken))
131 std::chrono::duration_cast<std::chrono::milliseconds>(timeout)
146 using RetType = std::decay_t<std::invoke_result_t<
decltype(fn)>>;
147 static_assert(not std::is_same_v<RetType, std::any>);
149 auto const millis = std::chrono::duration_cast<std::chrono::milliseconds>(interval);
151 pimpl_->executeRepeatedly(millis, [fn = std::forward<
decltype(fn)>(fn)] mutable -> std::any {
152 std::invoke(std::forward<decltype(fn)>(fn));
167 pimpl_->submit(std::forward<
decltype(fn)>(fn));
172 virtual ~Concept() =
default;
177 std::optional<std::chrono::milliseconds> timeout = std::nullopt
181 executeRepeatedly(std::chrono::milliseconds, std::function<std::any()>) = 0;
182 virtual void submit(std::function<
void()>) = 0;
185 template <
typename StrandType>
186 struct Model : Concept {
189 template <
typename SType>
190 requires std::is_same_v<SType, StrandType>
191 Model(SType&& strand) : strand{std::forward<SType>(strand)}
195 [[nodiscard]] impl::ErasedOperation
196 execute(std::function<std::any(AnyStopToken)> fn, std::optional<std::chrono::milliseconds> timeout)
override
198 return strand.execute(std::move(fn), timeout);
201 [[nodiscard]] impl::ErasedOperation
202 execute(std::function<std::any()> fn)
override
204 return strand.execute(std::move(fn));
207 impl::ErasedOperation
208 executeRepeatedly(std::chrono::milliseconds interval, std::function<std::any()> fn)
override
210 return strand.executeRepeatedly(interval, std::move(fn));
214 submit(std::function<
void()> fn)
override
216 return strand.submit(std::move(fn));
221 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:144
void submit(SomeHandlerWithoutStopToken auto &&fn)
Schedule an operation on the execution context without expectations of a result.
Definition AnyStrand.hpp:165
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
Definition ErasedOperation.hpp:34
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