12 #include <mrpt/config.h>
19 #include <type_traits>
25 #if defined(__clang__) && (__GLIBCXX__ <= 20180419)
26 #define HAS_BROKEN_CLANG_STD_VISIT
90 #if !MRPT_IS_BIG_ENDIAN
92 return ReadBuffer(ptr, ElementCount *
sizeof(T));
95 const size_t nread =
ReadBuffer(ptr, ElementCount *
sizeof(T));
96 for (
size_t i = 0; i < ElementCount; i++)
107 void WriteBuffer(
const void* Buffer,
size_t Count);
126 template <
typename T>
129 #if !MRPT_IS_BIG_ENDIAN
134 for (
size_t i = 0; i < ElementCount; i++) (*
this) << ptr[i];
140 template <
typename STORED_TYPE,
typename CAST_TO_TYPE>
145 read_here =
static_cast<CAST_TO_TYPE
>(var);
148 template <
typename STORED_TYPE>
155 template <
typename TYPE_TO_STORE,
typename TYPE_FROM_ACTUAL>
158 (*this) <<
static_cast<TYPE_TO_STORE
>(
value);
180 template <
typename T>
188 if (strClassName !=
"nullptr")
194 "Stored object has class '%s' which is not registered!",
195 strClassName.c_str());
199 obj.get() , strClassName, isOldFormat,
203 return typename T::Ptr();
207 return std::dynamic_pointer_cast<T>(
obj);
212 template <
typename RET>
215 throw std::runtime_error(
"Can't match variant type");
219 template <
typename RET,
typename T,
typename...
R>
224 if (
IS_CLASS(ptr,
typename T::element_type))
225 return std::dynamic_pointer_cast<typename T::element_type>(ptr);
229 template <
typename RET,
typename T,
typename...
R>
234 if (
IS_CLASS(ptr, T))
return dynamic_cast<T&
>(*ptr);
250 template <
typename... T>
262 "Stored object has class '%s' which is not registered!",
263 strClassName.c_str());
264 if (strClassName !=
"nullptr")
271 return std::variant<T...>();
279 #ifndef HAS_BROKEN_CLANG_STD_VISIT
282 template <
typename T>
296 template <
typename T>
371 virtual size_t write(
const void* buf,
size_t len) = 0;
376 virtual size_t read(
void* buf,
size_t len) = 0;
391 #define DECLARE_CArchive_READ_WRITE_SIMPLE_TYPE(T) \
392 CArchive& operator<<(CArchive& out, const T a); \
393 CArchive& operator>>(CArchive& in, T& a)
412 #define MRPT_READ_POD(_STREAM, _VARIABLE) \
415 const std::remove_cv_t<std::remove_reference_t<decltype(_VARIABLE)>> \
416 val = _STREAM.ReadPOD<std::remove_cv_t< \
417 std::remove_reference_t<decltype(_VARIABLE)>>>(); \
418 ::memcpy(&_VARIABLE, &val, sizeof(val)); \
430 CArchive&
operator<<(CArchive&,
const std::vector<int32_t>&
a);
431 CArchive&
operator<<(CArchive&,
const std::vector<uint32_t>&
a);
432 CArchive&
operator<<(CArchive&,
const std::vector<uint16_t>&
a);
433 CArchive&
operator<<(CArchive&,
const std::vector<int16_t>&
a);
434 CArchive&
operator<<(CArchive&,
const std::vector<uint32_t>&
a);
435 CArchive&
operator<<(CArchive&,
const std::vector<int64_t>&
a);
436 CArchive&
operator<<(CArchive&,
const std::vector<uint8_t>&
a);
437 CArchive&
operator<<(CArchive&,
const std::vector<int8_t>&
a);
439 CArchive&
operator<<(CArchive&,
const std::vector<bool>&
a);
440 CArchive&
operator<<(CArchive&,
const std::vector<std::string>&);
442 #if MRPT_WORD_SIZE != 32
443 CArchive&
operator<<(CArchive&,
const std::vector<size_t>&
a);
451 CArchive&
operator>>(CArchive&
in, std::vector<uint32_t>&
a);
452 CArchive&
operator>>(CArchive&
in, std::vector<uint16_t>&
a);
459 CArchive&
operator>>(CArchive&
in, std::vector<std::string>&
a);
463 CArchive&
operator>>(CArchive&
s, std::vector<float>&
a);
464 CArchive&
operator>>(CArchive&
s, std::vector<double>&
a);
465 CArchive&
operator<<(CArchive&
s,
const std::vector<float>&
a);
466 CArchive&
operator<<(CArchive&
s,
const std::vector<double>&
a);
468 #if MRPT_WORD_SIZE != 32
469 CArchive&
operator>>(CArchive&
s, std::vector<size_t>&
a);
474 typename T, std::enable_if_t<std::is_base_of<
482 template <
typename... T>
489 template <
typename... T>
492 pObj.match([&](
auto&
t) { out <<
t; });
498 class T, std::enable_if_t<!std::is_base_of<
504 out << mrpt::typemeta::TTypeName<T>::get();
516 class T, std::enable_if_t<!std::is_base_of<
518 CArchive&
operator>>(CArchive&
in, std::shared_ptr<T>& pObj)
539 template <
class STREAM>
548 size_t write(
const void* d,
size_t n)
override {
return m_s.Write(d,
n); }
549 size_t read(
void* d,
size_t n)
override {
return m_s.Read(d,
n); }
554 template <
class STREAM>
#define IS_CLASS(ptrObj, class_name)
Evaluates to true if the given pointer to an object (derived from mrpt::rtti::CObject) is of the give...
std::chrono::time_point< Clock > time_point
Virtual base class for "archives": classes abstracting I/O streams.
void sendMessage(const CMessage &msg)
Send a message to the device.
void WriteObject(const CSerializable &o)
void WriteVariant(T t)
Writes a Variant to the stream.
CSerializable::Ptr ReadObject()
Reads an object from stream, its class determined at runtime, and returns a smart pointer to the obje...
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...
virtual size_t read(void *buf, size_t len)=0
Reads a block of bytes.
T ReadPOD()
Reads a simple POD type and returns by value.
virtual size_t write(const void *buf, size_t len)=0
Writes a block of bytes.
void WriteAs(const TYPE_FROM_ACTUAL &value)
bool receiveMessage(CMessage &msg)
Tries to receive a message from the device.
STORED_TYPE ReadAs()
De-serialize a variable and returns it by value.
CArchive & operator>>(CSerializable &obj)
Reads a CSerializable object from the stream.
void WriteBuffer(const void *Buffer, size_t Count)
Writes a block of bytes to the stream from Buffer.
RET ReadVariant_helper(CSerializable::Ptr &ptr, std::enable_if_t<!mrpt::is_shared_ptr< T >::value > *=nullptr)
void WriteObject(const CSerializable *o)
Writes an object to the stream.
CArchive & operator<<(const CSerializable &obj)
Write a CSerializable object to a stream in the binary MRPT format.
RET ReadVariant_helper(CSerializable::Ptr &ptr, std::enable_if_t< mrpt::is_shared_ptr< T >::value > *=nullptr)
std::variant< T... > ReadVariant()
Reads a variant from stream, its class determined at runtime, and returns a variant to the object.
size_t ReadBuffer(void *Buffer, size_t Count)
Reads a block of bytes from the stream into Buffer.
void internal_ReadObject(CSerializable *newObj, const std::string &className, bool isOldFormat, int8_t version)
Read the object.
RET ReadVariant_helper(CSerializable::Ptr &ptr)
void WriteBufferFixEndianness(const T *ptr, size_t ElementCount)
Writes a sequence of elemental datatypes, taking care of reordering their bytes from the running arch...
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...
void internal_ReadObjectHeader(std::string &className, bool &isOldFormat, int8_t &version)
Read the object Header.
T::Ptr ReadObject()
Reads an object from stream, its class determined at runtime, and returns a smart pointer to the obje...
CArchive for mrpt::io::CStream classes (use as template argument).
CArchiveStreamBase(STREAM &s)
size_t write(const void *d, size_t n) override
Writes a block of bytes.
size_t read(void *d, size_t n) override
Reads a block of bytes.
Used in mrpt::serialization::CArchive.
CExceptionEOF(const std::string &s)
A class that contain generic messages, that can be sent and received from a "CClientTCPSocket" object...
The virtual base class which provides a unified interface for all persistent objects in MRPT.
std::shared_ptr< CSerializable > Ptr
#define ASSERT_EQUAL_(__A, __B)
Assert comparing two values, reporting their actual values upon failure.
#define THROW_EXCEPTION_FMT(_FORMAT_STRING,...)
GLsizei GLsizei GLuint * obj
GLubyte GLubyte GLubyte a
GLsizei const GLfloat * value
GLsizei const GLchar ** string
void reverseBytesInPlace(bool &v_in_out)
Reverse the order of the bytes of a given type (useful for transforming btw little/big endian)
const TRuntimeClassId * findRegisteredClass(const std::string &className)
Return info about a given class by its name, or nullptr if the class is not registered.
CArchive & operator<<(CArchive &s, const mrpt::aligned_std_vector< float > &a)
CArchive & operator>>(CArchive &s, mrpt::aligned_std_vector< float > &a)
DECLARE_CArchive_READ_WRITE_SIMPLE_TYPE(bool)
CArchiveStreamBase< STREAM > archiveFrom(STREAM &s)
Helper function to create a templatized wrapper CArchive object for a: MRPT's CStream,...
unsigned __int16 uint16_t
unsigned __int32 uint32_t
unsigned __int64 uint64_t
This is useful for checking ::Ptr types.
A structure that holds runtime class type information.
mrpt::rtti::CObject * createObject() const