rclcpp  master
C++ ROS Client Library API
parameter_value.hpp
Go to the documentation of this file.
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 #ifndef RCLCPP__PARAMETER_VALUE_HPP_
16 #define RCLCPP__PARAMETER_VALUE_HPP_
17 
18 #include <exception>
19 #include <iostream>
20 #include <ostream>
21 #include <sstream>
22 #include <string>
23 #include <vector>
24 
25 #include "rcl_interfaces/msg/parameter_type.hpp"
26 #include "rcl_interfaces/msg/parameter_value.hpp"
28 
29 namespace rclcpp
30 {
32 {
43 };
44 
48 to_string(const ParameterType type);
49 
52 operator<<(std::ostream & os, const ParameterType type);
53 
56 {
57 public:
59 
65  : std::runtime_error("expected [" + to_string(expected) + "] got [" + to_string(actual) + "]")
66  {}
67 };
68 
71 {
72 public:
78  explicit ParameterValue(const rcl_interfaces::msg::ParameterValue & value);
81  explicit ParameterValue(const bool bool_value);
84  explicit ParameterValue(const int int_value);
87  explicit ParameterValue(const int64_t int_value);
90  explicit ParameterValue(const float double_value);
93  explicit ParameterValue(const double double_value);
96  explicit ParameterValue(const std::string & string_value);
99  explicit ParameterValue(const char * string_value);
102  explicit ParameterValue(const std::vector<uint8_t> & byte_array_value);
105  explicit ParameterValue(const std::vector<bool> & bool_array_value);
108  explicit ParameterValue(const std::vector<int> & int_array_value);
111  explicit ParameterValue(const std::vector<int64_t> & int_array_value);
114  explicit ParameterValue(const std::vector<float> & double_array_value);
117  explicit ParameterValue(const std::vector<double> & double_array_value);
120  explicit ParameterValue(const std::vector<std::string> & string_array_value);
121 
125  get_type() const;
126 
129  rcl_interfaces::msg::ParameterValue
130  to_value_msg() const;
131 
132  // The following get() variants require the use of ParameterType
133 
134  template<ParameterType type>
136  get() const
137  {
140  }
141  return value_.bool_value;
142  }
143 
144  template<ParameterType type>
146  get() const
147  {
150  }
151  return value_.integer_value;
152  }
153 
154  template<ParameterType type>
156  get() const
157  {
160  }
161  return value_.double_value;
162  }
163 
164  template<ParameterType type>
166  get() const
167  {
170  }
171  return value_.string_value;
172  }
173 
174  template<ParameterType type>
175  typename std::enable_if<
177  get() const
178  {
181  }
182  return value_.byte_array_value;
183  }
184 
185  template<ParameterType type>
186  typename std::enable_if<
188  get() const
189  {
192  }
193  return value_.bool_array_value;
194  }
195 
196  template<ParameterType type>
197  typename std::enable_if<
199  get() const
200  {
203  }
204  return value_.integer_array_value;
205  }
206 
207  template<ParameterType type>
208  typename std::enable_if<
210  get() const
211  {
214  }
215  return value_.double_array_value;
216  }
217 
218  template<ParameterType type>
219  typename std::enable_if<
221  get() const
222  {
225  }
226  return value_.string_array_value;
227  }
228 
229  // The following get() variants allow the use of primitive types
230 
231  template<typename type>
233  get() const
234  {
235  return get<ParameterType::PARAMETER_BOOL>();
236  }
237 
238  template<typename type>
239  typename std::enable_if<
241  get() const
242  {
243  return get<ParameterType::PARAMETER_INTEGER>();
244  }
245 
246  template<typename type>
248  get() const
249  {
250  return get<ParameterType::PARAMETER_DOUBLE>();
251  }
252 
253  template<typename type>
255  get() const
256  {
257  return get<ParameterType::PARAMETER_STRING>();
258  }
259 
260  template<typename type>
261  typename std::enable_if<
263  type, const std::vector<uint8_t> &>::value, const std::vector<uint8_t> &>::type
264  get() const
265  {
266  return get<ParameterType::PARAMETER_BYTE_ARRAY>();
267  }
268 
269  template<typename type>
270  typename std::enable_if<
272  type, const std::vector<bool> &>::value, const std::vector<bool> &>::type
273  get() const
274  {
275  return get<ParameterType::PARAMETER_BOOL_ARRAY>();
276  }
277 
278  template<typename type>
279  typename std::enable_if<
281  type, const std::vector<int64_t> &>::value, const std::vector<int64_t> &>::type
282  get() const
283  {
284  return get<ParameterType::PARAMETER_INTEGER_ARRAY>();
285  }
286 
287  template<typename type>
288  typename std::enable_if<
290  type, const std::vector<double> &>::value, const std::vector<double> &>::type
291  get() const
292  {
293  return get<ParameterType::PARAMETER_DOUBLE_ARRAY>();
294  }
295 
296  template<typename type>
297  typename std::enable_if<
299  type, const std::vector<std::string> &>::value, const std::vector<std::string> &>::type
300  get() const
301  {
302  return get<ParameterType::PARAMETER_STRING_ARRAY>();
303  }
304 
305 private:
306  rcl_interfaces::msg::ParameterValue value_;
307 };
308 
312 to_string(const ParameterValue & type);
313 
314 } // namespace rclcpp
315 
316 #endif // RCLCPP__PARAMETER_VALUE_HPP_
Definition: parameter_value.hpp:34
Definition: parameter_value.hpp:41
Definition: parameter_value.hpp:42
std::string to_string(const ParameterType type)
Return the name of a parameter type.
Definition: allocator_common.hpp:24
Definition: parameter_value.hpp:39
ParameterType
Definition: parameter_value.hpp:31
Definition: parameter_value.hpp:36
Definition: parameter_value.hpp:40
#define RCLCPP_PUBLIC
Definition: visibility_control.hpp:50
Definition: parameter_value.hpp:37
ParameterTypeException(ParameterType expected, ParameterType actual)
Construct an instance.
Definition: parameter_value.hpp:64
Definition: parameter_value.hpp:33
Indicate the parameter type does not match the expected type.
Definition: parameter_value.hpp:55
Store the type and value of a parameter.
Definition: parameter_value.hpp:70
std::ostream & operator<<(std::ostream &os, const rclcpp::Parameter &pv)
Definition: parameter_value.hpp:38
Definition: parameter_value.hpp:35