Go to the documentation of this file.
12 #include <mrpt/config.h>
18 #include <type_traits>
25 namespace serialization
86 #if !MRPT_IS_BIG_ENDIAN
88 return ReadBuffer(ptr, ElementCount *
sizeof(T));
91 const size_t nread =
ReadBuffer(ptr, ElementCount *
sizeof(T));
92 for (
size_t i = 0; i < ElementCount; i++)
103 void WriteBuffer(
const void* Buffer,
size_t Count);
122 template <
typename T>
125 #if !MRPT_IS_BIG_ENDIAN
130 for (
size_t i = 0; i < ElementCount; i++) (*
this) << ptr[i];
136 template <
typename STORED_TYPE,
typename CAST_TO_TYPE>
141 read_here =
static_cast<CAST_TO_TYPE
>(var);
144 template <
typename STORED_TYPE>
151 template <
typename TYPE_TO_STORE,
typename TYPE_FROM_ACTUAL>
154 (*this) <<
static_cast<TYPE_TO_STORE
>(
value);
176 template <
typename T>
184 if (strClassName !=
"nullptr")
190 "Stored object has class '%s' which is not registered!",
191 strClassName.c_str());
195 obj.get() , strClassName, isOldFormat,
199 return typename T::Ptr();
203 return std::dynamic_pointer_cast<T>(
obj);
208 template <
typename RET>
211 throw std::runtime_error(
"Can't match variant type");
215 template <
typename RET,
typename T,
typename...
R>
220 if (
IS_CLASS(ptr,
typename T::element_type))
221 return std::dynamic_pointer_cast<typename T::element_type>(ptr);
225 template <
typename RET,
typename T,
typename...
R>
230 if (
IS_CLASS(ptr, T))
return dynamic_cast<T&
>(*ptr);
235 template <
typename T,
typename T2,
typename ... REST>
238 return T::GetRuntimeClassIdStatic().
className == strClassName || T::GetRuntimeClassIdStatic().altName == strClassName ?
242 template <
typename T>
245 return T::GetRuntimeClassIdStatic().
className == strClassName || T::GetRuntimeClassIdStatic().altName == strClassName ?
246 &T::GetRuntimeClassIdStatic() :
nullptr;
260 template <
typename... T>
272 "Stored object has class '%s' which is not registered!",
273 strClassName.c_str());
274 if (strClassName !=
"nullptr")
281 return std::variant<T...>();
291 template <
typename T>
304 template <
typename T>
379 virtual size_t write(
const void* buf,
size_t len) = 0;
384 virtual size_t read(
void* buf,
size_t len) = 0;
399 #define DECLARE_CArchive_READ_WRITE_SIMPLE_TYPE(T) \
400 CArchive& operator<<(CArchive& out, const T a); \
401 CArchive& operator>>(CArchive& in, T& a)
417 #define MRPT_READ_POD(_STREAM, _VARIABLE) \
420 const std::remove_cv_t<std::remove_reference_t<decltype(_VARIABLE)>> \
421 val = _STREAM.ReadPOD<std::remove_cv_t< \
422 std::remove_reference_t<decltype(_VARIABLE)>>>(); \
423 ::memcpy(&_VARIABLE, &val, sizeof(val)); \
435 CArchive&
operator<<(CArchive&,
const std::vector<int32_t>&
a);
436 CArchive&
operator<<(CArchive&,
const std::vector<uint32_t>&
a);
437 CArchive&
operator<<(CArchive&,
const std::vector<uint16_t>&
a);
438 CArchive&
operator<<(CArchive&,
const std::vector<int16_t>&
a);
439 CArchive&
operator<<(CArchive&,
const std::vector<uint32_t>&
a);
440 CArchive&
operator<<(CArchive&,
const std::vector<int64_t>&
a);
441 CArchive&
operator<<(CArchive&,
const std::vector<uint8_t>&
a);
442 CArchive&
operator<<(CArchive&,
const std::vector<int8_t>&
a);
444 CArchive&
operator<<(CArchive&,
const std::vector<bool>&
a);
445 CArchive&
operator<<(CArchive&,
const std::vector<std::string>&);
447 #if MRPT_WORD_SIZE != 32 // If it's 32 bit, size_t <=> uint32_t
448 CArchive&
operator<<(CArchive&,
const std::vector<size_t>&
a);
456 CArchive&
operator>>(CArchive&
in, std::vector<uint32_t>&
a);
457 CArchive&
operator>>(CArchive&
in, std::vector<uint16_t>&
a);
464 CArchive&
operator>>(CArchive&
in, std::vector<std::string>&
a);
468 CArchive&
operator>>(CArchive&
s, std::vector<float>&
a);
469 CArchive&
operator>>(CArchive&
s, std::vector<double>&
a);
470 CArchive&
operator<<(CArchive&
s,
const std::vector<float>&
a);
471 CArchive&
operator<<(CArchive&
s,
const std::vector<double>&
a);
473 #if MRPT_WORD_SIZE != 32 // If it's 32 bit, size_t <=> uint32_t
474 CArchive&
operator>>(CArchive&
s, std::vector<size_t>&
a);
479 typename T, std::enable_if_t<std::is_base_of<
483 pObj =
in.ReadObject<T>();
487 template <
typename... T>
494 template <
typename... T>
496 CArchive& out,
const typename std::variant<T...>& pObj)
498 pObj.match([&](
auto&
t) { out <<
t; });
504 class T, std::enable_if_t<!std::is_base_of<
510 out << mrpt::typemeta::TTypeName<T>::get();
522 class T, std::enable_if_t<!std::is_base_of<
524 CArchive&
operator>>(CArchive&
in, std::shared_ptr<T>& pObj)
545 template <
class STREAM>
554 size_t write(
const void* d,
size_t n)
override {
return m_s.Write(d,
n); }
555 size_t read(
void* d,
size_t n)
override {
return m_s.Read(d,
n); }
560 template <
class STREAM>
CArchive for mrpt::io::CStream classes (use as template argument).
const mrpt::rtti::TRuntimeClassId * findRegisteredClassInList(std::string_view strClassName)
A structure that holds runtime class type information.
DECLARE_CArchive_READ_WRITE_SIMPLE_TYPE(bool)
std::shared_ptr< CSerializable > Ptr
#define ASSERT_EQUAL_(__A, __B)
Assert comparing two values, reporting their actual values upon failure.
unsigned __int16 uint16_t
virtual size_t read(void *buf, size_t len)=0
Reads a block of bytes.
This is useful for checking ::Ptr types.
GLsizei GLsizei GLuint * obj
CExceptionEOF(const std::string &s)
#define THROW_EXCEPTION_FMT(_FORMAT_STRING,...)
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
size_t ReadBuffer(void *Buffer, size_t Count)
Reads a block of bytes from the stream into Buffer.
CArchive & operator>>(CArchive &s, mrpt::aligned_std_vector< float > &a)
RET ReadVariant_helper(CSerializable::Ptr &ptr, std::enable_if_t< mrpt::is_shared_ptr< T >::value > *=nullptr)
RET ReadVariant_helper(CSerializable::Ptr &ptr)
Virtual base class for "archives": classes abstracting I/O streams.
virtual size_t write(const void *buf, size_t len)=0
Writes a block of bytes.
STORED_TYPE ReadAs()
De-serialize a variable and returns it by value.
A class that contain generic messages, that can be sent and received from a "CClientTCPSocket" object...
void WriteBuffer(const void *Buffer, size_t Count)
Writes a block of bytes to the stream from Buffer.
void WriteAs(const TYPE_FROM_ACTUAL &value)
T::Ptr ReadObject()
Reads an object from stream, its class determined at runtime, and returns a smart pointer to the obje...
bool receiveMessage(CMessage &msg)
Tries to receive a message from the device.
void reverseBytesInPlace(bool &v_in_out)
Reverse the order of the bytes of a given type (useful for transforming btw little/big endian)
#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...
unsigned __int64 uint64_t
The virtual base class which provides a unified interface for all persistent objects in MRPT.
std::variant< T... > ReadVariant()
Reads a variant from stream, its class determined at runtime, and returns a variant to the object.
T ReadPOD()
Reads a simple POD type and returns by value.
void internal_ReadObject(CSerializable *newObj, const std::string &className, bool isOldFormat, int8_t version)
Read the object.
mrpt::rtti::CObject * createObject() const
RET ReadVariant_helper(CSerializable::Ptr &ptr, std::enable_if_t<!mrpt::is_shared_ptr< T >::value > *=nullptr)
void WriteVariant(T t)
Writes a Variant to the stream.
CArchive & operator<<(CArchive &s, const mrpt::aligned_std_vector< float > &a)
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...
CArchiveStreamBase< STREAM > archiveFrom(STREAM &s)
Helper function to create a templatized wrapper CArchive object for a: MRPT's CStream,...
void WriteObject(const CSerializable *o)
Writes an object to the stream.
void sendMessage(const CMessage &msg)
Send a message to the device.
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...
Used in mrpt::serialization::CArchive.
GLsizei const GLfloat * value
size_t read(void *d, size_t n) override
Reads a block of bytes.
CArchive & operator<<(const CSerializable &obj)
Write a CSerializable object to a stream in the binary MRPT format.
CSerializable::Ptr ReadObject()
Reads an object from stream, its class determined at runtime, and returns a smart pointer to the obje...
GLsizei const GLchar ** string
void WriteObject(const CSerializable &o)
void WriteBufferFixEndianness(const T *ptr, size_t ElementCount)
Writes a sequence of elemental datatypes, taking care of reordering their bytes from the running arch...
CArchiveStreamBase(STREAM &s)
unsigned __int32 uint32_t
size_t write(const void *d, size_t n) override
Writes a block of bytes.
const TRuntimeClassId * findRegisteredClass(const std::string &className)
Return info about a given class by its name, or nullptr if the class is not registered.
GLubyte GLubyte GLubyte a
void internal_ReadObjectHeader(std::string &className, bool &isOldFormat, int8_t &version)
Read the object Header.
const mrpt::rtti::TRuntimeClassId * findRegisteredClassInList(std::string_view strClassName)
CArchive & operator>>(CSerializable &obj)
Reads a CSerializable object from the stream.
Page generated by Doxygen 1.8.17 for MRPT 1.9.9 Git: ad3a9d8ae Tue May 1 23:10:22 2018 -0700 at mié 12 jul 2023 10:03:34 CEST | |