ROS 2 rclcpp + rcl - humble  humble
ROS 2 C++ Client Library with ROS Client Library
parameter_event_handler.hpp
1 // Copyright 2019 Intel Corporation
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #ifndef RCLCPP__PARAMETER_EVENT_HANDLER_HPP_
16 #define RCLCPP__PARAMETER_EVENT_HANDLER_HPP_
17 
18 #include <list>
19 #include <memory>
20 #include <string>
21 #include <unordered_map>
22 #include <utility>
23 #include <vector>
24 
25 #include "rclcpp/create_subscription.hpp"
26 #include "rclcpp/node_interfaces/get_node_base_interface.hpp"
27 #include "rclcpp/node_interfaces/get_node_topics_interface.hpp"
28 #include "rclcpp/node_interfaces/node_base_interface.hpp"
29 #include "rclcpp/node_interfaces/node_topics_interface.hpp"
30 #include "rclcpp/parameter.hpp"
31 #include "rclcpp/qos.hpp"
32 #include "rclcpp/subscription.hpp"
33 #include "rclcpp/visibility_control.hpp"
34 #include "rcl_interfaces/msg/parameter_event.hpp"
35 
36 namespace rclcpp
37 {
38 
40 {
41  RCLCPP_SMART_PTR_DEFINITIONS(ParameterCallbackHandle)
42 
43  using ParameterCallbackType = std::function<void (const rclcpp::Parameter &)>;
44 
45  std::string parameter_name;
46  std::string node_name;
47  ParameterCallbackType callback;
48 };
49 
51 {
52  RCLCPP_SMART_PTR_DEFINITIONS(ParameterEventCallbackHandle)
53 
54  using ParameterEventCallbackType =
55  std::function<void (const rcl_interfaces::msg::ParameterEvent &)>;
56 
57  ParameterEventCallbackType callback;
58 };
59 
61 
160 {
161 public:
163 
167  template<typename NodeT>
169  NodeT node,
170  const rclcpp::QoS & qos =
171  rclcpp::QoS(rclcpp::QoSInitialization::from_rmw(rmw_qos_profile_parameter_events)))
172  : node_base_(rclcpp::node_interfaces::get_node_base_interface(node))
173  {
174  auto node_topics = rclcpp::node_interfaces::get_node_topics_interface(node);
175 
176  callbacks_ = std::make_shared<Callbacks>();
177 
178  event_subscription_ = rclcpp::create_subscription<rcl_interfaces::msg::ParameterEvent>(
179  node_topics, "/parameter_events", qos,
180  [callbacks = callbacks_](const rcl_interfaces::msg::ParameterEvent & event) {
181  callbacks->event_callback(event);
182  });
183  }
184 
185  using ParameterEventCallbackType =
186  ParameterEventCallbackHandle::ParameterEventCallbackType;
187 
189 
195  RCLCPP_PUBLIC
196  ParameterEventCallbackHandle::SharedPtr
198  ParameterEventCallbackType callback);
199 
201 
204  RCLCPP_PUBLIC
205  void
207  ParameterEventCallbackHandle::SharedPtr callback_handle);
208 
209  using ParameterCallbackType = ParameterCallbackHandle::ParameterCallbackType;
210 
212 
220  RCLCPP_PUBLIC
221  ParameterCallbackHandle::SharedPtr
223  const std::string & parameter_name,
224  ParameterCallbackType callback,
225  const std::string & node_name = "");
226 
228 
235  RCLCPP_PUBLIC
236  void
238  ParameterCallbackHandle::SharedPtr callback_handle);
239 
241 
251  RCLCPP_PUBLIC
252  static bool
254  const rcl_interfaces::msg::ParameterEvent & event,
255  rclcpp::Parameter & parameter,
256  const std::string & parameter_name,
257  const std::string & node_name = "");
258 
260 
272  RCLCPP_PUBLIC
273  static rclcpp::Parameter
275  const rcl_interfaces::msg::ParameterEvent & event,
276  const std::string & parameter_name,
277  const std::string & node_name = "");
278 
280 
284  RCLCPP_PUBLIC
285  static std::vector<rclcpp::Parameter>
287  const rcl_interfaces::msg::ParameterEvent & event);
288 
289  using CallbacksContainerType = std::list<ParameterCallbackHandle::WeakPtr>;
290 
291 protected:
292  // *INDENT-OFF* Uncrustify doesn't handle indented public/private labels
293  // Hash function for string pair required in std::unordered_map
294  // See: https://stackoverflow.com/questions/35985960/c-why-is-boosthash-combine-the-best-way-to-combine-hash-values
296  {
297  public:
298  template<typename T>
299  inline void hash_combine(std::size_t & seed, const T & v) const
300  {
301  std::hash<T> hasher;
302  seed ^= hasher(v) + 0x9e3779b9 + (seed << 6) + (seed >> 2);
303  }
304 
305  inline size_t operator()(const std::pair<std::string, std::string> & s) const
306  {
307  size_t seed = 0;
308  hash_combine(seed, s.first);
309  hash_combine(seed, s.second);
310  return seed;
311  }
312  };
313  // *INDENT-ON*
314 
315  struct Callbacks
316  {
317  std::recursive_mutex mutex_;
318 
319  // Map container for registered parameters
320  std::unordered_map<
321  std::pair<std::string, std::string>,
322  CallbacksContainerType,
324  > parameter_callbacks_;
325 
326  std::list<ParameterEventCallbackHandle::WeakPtr> event_callbacks_;
327 
329  RCLCPP_PUBLIC
330  void
331  event_callback(const rcl_interfaces::msg::ParameterEvent & event);
332  };
333 
334  std::shared_ptr<Callbacks> callbacks_;
335 
336  // Utility function for resolving node path.
337  std::string resolve_path(const std::string & path);
338 
339  // Node interface used for base functionality
340  std::shared_ptr<rclcpp::node_interfaces::NodeBaseInterface> node_base_;
341 
343 };
344 
345 } // namespace rclcpp
346 
347 #endif // RCLCPP__PARAMETER_EVENT_HANDLER_HPP_
A class used to "handle" (monitor and respond to) changes to parameters.
RCLCPP_PUBLIC void remove_parameter_callback(ParameterCallbackHandle::SharedPtr callback_handle)
Remove a parameter callback registered with add_parameter_callback.
static RCLCPP_PUBLIC bool get_parameter_from_event(const rcl_interfaces::msg::ParameterEvent &event, rclcpp::Parameter &parameter, const std::string &parameter_name, const std::string &node_name="")
Get an rclcpp::Parameter from a parameter event.
static RCLCPP_PUBLIC std::vector< rclcpp::Parameter > get_parameters_from_event(const rcl_interfaces::msg::ParameterEvent &event)
Get all rclcpp::Parameter values from a parameter event.
RCLCPP_PUBLIC void remove_parameter_event_callback(ParameterEventCallbackHandle::SharedPtr callback_handle)
Remove parameter event callback registered with add_parameter_event_callback.
ParameterEventHandler(NodeT node, const rclcpp::QoS &qos=rclcpp::QoS(rclcpp::QoSInitialization::from_rmw(rmw_qos_profile_parameter_events)))
Construct a parameter events monitor.
RCLCPP_PUBLIC ParameterEventCallbackHandle::SharedPtr add_parameter_event_callback(ParameterEventCallbackType callback)
Set a callback for all parameter events.
RCLCPP_PUBLIC ParameterCallbackHandle::SharedPtr add_parameter_callback(const std::string &parameter_name, ParameterCallbackType callback, const std::string &node_name="")
Add a callback for a specified parameter.
Structure to store an arbitrary parameter with templated get/set methods.
Definition: parameter.hpp:53
Encapsulation of Quality of Service settings.
Definition: qos.hpp:111
Subscription implementation, templated on the type of message this subscription receives.
Versions of rosidl_typesupport_cpp::get_message_type_support_handle that handle adapted types.
RCLCPP_PUBLIC void event_callback(const rcl_interfaces::msg::ParameterEvent &event)
Callback for parameter events subscriptions.
static QoSInitialization from_rmw(const rmw_qos_profile_t &rmw_qos)
Create a QoSInitialization from an existing rmw_qos_profile_t, using its history and depth.
Definition: qos.cpp:51