3#include "util/MoveTracker.hpp"
4#include "util/Repeat.hpp"
5#include "util/async/Concepts.hpp"
6#include "util/async/Outcome.hpp"
7#include "util/async/context/impl/Cancellation.hpp"
8#include "util/async/context/impl/Timer.hpp"
10#include <fmt/format.h>
14#include <condition_variable>
25template <
typename OutcomeType>
28 std::future<typename OutcomeType::DataType> future_;
31 using DataType =
typename OutcomeType::DataType;
33 explicit BasicOperation(OutcomeType* outcome) : future_{outcome->getStdFuture()}
37 BasicOperation(BasicOperation&&) =
default;
39 BasicOperation(BasicOperation
const&) =
delete;
54template <
typename CtxType,
typename OpType>
58 std::condition_variable ready_;
59 std::optional<OpType> op_{std::nullopt};
65 std::lock_guard
const lock{m_};
66 op_.emplace(std::forward<
decltype(op)>(op));
73 std::unique_lock lock{m_};
74 ready_.wait(lock, [
this] {
return op_.has_value(); });
79 std::shared_ptr<State> state = std::make_shared<State>();
80 typename CtxType::Timer timer;
82 BasicScheduledOperation(
auto& executor,
auto delay,
auto&& fn)
86 [state = state, fn = std::forward<decltype(fn)>(fn)](auto ec) mutable {
87 state->emplace(fn(ec));
93 ~BasicScheduledOperation()
override
99 BasicScheduledOperation(BasicScheduledOperation
const&) =
default;
100 BasicScheduledOperation&
101 operator=(BasicScheduledOperation
const&) =
default;
102 BasicScheduledOperation(BasicScheduledOperation&&) =
default;
103 BasicScheduledOperation&
104 operator=(BasicScheduledOperation&&) =
default;
109 return state->get().get();
125 requestStop() noexcept
126 requires(SomeStoppableOperation<OpType>)
128 state->get().requestStop();
136 if constexpr (SomeStoppableOperation<OpType>)
149template <
typename RetType,
typename StopSourceType>
154 StopSourceType stopSource_;
163 : impl::BasicOperation<OutcomeType>(outcome), stopSource_(outcome->getStopSource())
173 StoppableOperation(StoppableOperation
const&) =
delete;
175 operator=(StoppableOperation
const&) =
delete;
176 StoppableOperation(StoppableOperation&&) =
default;
178 operator=(StoppableOperation&&) =
default;
184 stopSource_.requestStop();
193template <
typename RetType>
202template <
typename CtxType,
typename OpType>
214template <
typename CtxType>
217 std::function<void()> action_;
228 template <std::invocable FnType>
231 , action_([fn = std::forward<FnType>(fn), &executor] { boost::asio::post(executor, fn); })
233 repeat_.
start(interval, action_);
236 ~RepeatingOperation()
override
242 RepeatingOperation(RepeatingOperation
const&) =
delete;
244 operator=(RepeatingOperation
const&) =
delete;
245 RepeatingOperation(RepeatingOperation&&) =
default;
247 operator=(RepeatingOperation&&) =
default;
A base-class that can be used to check whether the current instance was moved from.
Definition MoveTracker.hpp:10
A class to repeat some action at a regular interval.
Definition Repeat.hpp:25
void start(std::chrono::steady_clock::duration interval, Action &&action)
Start asynchronously repeating.
Definition Repeat.hpp:76
void invoke()
Force-invoke the operation.
Definition Operation.hpp:267
RepeatingOperation(auto &executor, std::chrono::steady_clock::duration interval, FnType &&fn)
Construct a new Repeating Operation object.
Definition Operation.hpp:229
void abort() noexcept
Aborts the operation and the repeating timer.
Definition Operation.hpp:255
The future side of async operations that can be stopped.
Definition Operation.hpp:151
StoppableOperation(OutcomeType *outcome)
Construct a new Stoppable Operation object.
Definition Operation.hpp:162
void requestStop() noexcept
Requests the operation to stop.
Definition Operation.hpp:182
Stoppable outcome.
Definition Outcome.hpp:97
Definition Operation.hpp:26
Definition Operation.hpp:56
This namespace implements an async framework built on top of execution contexts.
Definition AnyExecutionContext.hpp:17
impl::BasicScheduledOperation< CtxType, OpType > ScheduledOperation
The future side of async operations that can be scheduled.
Definition Operation.hpp:203
impl::BasicOperation< Outcome< RetType > > Operation
The future side of async operations that cannot be stopped.
Definition Operation.hpp:194
Definition Operation.hpp:55