39 #include <type_traits> 41 #include <string_view> 66 const uint32_t
size = *(
reinterpret_cast<const uint32_t*
>( &raw[0] ));
67 const char*
data = (&raw[4]);
86 _storage.raw_data = other._storage.raw_data;
95 const uint32_t
size = *(
reinterpret_cast<const uint32_t*
>( &raw[0] ));
96 const char*
data = (&raw[4]);
113 template<
typename T> T
convert( )
const;
115 template<
typename T> T
extract( )
const;
117 template <
typename T>
void assign(
const T& value);
119 void assign(
const char* buffer,
size_t length);
144 static_assert (std::numeric_limits<T>::is_specialized ||
145 std::is_same<T, RosMsgParser::Time>::value ||
146 std::is_same<T, std::string_view>::value ||
147 std::is_same<T, std::string>::value
148 ,
"not a valid type");
177 static_assert (std::numeric_limits<T>::is_specialized ||
178 std::is_same<T, RosMsgParser::Time>::value
179 ,
"not a valid type");
181 if(
_type != RosMsgParser::getType<T>() )
185 return *
reinterpret_cast<const T*
>( &
_storage.raw_data[0] );
195 const uint32_t
size = *(
reinterpret_cast<const uint32_t*
>( &
_storage.raw_string[0] ));
196 char*
data =
static_cast<char*
>(&
_storage.raw_string[4]);
206 const uint32_t
size = *(
reinterpret_cast<const uint32_t*
>( &
_storage.raw_string[0] ));
207 char*
data =
static_cast<char*
>(&
_storage.raw_string[4]);
208 return std::string(data, size);
215 static_assert (std::numeric_limits<T>::is_specialized ||
216 std::is_same<T, RosMsgParser::Time>::value
217 ,
"not a valid type");
220 _type = RosMsgParser::getType<T>() ;
221 *
reinterpret_cast<T *
>( &
_storage.raw_data[0] ) = value;
238 _storage.raw_string =
new char[size+5];
239 *
reinterpret_cast<uint32_t *
>( &
_storage.raw_string[0] ) = size;
240 memcpy(&
_storage.raw_string[4] , buffer, size );
248 assign( value.data(), value.size() );
253 assign( value.data(), value.size() );
260 static_assert (std::numeric_limits<DST>::is_specialized ||
261 std::is_same<DST, RosMsgParser::Time>::value
262 ,
"not a valid type");
272 case INT8: convert_impl<int8_t, DST>(*
reinterpret_cast<const int8_t*
>(
raw_data), target );
break;
274 case INT16: convert_impl<int16_t, DST>(*
reinterpret_cast<const int16_t*
>(
raw_data), target );
break;
275 case INT32: convert_impl<int32_t, DST>(*
reinterpret_cast<const int32_t*
>(
raw_data), target );
break;
276 case INT64: convert_impl<int64_t, DST>(*
reinterpret_cast<const int64_t*
>(
raw_data), target );
break;
280 case UINT8: convert_impl<uint8_t, DST>(*
reinterpret_cast<const uint8_t*
>(
raw_data), target );
break;
282 case UINT16: convert_impl<uint16_t, DST>(*
reinterpret_cast<const uint16_t*
>(
raw_data), target );
break;
283 case UINT32: convert_impl<uint32_t, DST>(*
reinterpret_cast<const uint32_t*
>(
raw_data), target );
break;
284 case UINT64: convert_impl<uint64_t, DST>(*
reinterpret_cast<const uint64_t*
>(
raw_data), target );
break;
286 case FLOAT32: convert_impl<float, DST>(*
reinterpret_cast<const float*
>(
raw_data), target );
break;
287 case FLOAT64: convert_impl<double, DST>(*
reinterpret_cast<const double*
>(
raw_data), target );
break;
290 throw TypeException(
"String will not be converted to a numerical value implicitly");
295 throw TypeException(
"ros::Duration and ros::Time can be converted only to double (will be seconds)");
298 default:
throw TypeException(
"Variant::convert -> cannot convert type" + std::to_string(
_type));
break;
313 case INT8: convert_impl<int8_t, double>(*
reinterpret_cast<const int8_t*
>(
raw_data), target );
break;
315 case INT16: convert_impl<int16_t, double>(*
reinterpret_cast<const int16_t*
>(
raw_data), target );
break;
316 case INT32: convert_impl<int32_t, double>(*
reinterpret_cast<const int32_t*
>(
raw_data), target );
break;
317 case INT64: convert_impl<int64_t, double>(*
reinterpret_cast<const int64_t*
>(
raw_data), target );
break;
321 case UINT8: convert_impl<uint8_t, double>(*
reinterpret_cast<const uint8_t*
>(
raw_data), target );
break;
323 case UINT16: convert_impl<uint16_t, double>(*
reinterpret_cast<const uint16_t*
>(
raw_data), target );
break;
324 case UINT32: convert_impl<uint32_t, double>(*
reinterpret_cast<const uint32_t*
>(
raw_data), target );
break;
325 case UINT64: convert_impl<uint64_t, double>(*
reinterpret_cast<const uint64_t*
>(
raw_data), target );
break;
327 case FLOAT32: convert_impl<float, double>(*
reinterpret_cast<const float*
>(
raw_data), target );
break;
328 case FLOAT64:
return extract<double>();
331 throw TypeException(
"String will not be converted to a double implicitly");
337 target = tmp.
toSec();
340 default:
throw TypeException(
"Variant::convert -> cannot convert type" + std::to_string(
_type));
350 throw TypeException(
"Variant::convert -> cannot convert RosMsgParser::Time");
352 return extract<RosMsgParser::Time>();
360 throw TypeException(
"Variant::convert -> cannot convert to std::string");
362 return extract<std::string>();
const uint8_t * getRawStorage() const
void assign(const T &value)
std::array< uint8_t, 8 > raw_data
basic_string_view< char > string_view
union RosMsgParser::Variant::@55 _storage
NLOHMANN_BASIC_JSON_TPL_DECLARATION void swap(nlohmann::NLOHMANN_BASIC_JSON_TPL &j1, nlohmann::NLOHMANN_BASIC_JSON_TPL &j2) noexcept(//NOLINT(readability-inconsistent-declaration-parameter-name) is_nothrow_move_constructible< nlohmann::NLOHMANN_BASIC_JSON_TPL >::value &&//NOLINT(misc-redundant-expression) is_nothrow_move_assignable< nlohmann::NLOHMANN_BASIC_JSON_TPL >::value)
exchanges the values of two JSON objects
Variant(const Variant &other)
Variant & operator=(const Variant &other)
void clearStringIfNecessary()
BuiltinType getTypeID() const
span_constexpr std::size_t size(span< T, Extent > const &spn)