ROS 2 rclcpp + rcl - kilted  kilted
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 
178 {
179 public:
181 
185  template<typename NodeT>
187  NodeT node,
188  const rclcpp::QoS & qos =
189  rclcpp::QoS(rclcpp::QoSInitialization::from_rmw(rmw_qos_profile_parameter_events)))
190  : node_base_(rclcpp::node_interfaces::get_node_base_interface(node))
191  {
192  auto node_topics = rclcpp::node_interfaces::get_node_topics_interface(node);
193 
194  callbacks_ = std::make_shared<Callbacks>();
195 
196  event_subscription_ = rclcpp::create_subscription<rcl_interfaces::msg::ParameterEvent>(
197  node_topics, "/parameter_events", qos,
198  [callbacks = callbacks_](const rcl_interfaces::msg::ParameterEvent & event) {
199  callbacks->event_callback(event);
200  });
201  }
202 
203  using ParameterEventCallbackType =
204  ParameterEventCallbackHandle::ParameterEventCallbackType;
205 
207 
216  RCLCPP_PUBLIC
217  RCUTILS_WARN_UNUSED
218  ParameterEventCallbackHandle::SharedPtr
220  ParameterEventCallbackType callback);
221 
223 
226  RCLCPP_PUBLIC
227  void
229  ParameterEventCallbackHandle::SharedPtr callback_handle);
230 
231  using ParameterCallbackType = ParameterCallbackHandle::ParameterCallbackType;
232 
234 
246  RCLCPP_PUBLIC
247  RCUTILS_WARN_UNUSED
248  ParameterCallbackHandle::SharedPtr
250  const std::string & parameter_name,
251  ParameterCallbackType callback,
252  const std::string & node_name = "");
253 
255 
262  RCLCPP_PUBLIC
263  void
265  ParameterCallbackHandle::SharedPtr callback_handle);
266 
268 
278  RCLCPP_PUBLIC
279  static bool
281  const rcl_interfaces::msg::ParameterEvent & event,
282  rclcpp::Parameter & parameter,
283  const std::string & parameter_name,
284  const std::string & node_name = "");
285 
287 
300  RCLCPP_PUBLIC
301  static rclcpp::Parameter
303  const rcl_interfaces::msg::ParameterEvent & event,
304  const std::string & parameter_name,
305  const std::string & node_name = "");
306 
308 
312  RCLCPP_PUBLIC
313  static std::vector<rclcpp::Parameter>
315  const rcl_interfaces::msg::ParameterEvent & event);
316 
317  using CallbacksContainerType = std::list<ParameterCallbackHandle::WeakPtr>;
318 
319 protected:
320  // *INDENT-OFF* Uncrustify doesn't handle indented public/private labels
321  // Hash function for string pair required in std::unordered_map
322  // See: https://stackoverflow.com/questions/35985960/c-why-is-boosthash-combine-the-best-way-to-combine-hash-values
324  {
325  public:
326  template<typename T>
327  inline void hash_combine(std::size_t & seed, const T & v) const
328  {
329  std::hash<T> hasher;
330  seed ^= hasher(v) + 0x9e3779b9 + (seed << 6) + (seed >> 2);
331  }
332 
333  inline size_t operator()(const std::pair<std::string, std::string> & s) const
334  {
335  size_t seed = 0;
336  hash_combine(seed, s.first);
337  hash_combine(seed, s.second);
338  return seed;
339  }
340  };
341  // *INDENT-ON*
342 
343  struct Callbacks
344  {
345  std::recursive_mutex mutex_;
346 
347  // Map container for registered parameters
348  std::unordered_map<
349  std::pair<std::string, std::string>,
350  CallbacksContainerType,
352  > parameter_callbacks_;
353 
354  std::list<ParameterEventCallbackHandle::WeakPtr> event_callbacks_;
355 
357  RCLCPP_PUBLIC
358  void
359  event_callback(const rcl_interfaces::msg::ParameterEvent & event);
360  };
361 
362  std::shared_ptr<Callbacks> callbacks_;
363 
364  // Utility function for resolving node path.
365  std::string resolve_path(const std::string & path);
366 
367  // Node interface used for base functionality
368  std::shared_ptr<rclcpp::node_interfaces::NodeBaseInterface> node_base_;
369 
371 };
372 
373 } // namespace rclcpp
374 
375 #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