ROS 2 rclcpp + rcl - humble  humble
ROS 2 C++ Client Library with ROS Client Library
resolve_parameter_overrides.cpp
1 // Copyright 2021 Open Source Robotics Foundation, Inc.
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 #include "resolve_parameter_overrides.hpp"
16 
17 #include <string>
18 #include <map>
19 #include <vector>
20 
21 #include "rcl_yaml_param_parser/parser.h"
22 #include "rcpputils/scope_exit.hpp"
23 
24 #include "rclcpp/parameter_map.hpp"
25 
26 std::map<std::string, rclcpp::ParameterValue>
27 rclcpp::detail::resolve_parameter_overrides(
28  const std::string & node_fqn,
29  const std::vector<rclcpp::Parameter> & parameter_overrides,
30  const rcl_arguments_t * local_args,
31  const rcl_arguments_t * global_args)
32 {
33  std::map<std::string, rclcpp::ParameterValue> result;
34 
35  // global before local so that local overwrites global
36  std::array<const rcl_arguments_t *, 2> argument_sources = {global_args, local_args};
37 
38  // Get fully qualified node name post-remapping to use to find node's params in yaml files
39 
40  for (const rcl_arguments_t * source : argument_sources) {
41  if (!source) {
42  continue;
43  }
44  rcl_params_t * params = NULL;
45  rcl_ret_t ret = rcl_arguments_get_param_overrides(source, &params);
46  if (RCL_RET_OK != ret) {
47  rclcpp::exceptions::throw_from_rcl_error(ret);
48  }
49  if (params) {
50  auto cleanup_params = rcpputils::make_scope_exit(
51  [params]() {
52  rcl_yaml_node_struct_fini(params);
53  });
54  rclcpp::ParameterMap initial_map = rclcpp::parameter_map_from(params, node_fqn.c_str());
55 
56  if (initial_map.count(node_fqn) > 0) {
57  // Combine parameter yaml files, overwriting values in older ones
58  for (const rclcpp::Parameter & param : initial_map.at(node_fqn)) {
59  result[param.get_name()] =
60  rclcpp::ParameterValue(param.get_value_message());
61  }
62  }
63  }
64  }
65 
66  // parameter overrides passed to constructor will overwrite overrides from yaml file sources
67  for (auto & param : parameter_overrides) {
68  result[param.get_name()] =
69  rclcpp::ParameterValue(param.get_value_message());
70  }
71  return result;
72 }
RCL_PUBLIC RCL_WARN_UNUSED rcl_ret_t rcl_arguments_get_param_overrides(const rcl_arguments_t *arguments, rcl_params_t **parameter_overrides)
Return all parameter overrides parsed from the command line.
Store the type and value of a parameter.
Structure to store an arbitrary parameter with templated get/set methods.
Definition: parameter.hpp:53
std::unordered_map< std::string, std::vector< Parameter > > ParameterMap
A map of fully qualified node names to a list of parameters.
RCLCPP_PUBLIC ParameterMap parameter_map_from(const rcl_params_t *const c_params)
Hold output of parsing command line arguments.
Definition: arguments.h:36
#define RCL_RET_OK
Success return code.
Definition: types.h:26
rmw_ret_t rcl_ret_t
The type that holds an rcl return code.
Definition: types.h:23