39 #include <type_traits>
41 #include <string_view>
64 const char* raw = other._storage.raw_string;
65 const uint32_t
size = *(
reinterpret_cast<const uint32_t*
>(&raw[0]));
66 const char*
data = (&raw[4]);
72 _storage.raw_data = other._storage.raw_data;
87 _storage.raw_data = other._storage.raw_data;
95 const char* raw = other._storage.raw_string;
96 const uint32_t
size = *(
reinterpret_cast<const uint32_t*
>(&raw[0]));
97 const char*
data = (&raw[4]);
103 _storage.raw_data = other._storage.raw_data;
108 template <
typename T>
112 Variant(
const char* buffer,
size_t length);
116 template <
typename T>
119 template <
typename T>
122 template <
typename T>
123 void assign(
const T& value);
125 void assign(
const char* buffer,
size_t length);
144 template <
typename T>
147 static_assert(std::numeric_limits<T>::is_specialized ||
148 std::is_same<T, RosMsgParser::Time>::value ||
149 std::is_same<T, std::string_view>::value ||
150 std::is_same<T, std::string>::value,
180 template <
typename T>
183 static_assert(std::numeric_limits<T>::is_specialized ||
184 std::is_same<T, RosMsgParser::Time>::value,
187 if (
_type != RosMsgParser::getType<T>())
191 return *
reinterpret_cast<const T*
>(&
_storage.raw_data[0]);
201 const uint32_t
size = *(
reinterpret_cast<const uint32_t*
>(&
_storage.raw_string[0]));
213 const uint32_t
size = *(
reinterpret_cast<const uint32_t*
>(&
_storage.raw_string[0]));
214 char*
data =
static_cast<char*
>(&
_storage.raw_string[4]);
220 template <
typename T>
223 static_assert(std::numeric_limits<T>::is_specialized ||
224 std::is_same<T, RosMsgParser::Time>::value,
228 _type = RosMsgParser::getType<T>();
229 *
reinterpret_cast<T*
>(&
_storage.raw_data[0]) = value;
247 *
reinterpret_cast<uint32_t*
>(&
_storage.raw_string[0]) =
size;
255 assign(value.data(), value.size());
261 assign(value.data(), value.size());
266 template <
typename DST>
269 static_assert(std::numeric_limits<DST>::is_specialized ||
270 std::is_same<DST, RosMsgParser::Time>::value,
282 convert_impl<int8_t, DST>(*
reinterpret_cast<const int8_t*
>(
raw_data), target);
286 convert_impl<int16_t, DST>(*
reinterpret_cast<const int16_t*
>(
raw_data), target);
289 convert_impl<int32_t, DST>(*
reinterpret_cast<const int32_t*
>(
raw_data), target);
292 convert_impl<int64_t, DST>(*
reinterpret_cast<const int64_t*
>(
raw_data), target);
298 convert_impl<uint8_t, DST>(*
reinterpret_cast<const uint8_t*
>(
raw_data), target);
302 convert_impl<uint16_t, DST>(*
reinterpret_cast<const uint16_t*
>(
raw_data), target);
305 convert_impl<uint32_t, DST>(*
reinterpret_cast<const uint32_t*
>(
raw_data), target);
308 convert_impl<uint64_t, DST>(*
reinterpret_cast<const uint64_t*
>(
raw_data), target);
312 convert_impl<float, DST>(*
reinterpret_cast<const float*
>(
raw_data), target);
315 convert_impl<double, DST>(*
reinterpret_cast<const double*
>(
raw_data), target);
319 throw TypeException(
"String will not be converted to a numerical value implicitly");
325 throw TypeException(
"ros::Duration and ros::Time can be converted only to double "
326 "(will be seconds)");
331 throw TypeException(
"Variant::convert -> cannot convert type" +
332 std::to_string(
_type));
349 convert_impl<int8_t, double>(*
reinterpret_cast<const int8_t*
>(
raw_data), target);
353 convert_impl<int16_t, double>(*
reinterpret_cast<const int16_t*
>(
raw_data), target);
356 convert_impl<int32_t, double>(*
reinterpret_cast<const int32_t*
>(
raw_data), target);
359 convert_impl<int64_t, double>(*
reinterpret_cast<const int64_t*
>(
raw_data), target);
365 convert_impl<uint8_t, double>(*
reinterpret_cast<const uint8_t*
>(
raw_data), target);
369 convert_impl<uint16_t, double>(*
reinterpret_cast<const uint16_t*
>(
raw_data),
373 convert_impl<uint32_t, double>(*
reinterpret_cast<const uint32_t*
>(
raw_data),
377 convert_impl<uint64_t, double>(*
reinterpret_cast<const uint64_t*
>(
raw_data),
382 convert_impl<float, double>(*
reinterpret_cast<const float*
>(
raw_data), target);
385 return extract<double>();
388 throw TypeException(
"String will not be converted to a double implicitly");
395 target = tmp.
toSec();
400 throw TypeException(
"Variant::convert -> cannot convert type" +
401 std::to_string(
_type));
411 throw TypeException(
"Variant::convert -> cannot convert RosMsgParser::Time");
413 return extract<RosMsgParser::Time>();
421 throw TypeException(
"Variant::convert -> cannot convert to std::string");
423 return extract<std::string>();