MRPT  2.0.1
CArchive.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-2020, 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 
11 #include <mrpt/config.h> // MRPT_IS_BIG_ENDIAN
12 #include <mrpt/core/Clock.h>
17 #include <cstdint>
18 #include <cstring> // memcpy
19 #include <stdexcept>
20 #include <string>
21 #include <type_traits> // remove_reference_t, is_polymorphic
22 #include <variant>
23 #include <vector>
24 
25 // See: https://gcc.gnu.org/viewcvs/gcc?view=revision&revision=258854
26 // See: https://stackoverflow.com/a/46507150/1631514
27 #if defined(__clang__) && (__clang_major__ <= 7)
28 #define HAS_BROKEN_CLANG_STD_VISIT
29 #endif
30 
31 namespace mrpt::serialization
32 {
33 class CMessage;
34 
35 /** Used in mrpt::serialization::CArchive */
36 class CExceptionEOF : public std::runtime_error
37 {
38  public:
39  CExceptionEOF(const std::string& s) : std::runtime_error(s) {}
40 };
41 
42 /** Virtual base class for "archives": classes abstracting I/O streams.
43  * This class separates the implementation details of serialization (in
44  * CSerializable) and data storage (CArchive children: files, sockets,...).
45  *
46  * Two main sets of implementations are provided:
47  * - archiveFrom: for MRPT mrpt::io::CArchive objects, and
48  * - CArchiveStdIStream and CArchiveStdOStream: for std::istream and
49  * std::ostream, respectively.
50  *
51  * \sa mrpt::io::CArchive, mrpt::serialization::CSerializable
52  * \ingroup mrpt_serialization_grp
53  */
54 class CArchive
55 {
56  public:
57  CArchive() = default;
58  virtual ~CArchive() = default;
59 
61  using UniquePtr = std::unique_ptr<CArchive>;
62 
63  /** @name Serialization API for generic "archives" I/O streams
64  * @{ */
65  /** Reads a block of bytes from the stream into Buffer
66  * \exception std::exception On any error, or if ZERO bytes are read.
67  * \return The amound of bytes actually read.
68  * \note This method is endianness-dependent.
69  * \sa ReadBufferImmediate ; Important, see: ReadBufferFixEndianness,
70  */
71  size_t ReadBuffer(void* Buffer, size_t Count);
72 
73  /** Reads a sequence of elemental datatypes, taking care of reordering their
74  *bytes from the MRPT stream standard (little endianness) to the format of
75  *the running architecture.
76  * \param ElementCount The number of elements (not bytes) to read.
77  * \param ptr A pointer to the first output element in an array (or
78  *std::vector<>, etc...).
79  * \return The amound of *bytes* (not elements) actually read (under error
80  *situations, the last element may be invalid if the data stream abruptly
81  *ends).
82  * Example of usage:
83  * \code
84  * uint32_t N;
85  * s >> N;
86  * vector<float> vec(N);
87  * if (N)
88  * s.ReadBufferFixEndianness<float>(&vec[0],N);
89  * \endcode
90  * \exception std::exception On any error, or if ZERO bytes are read.
91  * \sa ReadBufferFixEndianness, ReadBuffer
92  */
93  template <typename T>
94  size_t ReadBufferFixEndianness(T* ptr, size_t ElementCount)
95  {
96 #if !MRPT_IS_BIG_ENDIAN
97  // little endian: no conversion needed.
98  return ReadBuffer(ptr, ElementCount * sizeof(T));
99 #else
100  // big endian: convert.
101  const size_t nread = ReadBuffer(ptr, ElementCount * sizeof(T));
102  for (size_t i = 0; i < ElementCount; i++)
104  return nread;
105 #endif
106  }
107 
108  /** Writes a block of bytes to the stream from Buffer.
109  * \exception std::exception On any error
110  * \sa Important, see: WriteBufferFixEndianness
111  * \note This method is endianness-dependent.
112  */
113  void WriteBuffer(const void* Buffer, size_t Count);
114 
115  /** Writes a sequence of elemental datatypes, taking care of reordering
116  * their bytes from the running architecture to MRPT stream standard (little
117  * endianness).
118  * \param ElementCount The number of elements (not bytes) to write.
119  * \param ptr A pointer to the first input element in an array (or
120  * std::vector<>, etc...).
121  * Example of usage:
122  * \code
123  * vector<float> vec = ...
124  * uint32_t N = vec.size();
125  * s << N
126  * if (N)
127  * s.WriteBufferFixEndianness<float>(&vec[0],N);
128  * \endcode
129  * \exception std::exception On any error
130  * \sa WriteBuffer
131  */
132  template <typename T>
133  void WriteBufferFixEndianness(const T* ptr, size_t ElementCount)
134  {
135 #if !MRPT_IS_BIG_ENDIAN
136  // little endian: no conversion needed.
137  return WriteBuffer(ptr, ElementCount * sizeof(T));
138 #else
139  // big endian: the individual "<<" functions already convert endiannes
140  for (size_t i = 0; i < ElementCount; i++) (*this) << ptr[i];
141 #endif
142  }
143  /** Read a value from a stream stored in a type different of the target
144  * variable, making the conversion via static_cast. Useful for coding
145  * backwards compatible de-serialization blocks */
146  template <typename STORED_TYPE, typename CAST_TO_TYPE>
147  void ReadAsAndCastTo(CAST_TO_TYPE& read_here)
148  {
149  STORED_TYPE var;
150  (*this) >> var;
151  read_here = static_cast<CAST_TO_TYPE>(var);
152  }
153  /** De-serialize a variable and returns it by value. */
154  template <typename STORED_TYPE>
155  STORED_TYPE ReadAs()
156  {
157  STORED_TYPE var;
158  (*this) >> var;
159  return var;
160  }
161  template <typename TYPE_TO_STORE, typename TYPE_FROM_ACTUAL>
162  CArchive& WriteAs(const TYPE_FROM_ACTUAL& value)
163  {
164  (*this) << static_cast<TYPE_TO_STORE>(value);
165  return *this;
166  }
167  /** Writes an object to the stream.
168  */
169  void WriteObject(const CSerializable* o);
170  void WriteObject(const CSerializable& o) { WriteObject(&o); }
171  /** Reads an object from stream, its class determined at runtime, and
172  * returns a smart pointer to the object.
173  * \exception std::exception On I/O error or undefined class.
174  * \exception CExceptionEOF On an End-Of-File condition found
175  * at a correct place: an EOF that abruptly finishes in the middle of one
176  * object raises a plain std::exception instead.
177  */
178  CSerializable::Ptr ReadObject() { return ReadObject<CSerializable>(); }
179  /** Reads an object from stream, its class determined at runtime, and
180  * returns a smart pointer to the object. This version is similar to
181  * std::make_shared<T>.
182  * \exception std::exception On I/O error or undefined class.
183  * \exception CExceptionEOF On an End-Of-File condition found
184  * at a correct place: an EOF that abruptly finishes in the middle of one
185  * object raises a plain std::exception instead.
186  */
187  template <typename T>
188  typename T::Ptr ReadObject()
189  {
190  CSerializable::Ptr obj;
191  std::string strClassName;
192  bool isOldFormat{false};
193  int8_t version{-1};
194  internal_ReadObjectHeader(strClassName, isOldFormat, version);
195  if (strClassName != "nullptr")
196  {
197  const mrpt::rtti::TRuntimeClassId* classId =
198  mrpt::rtti::findRegisteredClass(strClassName);
199  if (!classId)
201  "Stored object has class '%s' which is not registered!",
202  strClassName.c_str());
204  }
206  obj.get() /* may be nullptr */, strClassName, isOldFormat,
207  version); // must be called to read the END FLAG byte
208  if (!obj)
209  {
210  return typename T::Ptr();
211  }
212  else
213  {
214  return std::dynamic_pointer_cast<T>(obj);
215  }
216  }
217 
218  private:
219  template <typename RET>
221  {
222  throw std::runtime_error("Can't match variant type");
223  return RET();
224  }
225 
226  template <typename RET, typename T, typename... R>
228  CSerializable::Ptr& ptr,
229  std::enable_if_t<mrpt::is_shared_ptr<T>::value>* = nullptr)
230  {
231  if (IS_CLASS(*ptr, typename T::element_type))
232  return std::dynamic_pointer_cast<typename T::element_type>(ptr);
233  return ReadVariant_helper<RET, R...>(ptr);
234  }
235 
236  template <typename RET, typename T, typename... R>
238  CSerializable::Ptr& ptr,
239  std::enable_if_t<!mrpt::is_shared_ptr<T>::value>* = nullptr)
240  {
241  if (IS_CLASS(*ptr, T)) return dynamic_cast<T&>(*ptr);
242  return ReadVariant_helper<RET, R...>(ptr);
243  }
244 
245  public:
246  /** Reads a variant from stream, its class determined at runtime, and
247  * returns a variant to the object.
248  * To be compatible with the current polymorphic system this support smart
249  * pointer types.
250  * For pointer types, This will bind to the first possible pointer type.
251  * variant<CSerializable::Ptr, CRenderizable::Ptr>
252  * \exception std::exception On I/O error or undefined class.
253  * \exception CExceptionEOF On an End-Of-File condition found
254  * at a correct place: an EOF that abruptly finishes in the middle of one
255  * object raises a plain std::exception instead.
256  */
257  template <typename... T>
258  typename std::variant<T...> ReadVariant()
259  {
260  CSerializable::Ptr obj;
261  std::string strClassName;
262  bool isOldFormat;
263  int8_t version;
264  internal_ReadObjectHeader(strClassName, isOldFormat, version);
265  const mrpt::rtti::TRuntimeClassId* classId =
266  mrpt::rtti::findRegisteredClass(strClassName);
267  if (!classId)
269  "Stored object has class '%s' which is not registered!",
270  strClassName.c_str());
271  if (strClassName != "nullptr")
272  {
274  }
275  internal_ReadObject(obj.get(), strClassName, isOldFormat, version);
276  if (!obj)
277  {
278  return std::variant<T...>();
279  }
280  else
281  {
282  return ReadVariant_helper<std::variant<T...>, T...>(obj);
283  }
284  }
285 
286 #if !defined(HAS_BROKEN_CLANG_STD_VISIT)
287  /** Writes a Variant to the stream */
288  template <typename T>
289  void WriteVariant(T t)
290  {
291  std::visit([&](auto& o) { this->WriteObject(o); }, t);
292  }
293 #endif
294 
295  /** Reads a simple POD type and returns by value. Useful when `stream >>
296  * var;`
297  * cannot be used becuase of errors of misaligned reference binding.
298  * Use with macro `MRPT_READ_POD` to avoid typing the type T yourself.
299  * \note [New in MRPT 2.0.0]
300  * \note Write operator `s << var;` is safe for misaligned variables.
301  */
302  template <typename T>
304  {
305  T ret;
306  ReadBufferFixEndianness(&ret, 1);
307  return ret;
308  }
309 
310  /** Reads an object from stream, where its class must be the same
311  * as the supplied object, where the loaded object will be stored in.
312  * \exception std::exception On I/O error or different class found.
313  * \exception CExceptionEOF On an End-Of-File condition found
314  * at a correct place: an EOF that abruptly finishes in the middle of one
315  * object raises a plain std::exception instead.
316  */
317  void ReadObject(CSerializable* existingObj);
318 
319  /** Send a message to the device.
320  * Note that only the low byte from the "type" field will be used.
321  *
322  * For frames of size < 255 the frame format is an array of bytes in this
323  * order:
324  * \code
325  * <START_FLAG> <HEADER> <LENGTH> <BODY> <END_FLAG>
326  * <START_FLAG> = 0x69
327  * <HEADER> = A header byte
328  * <LENGHT> = Number of bytes of BODY
329  * <BODY> = N x bytes
330  * <END_FLAG> = 0X96
331  * Total length = <LENGTH> + 4
332  * \endcode
333  *
334  * For frames of size > 255 the frame format is an array of bytes in this
335  * order:
336  * \code
337  * <START_FLAG> <HEADER> <HIBYTE(LENGTH)> <LOBYTE(LENGTH)> <BODY>
338  * <END_FLAG>
339  * <START_FLAG> = 0x79
340  * <HEADER> = A header byte
341  * <LENGHT> = Number of bytes of BODY
342  * <BODY> = N x bytes
343  * <END_FLAG> = 0X96
344  * Total length = <LENGTH> + 5
345  * \endcode
346  *
347  * \exception std::exception On communication errors
348  */
349  void sendMessage(const CMessage& msg);
350 
351  /** Tries to receive a message from the device.
352  * \exception std::exception On communication errors
353  * \returns True if successful, false if there is no new data from the
354  * device (but communications seem to work fine)
355  * \sa The frame format is described in sendMessage()
356  */
357  bool receiveMessage(CMessage& msg);
358 
359  /** Write a CSerializable object to a stream in the binary MRPT format */
360  CArchive& operator<<(const CSerializable& obj);
361  /** \overload */
362  CArchive& operator<<(const CSerializable::Ptr& pObj);
363  /** Reads a CSerializable object from the stream */
365  /** \overload */
367 
368  /** @} */
369 
370  protected:
371  /** @name Virtual methods of the CArchive interface
372  * @{ */
373  /** Writes a block of bytes.
374  * \exception std::exception On any error
375  * \return Number of bytes actually written.
376  */
377  virtual size_t write(const void* buf, size_t len) = 0;
378  /** Reads a block of bytes.
379  * \exception std::exception On any error, or if ZERO bytes are read.
380  * \return Number of bytes actually read if >0.
381  */
382  virtual size_t read(void* buf, size_t len) = 0;
383  /** @} */
384 
385  /** Read the object */
386  void internal_ReadObject(
387  CSerializable* newObj, const std::string& className, bool isOldFormat,
388  int8_t version);
389 
390  /** Read the object Header*/
392  std::string& className, bool& isOldFormat, int8_t& version);
393 };
394 
395 // Note: write op accepts parameters by value on purpose, to avoid misaligned
396 // reference binding errors.
397 template <class T, class... Ts>
398 using is_any = std::disjunction<std::is_same<T, Ts>...>;
399 
400 template <typename T>
401 using is_simple_type = is_any<
402  T, bool, uint8_t, int8_t, uint16_t, int16_t, uint32_t, int32_t, uint64_t,
403  int64_t, float, double, long double>;
404 
405 #if MRPT_IS_BIG_ENDIAN
406 // Big endian system: Convert into little-endian for streaming
407 template <typename T, std::enable_if_t<is_simple_type<T>::value, int> = 0>
409 {
411  out.WriteBuffer((void*)&a, sizeof(a));
412  return out;
413 }
414 
415 template <typename T, std::enable_if_t<is_simple_type<T>::value, int> = 0>
416 CArchive& operator>>(CArchive& in, T& a)
417 {
418  T b;
419  in.ReadBuffer((void*)&b, sizeof(a));
421  std::memcpy(&a, &b, sizeof(b));
422  return in;
423 }
424 
425 #else
426 // Little endian system:
427 template <typename T, std::enable_if_t<is_simple_type<T>::value, int> = 0>
429 {
430  out.WriteBuffer((void*)&a, sizeof(a));
431  return out;
432 }
433 
434 template <typename T, std::enable_if_t<is_simple_type<T>::value, int> = 0>
436 {
437  in.ReadBuffer((void*)&a, sizeof(a));
438  return in;
439 }
440 #endif
441 
442 CArchive& operator<<(CArchive& out, const mrpt::Clock::time_point& a);
443 CArchive& operator>>(CArchive& in, mrpt::Clock::time_point& a);
444 
445 #define MRPT_READ_POD(_STREAM, _VARIABLE) \
446  do \
447  { \
448  const std::remove_cv_t<std::remove_reference_t<decltype(_VARIABLE)>> \
449  val = _STREAM.ReadPOD<std::remove_cv_t< \
450  std::remove_reference_t<decltype(_VARIABLE)>>>(); \
451  std::memcpy(&_VARIABLE, &val, sizeof(val)); \
452  } while (0)
453 
454 // Why this shouldn't be templatized?: There's a more modern system
455 // in MRPT that serializes any kind of vector<T>, deque<T>, etc... but
456 // to keep COMPATIBILITY with old serialized objects we must preserve
457 // the ones listed here:
458 
459 // Write --------------------
460 CArchive& operator<<(CArchive& s, const std::string& str);
461 
462 CArchive& operator<<(CArchive&, const std::vector<int32_t>& a);
463 CArchive& operator<<(CArchive&, const std::vector<uint32_t>& a);
464 CArchive& operator<<(CArchive&, const std::vector<uint16_t>& a);
465 CArchive& operator<<(CArchive&, const std::vector<int16_t>& a);
466 CArchive& operator<<(CArchive&, const std::vector<uint32_t>& a);
467 CArchive& operator<<(CArchive&, const std::vector<int64_t>& a);
468 CArchive& operator<<(CArchive&, const std::vector<uint8_t>& a);
469 CArchive& operator<<(CArchive&, const std::vector<int8_t>& a);
470 
471 CArchive& operator<<(CArchive&, const std::vector<bool>& a);
472 CArchive& operator<<(CArchive&, const std::vector<std::string>&);
473 
474 #if MRPT_WORD_SIZE != 32 // If it's 32 bit, size_t <=> uint32_t
475 CArchive& operator<<(CArchive&, const std::vector<size_t>& a);
476 #endif
477 
478 // Read --------------------
479 CArchive& operator>>(CArchive& in, std::string& str);
480 
481 CArchive& operator>>(CArchive& in, std::vector<int32_t>& a);
482 CArchive& operator>>(CArchive& in, std::vector<uint32_t>& a);
483 CArchive& operator>>(CArchive& in, std::vector<uint16_t>& a);
484 CArchive& operator>>(CArchive& in, std::vector<int16_t>& a);
485 CArchive& operator>>(CArchive& in, std::vector<int64_t>& a);
486 CArchive& operator>>(CArchive& in, std::vector<uint8_t>& a);
487 CArchive& operator>>(CArchive& in, std::vector<int8_t>& a);
488 CArchive& operator>>(CArchive& in, std::vector<bool>& a);
489 
490 CArchive& operator>>(CArchive& in, std::vector<std::string>& a);
491 
492 // For backward compatibility, since in MRPT<0.8.1 vector_XXX and
493 // std::vector<XXX> were exactly equivalent, now there're not.
494 CArchive& operator>>(CArchive& s, std::vector<float>& a);
495 CArchive& operator>>(CArchive& s, std::vector<double>& a);
496 CArchive& operator<<(CArchive& s, const std::vector<float>& a);
497 CArchive& operator<<(CArchive& s, const std::vector<double>& a);
498 
499 #if MRPT_WORD_SIZE != 32 // If it's 32 bit, size_t <=> uint32_t
500 CArchive& operator>>(CArchive& s, std::vector<size_t>& a);
501 #endif
502 //
503 
504 template <
505  typename T, std::enable_if_t<std::is_base_of_v<
506  mrpt::serialization::CSerializable, T>>* = nullptr>
508 {
509  pObj = in.ReadObject<T>();
510  return in;
511 }
512 
513 template <typename... T>
514 CArchive& operator>>(CArchive& in, typename std::variant<T...>& pObj)
515 {
516  pObj = in.ReadVariant<T...>();
517  return in;
518 }
519 
520 template <typename... T>
521 CArchive& operator<<(CArchive& out, const typename std::variant<T...>& pObj)
522 {
523  pObj.match([&](auto& t) { out << t; });
524  return out;
525 }
526 
527 /** Write a shared_ptr to a non-CSerializable object */
528 template <
529  class T, std::enable_if_t<!std::is_base_of_v<
530  mrpt::serialization::CSerializable, T>>* = nullptr>
531 CArchive& operator<<(CArchive& out, const std::shared_ptr<T>& pObj)
532 {
533  if (pObj)
534  {
535  out << mrpt::typemeta::TTypeName<T>::get();
536  out << *pObj;
537  }
538  else
539  {
540  out << std::string("nullptr");
541  }
542  return out;
543 }
544 
545 /** Read a smart pointer to a non-CSerializable (POD,...) data type*/
546 template <
547  class T, std::enable_if_t<!std::is_base_of_v<
548  mrpt::serialization::CSerializable, T>>* = nullptr>
549 CArchive& operator>>(CArchive& in, std::shared_ptr<T>& pObj)
550 {
551  std::string stored_name;
552  in >> stored_name;
553  const std::string expected_name =
555  if (stored_name == std::string("nullptr"))
556  {
557  pObj.reset();
558  }
559  else
560  {
561  ASSERT_EQUAL_(expected_name, stored_name);
562  pObj.reset(new T);
563  in >> *pObj;
564  }
565  return in;
566 }
567 
568 /** CArchive for mrpt::io::CStream classes (use as template argument).
569  * \sa Easier to use via function archiveFrom() */
570 template <class STREAM>
572 {
573  STREAM& m_s;
574 
575  public:
576  CArchiveStreamBase(STREAM& s) : m_s(s) {}
577 
578  protected:
579  size_t write(const void* d, size_t n) override { return m_s.Write(d, n); }
580  size_t read(void* d, size_t n) override { return m_s.Read(d, n); }
581 };
582 
583 /** Helper function to create a templatized wrapper CArchive object for a:
584  * MRPT's `CStream`, `std::istream`, `std::ostream`, `std::stringstream`.
585  * \note Use with `std::{.*}stream` requires including
586  * `<mrpt/serialization/archiveFrom_std_streams.h>` and explicitly specifying
587  * the template parameter like: `archiveFrom<std::istream>` or
588  * `archiveFrom<std::ostream>`.
589  * \sa \ref mrpt_serialization_grp, and example serialization_stl/test.cpp
590  */
591 template <class STREAM>
593 {
594  return CArchiveStreamBase<STREAM>(s);
595 }
596 
597 /** Like archiveFrom(), returning a shared_ptr<>. */
598 template <class STREAM>
600 {
601  return std::make_shared<CArchiveStreamBase<STREAM>>(s);
602 }
603 
604 /** Like archiveFrom(), returning a unique_ptr<>. */
605 template <class STREAM>
607 {
608  return std::make_unique<CArchiveStreamBase<STREAM>>(s);
609 }
610 
611 } // namespace mrpt::serialization
612 
613 namespace mrpt::rtti
614 {
615 // for std::variant
616 template <>
617 struct CLASS_ID_impl<std::monostate>
618 {
619  static constexpr const mrpt::rtti::TRuntimeClassId* get()
620  {
621  return nullptr;
622  }
623 };
624 
625 } // namespace mrpt::rtti
std::shared_ptr< CObject > createObject() const
Definition: CObject.cpp:79
T ReadPOD()
Reads a simple POD type and returns by value.
Definition: CArchive.h:303
CExceptionEOF(const std::string &s)
Definition: CArchive.h:39
size_t read(void *d, size_t n) override
Reads a block of bytes.
Definition: CArchive.h:580
std::chrono::time_point< Clock > time_point
Definition: Clock.h:25
CArchive & WriteAs(const TYPE_FROM_ACTUAL &value)
Definition: CArchive.h:162
std::disjunction< std::is_same< T, Ts >... > is_any
Definition: CArchive.h:398
void WriteBufferFixEndianness(const T *ptr, size_t ElementCount)
Writes a sequence of elemental datatypes, taking care of reordering their bytes from the running arch...
Definition: CArchive.h:133
A structure that holds runtime class type information.
Definition: CObject.h:31
void WriteObject(const CSerializable &o)
Definition: CArchive.h:170
STL namespace.
void WriteBuffer(const void *Buffer, size_t Count)
Writes a block of bytes to the stream from Buffer.
Definition: CArchive.cpp:49
RET ReadVariant_helper(CSerializable::Ptr &ptr, std::enable_if_t<!mrpt::is_shared_ptr< T >::value > *=nullptr)
Definition: CArchive.h:237
void sendMessage(const CMessage &msg)
Send a message to the device.
Definition: CArchive.cpp:521
void reverseBytesInPlace(bool &v_in_out)
Reverse the order of the bytes of a given type (useful for transforming btw little/big endian) ...
virtual size_t read(void *buf, size_t len)=0
Reads a block of bytes.
CArchive & operator>>(CSerializable &obj)
Reads a CSerializable object from the stream.
Definition: CArchive.cpp:158
This is useful for checking ::Ptr types.
Definition: is_shared_ptr.h:20
CArchiveStreamBase< STREAM > archiveFrom(STREAM &s)
Helper function to create a templatized wrapper CArchive object for a: MRPT&#39;s CStream, std::istream, std::ostream, std::stringstream.
Definition: CArchive.h:592
void internal_ReadObjectHeader(std::string &className, bool &isOldFormat, int8_t &version)
Read the object Header.
Definition: CArchive.cpp:329
virtual ~CArchive()=default
void WriteVariant(T t)
Writes a Variant to the stream.
Definition: CArchive.h:289
void ReadAsAndCastTo(CAST_TO_TYPE &read_here)
Read a value from a stream stored in a type different of the target variable, making the conversion v...
Definition: CArchive.h:147
#define ASSERT_EQUAL_(__A, __B)
Assert comparing two values, reporting their actual values upon failure.
Definition: exceptions.h:137
STORED_TYPE ReadAs()
De-serialize a variable and returns it by value.
Definition: CArchive.h:155
size_t write(const void *d, size_t n) override
Writes a block of bytes.
Definition: CArchive.h:579
CArchive::Ptr archivePtrFrom(STREAM &s)
Like archiveFrom(), returning a shared_ptr<>.
Definition: CArchive.h:599
CArchive & operator<<(const CSerializable &obj)
Write a CSerializable object to a stream in the binary MRPT format.
Definition: CArchive.cpp:146
T::Ptr ReadObject()
Reads an object from stream, its class determined at runtime, and returns a smart pointer to the obje...
Definition: CArchive.h:188
is_any< T, bool, uint8_t, int8_t, uint16_t, int16_t, uint32_t, int32_t, uint64_t, int64_t, float, double, long double > is_simple_type
Definition: CArchive.h:403
#define IS_CLASS(obj, class_name)
True if the given reference to object (derived from mrpt::rtti::CObject) is of the given class...
Definition: CObject.h:146
const TRuntimeClassId * findRegisteredClass(const std::string &className, const bool allow_ignore_namespace=true)
Return info about a given class by its name, or nullptr if the class is not registered.
bool receiveMessage(CMessage &msg)
Tries to receive a message from the device.
Definition: CArchive.cpp:555
virtual size_t write(const void *buf, size_t len)=0
Writes a block of bytes.
CArchive for mrpt::io::CStream classes (use as template argument).
Definition: CArchive.h:571
CArchive & operator>>(CArchive &s, mrpt::aligned_std_vector< float > &a)
Definition: CArchive.cpp:250
Virtual base class for "archives": classes abstracting I/O streams.
Definition: CArchive.h:54
A class that contain generic messages, that can be sent and received from a "CClientTCPSocket" object...
Definition: CMessage.h:27
void internal_ReadObject(CSerializable *newObj, const std::string &className, bool isOldFormat, int8_t version)
Read the object.
Definition: CArchive.cpp:428
const float R
mrpt::vision::TStereoCalibResults out
RET ReadVariant_helper(CSerializable::Ptr &ptr)
Definition: CArchive.h:220
RET ReadVariant_helper(CSerializable::Ptr &ptr, std::enable_if_t< mrpt::is_shared_ptr< T >::value > *=nullptr)
Definition: CArchive.h:227
CSerializable::Ptr ReadObject()
Reads an object from stream, its class determined at runtime, and returns a smart pointer to the obje...
Definition: CArchive.h:178
CArchive & operator<<(CArchive &s, const mrpt::aligned_std_vector< float > &a)
Definition: CArchive.cpp:199
The virtual base class which provides a unified interface for all persistent objects in MRPT...
Definition: CSerializable.h:30
size_t ReadBuffer(void *Buffer, size_t Count)
Reads a block of bytes from the stream into Buffer.
Definition: CArchive.cpp:25
CArchive::UniquePtr archiveUniquePtrFrom(STREAM &s)
Like archiveFrom(), returning a unique_ptr<>.
Definition: CArchive.h:606
static CAST_TO::Ptr from(const CAST_FROM_PTR &ptr)
Definition: CObject.h:356
void WriteObject(const CSerializable *o)
Writes an object to the stream.
Definition: CArchive.cpp:101
#define THROW_EXCEPTION_FMT(_FORMAT_STRING,...)
Definition: exceptions.h:69
size_t ReadBufferFixEndianness(T *ptr, size_t ElementCount)
Reads a sequence of elemental datatypes, taking care of reordering their bytes from the MRPT stream s...
Definition: CArchive.h:94
std::variant< T... > ReadVariant()
Reads a variant from stream, its class determined at runtime, and returns a variant to the object...
Definition: CArchive.h:258
static constexpr auto get()
Definition: TTypeName.h:71
Used in mrpt::serialization::CArchive.
Definition: CArchive.h:36
void memcpy(void *dest, size_t destSize, const void *src, size_t copyCount) noexcept
An OS and compiler independent version of "memcpy".
std::unique_ptr< CArchive > UniquePtr
Definition: CArchive.h:61



Page generated by Doxygen 1.8.14 for MRPT 2.0.1 Git: 0fef1a6d7 Fri Apr 3 23:00:21 2020 +0200 at vie abr 3 23:20:28 CEST 2020