11 #include <mrpt/config.h> 20 #include <type_traits> 26 #if defined(__clang__) && (__clang_major__ <= 7) 27 #define HAS_BROKEN_CLANG_STD_VISIT 91 #if !MRPT_IS_BIG_ENDIAN 93 return ReadBuffer(ptr, ElementCount *
sizeof(T));
96 const size_t nread =
ReadBuffer(ptr, ElementCount *
sizeof(T));
97 for (
size_t i = 0; i < ElementCount; i++)
108 void WriteBuffer(
const void* Buffer,
size_t Count);
127 template <
typename T>
130 #if !MRPT_IS_BIG_ENDIAN 135 for (
size_t i = 0; i < ElementCount; i++) (*
this) << ptr[i];
141 template <
typename STORED_TYPE,
typename CAST_TO_TYPE>
146 read_here =
static_cast<CAST_TO_TYPE
>(var);
149 template <
typename STORED_TYPE>
156 template <
typename TYPE_TO_STORE,
typename TYPE_FROM_ACTUAL>
159 (*this) <<
static_cast<TYPE_TO_STORE
>(
value);
181 template <
typename T>
186 bool isOldFormat{
false};
189 if (strClassName !=
"nullptr")
195 "Stored object has class '%s' which is not registered!",
196 strClassName.c_str());
200 obj.get() , strClassName, isOldFormat,
204 return typename T::Ptr();
208 return std::dynamic_pointer_cast<T>(
obj);
213 template <
typename RET>
216 throw std::runtime_error(
"Can't match variant type");
220 template <
typename RET,
typename T,
typename...
R>
225 if (
IS_CLASS(ptr,
typename T::element_type))
226 return std::dynamic_pointer_cast<
typename T::element_type>(ptr);
230 template <
typename RET,
typename T,
typename...
R>
235 if (
IS_CLASS(*ptr, T))
return dynamic_cast<T&
>(*ptr);
251 template <
typename... T>
263 "Stored object has class '%s' which is not registered!",
264 strClassName.c_str());
265 if (strClassName !=
"nullptr")
272 return std::variant<T...>();
280 #if !defined(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 template <
class T,
class... Ts>
392 using is_any = std::disjunction<std::is_same<T, Ts>...>;
394 template <
typename T>
399 #if MRPT_IS_BIG_ENDIAN 413 in.ReadBuffer((
void*)&
b,
sizeof(
a));
439 #define MRPT_READ_POD(_STREAM, _VARIABLE) \ 442 const std::remove_cv_t<std::remove_reference_t<decltype(_VARIABLE)>> \ 443 val = _STREAM.ReadPOD<std::remove_cv_t< \ 444 std::remove_reference_t<decltype(_VARIABLE)>>>(); \ 445 ::memcpy(&_VARIABLE, &val, sizeof(val)); \ 456 CArchive& operator<<(CArchive&, const std::vector<int32_t>&
a);
457 CArchive& operator<<(CArchive&, const std::vector<uint32_t>&
a);
458 CArchive& operator<<(CArchive&, const std::vector<uint16_t>&
a);
459 CArchive& operator<<(CArchive&, const std::vector<int16_t>&
a);
460 CArchive& operator<<(CArchive&, const std::vector<uint32_t>&
a);
461 CArchive& operator<<(CArchive&, const std::vector<int64_t>&
a);
462 CArchive& operator<<(CArchive&, const std::vector<uint8_t>&
a);
463 CArchive& operator<<(CArchive&, const std::vector<int8_t>&
a);
465 CArchive& operator<<(CArchive&, const std::vector<bool>&
a);
466 CArchive& operator<<(CArchive&, const std::vector<std::string>&);
468 #if MRPT_WORD_SIZE != 32 // If it's 32 bit, size_t <=> uint32_t 469 CArchive& operator<<(CArchive&, const std::vector<size_t>&
a);
476 CArchive&
operator>>(CArchive&
in, std::vector<uint32_t>&
a);
477 CArchive&
operator>>(CArchive&
in, std::vector<uint16_t>&
a);
484 CArchive&
operator>>(CArchive&
in, std::vector<std::string>&
a);
488 CArchive&
operator>>(CArchive&
s, std::vector<float>&
a);
489 CArchive&
operator>>(CArchive&
s, std::vector<double>&
a);
490 CArchive& operator<<(CArchive& s, const std::vector<float>&
a);
491 CArchive& operator<<(CArchive& s, const std::vector<double>&
a);
493 #if MRPT_WORD_SIZE != 32 // If it's 32 bit, size_t <=> uint32_t 494 CArchive&
operator>>(CArchive&
s, std::vector<size_t>&
a);
499 typename T, std::enable_if_t<std::is_base_of_v<
503 pObj =
in.ReadObject<T>();
507 template <
typename... T>
514 template <
typename... T>
517 pObj.match([&](
auto&
t) { out <<
t; });
523 class T, std::enable_if_t<!std::is_base_of_v<
525 CArchive& operator<<(CArchive& out, const std::shared_ptr<T>& pObj)
529 out << mrpt::typemeta::TTypeName<T>::get();
541 class T, std::enable_if_t<!std::is_base_of_v<
564 template <
class STREAM>
573 size_t write(
const void* d,
size_t n)
override {
return m_s.Write(d,
n); }
574 size_t read(
void* d,
size_t n)
override {
return m_s.Read(d,
n); }
585 template <
class STREAM>
std::shared_ptr< CObject > createObject() const
T ReadPOD()
Reads a simple POD type and returns by value.
CExceptionEOF(const std::string &s)
unsigned __int16 uint16_t
size_t read(void *d, size_t n) override
Reads a block of bytes.
std::chrono::time_point< Clock > time_point
std::disjunction< std::is_same< T, Ts >... > is_any
void WriteBufferFixEndianness(const T *ptr, size_t ElementCount)
Writes a sequence of elemental datatypes, taking care of reordering their bytes from the running arch...
A structure that holds runtime class type information.
void WriteObject(const CSerializable &o)
void WriteAs(const TYPE_FROM_ACTUAL &value)
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 sendMessage(const CMessage &msg)
Send a message to 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) ...
GLsizei GLsizei GLuint * obj
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.
This is useful for checking ::Ptr types.
CArchiveStreamBase< STREAM > archiveFrom(STREAM &s)
Helper function to create a templatized wrapper CArchive object for a: MRPT's CStream, std::istream, std::ostream, std::stringstream.
void internal_ReadObjectHeader(std::string &className, bool &isOldFormat, int8_t &version)
Read the object Header.
virtual ~CArchive()=default
void WriteVariant(T t)
Writes a Variant to the stream.
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...
#define ASSERT_EQUAL_(__A, __B)
Assert comparing two values, reporting their actual values upon failure.
STORED_TYPE ReadAs()
De-serialize a variable and returns it by value.
CArchiveStreamBase(STREAM &s)
size_t write(const void *d, size_t n) override
Writes a block of bytes.
CArchive & operator<<(const CSerializable &obj)
Write a CSerializable object to a stream in the binary MRPT format.
T::Ptr ReadObject()
Reads an object from stream, its class determined at runtime, and returns a smart pointer to the obje...
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
#define IS_CLASS(obj, class_name)
True if the given reference to object (derived from mrpt::rtti::CObject) is of the given class...
GLsizei const GLchar ** string
bool receiveMessage(CMessage &msg)
Tries to receive a message from the device.
virtual size_t write(const void *buf, size_t len)=0
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.
CArchive for mrpt::io::CStream classes (use as template argument).
CArchive & operator>>(CArchive &s, mrpt::aligned_std_vector< float > &a)
unsigned __int64 uint64_t
Virtual base class for "archives": classes abstracting I/O streams.
A class that contain generic messages, that can be sent and received from a "CClientTCPSocket" object...
void internal_ReadObject(CSerializable *newObj, const std::string &className, bool isOldFormat, int8_t version)
Read the object.
RET ReadVariant_helper(CSerializable::Ptr &ptr)
RET ReadVariant_helper(CSerializable::Ptr &ptr, std::enable_if_t< mrpt::is_shared_ptr< T >::value > *=nullptr)
CSerializable::Ptr ReadObject()
Reads an object from stream, its class determined at runtime, and returns a smart pointer to the obje...
CArchive & operator<<(CArchive &s, const mrpt::aligned_std_vector< float > &a)
The virtual base class which provides a unified interface for all persistent objects in MRPT...
size_t ReadBuffer(void *Buffer, size_t Count)
Reads a block of bytes from the stream into Buffer.
static CAST_TO::Ptr from(const CAST_FROM_PTR &ptr)
void WriteObject(const CSerializable *o)
Writes an object to the stream.
GLsizei const GLfloat * value
#define THROW_EXCEPTION_FMT(_FORMAT_STRING,...)
unsigned __int32 uint32_t
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...
GLubyte GLubyte GLubyte a
std::variant< T... > ReadVariant()
Reads a variant from stream, its class determined at runtime, and returns a variant to the object...
Used in mrpt::serialization::CArchive.
void memcpy(void *dest, size_t destSize, const void *src, size_t copyCount) noexcept
An OS and compiler independent version of "memcpy".