rclcpp  master
C++ ROS Client Library API
parameter_event_handler.hpp
Go to the documentation of this file.
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 
30 #include "rclcpp/parameter.hpp"
31 #include "rclcpp/qos.hpp"
32 #include "rclcpp/subscription.hpp"
34 #include "rcl_interfaces/msg/parameter_event.hpp"
35 
36 namespace rclcpp
37 {
38 
40 {
42 
43  using ParameterCallbackType = std::function<void (const rclcpp::Parameter &)>;
44 
46  std::string node_name;
48 };
49 
51 {
53 
55  std::function<void (const rcl_interfaces::msg::ParameterEvent &)>;
56 
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  {
175 
176  event_subscription_ = rclcpp::create_subscription<rcl_interfaces::msg::ParameterEvent>(
177  node_topics, "/parameter_events", qos,
178  std::bind(&ParameterEventHandler::event_callback, this, std::placeholders::_1));
179  }
180 
181  using ParameterEventCallbackType =
183 
185 
192  ParameterEventCallbackHandle::SharedPtr
193  add_parameter_event_callback(
195 
197 
201  void
202  remove_parameter_event_callback(
203  ParameterEventCallbackHandle::SharedPtr callback_handle);
204 
206 
208 
217  ParameterCallbackHandle::SharedPtr
218  add_parameter_callback(
219  const std::string & parameter_name,
221  const std::string & node_name = "");
222 
224 
232  void
233  remove_parameter_callback(
234  ParameterCallbackHandle::SharedPtr callback_handle);
235 
237 
248  static bool
249  get_parameter_from_event(
250  const rcl_interfaces::msg::ParameterEvent & event,
251  rclcpp::Parameter & parameter,
253  const std::string node_name = "");
254 
256 
269  static rclcpp::Parameter
270  get_parameter_from_event(
271  const rcl_interfaces::msg::ParameterEvent & event,
273  const std::string node_name = "");
274 
276 
282  get_parameters_from_event(
283  const rcl_interfaces::msg::ParameterEvent & event);
284 
286 
287 protected:
290  void
291  event_callback(const rcl_interfaces::msg::ParameterEvent & event);
292 
293  // Utility function for resolving node path.
294  std::string resolve_path(const std::string & path);
295 
296  // Node interface used for base functionality
298 
299  // *INDENT-OFF* Uncrustify doesn't handle indented public/private labels
300  // Hash function for string pair required in std::unordered_map
301  // See: https://stackoverflow.com/questions/35985960/c-why-is-boosthash-combine-the-best-way-to-combine-hash-values
303  {
304  public:
305  template<typename T>
306  inline void hash_combine(std::size_t & seed, const T & v) const
307  {
308  std::hash<T> hasher;
309  seed ^= hasher(v) + 0x9e3779b9 + (seed << 6) + (seed >> 2);
310  }
311 
312  inline size_t operator()(const std::pair<std::string, std::string> & s) const
313  {
314  size_t seed = 0;
315  hash_combine(seed, s.first);
316  hash_combine(seed, s.second);
317  return seed;
318  }
319  };
320  // *INDENT-ON*
321 
322  // Map container for registered parameters
325  CallbacksContainerType,
326  StringPairHash
328 
330 
332 
334 };
335 
336 } // namespace rclcpp
337 
338 #endif // RCLCPP__PARAMETER_EVENT_HANDLER_HPP_
get_node_topics_interface.hpp
rclcpp::ParameterEventHandler::StringPairHash::hash_combine
void hash_combine(std::size_t &seed, const T &v) const
Definition: parameter_event_handler.hpp:306
rclcpp::ParameterCallbackHandle
Definition: parameter_event_handler.hpp:39
rclcpp::ParameterEventCallbackHandle
Definition: parameter_event_handler.hpp:50
std::bind
T bind(T... args)
std::string
std::shared_ptr< rclcpp::node_interfaces::NodeBaseInterface >
rclcpp::ParameterEventHandler::StringPairHash
Definition: parameter_event_handler.hpp:302
std::list
rclcpp::node_interfaces::get_node_base_interface
std::shared_ptr< rclcpp::node_interfaces::NodeBaseInterface > get_node_base_interface(NodeType &&node)
Get the NodeBaseInterface as a shared pointer from a pointer to a "Node like" object.
Definition: get_node_base_interface.hpp:72
std::pair
std::vector< rclcpp::Parameter >
rclcpp::ParameterCallbackHandle::callback
ParameterCallbackType callback
Definition: parameter_event_handler.hpp:47
rclcpp::ParameterEventHandler
A class used to "handle" (monitor and respond to) changes to parameters.
Definition: parameter_event_handler.hpp:159
std::recursive_mutex
rclcpp::ParameterEventHandler::node_base_
std::shared_ptr< rclcpp::node_interfaces::NodeBaseInterface > node_base_
Definition: parameter_event_handler.hpp:297
std::function< void(const rclcpp::Parameter &)>
rclcpp::ParameterEventHandler::mutex_
std::recursive_mutex mutex_
Definition: parameter_event_handler.hpp:333
rclcpp::ParameterEventHandler::ParameterEventHandler
ParameterEventHandler(NodeT node, const rclcpp::QoS &qos=rclcpp::QoS(rclcpp::QoSInitialization::from_rmw(rmw_qos_profile_parameter_events)))
Construct a parameter events monitor.
Definition: parameter_event_handler.hpp:168
rclcpp
This header provides the get_node_base_interface() template function.
Definition: allocator_common.hpp:24
qos.hpp
rclcpp::Subscription
Subscription implementation, templated on the type of message this subscription receives.
Definition: subscription.hpp:69
node_topics_interface.hpp
RCLCPP_PUBLIC
#define RCLCPP_PUBLIC
Definition: visibility_control.hpp:50
rclcpp::QoS
Encapsulation of Quality of Service settings.
Definition: qos.hpp:110
RCLCPP_SMART_PTR_DEFINITIONS
#define RCLCPP_SMART_PTR_DEFINITIONS(...)
Definition: macros.hpp:36
rclcpp::ParameterCallbackHandle::ParameterCallbackType
std::function< void(const rclcpp::Parameter &)> ParameterCallbackType
Definition: parameter_event_handler.hpp:43
rclcpp::QoSInitialization::from_rmw
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.
rclcpp::ParameterEventCallbackHandle::callback
ParameterEventCallbackType callback
Definition: parameter_event_handler.hpp:57
rclcpp::ParameterCallbackHandle::parameter_name
std::string parameter_name
Definition: parameter_event_handler.hpp:45
node_base_interface.hpp
rclcpp::ParameterEventHandler::event_callbacks_
std::list< ParameterEventCallbackHandle::WeakPtr > event_callbacks_
Definition: parameter_event_handler.hpp:331
rclcpp::ParameterEventHandler::event_subscription_
rclcpp::Subscription< rcl_interfaces::msg::ParameterEvent >::SharedPtr event_subscription_
Definition: parameter_event_handler.hpp:329
create_subscription.hpp
rclcpp::ParameterEventHandler::event_callback
void event_callback(const rcl_interfaces::msg::ParameterEvent &event)
Callback for parameter events subscriptions.
get_node_base_interface.hpp
rclcpp::node_interfaces::get_node_topics_interface
std::shared_ptr< rclcpp::node_interfaces::NodeTopicsInterface > get_node_topics_interface(NodeType &&node)
Get the NodeTopicsInterface as a shared pointer from a pointer to a "Node like" object.
Definition: get_node_topics_interface.hpp:72
parameter.hpp
visibility_control.hpp
std
rclcpp::ParameterEventHandler::StringPairHash::operator()
size_t operator()(const std::pair< std::string, std::string > &s) const
Definition: parameter_event_handler.hpp:312
rclcpp::ParameterEventCallbackHandle::ParameterEventCallbackType
std::function< void(const rcl_interfaces::msg::ParameterEvent &)> ParameterEventCallbackType
Definition: parameter_event_handler.hpp:55
std::size_t
rclcpp::ParameterCallbackHandle::node_name
std::string node_name
Definition: parameter_event_handler.hpp:46
rclcpp::Parameter
Structure to store an arbitrary parameter with templated get/set methods.
Definition: parameter.hpp:52
rclcpp::ParameterEventHandler::parameter_callbacks_
std::unordered_map< std::pair< std::string, std::string >, CallbacksContainerType, StringPairHash > parameter_callbacks_
Definition: parameter_event_handler.hpp:327
std::unordered_map
subscription.hpp
std::hash