3#include "util/async/AnyOperation.hpp"
4#include "util/async/AnyStopToken.hpp"
5#include "util/async/Concepts.hpp"
6#include "util/async/impl/ErasedOperation.hpp"
29 template <NotSameAs<AnyStrand> StrandType>
31 : pimpl_{std::make_shared<Model<StrandType>>(std::forward<StrandType>(strand))}
48 using RetType = std::decay_t<std::invoke_result_t<
decltype(fn)>>;
49 static_assert(not std::is_same_v<RetType, std::any>);
52 pimpl_->execute([fn = std::forward<
decltype(fn)>(fn)] mutable -> std::any {
53 if constexpr (std::is_void_v<RetType>) {
54 std::invoke(std::forward<decltype(fn)>(fn));
57 return std::make_any<RetType>(std::invoke(std::forward<decltype(fn)>(fn)));
72 using RetType = std::decay_t<std::invoke_result_t<
decltype(fn),
AnyStopToken>>;
73 static_assert(not std::is_same_v<RetType, std::any>);
77 [fn = std::forward<
decltype(fn)>(fn)](
auto stopToken) mutable -> std::any {
78 if constexpr (std::is_void_v<RetType>) {
79 std::invoke(std::forward<decltype(fn)>(fn), std::move(stopToken));
82 return std::make_any<RetType>(
83 std::invoke(std::forward<decltype(fn)>(fn), std::move(stopToken))
101 using RetType = std::decay_t<std::invoke_result_t<
decltype(fn),
AnyStopToken>>;
102 static_assert(not std::is_same_v<RetType, std::any>);
106 [fn = std::forward<
decltype(fn)>(fn)](
auto stopToken) mutable -> std::any {
107 if constexpr (std::is_void_v<RetType>) {
108 std::invoke(std::forward<decltype(fn)>(fn), std::move(stopToken));
111 return std::make_any<RetType>(
112 std::invoke(std::forward<decltype(fn)>(fn), std::move(stopToken))
116 std::chrono::duration_cast<std::chrono::milliseconds>(timeout)
131 using RetType = std::decay_t<std::invoke_result_t<
decltype(fn)>>;
132 static_assert(not std::is_same_v<RetType, std::any>);
134 auto const millis = std::chrono::duration_cast<std::chrono::milliseconds>(interval);
136 pimpl_->executeRepeatedly(
137 millis, [fn = std::forward<
decltype(fn)>(fn)] mutable -> std::any {
138 std::invoke(std::forward<decltype(fn)>(fn));
154 pimpl_->submit(std::forward<
decltype(fn)>(fn));
159 virtual ~Concept() =
default;
164 std::optional<std::chrono::milliseconds> timeout = std::nullopt
168 executeRepeatedly(std::chrono::milliseconds, std::function<std::any()>) = 0;
169 virtual void submit(std::function<
void()>) = 0;
172 template <
typename StrandType>
173 struct Model : Concept {
176 template <
typename SType>
177 requires std::is_same_v<SType, StrandType>
178 Model(SType&& strand) : strand{std::forward<SType>(strand)}
182 [[nodiscard]] impl::ErasedOperation
184 std::function<std::any(AnyStopToken)> fn,
185 std::optional<std::chrono::milliseconds> timeout
188 return strand.execute(std::move(fn), timeout);
191 [[nodiscard]] impl::ErasedOperation
192 execute(std::function<std::any()> fn)
override
194 return strand.execute(std::move(fn));
197 impl::ErasedOperation
198 executeRepeatedly(std::chrono::milliseconds interval, std::function<std::any()> fn)
override
200 return strand.executeRepeatedly(interval, std::move(fn));
204 submit(std::function<
void()> fn)
override
206 return strand.submit(std::move(fn));
211 std::shared_ptr<Concept> pimpl_;
A type-erased operation that can be executed via AnyExecutionContext.
Definition AnyOperation.hpp:25
A type-erased stop token.
Definition AnyStopToken.hpp:18
A type-erased execution context.
Definition AnyStrand.hpp:21
auto executeRepeatedly(SomeStdDuration auto interval, SomeHandlerWithoutStopToken auto &&fn)
Schedule a repeating operation on the execution context.
Definition AnyStrand.hpp:129
void submit(SomeHandlerWithoutStopToken auto &&fn)
Schedule an operation on the execution context without expectations of a result.
Definition AnyStrand.hpp:152
AnyStrand(StrandType &&strand)
Construct a new Any Strand object.
Definition AnyStrand.hpp:30
auto execute(SomeHandlerWithoutStopToken auto &&fn)
Execute a function without a stop token on the strand.
Definition AnyStrand.hpp:46
auto execute(SomeHandlerWith< AnyStopToken > auto &&fn)
Execute a function taking a stop token on the strand.
Definition AnyStrand.hpp:70
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:99
Definition ErasedOperation.hpp:15
Specifies the interface for a handler that can be invoked with the specified args.
Definition Concepts.hpp:165
Specifies the interface for a handler that can be stopped.
Definition Concepts.hpp:157
Specifies that the type must be some std::duration.
Definition Concepts.hpp:173
This namespace implements an async framework built on top of execution contexts.
Definition AnyExecutionContext.hpp:17