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"
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;
102 : timer(executor, delay, [state = state, fn = std::forward<decltype(fn)>(fn)](auto ec) mutable {
103 state->emplace(fn(ec));
108 ~BasicScheduledOperation()
override
114 BasicScheduledOperation(BasicScheduledOperation
const&) =
default;
115 BasicScheduledOperation&
116 operator=(BasicScheduledOperation
const&) =
default;
117 BasicScheduledOperation(BasicScheduledOperation&&) =
default;
118 BasicScheduledOperation&
119 operator=(BasicScheduledOperation&&) =
default;
124 return state->get().get();
140 requestStop() noexcept
141 requires(SomeStoppableOperation<OpType>)
143 state->get().requestStop();
151 if constexpr (SomeStoppableOperation<OpType>)
164template <
typename RetType,
typename StopSourceType>
165class StoppableOperation :
public impl::BasicOperation<StoppableOutcome<RetType, StopSourceType>>,
167 using OutcomeType = StoppableOutcome<RetType, StopSourceType>;
169 StopSourceType stopSource_;
178 : impl::BasicOperation<
OutcomeType>(outcome), stopSource_(outcome->getStopSource())
188 StoppableOperation(StoppableOperation
const&) =
delete;
190 operator=(StoppableOperation
const&) =
delete;
191 StoppableOperation(StoppableOperation&&) =
default;
193 operator=(StoppableOperation&&) =
default;
199 stopSource_.requestStop();
208template <
typename RetType>
217template <
typename CtxType,
typename OpType>
228template <
typename CtxType>
231 std::function<void()> action_;
242 template <std::invocable FnType>
244 : repeat_(executor), action_([fn = std::forward<FnType>(fn), &executor] { boost::asio::post(executor, fn); })
246 repeat_.
start(interval, action_);
249 ~RepeatingOperation()
override
255 RepeatingOperation(RepeatingOperation
const&) =
delete;
257 operator=(RepeatingOperation
const&) =
delete;
258 RepeatingOperation(RepeatingOperation&&) =
default;
260 operator=(RepeatingOperation&&) =
default;
A base-class that can be used to check whether the current instance was moved from.
Definition MoveTracker.hpp:29
MoveTracker & operator=(MoveTracker &&other)
Move operator sets the moved-from state on other and resets the state on this
Definition MoveTracker.hpp:63
bool wasMoved() const noexcept
The function to be used by clients in order to check whether the instance was moved from.
Definition MoveTracker.hpp:38
A class to repeat some action at a regular interval.
Definition Repeat.hpp:41
void stop()
Stop repeating.
Definition Repeat.cpp:25
void start(std::chrono::steady_clock::duration interval, Action &&action)
Start asynchronously repeating.
Definition Repeat.hpp:89
The future side of async operations that automatically repeat until aborted.
Definition Operation.hpp:229
void invoke()
Force-invoke the operation.
Definition Operation.hpp:280
RepeatingOperation(auto &executor, std::chrono::steady_clock::duration interval, FnType &&fn)
Construct a new Repeating Operation object.
Definition Operation.hpp:243
void abort() noexcept
Aborts the operation and the repeating timer.
Definition Operation.hpp:268
The future side of async operations that can be stopped.
Definition Outcome.hpp:30
StoppableOperation(OutcomeType *outcome)
Construct a new Stoppable Operation object.
Definition Operation.hpp:177
void requestStop() noexcept
Requests the operation to stop.
Definition Operation.hpp:197
Stoppable outcome.
Definition Outcome.hpp:116
Definition Outcome.hpp:35
Definition Operation.hpp:75
This namespace implements an async framework built on top of execution contexts.
Definition AnyExecutionContext.hpp:36
Definition Operation.hpp:74