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>
43template <
typename OutcomeType>
46 std::future<typename OutcomeType::DataType> future_;
49 using DataType =
typename OutcomeType::DataType;
51 explicit BasicOperation(OutcomeType* outcome) : future_{outcome->getStdFuture()}
55 BasicOperation(BasicOperation&&) =
default;
57 BasicOperation(BasicOperation
const&) =
delete;
72template <
typename CtxType,
typename OpType>
76 std::condition_variable ready_;
77 std::optional<OpType> op_{std::nullopt};
83 std::lock_guard
const lock{m_};
84 op_.emplace(std::forward<
decltype(op)>(op));
91 std::unique_lock lock{m_};
92 ready_.wait(lock, [
this] {
return op_.has_value(); });
97 std::shared_ptr<State> state = std::make_shared<State>();
98 typename CtxType::Timer timer;
101 : timer(executor, delay, [state = state, fn = std::forward<decltype(fn)>(fn)](auto ec) mutable {
102 state->emplace(fn(ec));
107 ~BasicScheduledOperation()
override
113 BasicScheduledOperation(BasicScheduledOperation
const&) =
default;
114 BasicScheduledOperation&
115 operator=(BasicScheduledOperation
const&) =
default;
116 BasicScheduledOperation(BasicScheduledOperation&&) =
default;
117 BasicScheduledOperation&
118 operator=(BasicScheduledOperation&&) =
default;
123 return state->get().get();
139 requestStop() noexcept
140 requires(SomeStoppableOperation<OpType>)
142 state->get().requestStop();
150 if constexpr (SomeStoppableOperation<OpType>)
163template <
typename RetType,
typename StopSourceType>
164class StoppableOperation :
public impl::BasicOperation<StoppableOutcome<RetType, StopSourceType>>,
166 using OutcomeType = StoppableOutcome<RetType, StopSourceType>;
168 StopSourceType stopSource_;
177 : impl::BasicOperation<
OutcomeType>(outcome), stopSource_(outcome->getStopSource())
187 StoppableOperation(StoppableOperation
const&) =
delete;
189 operator=(StoppableOperation
const&) =
delete;
190 StoppableOperation(StoppableOperation&&) =
default;
192 operator=(StoppableOperation&&) =
default;
198 stopSource_.requestStop();
207template <
typename RetType>
216template <
typename CtxType,
typename OpType>
227template <
typename CtxType>
240 RepeatingOperation(
auto& executor, std::chrono::steady_clock::duration interval, std::invocable
auto&& fn)
243 repeat_.
start(interval, std::forward<
decltype(fn)>(fn));
252 RepeatingOperation(RepeatingOperation
const&) =
delete;
254 operator=(RepeatingOperation
const&) =
delete;
255 RepeatingOperation(RepeatingOperation&&) =
default;
257 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:228
RepeatingOperation(auto &executor, std::chrono::steady_clock::duration interval, std::invocable auto &&fn)
Construct a new Repeating Operation object.
Definition Operation.hpp:240
void abort() noexcept
Aborts the operation and the repeating timer.
Definition Operation.hpp:265
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:176
void requestStop() noexcept
Requests the operation to stop.
Definition Operation.hpp:196
Stoppable outcome.
Definition Outcome.hpp:116
Definition Outcome.hpp:35
Definition Operation.hpp:74
This namespace implements an async framework built on top of execution contexts.
Definition AnyExecutionContext.hpp:36
Definition Operation.hpp:73