Message.h
Go to the documentation of this file.
1 #pragma once
2 
4 #include <swarmros/Exception.h>
5 #include <ros/ros.h>
6 #include <ros/message_traits.h>
7 #include <std_msgs/Header.h>
8 
10 {
16  class Message
17  {
18  private:
19 
26 
31  std::string _type;
32 
37  std_msgs::Header _header;
38 
39  protected:
40 
46  : _serializer(nullptr), _type("*") { }
47 
53  Message(const std::string& type)
54  {
55  SetType(type);
56  }
57 
58  public:
59 
66  void SetType(const std::string& type)
67  {
68  _serializer = &MessageSerializer::MessageSerializerForType(type, "swarmros");
69  _type = _serializer->GetFullName();
70  }
71 
78  const std::string& GetType() const
79  {
80  return _type;
81  }
82 
87  void EraseType()
88  {
89  _serializer = nullptr;
90  _type = "*";
91  }
92 
99  {
100  return _serializer;
101  }
102 
108  const std_msgs::Header& GetHeader() const
109  {
110  return _header;
111  }
112 
118  std_msgs::Header& GetMutableHeader()
119  {
120  return _header;
121  }
122  };
123 }
124 
125 namespace ros::message_traits
126 {
131  template<>
132  struct IsMessage<swarmros::introspection::Message> : TrueType { };
133 
138  template<>
139  struct IsMessage<const swarmros::introspection::Message> : TrueType { };
140 
146  template<>
147  struct MD5Sum<swarmros::introspection::Message>
148  {
156  static const char* value(const swarmros::introspection::Message& message)
157  {
158  auto serializer = message.GetSerializer();
159  if (serializer != nullptr)
160  {
161  return serializer->GetHash().c_str();
162  }
163  else
164  {
165  throw Exception("Trying to serialize Message without serializer");
166  }
167  }
168 
174  static const char* value()
175  {
176  return "*";
177  }
178  };
179 
184  template<>
185  struct DataType<swarmros::introspection::Message>
186  {
194  static const char* value(const swarmros::introspection::Message& message)
195  {
196  auto serializer = message.GetSerializer();
197  if (serializer != nullptr)
198  {
199  return message.GetType().c_str();
200  }
201  else
202  {
203  throw Exception("Trying to serialize Message without serializer");
204  }
205  }
206 
212  static const char* value()
213  {
214  return "*";
215  }
216  };
217 
222  template<>
223  struct Definition<swarmros::introspection::Message>
224  {
232  static const char* value(const swarmros::introspection::Message& message)
233  {
234  auto serializer = message.GetSerializer();
235  if (serializer != nullptr)
236  {
237  return serializer->GetCanonicalDefinition().c_str();
238  }
239  else
240  {
241  throw Exception("Trying to serialize Message without serializer");
242  }
243  }
244  };
245 }
246 
247 namespace ros::serialization
248 {
256  template<>
257  struct Serializer<swarmros::introspection::Message>
258  {
266  {
267  auto serializer = message.GetSerializer();
268  if (serializer != nullptr)
269  {
270  if (serializer->HasHeader())
271  {
273  }
274  else
275  {
276  return 0;
277  }
278  }
279  else
280  {
281  throw Exception("Trying to serialize VariantMessage without serializer");
282  }
283  }
284 
291  template<typename Stream>
292  inline static void write(Stream& stream, const swarmros::introspection::Message& message)
293  {
294  auto serializer = message.GetSerializer();
295  if (serializer != nullptr)
296  {
297  if (serializer->HasHeader())
298  {
299  return Serializer<std_msgs::Header>::write(stream, message.GetHeader());
300  }
301  }
302  else
303  {
304  throw Exception("Trying to serialize VariantMessage without serializer");
305  }
306  }
307 
314  template<typename Stream>
315  inline static void read(Stream& stream, swarmros::introspection::Message& message)
316  {
317  auto serializer = message.GetSerializer();
318  if (serializer != nullptr)
319  {
320  if (serializer->HasHeader())
321  {
322  return Serializer<std_msgs::Header>::read(stream, message.GetMutableHeader());
323  }
324  }
325  else
326  {
327  throw Exception("Trying to deserialize VariantMessage without serializer");
328  }
329  }
330  };
331 }
const std_msgs::Header & GetHeader() const
Get a reference to the header.
Definition: Message.h:108
const std::string & GetCanonicalDefinition() const
Get the canonical definition of the message.
const MessageSerializer * GetSerializer() const
Get the associated serializer.
Definition: Message.h:98
static const char * value(const swarmros::introspection::Message &message)
Uses the message serializer to retreive the full name of the underlying data type.
Definition: Message.h:194
const std::string & GetHash() const
Get the MD5 hash of the message definition file.
static const char * value()
Returns a wildcard MD5 hash to accept all messages.
Definition: Message.h:174
static const char * value()
Returns a wildcard data type to accept all messages.
Definition: Message.h:212
static const MessageSerializer & MessageSerializerForType(const std::string &type, const std::string &parentPackage)
Look up or build a reader for a message type.
Message(const std::string &type)
Build an Message instance for a specific type.
Definition: Message.h:53
Serializer for full-fledged message types.
Message is the base class for message types supported by the introspection library.
Definition: Message.h:16
static const char * value(const swarmros::introspection::Message &message)
Uses the message serializer to retreive the canonical definition of the underlying data type...
Definition: Message.h:232
static void read(Stream &stream, swarmros::introspection::Message &message)
Reads the header from the stream, if present.
Definition: Message.h:315
void SetType(const std::string &type)
Set the type of the object and fetch its serializer.
Definition: Message.h:66
std::string _type
ROS type.
Definition: Message.h:31
std_msgs::Header & GetMutableHeader()
Get a mutable reference to the header.
Definition: Message.h:118
static void write(Stream &stream, const swarmros::introspection::Message &message)
Writes the header to the stream, if present.
Definition: Message.h:292
std_msgs::Header _header
Header.
Definition: Message.h:37
static uint32_t serializedLength(const swarmros::introspection::Message &message)
Calculates the size of the header, if present.
Definition: Message.h:265
const std::string & GetType() const
Get the fully qualified type of the message.
Definition: Message.h:78
static const char * value(const swarmros::introspection::Message &message)
Uses the message serializer to retreive the MD5 hash of the message definition.
Definition: Message.h:156
Message()
Construct an empty Message instance.
Definition: Message.h:45
void EraseType()
Erase type information from message.
Definition: Message.h:87
const MessageSerializer * _serializer
Reference to the serializer used to construct this message.
Definition: Message.h:25
virtual const std::string & GetFullName() const override
Get the fully qualified name for the type behind the serializer.


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