ROS 2 rclcpp + rcl - rolling  rolling-29de98cf
ROS 2 C++ Client Library with ROS Client Library
parameter_value.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 "rclcpp/parameter_value.hpp"
16 
17 #include <sstream>
18 #include <string>
19 #include <vector>
20 
21 #include "rclcpp/exceptions/exceptions.hpp"
22 
23 using rclcpp::ParameterType;
25 
26 std::string
27 rclcpp::to_string(const ParameterType type)
28 {
29  switch (type) {
30  case ParameterType::PARAMETER_NOT_SET:
31  return "not set";
32  case ParameterType::PARAMETER_BOOL:
33  return "bool";
34  case ParameterType::PARAMETER_INTEGER:
35  return "integer";
36  case ParameterType::PARAMETER_DOUBLE:
37  return "double";
38  case ParameterType::PARAMETER_STRING:
39  return "string";
40  case ParameterType::PARAMETER_BYTE_ARRAY:
41  return "byte_array";
42  case ParameterType::PARAMETER_BOOL_ARRAY:
43  return "bool_array";
44  case ParameterType::PARAMETER_INTEGER_ARRAY:
45  return "integer_array";
46  case ParameterType::PARAMETER_DOUBLE_ARRAY:
47  return "double_array";
48  case ParameterType::PARAMETER_STRING_ARRAY:
49  return "string_array";
50  }
51 
52  return "unknown type";
53 }
54 
55 std::ostream &
56 rclcpp::operator<<(std::ostream & os, const ParameterType type)
57 {
58  os << rclcpp::to_string(type);
59  return os;
60 }
61 
62 template<typename ValType, typename PrintType = ValType>
63 std::string
64 array_to_string(
65  const std::vector<ValType> & array,
66  const std::ios::fmtflags format_flags = std::ios::dec)
67 {
68  std::stringstream type_array;
69  bool first_item = true;
70  type_array << "[";
71  type_array.setf(format_flags, std::ios_base::basefield | std::ios::boolalpha);
72  type_array << std::showbase;
73  for (const ValType & value : array) {
74  if (!first_item) {
75  type_array << ", ";
76  } else {
77  first_item = false;
78  }
79  type_array << static_cast<PrintType>(value);
80  }
81  type_array << "]";
82  return type_array.str();
83 }
84 
85 std::string
87 {
88  switch (value.get_type()) {
89  case ParameterType::PARAMETER_NOT_SET:
90  return "not set";
91  case ParameterType::PARAMETER_BOOL:
92  return value.get<bool>() ? "true" : "false";
93  case ParameterType::PARAMETER_INTEGER:
94  return std::to_string(value.get<int>());
95  case ParameterType::PARAMETER_DOUBLE:
96  return std::to_string(value.get<double>());
97  case ParameterType::PARAMETER_STRING:
98  return value.get<std::string>();
99  case ParameterType::PARAMETER_BYTE_ARRAY:
100  return array_to_string<uint8_t, int>(value.get<std::vector<uint8_t>>(), std::ios::hex);
101  case ParameterType::PARAMETER_BOOL_ARRAY:
102  return array_to_string(value.get<std::vector<bool>>(), std::ios::boolalpha);
103  case ParameterType::PARAMETER_INTEGER_ARRAY:
104  return array_to_string(value.get<std::vector<int64_t>>());
105  case ParameterType::PARAMETER_DOUBLE_ARRAY:
106  return array_to_string(value.get<std::vector<double>>());
107  case ParameterType::PARAMETER_STRING_ARRAY:
108  return array_to_string(value.get<std::vector<std::string>>());
109  }
110 
111  return "unknown type";
112 }
113 
114 ParameterValue::ParameterValue()
115 {
116  value_.type = rcl_interfaces::msg::ParameterType::PARAMETER_NOT_SET;
117 }
118 
119 ParameterValue::ParameterValue(const rcl_interfaces::msg::ParameterValue & value)
120 {
121  value_ = value;
122  switch (value.type) {
123  case PARAMETER_BOOL:
124  case PARAMETER_INTEGER:
125  case PARAMETER_DOUBLE:
126  case PARAMETER_STRING:
127  case PARAMETER_BYTE_ARRAY:
128  case PARAMETER_BOOL_ARRAY:
129  case PARAMETER_INTEGER_ARRAY:
130  case PARAMETER_DOUBLE_ARRAY:
131  case PARAMETER_STRING_ARRAY:
132  case PARAMETER_NOT_SET:
133  break;
134  default:
135  throw rclcpp::exceptions::UnknownTypeError(std::to_string(value.type));
136  }
137 }
138 
139 ParameterValue::ParameterValue(const bool bool_value)
140 {
141  value_.bool_value = bool_value;
142  value_.type = rcl_interfaces::msg::ParameterType::PARAMETER_BOOL;
143 }
144 
145 ParameterValue::ParameterValue(const int int_value)
146 {
147  value_.integer_value = int_value;
148  value_.type = rcl_interfaces::msg::ParameterType::PARAMETER_INTEGER;
149 }
150 
151 ParameterValue::ParameterValue(const int64_t int_value)
152 {
153  value_.integer_value = int_value;
154  value_.type = rcl_interfaces::msg::ParameterType::PARAMETER_INTEGER;
155 }
156 
157 ParameterValue::ParameterValue(const float double_value)
158 {
159  value_.double_value = static_cast<double>(double_value);
160  value_.type = rcl_interfaces::msg::ParameterType::PARAMETER_DOUBLE;
161 }
162 
163 ParameterValue::ParameterValue(const double double_value)
164 {
165  value_.double_value = double_value;
166  value_.type = rcl_interfaces::msg::ParameterType::PARAMETER_DOUBLE;
167 }
168 
169 ParameterValue::ParameterValue(const std::string & string_value)
170 {
171  value_.string_value = string_value;
172  value_.type = rcl_interfaces::msg::ParameterType::PARAMETER_STRING;
173 }
174 
175 ParameterValue::ParameterValue(const char * string_value)
176 : ParameterValue(std::string(string_value))
177 {}
178 
179 ParameterValue::ParameterValue(const std::vector<uint8_t> & byte_array_value)
180 {
181  value_.byte_array_value = byte_array_value;
182  value_.type = rcl_interfaces::msg::ParameterType::PARAMETER_BYTE_ARRAY;
183 }
184 
185 ParameterValue::ParameterValue(const std::vector<bool> & bool_array_value)
186 {
187  value_.bool_array_value = bool_array_value;
188  value_.type = rcl_interfaces::msg::ParameterType::PARAMETER_BOOL_ARRAY;
189 }
190 
191 ParameterValue::ParameterValue(const std::vector<int> & int_array_value)
192 {
193  value_.integer_array_value.assign(int_array_value.cbegin(), int_array_value.cend());
194  value_.type = rcl_interfaces::msg::ParameterType::PARAMETER_INTEGER_ARRAY;
195 }
196 
197 ParameterValue::ParameterValue(const std::vector<int64_t> & int_array_value)
198 {
199  value_.integer_array_value = int_array_value;
200  value_.type = rcl_interfaces::msg::ParameterType::PARAMETER_INTEGER_ARRAY;
201 }
202 
203 ParameterValue::ParameterValue(const std::vector<float> & float_array_value)
204 {
205  value_.double_array_value.assign(float_array_value.cbegin(), float_array_value.cend());
206  value_.type = rcl_interfaces::msg::ParameterType::PARAMETER_DOUBLE_ARRAY;
207 }
208 
209 ParameterValue::ParameterValue(const std::vector<double> & double_array_value)
210 {
211  value_.double_array_value = double_array_value;
212  value_.type = rcl_interfaces::msg::ParameterType::PARAMETER_DOUBLE_ARRAY;
213 }
214 
215 ParameterValue::ParameterValue(const std::vector<std::string> & string_array_value)
216 {
217  value_.string_array_value = string_array_value;
218  value_.type = rcl_interfaces::msg::ParameterType::PARAMETER_STRING_ARRAY;
219 }
220 
221 ParameterType
223 {
224  return static_cast<ParameterType>(value_.type);
225 }
226 
227 rcl_interfaces::msg::ParameterValue
229 {
230  return value_;
231 }
232 
233 bool
235 {
236  return this->value_ == rhs.value_;
237 }
238 
239 bool
241 {
242  return this->value_ != rhs.value_;
243 }
Store the type and value of a parameter.
RCLCPP_PUBLIC rcl_interfaces::msg::ParameterValue to_value_msg() const
Return a message populated with the parameter value.
RCLCPP_PUBLIC ParameterType get_type() const
Return an enum indicating the type of the set value.
RCLCPP_PUBLIC bool operator==(const ParameterValue &rhs) const
Equal operator.
RCLCPP_PUBLIC bool operator!=(const ParameterValue &rhs) const
Not equal operator.
RCLCPP_PUBLIC ParameterValue()
Construct a parameter value with type PARAMETER_NOT_SET.
Thrown when an unknown type is passed.
Definition: exceptions.hpp:220
RCLCPP_PUBLIC std::string to_string(const FutureReturnCode &future_return_code)
String conversion function for FutureReturnCode.
RCLCPP_PUBLIC std::ostream & operator<<(std::ostream &os, const FutureReturnCode &future_return_code)
Stream operator for FutureReturnCode.