mrs_lib
Various reusable classes, functions and utilities for use in MRS projects
Loading...
Searching...
No Matches
coroutine_callback_helpers.hpp
1#ifndef MRS_LIB_INTERNAL_COROUTINE_CALLBACK_HELPERS_HPP_
2#define MRS_LIB_INTERNAL_COROUTINE_CALLBACK_HELPERS_HPP_
3
4#include <memory>
5#include <stdexcept>
6#include <string>
7#include <utility>
8
9#include <rclcpp/callback_group.hpp>
10#include <rclcpp/logger.hpp>
11#include <rclcpp/logging.hpp>
12
13#include "mrs_lib/coro/runners.hpp"
15
16
17namespace mrs_lib::internal
18{
19
30 inline bool is_callback_group_coro_compatible(const std::shared_ptr<rclcpp::CallbackGroup>& callback_group)
31 {
32 if (callback_group != nullptr)
33 {
34 return callback_group->type() == rclcpp::CallbackGroupType::Reentrant;
35 } else
36 {
37 return false;
38 }
39 }
40
51 inline void require_callback_group_coro_compatible(const std::shared_ptr<rclcpp::CallbackGroup>& callback_group)
52 {
53 if (!is_callback_group_coro_compatible(callback_group))
54 {
55 std::string msg = "Coroutine callbacks must be used with reentrant callback group.";
56 RCLCPP_ERROR(rclcpp::get_logger("mrs_lib"), "%s", msg.c_str());
57 throw std::logic_error(std::move(msg));
58 }
59 }
60
62 {
63 };
64
65 template <typename... Args>
66 requires((!std::is_reference_v<Args>) && ...)
67 auto get_detached_coro_callback_launcher(CoroCallback<void(Args...)> callback, std::shared_ptr<rclcpp::CallbackGroup> callback_group)
68 {
69 require_callback_group_coro_compatible(callback_group);
70 return get_detached_coro_callback_launcher(std::move(callback), NoCallbackGroupTag{});
71 }
72
73 template <typename... Args>
74 requires((!std::is_reference_v<Args>) && ...)
75 auto get_detached_coro_callback_launcher(CoroCallback<void(Args...)> callback, NoCallbackGroupTag)
76 {
77 return std::function([callback](Args... args) { coro::internal::start_task(callback, std::forward<Args>(args)...); });
78 }
79
80} // namespace mrs_lib::internal
81
82#endif // MRS_LIB_INTERNAL_COROUTINE_CALLBACK_HELPERS_HPP_
Base template for CoroCallback.
Definition callback.hpp:166
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.
Definition coroutine_callback_helpers.hpp:62