Field.cpp
Go to the documentation of this file.
6 #include <swarmros/Exception.h>
7 #include <swarmio/data/Helper.h>
8 
9 using namespace swarmros;
10 using namespace swarmros::introspection;
11 
12 void Field::WriteDefinition(std::stringstream& stream, bool forHash) const
13 {
14  WriteTypeName(stream, forHash);
15  stream << " " << GetName();
16 }
17 
18 void Field::WriteTypeName(std::stringstream& stream, bool forHash) const
19 {
20  if (forHash)
21  {
22  const auto messageSerializer = dynamic_cast<const MessageSerializer*>(&_serializer);
23  if (messageSerializer != nullptr)
24  {
25  stream << messageSerializer->GetHash();
26  }
27  else
28  {
29  stream << _serializer.GetName();
30  }
31  }
32  else
33  {
34  stream << _serializer.GetFullName();
35  }
36 }
37 
38 uint32_t Field::GetDefaultLength(const FieldStack& fieldStack) const
39 {
40  KeyedFieldStack current(fieldStack, GetName());
41  return _serializer.GetDefaultLength(current);
42 }
43 
44 uint32_t Field::CalculateSerializedLength(const swarmio::data::Variant& value, const FieldStack& fieldStack) const
45 {
47  {
48  KeyedFieldStack current(fieldStack, GetName());
49  return _serializer.CalculateSerializedLength(value, current);
50  }
51  else
52  {
53  throw SchemaMismatchException("Array found where scalar value was expected", fieldStack.GetLocation());
54  }
55 }
56 
57 void Field::Serialize(ros::serialization::OStream& stream, const swarmio::data::Variant& value, const FieldStack& fieldStack) const
58 {
60  {
61  KeyedFieldStack current(fieldStack, GetName());
62  _serializer.Serialize(stream, value, current);
63  }
64  else
65  {
66  throw SchemaMismatchException("Array found where scalar value was expected", fieldStack.GetLocation());
67  }
68 }
69 
70 void Field::EmitDefault(ros::serialization::OStream& stream, const FieldStack& fieldStack) const
71 {
72  KeyedFieldStack current(fieldStack, GetName());
73  _serializer.EmitDefault(stream, current);
74 }
75 
76 swarmio::data::Variant Field::Deserialize(ros::serialization::IStream& stream, const FieldStack& fieldStack) const
77 {
78  KeyedFieldStack current(fieldStack, GetName());
79  return _serializer.Deserialize(stream, current);
80 }
81 
82 swarmio::data::Variant Field::DeserializeArray(ros::serialization::IStream& stream, uint32_t count, const FieldStack& fieldStack) const
83 {
84  throw Exception("Fields cannot be deserialized as arrays");
85 }
86 
87 swarmio::data::discovery::Field Field::GetFieldDescriptor() const
88 {
90 }
virtual swarmio::data::Variant Deserialize(ros::serialization::IStream &stream, const FieldStack &fieldStack) const =0
Deserialize a stream into a variant.
virtual void Serialize(ros::serialization::OStream &stream, const swarmio::data::Variant &value, const FieldStack &fieldStack) const override
Serialize a variant onto a stream.
Definition: Field.cpp:57
virtual uint32_t CalculateSerializedLength(const swarmio::data::Variant &value, const FieldStack &fieldStack) const =0
Calculate the length of a serialized message in bytes.
Exception thrown when the schema does not match the variant.
virtual swarmio::data::discovery::Field GetFieldDescriptor() const override
Build a field descriptor for the underlying type.
Definition: Field.cpp:87
const std::string & GetHash() const
Get the MD5 hash of the message definition file.
Exception base class for swarmros exceptions.
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.
Serializer for full-fledged message types.
virtual uint32_t GetDefaultLength(const FieldStack &fieldStack) const override
Get the length of the default value.
Definition: Field.cpp:38
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 const std::string & GetFullName() const
Get the fully qualified name for the type behind the serializer.
Definition: Serializer.h:77
static bool IsArray(const Variant &value)
Check whether the variant contains an array type.
Definition: Helper.cpp:10
void WriteTypeName(std::stringstream &stream, bool forHash) const
Get the type name for the serializer.
Definition: Field.cpp:18
const Serializer & _serializer
Serializer.
Definition: Field.h:23
virtual uint32_t CalculateSerializedLength(const swarmio::data::Variant &value, const FieldStack &fieldStack) const override
Calculate the length of a serialized message in bytes.
Definition: Field.cpp:44
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 swarmio::data::Variant Deserialize(ros::serialization::IStream &stream, const FieldStack &fieldStack) const override
Deserialize a stream into a variant.
Definition: Field.cpp:76
virtual swarmio::data::Variant DeserializeArray(ros::serialization::IStream &stream, uint32_t count, const FieldStack &fieldStack) const override
Deserialize a stream into a variant array - not supported on Fields.
Definition: Field.cpp:82
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 EmitDefault(ros::serialization::OStream &stream, const FieldStack &fieldStack) const override
Write the default value to the stream.
Definition: Field.cpp:70


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