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