3#include "util/async/AnyOperation.hpp"
4#include "util/async/AnyStopToken.hpp"
5#include "util/async/Concepts.hpp"
6#include "util/async/impl/Any.hpp"
7#include "util/async/impl/ErasedOperation.hpp"
30 template <NotSameAs<AnyStrand> StrandType>
32 : pimpl_{std::make_shared<Model<StrandType>>(std::forward<StrandType>(strand))}
49 using RetType = std::decay_t<std::invoke_result_t<
decltype(fn)>>;
50 static_assert(not std::is_same_v<RetType, impl::Any>);
53 pimpl_->execute([fn = std::forward<
decltype(fn)>(fn)] mutable ->
impl::Any {
54 if constexpr (std::is_void_v<RetType>) {
55 std::invoke(std::forward<decltype(fn)>(fn));
58 return std::make_any<RetType>(std::invoke(std::forward<decltype(fn)>(fn)));
73 using RetType = std::decay_t<std::invoke_result_t<
decltype(fn),
AnyStopToken>>;
74 static_assert(not std::is_same_v<RetType, impl::Any>);
78 [fn = std::forward<
decltype(fn)>(fn)](
auto stopToken) mutable ->
impl::Any {
79 if constexpr (std::is_void_v<RetType>) {
80 std::invoke(std::forward<decltype(fn)>(fn), std::move(stopToken));
83 return std::make_any<RetType>(
84 std::invoke(std::forward<decltype(fn)>(fn), std::move(stopToken))
102 using RetType = std::decay_t<std::invoke_result_t<
decltype(fn),
AnyStopToken>>;
103 static_assert(not std::is_same_v<RetType, impl::Any>);
107 [fn = std::forward<
decltype(fn)>(fn)](
auto stopToken) mutable ->
impl::Any {
108 if constexpr (std::is_void_v<RetType>) {
109 std::invoke(std::forward<decltype(fn)>(fn), std::move(stopToken));
112 return std::make_any<RetType>(
113 std::invoke(std::forward<decltype(fn)>(fn), std::move(stopToken))
117 std::chrono::duration_cast<std::chrono::milliseconds>(timeout)
132 using RetType = std::decay_t<std::invoke_result_t<
decltype(fn)>>;
133 static_assert(not std::is_same_v<RetType, impl::Any>);
135 auto const millis = std::chrono::duration_cast<std::chrono::milliseconds>(interval);
137 pimpl_->executeRepeatedly(
138 millis, [fn = std::forward<
decltype(fn)>(fn)] mutable ->
impl::Any {
139 std::invoke(std::forward<decltype(fn)>(fn));
155 pimpl_->submit(std::forward<
decltype(fn)>(fn));
160 virtual ~Concept() =
default;
165 std::optional<std::chrono::milliseconds> timeout = std::nullopt
169 executeRepeatedly(std::chrono::milliseconds, std::function<
impl::Any()>) = 0;
170 virtual void submit(std::function<
void()>) = 0;
173 template <
typename StrandType>
174 struct Model : Concept {
177 template <
typename SType>
178 requires std::is_same_v<SType, StrandType>
179 Model(SType&& strand) : strand{std::forward<SType>(strand)}
183 [[nodiscard]] impl::ErasedOperation
185 std::function<impl::Any(AnyStopToken)> fn,
186 std::optional<std::chrono::milliseconds> timeout
189 return strand.execute(std::move(fn), timeout);
192 [[nodiscard]] impl::ErasedOperation
193 execute(std::function<impl::Any()> fn)
override
195 return strand.execute(std::move(fn));
198 impl::ErasedOperation
200 std::chrono::milliseconds interval,
201 std::function<impl::Any()> fn
204 return strand.executeRepeatedly(interval, std::move(fn));
208 submit(std::function<
void()> fn)
override
210 return strand.submit(std::move(fn));
215 std::shared_ptr<Concept> pimpl_;
A type-erased operation that can be executed via AnyExecutionContext.
Definition AnyOperation.hpp:26
A type-erased stop token.
Definition AnyStopToken.hpp:18
A type-erased execution context.
Definition AnyStrand.hpp:22
auto executeRepeatedly(SomeStdDuration auto interval, SomeHandlerWithoutStopToken auto &&fn)
Schedule a repeating operation on the execution context.
Definition AnyStrand.hpp:130
void submit(SomeHandlerWithoutStopToken auto &&fn)
Schedule an operation on the execution context without expectations of a result.
Definition AnyStrand.hpp:153
AnyStrand(StrandType &&strand)
Construct a new Any Strand object.
Definition AnyStrand.hpp:31
auto execute(SomeHandlerWithoutStopToken auto &&fn)
Execute a function without a stop token on the strand.
Definition AnyStrand.hpp:47
auto execute(SomeHandlerWith< AnyStopToken > auto &&fn)
Execute a function taking a stop token on the strand.
Definition AnyStrand.hpp:71
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:100
A wrapper for std::any used as the type-erased transport for async operation results.
Definition Any.hpp:20
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:18