22#include "util/MoveTracker.hpp"
23#include "util/Repeat.hpp"
24#include "util/async/Concepts.hpp"
25#include "util/async/Outcome.hpp"
26#include "util/async/context/impl/Cancellation.hpp"
27#include "util/async/context/impl/Timer.hpp"
29#include <fmt/format.h>
33#include <condition_variable>
44template <
typename OutcomeType>
47 std::future<typename OutcomeType::DataType> future_;
50 using DataType =
typename OutcomeType::DataType;
52 explicit BasicOperation(OutcomeType* outcome) : future_{outcome->getStdFuture()}
56 BasicOperation(BasicOperation&&) =
default;
58 BasicOperation(BasicOperation
const&) =
delete;
73template <
typename CtxType,
typename OpType>
77 std::condition_variable ready_;
78 std::optional<OpType> op_{std::nullopt};
84 std::lock_guard
const lock{m_};
85 op_.emplace(std::forward<
decltype(op)>(op));
92 std::unique_lock lock{m_};
93 ready_.wait(lock, [
this] {
return op_.has_value(); });
98 std::shared_ptr<State> state = std::make_shared<State>();
99 typename CtxType::Timer timer;
101 BasicScheduledOperation(
auto& executor,
auto delay,
auto&& fn)
105 [state = state, fn = std::forward<decltype(fn)>(fn)](auto ec) mutable {
106 state->emplace(fn(ec));
112 ~BasicScheduledOperation()
override
118 BasicScheduledOperation(BasicScheduledOperation
const&) =
default;
119 BasicScheduledOperation&
120 operator=(BasicScheduledOperation
const&) =
default;
121 BasicScheduledOperation(BasicScheduledOperation&&) =
default;
122 BasicScheduledOperation&
123 operator=(BasicScheduledOperation&&) =
default;
128 return state->get().get();
144 requestStop() noexcept
145 requires(SomeStoppableOperation<OpType>)
147 state->get().requestStop();
155 if constexpr (SomeStoppableOperation<OpType>)
168template <
typename RetType,
typename StopSourceType>
173 StopSourceType stopSource_;
182 : impl::BasicOperation<OutcomeType>(outcome), stopSource_(outcome->getStopSource())
192 StoppableOperation(StoppableOperation
const&) =
delete;
194 operator=(StoppableOperation
const&) =
delete;
195 StoppableOperation(StoppableOperation&&) =
default;
197 operator=(StoppableOperation&&) =
default;
203 stopSource_.requestStop();
212template <
typename RetType>
221template <
typename CtxType,
typename OpType>
233template <
typename CtxType>
236 std::function<void()> action_;
247 template <std::invocable FnType>
250 , action_([fn = std::forward<FnType>(fn), &executor] { boost::asio::post(executor, fn); })
252 repeat_.
start(interval, action_);
255 ~RepeatingOperation()
override
261 RepeatingOperation(RepeatingOperation
const&) =
delete;
263 operator=(RepeatingOperation
const&) =
delete;
264 RepeatingOperation(RepeatingOperation&&) =
default;
266 operator=(RepeatingOperation&&) =
default;
A base-class that can be used to check whether the current instance was moved from.
Definition MoveTracker.hpp:29
A class to repeat some action at a regular interval.
Definition Repeat.hpp:44
void start(std::chrono::steady_clock::duration interval, Action &&action)
Start asynchronously repeating.
Definition Repeat.hpp:95
void invoke()
Force-invoke the operation.
Definition Operation.hpp:286
RepeatingOperation(auto &executor, std::chrono::steady_clock::duration interval, FnType &&fn)
Construct a new Repeating Operation object.
Definition Operation.hpp:248
void abort() noexcept
Aborts the operation and the repeating timer.
Definition Operation.hpp:274
The future side of async operations that can be stopped.
Definition Operation.hpp:170
StoppableOperation(OutcomeType *outcome)
Construct a new Stoppable Operation object.
Definition Operation.hpp:181
void requestStop() noexcept
Requests the operation to stop.
Definition Operation.hpp:201
Stoppable outcome.
Definition Outcome.hpp:116
Definition Operation.hpp:45
Definition Operation.hpp:75
This namespace implements an async framework built on top of execution contexts.
Definition AnyExecutionContext.hpp:36
impl::BasicScheduledOperation< CtxType, OpType > ScheduledOperation
The future side of async operations that can be scheduled.
Definition Operation.hpp:222
impl::BasicOperation< Outcome< RetType > > Operation
The future side of async operations that cannot be stopped.
Definition Operation.hpp:213
Definition Operation.hpp:74