11 #include <mrpt/config.h> 21 #include <type_traits> 27 #if defined(__clang__) && (__clang_major__ <= 7) 28 #define HAS_BROKEN_CLANG_STD_VISIT 96 #if !MRPT_IS_BIG_ENDIAN 98 return ReadBuffer(ptr, ElementCount *
sizeof(T));
101 const size_t nread =
ReadBuffer(ptr, ElementCount *
sizeof(T));
102 for (
size_t i = 0; i < ElementCount; i++)
113 void WriteBuffer(
const void* Buffer,
size_t Count);
132 template <
typename T>
135 #if !MRPT_IS_BIG_ENDIAN 140 for (
size_t i = 0; i < ElementCount; i++) (*
this) << ptr[i];
146 template <
typename STORED_TYPE,
typename CAST_TO_TYPE>
151 read_here =
static_cast<CAST_TO_TYPE
>(var);
154 template <
typename STORED_TYPE>
161 template <
typename TYPE_TO_STORE,
typename TYPE_FROM_ACTUAL>
164 (*this) <<
static_cast<TYPE_TO_STORE
>(value);
187 template <
typename T>
191 std::string strClassName;
192 bool isOldFormat{
false};
195 if (strClassName !=
"nullptr")
201 "Stored object has class '%s' which is not registered!",
202 strClassName.c_str());
206 obj.get() , strClassName, isOldFormat,
210 return typename T::Ptr();
214 return std::dynamic_pointer_cast<T>(obj);
219 template <
typename RET>
222 throw std::runtime_error(
"Can't match variant type");
226 template <
typename RET,
typename T,
typename...
R>
231 if (
IS_CLASS(*ptr,
typename T::element_type))
232 return std::dynamic_pointer_cast<
typename T::element_type>(ptr);
236 template <
typename RET,
typename T,
typename...
R>
241 if (
IS_CLASS(*ptr, T))
return dynamic_cast<T&
>(*ptr);
257 template <
typename... T>
261 std::string strClassName;
269 "Stored object has class '%s' which is not registered!",
270 strClassName.c_str());
271 if (strClassName !=
"nullptr")
278 return std::variant<T...>();
286 #if !defined(HAS_BROKEN_CLANG_STD_VISIT) 288 template <
typename T>
291 std::visit([&](
auto& o) { this->
WriteObject(o); }, t);
302 template <
typename T>
377 virtual size_t write(
const void* buf,
size_t len) = 0;
382 virtual size_t read(
void* buf,
size_t len) = 0;
387 CSerializable* newObj,
const std::string& className,
bool isOldFormat,
392 std::string& className,
bool& isOldFormat, int8_t& version);
397 template <
class T,
class... Ts>
398 using is_any = std::disjunction<std::is_same<T, Ts>...>;
400 template <
typename T>
402 T, bool, uint8_t, int8_t, uint16_t, int16_t, uint32_t, int32_t, uint64_t,
403 int64_t, float, double,
long double>;
405 #if MRPT_IS_BIG_ENDIAN 407 template <typename T, std::enable_if_t<is_simple_type<T>::value,
int> = 0>
411 out.WriteBuffer((
void*)&a,
sizeof(a));
415 template <typename T, std::enable_if_t<is_simple_type<T>::value,
int> = 0>
427 template <typename T, std::enable_if_t<is_simple_type<T>::value,
int> = 0>
430 out.WriteBuffer((
void*)&a,
sizeof(a));
434 template <typename T, std::enable_if_t<is_simple_type<T>::value,
int> = 0>
445 #define MRPT_READ_POD(_STREAM, _VARIABLE) \ 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)); \ 460 CArchive&
operator<<(CArchive& s,
const std::string& str);
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);
471 CArchive& operator<<(CArchive&, const std::vector<bool>& a);
472 CArchive& operator<<(CArchive&, const std::vector<std::string>&);
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);
479 CArchive&
operator>>(CArchive& in, std::string& str);
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);
490 CArchive&
operator>>(CArchive& in, std::vector<std::string>& a);
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);
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);
505 typename T, std::enable_if_t<std::is_base_of_v<
513 template <
typename... T>
520 template <
typename... T>
523 pObj.match([&](
auto& t) {
out << t; });
529 class T, std::enable_if_t<!std::is_base_of_v<
531 CArchive& operator<<(CArchive& out, const std::shared_ptr<T>& pObj)
535 out << mrpt::typemeta::TTypeName<T>::get();
540 out << std::string(
"nullptr");
547 class T, std::enable_if_t<!std::is_base_of_v<
551 std::string stored_name;
553 const std::string expected_name =
555 if (stored_name == std::string(
"nullptr"))
570 template <
class STREAM>
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); }
591 template <
class STREAM>
598 template <
class STREAM>
601 return std::make_shared<CArchiveStreamBase<STREAM>>(s);
605 template <
class STREAM>
608 return std::make_unique<CArchiveStreamBase<STREAM>>(s);
std::shared_ptr< CObject > createObject() const
T ReadPOD()
Reads a simple POD type and returns by value.
CExceptionEOF(const std::string &s)
size_t read(void *d, size_t n) override
Reads a block of bytes.
std::chrono::time_point< Clock > time_point
CArchive & WriteAs(const TYPE_FROM_ACTUAL &value)
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 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) ...
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::Ptr archivePtrFrom(STREAM &s)
Like archiveFrom(), returning a shared_ptr<>.
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...
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.
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).
CArchive & operator>>(CArchive &s, mrpt::aligned_std_vector< float > &a)
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.
mrpt::vision::TStereoCalibResults out
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.
CArchive::UniquePtr archiveUniquePtrFrom(STREAM &s)
Like archiveFrom(), returning a unique_ptr<>.
static CAST_TO::Ptr from(const CAST_FROM_PTR &ptr)
void WriteObject(const CSerializable *o)
Writes an object to the stream.
#define THROW_EXCEPTION_FMT(_FORMAT_STRING,...)
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...
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".
std::unique_ptr< CArchive > UniquePtr