MRPT  1.9.9
gnss_messages_common.h
Go to the documentation of this file.
1 /* +------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | https://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2019, Individual contributors, see AUTHORS file |
6  | See: https://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See: https://www.mrpt.org/License |
8  +------------------------------------------------------------------------+ */
9 #pragma once
10 
13 #include <mrpt/system/datetime.h>
14 #include <cstring> // memset()
15 #include <iosfwd>
16 
17 namespace mrpt
18 {
19 namespace obs
20 {
21 /** GNSS (GPS) data structures, mainly for use within mrpt::obs::CObservationGPS
22  */
23 namespace gnss
24 {
25 /** Pure virtual base for all message types. \sa mrpt::obs::CObservationGPS */
27 {
28  /** Type of GNSS message */
30 
31  gnss_message(gnss_message_type_t msg_type_id) : message_type(msg_type_id) {}
32  /** Save to binary stream. Launches an exception upon error */
34  /** Load from binary stream into this existing object. Launches an exception
35  * upon error. */
37 
38  bool isOfType(const gnss_message_type_t type_id) const;
39  template <class MSG_CLASS>
40  bool isOfClass() const
41  {
42  return isOfType(MSG_CLASS::msg_type);
43  }
44 
45  /** Load from binary stream and creates object detecting its type (class
46  * factory). Launches an exception upon error */
49  /** Creates message \return nullptr on unknown msg type */
50  static gnss_message* Factory(const gnss_message_type_t msg_id);
51  /** Returns true if Factory() has a registered constructor for this msg type
52  */
53  static bool FactoryKnowsMsgType(const gnss_message_type_t msg_id);
54 
55  /** Dumps the contents of the observation in a human-readable form to a
56  * given output stream \sa dumpToConsole() */
57  virtual void dumpToStream(std::ostream& out) const = 0;
58 
59  /** If we are in a big endian system, reverse all fields >1 byte to fix its
60  * representation. Only in binary frames, text-based derived classes
61  * obviously do not need to reimplement this one. */
62  virtual void fixEndianness() {}
63 
64  /** Dumps the contents of the observation in a human-readable form to an
65  * std::ostream (set to std::cout to print to console) */
66  void dumpToConsole(std::ostream& o) const;
67  /** Dumps a header for getAllFieldValues() \return false if not implemented
68  * for this message type */
69  virtual bool getAllFieldDescriptions([[maybe_unused]] std::ostream& o) const
70  {
71  return false;
72  }
73  /** Dumps a line with the sequence of all field values (without a line feed
74  * at the end). \sa getAllFieldDescriptions() \return false if not
75  * implemented for this message type */
76  virtual bool getAllFieldValues([[maybe_unused]] std::ostream& o) const
77  {
78  return false;
79  }
80  /** Returns "NMEA_GGA", etc. */
81  const std::string& getMessageTypeAsString() const;
82  virtual ~gnss_message() = default;
83 
84  protected:
85  /** Save to binary stream. Launches an exception upon error */
86  virtual void internal_writeToStream(
88  /** Save to binary stream. Launches an exception upon error */
90 };
91 
92 /** A smart pointer to a GNSS message. \sa gnss_message,
93  * mrpt::obs::CObservationGPS */
95 {
96  protected:
97  gnss_message* ptr{nullptr};
98 
99  public:
100  /** Ctor (default: nullptr pointer) */
102  /** Makes a copy of the pointee */
104  /** Assigns a pointer. Memory now belongs to this class. */
105  explicit gnss_message_ptr(const gnss_message* p);
107  const gnss_message_ptr& o); // Makes a copy of the pointee
108  /** Dtor: it frees the pointee memory */
109  virtual ~gnss_message_ptr();
110  bool operator==(const gnss_message* o) const { return o == ptr; }
111  bool operator==(const gnss_message_ptr& o) const { return o.ptr == ptr; }
112  bool operator!=(const gnss_message* o) const { return o != ptr; }
113  bool operator!=(const gnss_message_ptr& o) const { return o.ptr != ptr; }
114  gnss_message*& get() { return ptr; }
115  const gnss_message* get() const { return ptr; }
117  {
118  ASSERT_(ptr);
119  return ptr;
120  }
121  const gnss_message* operator->() const
122  {
123  ASSERT_(ptr);
124  return ptr;
125  }
126  /** Replaces the pointee with a new pointer. Its memory now belongs to this
127  * object, do not free manually. */
128  void set(gnss_message* p);
129 };
130 
131 #define GNSS_MESSAGE_BINARY_BLOCK(DATA_PTR, DATA_LEN) \
132  protected: \
133  void internal_writeToStream(mrpt::serialization::CArchive& out) \
134  const override \
135  { \
136  out << static_cast<uint32_t>(DATA_LEN); \
137  auto nonconst_this = const_cast<std::remove_const< \
138  std::remove_reference<decltype(*this)>::type>::type*>(this); \
139  /* Temporarily switch to little endian for serialization only */ \
140  nonconst_this->fixEndianness(); \
141  out.WriteBuffer(DATA_PTR, DATA_LEN); \
142  nonconst_this->fixEndianness(); \
143  } \
144  void internal_readFromStream(mrpt::serialization::CArchive& in) override \
145  { \
146  uint32_t nBytesInStream; \
147  in >> nBytesInStream; \
148  ASSERT_EQUAL_(nBytesInStream, DATA_LEN); \
149  in.ReadBuffer(DATA_PTR, DATA_LEN); \
150  fixEndianness(); \
151  } \
152  \
153  public:
154 
155 #define GNSS_BINARY_MSG_DEFINITION_START(_MSG_ID) \
156  struct Message_##_MSG_ID : public gnss_message \
157  { \
158  GNSS_MESSAGE_BINARY_BLOCK(&fields, sizeof(fields)) \
159  enum : uint32_t \
160  { \
161  msg_type = _MSG_ID \
162  }; /* Static msg type (member expected by templates)*/ \
163  Message_##_MSG_ID() \
164  : gnss_message(static_cast<gnss_message_type_t>(msg_type)) \
165  { \
166  } \
167  struct content_t \
168  {
169 #define GNSS_BINARY_MSG_DEFINITION_MID \
170  content_t() = default; \
171  } \
172  ; \
173  content_t fields; /** Message content, accesible by individual fields */ \
174  void dumpToStream(std::ostream& out) const override;
175 
176 #define GNSS_BINARY_MSG_DEFINITION_MID_END \
177  } \
178  ;
179 
180 #define GNSS_BINARY_MSG_DEFINITION_END \
181  GNSS_BINARY_MSG_DEFINITION_MID \
182  GNSS_BINARY_MSG_DEFINITION_MID_END
183 
184 // Pragma to ensure we can safely serialize some of these structures
185 #pragma pack(push, 1)
186 
187 /** UTC (Coordinated Universal Time) time-stamp structure for GPS messages. \sa
188  * mrpt::obs::CObservationGPS */
189 struct UTC_time
190 {
191  uint8_t hour{0};
192  uint8_t minute{0};
193  double sec{0};
194 
195  UTC_time();
196  /** Build an MRPT timestamp with the hour/minute/sec of this structure and
197  * the date from the given timestamp. */
199  const mrpt::system::TTimeStamp& date) const;
200  bool operator==(const UTC_time& o) const
201  {
202  return hour == o.hour && minute == o.minute && sec == o.sec;
203  }
204  bool operator!=(const UTC_time& o) const
205  {
206  return hour != o.hour || minute != o.minute || sec != o.sec;
207  }
208  /** Save to binary stream. Launches an exception upon error */
210  /** Save to binary stream. Launches an exception upon error */
212 };
213 
214 #pragma pack(pop) // End of pack = 1
215 } // namespace gnss
216 } // namespace obs
217 } // namespace mrpt
bool operator==(const UTC_time &o) const
const gnss_message * operator->() const
virtual ~gnss_message()=default
virtual bool getAllFieldDescriptions([[maybe_unused]] std::ostream &o) const
Dumps a header for getAllFieldValues()
mrpt::system::TTimeStamp getAsTimestamp(const mrpt::system::TTimeStamp &date) const
Build an MRPT timestamp with the hour/minute/sec of this structure and the date from the given timest...
UTC (Coordinated Universal Time) time-stamp structure for GPS messages.
gnss_message_type_t
List of all known GNSS message types.
void readFromStream(mrpt::serialization::CArchive &in)
Save to binary stream.
gnss_message_ptr & operator=(const gnss_message_ptr &o)
virtual bool getAllFieldValues([[maybe_unused]] std::ostream &o) const
Dumps a line with the sequence of all field values (without a line feed at the end).
bool isOfType(const gnss_message_type_t type_id) const
bool operator!=(const gnss_message_ptr &o) const
void dumpToConsole(std::ostream &o) const
Dumps the contents of the observation in a human-readable form to an std::ostream (set to std::cout t...
bool operator==(const gnss_message_ptr &o) const
gnss_message(gnss_message_type_t msg_type_id)
void readFromStream(mrpt::serialization::CArchive &in)
Load from binary stream into this existing object.
#define ASSERT_(f)
Defines an assertion mechanism.
Definition: exceptions.h:120
mrpt::Clock::time_point TTimeStamp
A system independent time type, it holds the the number of 100-nanosecond intervals since January 1...
Definition: datetime.h:40
bool operator!=(const UTC_time &o) const
virtual void dumpToStream(std::ostream &out) const =0
Dumps the contents of the observation in a human-readable form to a given output stream.
virtual ~gnss_message_ptr()
Dtor: it frees the pointee memory.
static gnss_message * readAndBuildFromStream(mrpt::serialization::CArchive &in)
Load from binary stream and creates object detecting its type (class factory).
void writeToStream(mrpt::serialization::CArchive &out) const
Save to binary stream.
gnss_message_type_t message_type
Type of GNSS message.
GLsizei const GLchar ** string
Definition: glext.h:4116
static gnss_message * Factory(const gnss_message_type_t msg_id)
Creates message.
gnss_message_ptr()
Ctor (default: nullptr pointer)
bool operator!=(const gnss_message *o) const
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
Virtual base class for "archives": classes abstracting I/O streams.
Definition: CArchive.h:54
mrpt::vision::TStereoCalibResults out
Pure virtual base for all message types.
virtual void fixEndianness()
If we are in a big endian system, reverse all fields >1 byte to fix its representation.
bool operator==(const gnss_message *o) const
GLuint in
Definition: glext.h:7391
virtual void internal_readFromStream(mrpt::serialization::CArchive &in)=0
Save to binary stream.
static bool FactoryKnowsMsgType(const gnss_message_type_t msg_id)
Returns true if Factory() has a registered constructor for this msg type.
const std::string & getMessageTypeAsString() const
Returns "NMEA_GGA", etc.
virtual void internal_writeToStream(mrpt::serialization::CArchive &out) const =0
Save to binary stream.
GLfloat GLfloat p
Definition: glext.h:6398
void writeToStream(mrpt::serialization::CArchive &out) const
Save to binary stream.
A smart pointer to a GNSS message.



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: c1796881b Sat Nov 16 19:04:34 2019 +0100 at sáb nov 16 19:15:10 CET 2019