ConstantField.cpp
Go to the documentation of this file.
4 #include <regex>
5 
6 using namespace swarmros;
7 using namespace swarmros::introspection;
8 
9 // Regex to match true values
10 static const std::regex TruePattern("^\\s*(true|[0-9]*[1-9][0-9]*)\\s*(?:#.*)?$");
11 
12 // Regex to match false values
13 static const std::regex FalsePattern("^\\s*(false|0+)\\s*(?:#.*)?$");
14 
15 // Regex to match unsigned values
16 static const std::regex UnsignedPattern("^\\s*([0-9]+)\\s*(?:#.*)?$");
17 
18 // Regex to match unsigned values
19 static const std::regex SignedPattern("^\\s*(-?[0-9]+)\\s*(?:#.*)?$");
20 
21 // Regex to match floating point values
22 static const std::regex FloatPattern("^\\s*(-?[0-9]+(?:\\.[0-9]+)?)\\s*(?:#.*)?$");
23 
24 void ConstantField::ParseBool(const std::string& value)
25 {
26  std::smatch match;
27  if (std::regex_match(value, match, TruePattern))
28  {
29  _value.set_bool_value(true);
30  _rawValue = match[1].str();
31  }
32  else if (std::regex_match(value, match, FalsePattern))
33  {
34  _value.set_bool_value(false);
35  _rawValue = match[1].str();
36  }
37  else
38  {
39  throw UnqualifiedException("Invalid value for bool constant");
40  }
41 }
42 
43 void ConstantField::ParseUnsigned(const std::string& value)
44 {
45  std::smatch match;
46  if (std::regex_match(value, match, UnsignedPattern))
47  {
48  _value.set_uint_value(std::stoull(value));
49  _rawValue = match[1].str();
50  }
51  else
52  {
53  throw UnqualifiedException("Invalid value for unsigned constant");
54  }
55 }
56 
57 void ConstantField::ParseSigned(const std::string& value)
58 {
59  std::smatch match;
60  if (std::regex_match(value, match, SignedPattern))
61  {
62  _value.set_int_value(std::stoll(value));
63  _rawValue = match[1].str();
64  }
65  else
66  {
67  throw UnqualifiedException("Invalid value for signed constant");
68  }
69 }
70 
71 void ConstantField::ParseFloat(const std::string& value)
72 {
73  std::smatch match;
74  if (std::regex_match(value, match, FloatPattern))
75  {
76  _value.set_double_value(std::stod(value));
77  _rawValue = match[1].str();
78  }
79  else
80  {
81  throw UnqualifiedException("Invalid value for double constant");
82  }
83 }
84 
85 void ConstantField::ParseString(const std::string& value)
86 {
87  _rawValue = value;
88  _value.set_string_value(value);
89 }
90 
91 ConstantField::ConstantField(const std::string& name, const Serializer& serializer, const std::string& value)
92  : Field(name, serializer)
93 {
94  const PrimitiveSerializer* primitiveSerializer = dynamic_cast<const PrimitiveSerializer*>(&serializer);
95  if (primitiveSerializer != nullptr)
96  {
97  switch (primitiveSerializer->GetType())
98  {
100  ParseBool(value);
101  break;
102 
103  case PrimitiveType::INT8:
107  ParseSigned(value);
108  break;
109 
114  ParseUnsigned(value);
115  break;
116 
119  ParseFloat(value);
120  break;
121 
123  ParseString(value);
124  break;
125 
127  case PrimitiveType::TIME:
128  throw UnqualifiedException("Invalid primitive type for constant");
129 
130  default:
131  throw Exception("Unknown primitive type");
132  }
133  }
134  else
135  {
136  throw UnqualifiedException("Invalid type for constant");
137  }
138 }
139 
140 void ConstantField::WriteDefinition(std::stringstream& stream, bool forHash) const
141 {
142  WriteTypeName(stream, forHash);
143  if (forHash)
144  {
145  stream << " " << GetName() << "=" << _rawValue;
146  }
147  else
148  {
149  stream << " " << GetName() << " = " << _rawValue;
150  }
151 }
152 
153 uint32_t ConstantField::GetDefaultLength(const FieldStack& fieldStack) const
154 {
155  return 0;
156 }
157 
158 uint32_t ConstantField::CalculateSerializedLength(const swarmio::data::Variant& value, const FieldStack& fieldStack) const
159 {
160  return 0;
161 }
162 
163 void ConstantField::Serialize(ros::serialization::OStream& stream, const swarmio::data::Variant& value, const FieldStack& fieldStack) const
164 {
165  // Do nothing
166 }
167 
169 {
170  // Do nothing
171 }
172 
173 swarmio::data::Variant ConstantField::Deserialize(ros::serialization::IStream& stream, const FieldStack& fieldStack) const
174 {
175  return _value;
176 }
Serializer for primitive builtin types.
static const std::regex FalsePattern("^\\s*(false|0+)\\s*(?:#.*)?$")
virtual uint32_t GetDefaultLength(const FieldStack &fieldStack) const override
Get the length of the default value.
static const std::regex TruePattern("^\\s*(true|[0-9]*[1-9][0-9]*)\\s*(?:#.*)?$")
virtual void Serialize(ros::serialization::OStream &stream, const swarmio::data::Variant &value, const FieldStack &fieldStack) const override
Serialize a variant onto a stream.
Exception base class for swarmros exceptions.
virtual void EmitDefault(ros::serialization::OStream &stream, const FieldStack &fieldStack) const override
Write the default value to the stream.
void ParseSigned(const std::string &value)
Parse a signed integer value.
A Field represents an entry in a message reader that can read one of its fields.
Definition: Field.h:15
void ParseFloat(const std::string &value)
Parse a floating point value.
void ParseString(const std::string &value)
Parse a string value.
void ParseBool(const std::string &value)
Parse a boolean value.
static const std::regex FloatPattern("^\\s*(-?[0-9]+(?:\\.[0-9]+)?)\\s*(?:#.*)?$")
PrimitiveType GetType() const
Get underlying data type.
void WriteTypeName(std::stringstream &stream, bool forHash) const
Get the type name for the serializer.
Definition: Field.cpp:18
swarmio::data::Variant _value
Value.
Definition: ConstantField.h:21
Serializer is the base class for all binary message interpreters.
Definition: Serializer.h:17
const std::string & GetName() const
Get the non-qualified name for the type behind the serializer.
Definition: Serializer.h:66
static const std::regex SignedPattern("^\\s*(-?[0-9]+)\\s*(?:#.*)?$")
virtual uint32_t CalculateSerializedLength(const swarmio::data::Variant &value, const FieldStack &fieldStack) const override
Calculate the length of a serialized message in bytes.
virtual swarmio::data::Variant Deserialize(ros::serialization::IStream &stream, const FieldStack &fieldStack) const override
Deserialize a stream into a variant.
An exception that is expected to be caught and translated into another exception. ...
virtual void WriteDefinition(std::stringstream &stream, bool forHash) const override
Write the definition of this field to a string stream.
ConstantField(const std::string &name, const Serializer &serializer, const std::string &value)
Construct a new ConstantField object.
static const std::regex UnsignedPattern("^\\s*([0-9]+)\\s*(?:#.*)?$")
void ParseUnsigned(const std::string &value)
Parse an unsigned integer value.
std::string _rawValue
Raw string value.
Definition: ConstantField.h:27


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