ROS 2 rclcpp + rcl - rolling  rolling-20536064
ROS 2 C++ Client Library with ROS Client Library
parameter_map.cpp
1 // Copyright 2018 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 <string>
16 #include <regex>
17 #include <vector>
18 
19 #include "rcpputils/find_and_replace.hpp"
20 #include "rcpputils/scope_exit.hpp"
21 #include "rcl_yaml_param_parser/parser.h"
22 #include "rclcpp/parameter_map.hpp"
23 
28 
29 static bool is_node_name_matched(const std::string & node_name, const char * node_fqn)
30 {
31  // Update the regular expression ["/*" -> "(/\\w+)" and "/**" -> "(/\\w+)*"]
32  std::string regex = rcpputils::find_and_replace(node_name, "/*", "(/\\w+)");
33  return std::regex_match(node_fqn, std::regex(regex));
34 }
35 
37 rclcpp::parameter_map_from(const rcl_params_t * const c_params, const char * node_fqn)
38 {
39  if (NULL == c_params) {
40  throw InvalidParametersException("parameters struct is NULL");
41  } else if (NULL == c_params->node_names) {
42  throw InvalidParametersException("node names array is NULL");
43  } else if (NULL == c_params->params) {
44  throw InvalidParametersException("node params array is NULL");
45  }
46 
47  // Convert c structs into a list of parameters to set
48  ParameterMap parameters;
49  for (size_t n = 0; n < c_params->num_nodes; ++n) {
50  const char * c_node_name = c_params->node_names[n];
51  if (NULL == c_node_name) {
52  throw InvalidParametersException("Node name at index " + std::to_string(n) + " is NULL");
53  }
54 
56  std::string node_name("/");
57  if ('/' != c_node_name[0]) {
58  node_name += c_node_name;
59  } else {
60  node_name = c_node_name;
61  }
62 
63  if (node_fqn) {
64  if (!is_node_name_matched(node_name, node_fqn)) {
65  // No need to parse the items because the user just care about node_fqn
66  continue;
67  }
68 
69  node_name = node_fqn;
70  }
71 
72  const rcl_node_params_t * const c_params_node = &(c_params->params[n]);
73 
74  std::vector<Parameter> & params_node = parameters[node_name];
75  params_node.reserve(c_params_node->num_params);
76 
77  for (size_t p = 0; p < c_params_node->num_params; ++p) {
78  const char * const c_param_name = c_params_node->parameter_names[p];
79  if (NULL == c_param_name) {
80  std::string message(
81  "At node " + std::to_string(n) + " parameter " + std::to_string(p) + " name is NULL");
82  throw InvalidParametersException(message);
83  }
84  const rcl_variant_t * const c_param_value = &(c_params_node->parameter_values[p]);
85  ParameterValue value;
86  try {
87  value = parameter_value_from(c_param_value);
88  } catch (const InvalidParameterValueException & e) {
90  std::string("parameter_value_from failed for parameter '") +
91  c_param_name + "': " + e.what());
92  }
93  params_node.emplace_back(c_param_name, value);
94  }
95  }
96 
97  return parameters;
98 }
99 
101 rclcpp::parameter_value_from(const rcl_variant_t * const c_param_value)
102 {
103  if (NULL == c_param_value) {
104  throw InvalidParameterValueException("Passed argument is NULL");
105  }
106  if (c_param_value->bool_value) {
107  return ParameterValue(*(c_param_value->bool_value));
108  } else if (c_param_value->integer_value) {
109  return ParameterValue(*(c_param_value->integer_value));
110  } else if (c_param_value->double_value) {
111  return ParameterValue(*(c_param_value->double_value));
112  } else if (c_param_value->string_value) {
113  return ParameterValue(std::string(c_param_value->string_value));
114  } else if (c_param_value->byte_array_value) {
115  const rcl_byte_array_t * const byte_array = c_param_value->byte_array_value;
116  std::vector<uint8_t> bytes;
117  bytes.reserve(byte_array->size);
118  for (size_t v = 0; v < byte_array->size; ++v) {
119  bytes.push_back(byte_array->values[v]);
120  }
121  return ParameterValue(bytes);
122  } else if (c_param_value->bool_array_value) {
123  const rcl_bool_array_t * const bool_array = c_param_value->bool_array_value;
124  std::vector<bool> bools;
125  bools.reserve(bool_array->size);
126  for (size_t v = 0; v < bool_array->size; ++v) {
127  bools.push_back(bool_array->values[v]);
128  }
129  return ParameterValue(bools);
130  } else if (c_param_value->integer_array_value) {
131  const rcl_int64_array_t * const int_array = c_param_value->integer_array_value;
132  std::vector<int64_t> integers;
133  integers.reserve(int_array->size);
134  for (size_t v = 0; v < int_array->size; ++v) {
135  integers.push_back(int_array->values[v]);
136  }
137  return ParameterValue(integers);
138  } else if (c_param_value->double_array_value) {
139  const rcl_double_array_t * const double_array = c_param_value->double_array_value;
140  std::vector<double> doubles;
141  doubles.reserve(double_array->size);
142  for (size_t v = 0; v < double_array->size; ++v) {
143  doubles.push_back(double_array->values[v]);
144  }
145  return ParameterValue(doubles);
146  } else if (c_param_value->string_array_value) {
147  const rcutils_string_array_t * const string_array = c_param_value->string_array_value;
148  std::vector<std::string> strings;
149  strings.reserve(string_array->size);
150  for (size_t v = 0; v < string_array->size; ++v) {
151  strings.emplace_back(string_array->data[v]);
152  }
153  return ParameterValue(strings);
154  }
155 
157  "Invalid parameter value: rcl parameter structure"
158  "Contains no value");
159 }
160 
161 ParameterMap
162 rclcpp::parameter_map_from_yaml_file(const std::string & yaml_filename, const char * node_fqn)
163 {
164  rcutils_allocator_t allocator = rcutils_get_default_allocator();
165  rcl_params_t * rcl_parameters = rcl_yaml_node_struct_init(allocator);
166  RCPPUTILS_SCOPE_EXIT(rcl_yaml_node_struct_fini(rcl_parameters); );
167  const char * path = yaml_filename.c_str();
168  if (!rcl_parse_yaml_file(path, rcl_parameters)) {
169  rclcpp::exceptions::throw_from_rcl_error(RCL_RET_ERROR);
170  }
171 
172  return rclcpp::parameter_map_from(rcl_parameters, node_fqn);
173 }
174 
175 std::vector<rclcpp::Parameter>
176 rclcpp::parameters_from_map(const ParameterMap & parameter_map, const char * node_fqn)
177 {
178  std::vector<rclcpp::Parameter> parameters;
179  std::string node_name_old;
180  for (auto & [node_name, node_parameters] : parameter_map) {
181  if (node_fqn && !is_node_name_matched(node_name, node_fqn)) {
182  // No need to parse the items because the user just care about node_fqn
183  continue;
184  }
185  parameters.insert(parameters.end(), node_parameters.begin(), node_parameters.end());
186  }
187 
188  return parameters;
189 }
Store the type and value of a parameter.
Thrown if passed parameter value is invalid.
Definition: exceptions.hpp:260
Thrown if passed parameters are inconsistent or invalid.
Definition: exceptions.hpp:252
RCLCPP_PUBLIC ParameterMap parameter_map_from_yaml_file(const std::string &yaml_filename, const char *node_fqn=nullptr)
RCLCPP_PUBLIC std::vector< Parameter > parameters_from_map(const ParameterMap &parameter_map, const char *node_fqn=nullptr)
RCLCPP_PUBLIC ParameterMap parameter_map_from(const rcl_params_t *const c_params, const char *node_fqn=nullptr)
RCLCPP_PUBLIC ParameterValue parameter_value_from(const rcl_variant_t *const c_value)
std::unordered_map< std::string, std::vector< Parameter > > ParameterMap
A map of fully qualified node names to a list of parameters.
#define RCL_RET_ERROR
Unspecified error return code.
Definition: types.h:29