3#ifndef MRS_LIB_UTILITY_CALLBACK_HPP_
4#define MRS_LIB_UTILITY_CALLBACK_HPP_
16#include "mrs_lib/coro/task.hpp"
17#include "mrs_lib/utility/meta.hpp"
18#include "mrs_lib/utility/scope_cleanup.hpp"
36 explicit Reentrant() =
default;
60 template <
typename CallbackRetType =
void>
68 explicit CancelNew(std::function<CallbackRetType()> default_factory) : default_factory_(std::move(default_factory))
78 return default_factory_();
82 std::function<CallbackRetType()> default_factory_;
108 template <
typename F>
140 template <
typename T>
141 requires std::default_initializable<T>
156 template <
typename CallbackRetType>
164 template <
typename...>
167 static_assert(
false,
"Base template selected for CoroCallback");
185 template <
typename CallbackRetType,
typename... CallbackArgs>
207 template <
typename F,
typename... BoundArgs>
208 requires((std::is_member_function_pointer_v<F> || (std::is_pointer_v<F> && std::is_function_v<std::remove_pointer_t<F>>))
209 && std::invocable<F, const std::decay_t<BoundArgs>&..., CallbackArgs...>
210 && std::same_as<coro::Task<CallbackRetType>, std::invoke_result_t<F, const std::decay_t<BoundArgs>&..., CallbackArgs...>>)
212 : func_(bind_callback(std::move(reentrant_policy), callback_ptr, std::forward<BoundArgs>(args)...))
224 co_return co_await std::invoke(func_, std::forward<CallbackArgs>(args)...);
236 template <
typename F>
239 return std::forward<F>(func);
250 template <
typename F>
253 return [running = std::make_shared<std::atomic_bool>(
false), cancel_new_policy = std::move(cancel_new_policy),
254 func = std::forward<F>(func)](CallbackArgs... args) -> coro::Task<CallbackRetType> {
255 bool was_running = running->exchange(
true);
259 ScopeCleanup cleanup([&] { running->store(
false); });
260 co_return co_await func(std::forward<CallbackArgs>(args)...);
263 co_return cancel_new_policy.create_value();
278 template <
typename F,
typename... BoundArgs>
281 if (callback_ptr ==
nullptr)
283 throw std::logic_error(
"Cannot construct CoroCallback from nullptr.");
286 auto bound_callback = [callback = std::move(callback_ptr),
287 ... pre_args = std::forward<BoundArgs>(args)](CallbackArgs... args) -> coro::Task<CallbackRetType> {
288 co_return co_await std::invoke(callback, pre_args..., std::forward<CallbackArgs>(args)...);
292 [&](
auto&& reentrant_policy) -> std::function<FunctionSignature> {
293 return wrap_callback(std::forward<
decltype(reentrant_policy)>(reentrant_policy), std::move(bound_callback));
298 std::function<FunctionSignature> func_;
307 template <
typename...>
316 template <
typename Ret,
typename... Params,
typename... Args>
320 template <
typename... InnerParams>
321 using FuncSignature = Ret(InnerParams...);
333 template <
typename Ret,
typename Params,
typename Args>
341 template <
typename R,
typename... Params,
typename... Args,
bool Noexcept>
348 template <
typename C1,
typename C2,
typename R,
typename... Params,
typename... Args,
bool Noexcept>
355 template <
typename C1,
typename C2,
typename R,
typename... Params,
typename... Args,
bool Noexcept>
CallbackSignature< Ret, Params, Args >::type CallbackSignatureT
Helper type alias for CallbackSignature<Ret, Params, Args>::type.
Definition callback.hpp:334
CoroCallback(CoroReentrantPolicyVariant< CallbackRetType > reentrant_policy, F callback_ptr, BoundArgs &&... args)
Constructor for binding coroutine callbacks.
Definition callback.hpp:211
coro::Task< CallbackRetType > operator()(CallbackArgs... args) const
Executes the callback.
Definition callback.hpp:222
Base template for CoroCallback.
Definition callback.hpp:166
Task type for creating coroutines.
Definition task.hpp:268
All mrs_lib functions, classes, variables and definitions are contained in this namespace.
Definition attitude_converter.h:24
std::variant< coro_callback_tags::Reentrant, coro_callback_tags::CancelNew< CallbackRetType > > CoroReentrantPolicyVariant
Variant of CoroCallback reentrancy policies.
Definition callback.hpp:157
CoroCallback(auto, mrs_lib::Task< R >(*)(Params...) noexcept(Noexcept), Args &&...) -> CoroCallback< internal::CallbackSignatureT< R, meta::TypeList< Params... >, meta::TypeList< Args... > > >
Deduction guide for free functions.
Type trait to determine the signature of a callback function.
Definition callback.hpp:308