ROS 2 rclcpp + rcl - kilted  kilted
ROS 2 C++ Client Library with ROS Client Library
copy_all_parameter_values.hpp
1 // Copyright 2023 Open Navigation LLC
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__COPY_ALL_PARAMETER_VALUES_HPP_
16 #define RCLCPP__COPY_ALL_PARAMETER_VALUES_HPP_
17 
18 #include <string>
19 #include <vector>
20 
21 #include "rcl_interfaces/srv/list_parameters.hpp"
22 #include "rcl_interfaces/msg/parameter_descriptor.hpp"
23 #include "rcl_interfaces/msg/set_parameters_result.hpp"
24 
25 #include "rclcpp/parameter.hpp"
26 #include "rclcpp/logger.hpp"
27 #include "rclcpp/logging.hpp"
28 
29 namespace rclcpp
30 {
31 
40 template<typename NodeT1, typename NodeT2>
41 void
43  const NodeT1 & source, const NodeT2 & destination, const bool override_existing_params = false)
44 {
45  using Parameters = std::vector<rclcpp::Parameter>;
46  using Descriptions = std::vector<rcl_interfaces::msg::ParameterDescriptor>;
47  auto source_params = source->get_node_parameters_interface();
48  auto dest_params = destination->get_node_parameters_interface();
49  rclcpp::Logger logger = destination->get_node_logging_interface()->get_logger();
50 
51  std::vector<std::string> param_names = source_params->list_parameters({}, 0).names;
52  Parameters params = source_params->get_parameters(param_names);
53  Descriptions descriptions = source_params->describe_parameters(param_names);
54 
55  for (unsigned int idx = 0; idx != params.size(); idx++) {
56  if (!dest_params->has_parameter(params[idx].get_name())) {
57  dest_params->declare_parameter(
58  params[idx].get_name(), params[idx].get_parameter_value(), descriptions[idx]);
59  } else if (override_existing_params) {
60  try {
61  rcl_interfaces::msg::SetParametersResult result =
62  dest_params->set_parameters_atomically({params[idx]});
63  if (!result.successful) {
64  // Parameter update rejected or read-only
65  RCLCPP_WARN(
66  logger,
67  "Unable to set parameter (%s): %s!",
68  params[idx].get_name().c_str(), result.reason.c_str());
69  }
71  RCLCPP_WARN(
72  logger,
73  "Unable to set parameter (%s): incompatable parameter type (%s)!",
74  params[idx].get_name().c_str(), e.what());
75  }
76  }
77  }
78 }
79 
80 } // namespace rclcpp
81 
82 #endif // RCLCPP__COPY_ALL_PARAMETER_VALUES_HPP_
Thrown if requested parameter type is invalid.
Definition: exceptions.hpp:271
Versions of rosidl_typesupport_cpp::get_message_type_support_handle that handle adapted types.
void copy_all_parameter_values(const NodeT1 &source, const NodeT2 &destination, const bool override_existing_params=false)