ROS 2 rclcpp + rcl - jazzy  jazzy
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 
166 {
167 public:
169 
173  template<typename NodeT>
175  NodeT node,
176  const rclcpp::QoS & qos =
177  rclcpp::QoS(rclcpp::QoSInitialization::from_rmw(rmw_qos_profile_parameter_events)))
178  : node_base_(rclcpp::node_interfaces::get_node_base_interface(node))
179  {
180  auto node_topics = rclcpp::node_interfaces::get_node_topics_interface(node);
181 
182  callbacks_ = std::make_shared<Callbacks>();
183 
184  event_subscription_ = rclcpp::create_subscription<rcl_interfaces::msg::ParameterEvent>(
185  node_topics, "/parameter_events", qos,
186  [callbacks = callbacks_](const rcl_interfaces::msg::ParameterEvent & event) {
187  callbacks->event_callback(event);
188  });
189  }
190 
191  using ParameterEventCallbackType =
192  ParameterEventCallbackHandle::ParameterEventCallbackType;
193 
195 
204  RCLCPP_PUBLIC
205  RCUTILS_WARN_UNUSED
206  ParameterEventCallbackHandle::SharedPtr
208  ParameterEventCallbackType callback);
209 
211 
214  RCLCPP_PUBLIC
215  void
217  ParameterEventCallbackHandle::SharedPtr callback_handle);
218 
219  using ParameterCallbackType = ParameterCallbackHandle::ParameterCallbackType;
220 
222 
234  RCLCPP_PUBLIC
235  RCUTILS_WARN_UNUSED
236  ParameterCallbackHandle::SharedPtr
238  const std::string & parameter_name,
239  ParameterCallbackType callback,
240  const std::string & node_name = "");
241 
243 
250  RCLCPP_PUBLIC
251  void
253  ParameterCallbackHandle::SharedPtr callback_handle);
254 
256 
266  RCLCPP_PUBLIC
267  static bool
269  const rcl_interfaces::msg::ParameterEvent & event,
270  rclcpp::Parameter & parameter,
271  const std::string & parameter_name,
272  const std::string & node_name = "");
273 
275 
288  RCLCPP_PUBLIC
289  static rclcpp::Parameter
291  const rcl_interfaces::msg::ParameterEvent & event,
292  const std::string & parameter_name,
293  const std::string & node_name = "");
294 
296 
300  RCLCPP_PUBLIC
301  static std::vector<rclcpp::Parameter>
303  const rcl_interfaces::msg::ParameterEvent & event);
304 
305  using CallbacksContainerType = std::list<ParameterCallbackHandle::WeakPtr>;
306 
307 protected:
308  // *INDENT-OFF* Uncrustify doesn't handle indented public/private labels
309  // Hash function for string pair required in std::unordered_map
310  // See: https://stackoverflow.com/questions/35985960/c-why-is-boosthash-combine-the-best-way-to-combine-hash-values
312  {
313  public:
314  template<typename T>
315  inline void hash_combine(std::size_t & seed, const T & v) const
316  {
317  std::hash<T> hasher;
318  seed ^= hasher(v) + 0x9e3779b9 + (seed << 6) + (seed >> 2);
319  }
320 
321  inline size_t operator()(const std::pair<std::string, std::string> & s) const
322  {
323  size_t seed = 0;
324  hash_combine(seed, s.first);
325  hash_combine(seed, s.second);
326  return seed;
327  }
328  };
329  // *INDENT-ON*
330 
331  struct Callbacks
332  {
333  std::recursive_mutex mutex_;
334 
335  // Map container for registered parameters
336  std::unordered_map<
337  std::pair<std::string, std::string>,
338  CallbacksContainerType,
340  > parameter_callbacks_;
341 
342  std::list<ParameterEventCallbackHandle::WeakPtr> event_callbacks_;
343 
345  RCLCPP_PUBLIC
346  void
347  event_callback(const rcl_interfaces::msg::ParameterEvent & event);
348  };
349 
350  std::shared_ptr<Callbacks> callbacks_;
351 
352  // Utility function for resolving node path.
353  std::string resolve_path(const std::string & path);
354 
355  // Node interface used for base functionality
356  std::shared_ptr<rclcpp::node_interfaces::NodeBaseInterface> node_base_;
357 
359 };
360 
361 } // namespace rclcpp
362 
363 #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 RCUTILS_WARN_UNUSED ParameterEventCallbackHandle::SharedPtr add_parameter_event_callback(ParameterEventCallbackType callback)
Set a callback for all parameter events.
RCLCPP_PUBLIC RCUTILS_WARN_UNUSED 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:116
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:63