FixedArrayField.cpp
Go to the documentation of this file.
6 #include <swarmio/data/Helper.h>
7 
8 using namespace swarmros;
9 using namespace swarmros::introspection;
10 
11 void FixedArrayField::WriteDefinition(std::stringstream& stream, bool forHash) const
12 {
13  // Amusingly, array definitions are ignored for hash calculations
14  // But only for complex types. Ouch.
15  // https://github.com/ros/genmsg/issues/50
16  if (forHash && dynamic_cast<const MessageSerializer*>(&_serializer) != nullptr)
17  {
18  Field::WriteDefinition(stream, forHash);
19  }
20  else
21  {
22  stream << _serializer.GetFullName() << "[" << _count << "] " << GetName();
23  }
24 }
25 
26 uint32_t FixedArrayField::GetDefaultLength(const FieldStack& fieldStack) const
27 {
28  KeyedFieldStack current(fieldStack, GetName());
29  return _serializer.GetDefaultLength(fieldStack) * _count;
30 }
31 
32 uint32_t FixedArrayField::CalculateSerializedLength(const swarmio::data::Variant& value, const FieldStack& fieldStack) const
33 {
34  // Check array count
35  uint32_t count = swarmio::data::Helper::GetCount(value);
36  if (count != _count)
37  {
39  "Invalid count for FixedArrayField",
40  fieldStack.GetLocation(),
41  _count,
42  count);
43  }
44 
45  // Calculate as if it was a dynamic array but subtract
46  // the 4 bytes of count that would preceed it
47  return ArrayField::CalculateSerializedLength(value, fieldStack) - 4;
48 }
49 
50 void FixedArrayField::SerializeCount(ros::serialization::OStream& stream, uint32_t count, const FieldStack& fieldStack) const
51 {
52  if (count != _count)
53  {
55  "Invalid count for FixedArrayField",
56  fieldStack.GetLocation(),
57  _count,
58  count);
59  }
60 }
61 
63 {
64  IndexedFieldStack current(fieldStack);
65  for (uint32_t i = 0; i < _count; ++i)
66  {
67  _serializer.EmitDefault(stream, current);
68  ++current;
69  }
70 }
71 
73 {
74  return _count;
75 }
76 
77 swarmio::data::discovery::Field FixedArrayField::GetFieldDescriptor() const
78 {
79  auto field = _serializer.GetFieldDescriptor();
80  field.set_fixed_size(_count);
81  return field;
82 }
virtual uint32_t DeserializeCount(ros::serialization::IStream &stream, const FieldStack &fieldStack) const override
Deserialize the count of the array from a stream.
Exception thrown when the count of a fixed array does not match the count of the variant array...
virtual swarmio::data::discovery::Field GetFieldDescriptor() const =0
Build a field descriptor for the underlying type.
virtual std::string GetLocation() const =0
Construct the current location.
virtual swarmio::data::discovery::Field GetFieldDescriptor() const override
Build a field descriptor for the underlying type.
virtual void SerializeCount(ros::serialization::OStream &stream, uint32_t count, const FieldStack &fieldStack) const override
Serialize the count of the array onto a stream.
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 EmitDefault(ros::serialization::OStream &stream, const FieldStack &fieldStack) const =0
Write the default value to the stream.
virtual uint32_t GetDefaultLength(const FieldStack &fieldStack) const override
Get the length of the default value.
virtual const std::string & GetFullName() const
Get the fully qualified name for the type behind the serializer.
Definition: Serializer.h:77
virtual uint32_t CalculateSerializedLength(const swarmio::data::Variant &value, const FieldStack &fieldStack) const override
Calculate the length of a serialized message in bytes.
virtual void WriteDefinition(std::stringstream &stream, bool forHash) const override
Write the definition of this field to a string stream.
const Serializer & _serializer
Serializer.
Definition: Field.h:23
virtual void EmitDefault(ros::serialization::OStream &stream, const FieldStack &fieldStack) const override
Write the default value to the stream.
const std::string & GetName() const
Get the non-qualified name for the type behind the serializer.
Definition: Serializer.h:66
virtual uint32_t GetDefaultLength(const FieldStack &fieldStack) const =0
Get the length of the default value.
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


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