Callback wrappers
This page contains documentation for CoroCallback and related classes.
The CoroCallback is a wrapper used for passing coroutines as callbacks to other interfaces in mrs_lib. Since coroutines do not work well with mutually exclusive callback groups, it also provides policies for handling concurrent calls to the same callback.
mrs_lib::CoroCallback
-
template<typename CallbackRetType, typename ...CallbackArgs>
class CoroCallback<CallbackRetType(CallbackArgs...)> Wrapper for safe binding of coroutines to use as callbacks.
This wrapper is used to bind arguments to coroutines and pass them to other interfaces that will use this as a callback.
Since coroutines do not work well with mutually exclusive callback group, this wrapper adds some functionality to configure behavior of reentrant callbacks. The behavior is chosen using policy argument passed in the constructor.
- Template Parameters:
CallbackRetType – Return type of the callback coroutine.
CallbackArgs – Argument types of the callback coroutine.
Public Functions
-
template<typename F, typename ...BoundArgs>
inline explicit CoroCallback(CoroReentrantPolicyVariant<CallbackRetType> reentrant_policy, F callback_ptr, BoundArgs&&... args) Constructor for binding coroutine callbacks.
Constructs the callback by binding
argsto the front parameters of callback.The constructed callback will have reentrant behavior according to the specified policy.
See also
- Template Parameters:
F – Callback function type.
BoundArgs – Types of arguments to bind to the callback function.
- Parameters:
reentrant_policy – Reentrancy policy for the callback.
callback_ptr – Callback function pointer (or member function pointer).
args – Arguments to bind to the callback function.
-
inline coro::Task<CallbackRetType> operator()(CallbackArgs... args) const
Executes the callback.
- Parameters:
args – Arguments to pass to the callback.
- Returns:
The result of the callback execution.
-
template<typename...>
class CoroCallback Base template for CoroCallback.
Policies
-
template<typename CallbackRetType>
using mrs_lib::CoroReentrantPolicyVariant = std::variant<coro_callback_tags::Reentrant, coro_callback_tags::CancelNew<CallbackRetType>> Variant of CoroCallback reentrancy policies.
- Template Parameters:
CallbackRetType – Return type of the callback function.
-
namespace coro_callback_tags
Namespace for tag types used in CoroCallback.
Note
All of the policy tags are in the mrs_lib::coro_callback_tags namespace.
Example
1mrs_lib::Task<int> add(int a, int b)
2{
3 co_return a + b;
4}
5
6class MyNode : public rclcpp::Node
7{
8public:
9 using rclcpp::Node::Node;
10
11 mrs_lib::CoroCallback<int(int)> create_callback()
12 {
13 using mrs_lib::coro_callback_tags::Reentrant;
14 // Binding to methods of nodes. A callback like this would be typically
15 // passed to mrs_lib ros wrappers (timer, subscriber, ...).
16 return mrs_lib::CoroCallback(Reentrant{}, &MyNode::calculate, this);
17 }
18
19 mrs_lib::Task<int> calculate(int x)
20 {
21 co_return x + val_;
22 }
23
24private:
25 int val_ = 10;
26};
1using mrs_lib::CoroCallback;
2using mrs_lib::coro_callback_tags::CancelNew;
3using mrs_lib::coro_callback_tags::CancelNewDefault;
4using mrs_lib::coro_callback_tags::Reentrant;
5
6// Reentrant callbacks
7// No argument bound
8CoroCallback<int(int, int)> callback1 = CoroCallback(Reentrant{}, &add);
9// First argument bound
10CoroCallback<int(int)> callback2 = CoroCallback(Reentrant{}, &add, 1);
11
12// Cancel new callback
13// This callback will return default constructed int (0) if cancelled by the policy
14CoroCallback<int(int)> callback3 = CoroCallback(CancelNewDefault{}, &add, 1);
15// This callback will return 42 if cancelled by the policy
16CoroCallback<int(int)> callback4 = CoroCallback(CancelNew([] { return 42; }), &add, 1);