19 #include <mrpt/otherlibs/mapbox/variant.hpp> 21 #include <type_traits> 55 virtual size_t Read(
void* Buffer,
size_t Count) = 0;
60 virtual size_t Write(
const void* Buffer,
size_t Count) = 0;
107 template <
typename T>
110 #if !MRPT_IS_BIG_ENDIAN 112 return ReadBuffer(ptr, ElementCount *
sizeof(T));
115 const size_t nread =
ReadBuffer(ptr, ElementCount *
sizeof(T));
116 for (
size_t i = 0; i < ElementCount; i++)
139 void WriteBuffer(
const void* Buffer,
size_t Count);
158 template <
typename T>
161 #if !MRPT_IS_BIG_ENDIAN 166 for (
size_t i = 0; i < ElementCount; i++) (*
this) << ptr[i];
216 template <
typename T>
224 if (strClassName !=
"nullptr")
230 obj.get() , strClassName, isOldFormat,
234 return typename T::Ptr();
238 return std::dynamic_pointer_cast<T>(
obj);
243 template <
typename RET>
250 template <
typename RET,
typename T,
typename...
R>
255 if (
IS_CLASS(ptr,
typename T::element_type))
256 return std::dynamic_pointer_cast<
typename T::element_type>(ptr);
260 template <
typename RET,
typename T,
typename...
R>
265 if (
IS_CLASS(ptr, T))
return dynamic_cast<T&
>(*ptr);
281 template <
typename... T>
292 "Stored object has class '%s' which is not registered!",
293 strClassName.c_str())
294 if (strClassName !=
"nullptr")
311 template <
typename T>
324 template <
typename T>
352 template <
typename STORED_TYPE,
typename CAST_TO_TYPE>
357 read_here =
static_cast<CAST_TO_TYPE
>(var);
363 virtual int printf(
const char* fmt, ...)
369 template <typename CONTAINER_TYPE>
371 const
char* fmt, const CONTAINER_TYPE& V,
char separator = ',')
374 const size_t N = V.size();
375 for (
size_t i = 0; i < N; i++)
378 if (i != (N - 1)) this->
printf(
"%c", separator);
433 #define DECLARE_CSTREAM_READ_WRITE_SIMPLE_TYPE(T) \ 434 CStream& operator<<(mrpt::utils::CStream& out, const T a); \ 435 CStream& operator>>(mrpt::utils::CStream& in, T& a); 449 #ifdef HAVE_LONG_DOUBLE 453 #define MRPT_READ_POD(_STREAM, _VARIABLE) \ 456 const std::remove_cv_t<std::remove_reference_t<decltype(_VARIABLE)>> \ 457 val = _STREAM.ReadPOD<std::remove_cv_t< \ 458 std::remove_reference_t<decltype(_VARIABLE)>>>(); \ 459 ::memcpy(&_VARIABLE, &val, sizeof(val)); \ 480 CStream& operator<<(mrpt::utils::CStream&, const std::vector<std::string>&);
482 #if MRPT_WORD_SIZE != 32 // If it's 32 bit, size_t <=> uint32_t 483 CStream& operator<<(mrpt::utils::CStream&, const std::vector<size_t>&
a);
505 CStream& operator<<(mrpt::utils::CStream& s, const std::vector<float>&
a);
506 CStream& operator<<(mrpt::utils::CStream& s, const std::vector<double>&
a);
508 #if MRPT_WORD_SIZE != 32 // If it's 32 bit, size_t <=> uint32_t 512 template <
typename T, std::enable_if_t<std::is_base_of<
521 template <
typename... T>
529 template <
typename... T>
533 pObj.match([&](
auto&
t) { out <<
t; });
virtual uint64_t getPosition()=0
Method for getting the current cursor position, where 0 is the first byte and TotalBytesCount-1 the l...
void WriteObject(const CSerializable &o)
size_t ReadBuffer(void *Buffer, size_t Count)
Reads a block of bytes from the stream into Buffer.
virtual size_t Read(void *Buffer, size_t Count)=0
Introduces a pure virtual method responsible for reading from the stream.
bool getline(std::string &out_str)
Reads from the stream until a ' ' character is found ('' characters are ignored). ...
unsigned __int16 uint16_t
std::vector< uint32_t > vector_uint
virtual size_t ReadBufferImmediate(void *Buffer, size_t Count)
Reads a block of bytes from the stream into Buffer, and returns the amound of bytes actually read...
std::vector< uint8_t > vector_byte
TSeekOrigin
Used in CStream::Seek.
The virtual base class which provides a unified interface for all persistent objects in MRPT...
CStream & operator>>(CSerializable::Ptr &pObj)
void internal_ReadObjectHeader(std::string &className, bool &isOldFormat, int8_t &version)
Read the object Header.
#define THROW_EXCEPTION(msg)
#define THROW_EXCEPTION_FMT(_FORMAT_STRING,...)
void WriteBuffer(const void *Buffer, size_t Count)
Writes a block of bytes to the stream from Buffer.
T ReadPOD()
Reads a simple POD type and returns by value.
mrpt::utils::variant< T... > ReadVariant()
Reads a variant from stream, its class determined at runtime, and returns a variant to the object...
RET ReadVariant_helper(CSerializable::Ptr &ptr)
T::Ptr ReadObject()
Reads an object from stream, its class determined at runtime, and returns a smart pointer to the obje...
GLsizei GLsizei GLuint * obj
std::vector< int8_t > vector_signed_byte
#define MRPT_printf_format_check(_FMT_, _VARARGS_)
std::vector< bool > vector_bool
A type for passing a vector of bools.
std::vector< int64_t > vector_long
virtual size_t Write(const void *Buffer, size_t Count)=0
Introduces a pure virtual method responsible for writing to the stream.
void WriteBufferFixEndianness(const T *ptr, size_t ElementCount)
Writes a sequence of elemental datatypes, taking care of reordering their bytes from the running arch...
This base class is used to provide a unified interface to files,memory buffers,..Please see the deriv...
CSerializable::Ptr ReadObject()
Reads an object from stream, its class determined at runtime, and returns a smart pointer to the obje...
mrpt::utils::CObject * createObject() const
#define DECLARE_CSTREAM_READ_WRITE_SIMPLE_TYPE(T)
virtual int void printf_vector(const char *fmt, const CONTAINER_TYPE &V, char separator=',')
Prints a vector in the format [A,B,C,...] using CStream::printf, and the fmt string for each vector e...
virtual uint64_t getTotalBytesCount()=0
Returns the total amount of bytes in the stream.
mapbox::util::variant< T... > variant
void WriteObject(const CSerializable *o)
Writes an object to the stream.
CStream & operator<<(mrpt::utils::CStream &s, const char *a)
GLsizei const GLchar ** string
RET ReadVariant_helper(CSerializable::Ptr &ptr, std::enable_if_t<!is_shared_ptr< T >::value > *=nullptr)
std::shared_ptr< CSerializable > Ptr
const TRuntimeClassId * findRegisteredClass(const std::string &className)
Return info about a given class by its name, or nullptr if the class is not registered.
unsigned __int64 uint64_t
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...
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
bool receiveMessage(utils::CMessage &msg)
Tries to receive a message from 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...
void internal_ReadObject(CSerializable *newObj, const std::string &className, bool isOldFormat, int8_t version)
Read the object.
std::vector< uint16_t > vector_word
#define IS_CLASS(ptrObj, class_name)
Evaluates to true if the given pointer to an object (derived from mrpt::utils::CSerializable) is of t...
std::vector< int16_t > vector_signed_word
CStream & operator<<(const CSerializable::Ptr &pObj)
Write an object to a stream in the binary MRPT format.
void WriteVariant(T t)
Writes a Variant to the stream.
void sendMessage(const utils::CMessage &msg)
Send a message to the device.
This is useful for checking ::Ptr types.
RET ReadVariant_helper(CSerializable::Ptr &ptr, std::enable_if_t< is_shared_ptr< T >::value > *=nullptr)
std::vector< int32_t > vector_int
GLsizei const GLfloat * value
CStream & operator>>(mrpt::utils::CStream &in, char *a)
A structure that holds runtime class type information.
void reverseBytesInPlace(bool &v_in_out)
Reverse the order of the bytes of a given type (useful for transforming btw little/big endian) ...
unsigned __int32 uint32_t
GLubyte GLubyte GLubyte a
A class that contain generic messages, that can be sent and received from a "CClientTCPSocket" object...
virtual uint64_t Seek(uint64_t Offset, CStream::TSeekOrigin Origin=sFromBeginning)=0
Introduces a pure virtual method for moving to a specified position in the streamed resource...
virtual int printf(const char *fmt,...) MRPT_printf_format_check(2
Writes a string to the stream in a textual form.