ArrayField.cpp
Go to the documentation of this file.
5 #include <swarmio/data/Helper.h>
6 
7 using namespace swarmros;
8 using namespace swarmros::introspection;
9 
10 void ArrayField::WriteDefinition(std::stringstream& stream, bool forHash) const
11 {
12  // Amusingly, array definitions are ignored for hash calculations
13  // But only for complex types. Ouch.
14  // https://github.com/ros/genmsg/issues/50
15  if (forHash && dynamic_cast<const MessageSerializer*>(&_serializer) != nullptr)
16  {
17  Field::WriteDefinition(stream, forHash);
18  }
19  else
20  {
21  stream << _serializer.GetFullName() << "[] " << GetName();
22  }
23 }
24 
25 uint32_t ArrayField::GetDefaultLength(const FieldStack& fieldStack) const
26 {
27  return 4;
28 }
29 
30 uint32_t ArrayField::CalculateSerializedLength(const swarmio::data::Variant& value, const FieldStack& fieldStack) const
31 {
32  KeyedFieldStack current(fieldStack, GetName());
33  return 4 + _serializer.CalculateSerializedLength(value, current);
34 }
35 
36 void ArrayField::Serialize(ros::serialization::OStream& stream, const swarmio::data::Variant& value, const FieldStack& fieldStack) const
37 {
38  KeyedFieldStack current(fieldStack, GetName());
39  SerializeCount(stream, swarmio::data::Helper::GetCount(value), fieldStack);
40  _serializer.Serialize(stream, value, current);
41 }
42 
43 void ArrayField::SerializeCount(ros::serialization::OStream& stream, uint32_t count, const FieldStack& fieldStack) const
44 {
45  stream.next(count);
46 }
47 
48 void ArrayField::EmitDefault(ros::serialization::OStream& stream, const FieldStack& fieldStack) const
49 {
50  SerializeCount(stream, 0, fieldStack);
51 }
52 
53 swarmio::data::Variant ArrayField::Deserialize(ros::serialization::IStream& stream, const FieldStack& fieldStack) const
54 {
55  KeyedFieldStack current(fieldStack, GetName());
56  uint32_t count = DeserializeCount(stream, fieldStack);
57  return _serializer.DeserializeArray(stream, count, current);
58 }
59 
60 uint32_t ArrayField::DeserializeCount(ros::serialization::IStream& stream, const FieldStack& fieldStack) const
61 {
62  uint32_t count = 0;
63  stream.next(count);
64  return count;
65 }
66 
67 swarmio::data::discovery::Field ArrayField::GetFieldDescriptor() const
68 {
69  auto field = _serializer.GetFieldDescriptor();
70  field.set_is_variable_size(true);
71  return field;
72 }
virtual uint32_t CalculateSerializedLength(const swarmio::data::Variant &value, const FieldStack &fieldStack) const =0
Calculate the length of a serialized message in bytes.
virtual uint32_t GetDefaultLength(const FieldStack &fieldStack) const override
Get the length of the default value.
Definition: ArrayField.cpp:25
virtual swarmio::data::Variant DeserializeArray(ros::serialization::IStream &stream, uint32_t count, const FieldStack &fieldStack) const =0
Deserialize a stream into a variant array.
virtual uint32_t DeserializeCount(ros::serialization::IStream &stream, const FieldStack &fieldStack) const
Deserialize the count of the array from a stream.
Definition: ArrayField.cpp:60
ROS_FORCE_INLINE void next(T &t)
virtual swarmio::data::discovery::Field GetFieldDescriptor() const =0
Build a field descriptor for the underlying type.
static size_t GetCount(const Variant &value)
Get the item count of the variant array, or 1 if it is not an array.
Definition: Helper.cpp:26
virtual void WriteDefinition(std::stringstream &stream, bool forHash) const
Write the definition of this field to a string stream.
Definition: Field.cpp:12
virtual void Serialize(ros::serialization::OStream &stream, const swarmio::data::Variant &value, const FieldStack &fieldStack) const override
Serialize a variant onto a stream.
Definition: ArrayField.cpp:36
virtual const std::string & GetFullName() const
Get the fully qualified name for the type behind the serializer.
Definition: Serializer.h:77
virtual swarmio::data::discovery::Field GetFieldDescriptor() const override
Build a field descriptor for the underlying type.
Definition: ArrayField.cpp:67
virtual void SerializeCount(ros::serialization::OStream &stream, uint32_t count, const FieldStack &fieldStack) const
Serialize the count of the array onto a stream.
Definition: ArrayField.cpp:43
const Serializer & _serializer
Serializer.
Definition: Field.h:23
virtual swarmio::data::Variant Deserialize(ros::serialization::IStream &stream, const FieldStack &fieldStack) const override
Deserialize a stream into a variant.
Definition: ArrayField.cpp:53
const std::string & GetName() const
Get the non-qualified name for the type behind the serializer.
Definition: Serializer.h:66
ROS_FORCE_INLINE void next(const T &t)
virtual uint32_t CalculateSerializedLength(const swarmio::data::Variant &value, const FieldStack &fieldStack) const override
Calculate the length of a serialized message in bytes.
Definition: ArrayField.cpp:30
virtual void Serialize(ros::serialization::OStream &stream, const swarmio::data::Variant &value, const FieldStack &fieldStack) const =0
Serialize a variant onto a stream.
virtual void WriteDefinition(std::stringstream &stream, bool forHash) const override
Write the definition of this field to a string stream.
Definition: ArrayField.cpp:10
virtual void EmitDefault(ros::serialization::OStream &stream, const FieldStack &fieldStack) const override
Write the default value to the stream.
Definition: ArrayField.cpp:48


swarmros
Author(s):
autogenerated on Fri Apr 3 2020 03:42:47