6 #include <boost/numeric/conversion/cast.hpp> 15 auto targetType = swarmio::data::Variant::ValueCase::VALUE_NOT_SET;
16 switch (targetPrimitiveType)
19 targetType = swarmio::data::Variant::ValueCase::kBoolValue;
24 targetType = swarmio::data::Variant::ValueCase::kDoubleValue;
32 targetType = swarmio::data::Variant::ValueCase::kIntValue;
40 targetType = swarmio::data::Variant::ValueCase::kUintValue;
44 targetType = swarmio::data::Variant::ValueCase::kStringValue;
48 throw Exception(
"Unknown primitive type");
53 template<
typename T,
typename O>
58 stream.
next((T)boost::numeric_cast<T>(value));
60 catch (
const boost::numeric::bad_numeric_cast&)
68 stream.
next((uint8_t)(value ? 1 : 0));
73 uint32_t size = value.size();
74 SerializeAs<uint32_t>(stream, size, fieldStack);
75 memcpy(stream.advance(size), value.c_str(), size);
81 int32_t secs = value / 1000000000;
82 SerializeAs<int32_t>(stream, secs, fieldStack);
85 int32_t nsecs = -(-value % 1000000000);
86 SerializeAs<int32_t>(stream, nsecs, fieldStack);
90 int32_t nsecs = value % 1000000000;
91 SerializeAs<int32_t>(stream, nsecs, fieldStack);
105 uint32_t secs = value / 1000000000;
106 SerializeAs<uint32_t>(stream, secs, fieldStack);
107 uint32_t nsecs = value % 1000000000;
108 SerializeAs<uint32_t>(stream, nsecs, fieldStack);
117 SerializeAs<int8_t>(stream, value, fieldStack);
121 SerializeAs<int16_t>(stream, value, fieldStack);
125 SerializeAs<int32_t>(stream, value, fieldStack);
129 SerializeAs<int64_t>(stream, value, fieldStack);
133 SerializeAs<uint8_t>(stream, value, fieldStack);
137 SerializeAs<uint16_t>(stream, value, fieldStack);
141 SerializeAs<uint32_t>(stream, value, fieldStack);
145 SerializeAs<uint64_t>(stream, value, fieldStack);
167 SerializeAs<uint8_t>(stream, value, fieldStack);
171 SerializeAs<uint16_t>(stream, value, fieldStack);
175 SerializeAs<uint32_t>(stream, value, fieldStack);
179 SerializeAs<uint64_t>(stream, value, fieldStack);
183 SerializeAs<int8_t>(stream, value, fieldStack);
187 SerializeAs<int16_t>(stream, value, fieldStack);
191 SerializeAs<int32_t>(stream, value, fieldStack);
195 SerializeAs<int64_t>(stream, value, fieldStack);
231 SerializeAs<float>(stream, value, fieldStack);
235 SerializeAs<double>(stream, value, fieldStack);
263 switch (value.value_case())
265 case swarmio::data::Variant::ValueCase::kBoolArray:
266 for (
const auto e : value.bool_array().elements())
273 case swarmio::data::Variant::ValueCase::kIntArray:
274 for (
const auto e : value.int_array().elements())
281 case swarmio::data::Variant::ValueCase::kUintArray:
282 for (
const auto e : value.uint_array().elements())
289 case swarmio::data::Variant::ValueCase::kDoubleArray:
290 for (
const auto e : value.double_array().elements())
297 case swarmio::data::Variant::ValueCase::kStringArray:
298 for (
const auto& e : value.string_array().elements())
312 switch (value.value_case())
314 case swarmio::data::Variant::ValueCase::kBoolValue:
318 case swarmio::data::Variant::ValueCase::kIntValue:
322 case swarmio::data::Variant::ValueCase::kUintValue:
326 case swarmio::data::Variant::ValueCase::kDoubleValue:
330 case swarmio::data::Variant::ValueCase::kStringValue:
354 return value ?
true :
false;
360 uint32_t length = DeserializeAs<uint32_t>(stream);
361 return std::string((
const char*)stream.
advance(length), length);
366 uint32_t secs = DeserializeAs<uint32_t>(stream);
367 uint32_t nsecs = DeserializeAs<uint32_t>(stream);
368 return secs * 1000000000 + nsecs;
373 int32_t secs = DeserializeAs<int32_t>(stream);
374 int32_t nsecs = DeserializeAs<int32_t>(stream);
375 return secs * 1000000000 + nsecs;
380 swarmio::data::Variant value;
384 value.set_bool_value(DeserializeAs<bool>(stream));
388 value.set_int_value(DeserializeAs<int8_t>(stream));
392 value.set_uint_value(DeserializeAs<uint8_t>(stream));
396 value.set_int_value(DeserializeAs<int16_t>(stream));
400 value.set_uint_value(DeserializeAs<uint16_t>(stream));
404 value.set_int_value(DeserializeAs<int32_t>(stream));
412 value.set_uint_value(DeserializeAs<uint32_t>(stream));
420 value.set_int_value(DeserializeAs<int64_t>(stream));
424 value.set_uint_value(DeserializeAs<uint64_t>(stream));
428 value.set_double_value(DeserializeAs<float>(stream));
432 value.set_double_value(DeserializeAs<double>(stream));
436 value.set_string_value(DeserializeAs<std::string>(stream));
440 throw Exception(
"Unknown primitive type");
447 swarmio::data::Variant value;
450 for (uint32_t i = 0; i < count; ++i)
455 value.mutable_bool_array()->add_elements(DeserializeAs<bool>(stream));
459 value.mutable_int_array()->add_elements(DeserializeAs<int8_t>(stream));
463 value.mutable_uint_array()->add_elements(DeserializeAs<uint8_t>(stream));
467 value.mutable_int_array()->add_elements(DeserializeAs<int16_t>(stream));
471 value.mutable_uint_array()->add_elements(DeserializeAs<uint16_t>(stream));
475 value.mutable_int_array()->add_elements(DeserializeAs<int32_t>(stream));
483 value.mutable_uint_array()->add_elements(DeserializeAs<uint32_t>(stream));
491 value.mutable_int_array()->add_elements(DeserializeAs<int64_t>(stream));
495 value.mutable_uint_array()->add_elements(DeserializeAs<uint64_t>(stream));
499 value.mutable_double_array()->add_elements(DeserializeAs<float>(stream));
503 value.mutable_double_array()->add_elements(DeserializeAs<double>(stream));
507 value.mutable_string_array()->add_elements(DeserializeAs<std::string>(stream));
511 throw Exception(
"Unknown primitive type");
520 value.mutable_bool_array();
524 value.mutable_int_array();
528 value.mutable_uint_array();
532 value.mutable_int_array();
536 value.mutable_uint_array();
540 value.mutable_int_array();
544 value.mutable_int_array();
548 value.mutable_uint_array();
552 value.mutable_uint_array();
556 value.mutable_int_array();
560 value.mutable_uint_array();
564 value.mutable_double_array();
568 value.mutable_double_array();
572 value.mutable_string_array();
576 throw Exception(
"Unknown primitive type");
609 throw Exception(
"Unknown primitive type");
617 swarmio::data::Variant::ValueCase expectedType;
621 expectedType = swarmio::data::Variant::ValueCase::kBoolValue;
626 expectedType = swarmio::data::Variant::ValueCase::kIntValue;
631 expectedType = swarmio::data::Variant::ValueCase::kUintValue;
636 expectedType = swarmio::data::Variant::ValueCase::kIntValue;
641 expectedType = swarmio::data::Variant::ValueCase::kUintValue;
646 expectedType = swarmio::data::Variant::ValueCase::kIntValue;
651 expectedType = swarmio::data::Variant::ValueCase::kUintValue;
656 expectedType = swarmio::data::Variant::ValueCase::kDoubleValue;
662 expectedType = swarmio::data::Variant::ValueCase::kIntValue;
668 expectedType = swarmio::data::Variant::ValueCase::kUintValue;
673 expectedType = swarmio::data::Variant::ValueCase::kDoubleValue;
678 expectedType = swarmio::data::Variant::ValueCase::kStringValue;
683 throw Exception(
"Unknown primitive type");
688 if (baseType == swarmio::data::Variant::ValueCase::kUintValue && expectedType == swarmio::data::Variant::ValueCase::kIntValue)
690 baseType = expectedType;
692 else if (baseType == swarmio::data::Variant::ValueCase::kIntValue && expectedType == swarmio::data::Variant::ValueCase::kUintValue)
694 baseType = expectedType;
696 if (expectedType != baseType)
699 "Unexpected source type for PrimitiveSerializer",
712 length = length * value.string_array().elements_size();
715 for (
const auto& element : value.string_array().elements())
717 length += element.size();
723 length += value.string_value().size();
739 SerializeAs<bool>(stream,
false, fieldStack);
743 SerializeAs<int8_t>(stream, 0, fieldStack);
747 SerializeAs<int16_t>(stream, 0, fieldStack);
751 SerializeAs<int32_t>(stream, 0, fieldStack);
755 SerializeAs<int32_t>(stream, 0, fieldStack);
756 SerializeAs<int32_t>(stream, 0, fieldStack);
760 SerializeAs<int64_t>(stream, 0, fieldStack);
764 SerializeAs<uint8_t>(stream, 0, fieldStack);
768 SerializeAs<uint16_t>(stream, 0, fieldStack);
772 SerializeAs<uint32_t>(stream, 0, fieldStack);
776 SerializeAs<uint32_t>(stream, 0, fieldStack);
777 SerializeAs<uint32_t>(stream, 0, fieldStack);
781 SerializeAs<uint64_t>(stream, 0, fieldStack);
785 SerializeAs<float>(stream, 0.0, fieldStack);
789 SerializeAs<double>(stream, 0.0, fieldStack);
797 throw Exception(
"Unknown primitive type");
803 swarmio::data::discovery::Field field;
815 field.set_type(swarmio::data::discovery::INT);
823 field.set_type(swarmio::data::discovery::UINT);
828 field.set_type(swarmio::data::discovery::DOUBLE);
836 throw Exception(
"Unknown primitive type");
static Variant::ValueCase GetBaseType(const Variant &value)
Get the base type of the variant array, or its type if it is not an array.
static void SerializeAsTime(ros::serialization::OStream &stream, O value, const FieldStack &fieldStack)
Exception thrown when the schema does not match the variant.
virtual swarmio::data::Variant Deserialize(ros::serialization::IStream &stream, const FieldStack &fieldStack) const override
Deserialize a stream into a variant.
PrimitiveType
Values correspond to builtin types of the ROS msg specification.
int64_t DeserializeAsDuration(ros::serialization::IStream &stream)
ROS_FORCE_INLINE void next(T &t)
Exception base class for swarmros exceptions.
ROS_FORCE_INLINE uint8_t * advance(uint32_t len)
virtual void Serialize(ros::serialization::OStream &stream, const swarmio::data::Variant &value, const FieldStack &fieldStack) const override
Serialize a variant onto a stream.
static void SerializeAsDuration(ros::serialization::OStream &stream, O value, const FieldStack &fieldStack)
virtual std::string GetLocation() const =0
Construct the current location.
static void SerializeAsBool(ros::serialization::OStream &stream, bool value, const FieldStack &fieldStack)
virtual void EmitDefault(ros::serialization::OStream &stream, const FieldStack &fieldStack) const override
Write the default value to the stream.
static size_t GetCount(const Variant &value)
Get the item count of the variant array, or 1 if it is not an array.
uint64_t DeserializeAsTime(ros::serialization::IStream &stream)
static bool IsArray(const Variant &value)
Check whether the variant contains an array type.
virtual uint32_t GetDefaultLength(const FieldStack &fieldStack) const override
Get the length of the default value.
static void SerializeAsString(ros::serialization::OStream &stream, const std::string &value, const FieldStack &fieldStack)
static void ThrowTypeMismatchException(swarmio::data::Variant::ValueCase sourceType, PrimitiveType targetPrimitiveType, const FieldStack &fieldStack)
virtual swarmio::data::discovery::Field GetFieldDescriptor() const override
Build a field descriptor for the underlying type.
Exception thrown when the expected type of the Variant does not match the actual type.
virtual uint32_t CalculateSerializedLength(const swarmio::data::Variant &value, const FieldStack &fieldStack) const override
Calculate the length of a serialized message in bytes.
static void SerializeAs(ros::serialization::OStream &stream, O value, const FieldStack &fieldStack)
virtual swarmio::data::Variant DeserializeArray(ros::serialization::IStream &stream, uint32_t count, const FieldStack &fieldStack) const override
Deserialize a stream into a variant array.
ROS_FORCE_INLINE void next(const T &t)
static T DeserializeAs(ros::serialization::IStream &stream)