MRPT  1.9.9
List of all members | Public Types | Public Member Functions | Protected Member Functions
mrpt::serialization::CArchive Class Referenceabstract

Detailed Description

Virtual base class for "archives": classes abstracting I/O streams.

This class separates the implementation details of serialization (in CSerializable) and data storage (CArchive children: files, sockets,...).

Two main sets of implementations are provided:

See also
mrpt::io::CArchive, mrpt::serialization::CSerializable

Definition at line 54 of file CArchive.h.

#include <mrpt/serialization/CArchive.h>

Inheritance diagram for mrpt::serialization::CArchive:

Public Types

using Ptr = std::shared_ptr< CArchive >
 
using UniquePtr = std::unique_ptr< CArchive >
 

Public Member Functions

 CArchive ()=default
 
virtual ~CArchive ()=default
 

Protected Member Functions

void internal_ReadObject (CSerializable *newObj, const std::string &className, bool isOldFormat, int8_t version)
 Read the object. More...
 
void internal_ReadObjectHeader (std::string &className, bool &isOldFormat, int8_t &version)
 Read the object Header. More...
 
Virtual methods of the CArchive interface
virtual size_t write (const void *buf, size_t len)=0
 Writes a block of bytes. More...
 
virtual size_t read (void *buf, size_t len)=0
 Reads a block of bytes. More...
 

Serialization API for generic "archives" I/O streams

size_t ReadBuffer (void *Buffer, size_t Count)
 Reads a block of bytes from the stream into Buffer. More...
 
template<typename 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 standard (little endianness) to the format of the running architecture. More...
 
void WriteBuffer (const void *Buffer, size_t Count)
 Writes a block of bytes to the stream from Buffer. More...
 
template<typename T >
void WriteBufferFixEndianness (const T *ptr, size_t ElementCount)
 Writes a sequence of elemental datatypes, taking care of reordering their bytes from the running architecture to MRPT stream standard (little endianness). More...
 
template<typename STORED_TYPE , typename CAST_TO_TYPE >
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 via static_cast. More...
 
template<typename STORED_TYPE >
STORED_TYPE ReadAs ()
 De-serialize a variable and returns it by value. More...
 
template<typename TYPE_TO_STORE , typename TYPE_FROM_ACTUAL >
void WriteAs (const TYPE_FROM_ACTUAL &value)
 
void WriteObject (const CSerializable *o)
 Writes an object to the stream. More...
 
void WriteObject (const CSerializable &o)
 
CSerializable::Ptr ReadObject ()
 Reads an object from stream, its class determined at runtime, and returns a smart pointer to the object. More...
 
template<typename T >
T::Ptr ReadObject ()
 Reads an object from stream, its class determined at runtime, and returns a smart pointer to the object. More...
 
template<typename... T>
std::variant< T... > ReadVariant ()
 Reads a variant from stream, its class determined at runtime, and returns a variant to the object. More...
 
template<typename T >
void WriteVariant (T t)
 Writes a Variant to the stream. More...
 
template<typename T >
ReadPOD ()
 Reads a simple POD type and returns by value. More...
 
void ReadObject (CSerializable *existingObj)
 Reads an object from stream, where its class must be the same as the supplied object, where the loaded object will be stored in. More...
 
void sendMessage (const CMessage &msg)
 Send a message to the device. More...
 
bool receiveMessage (CMessage &msg)
 Tries to receive a message from the device. More...
 
CArchiveoperator<< (const CSerializable &obj)
 Write a CSerializable object to a stream in the binary MRPT format. More...
 
CArchiveoperator<< (const CSerializable::Ptr &pObj)
 
CArchiveoperator>> (CSerializable &obj)
 Reads a CSerializable object from the stream. More...
 
CArchiveoperator>> (CSerializable::Ptr &pObj)
 
template<typename RET >
RET ReadVariant_helper (CSerializable::Ptr &ptr)
 
template<typename RET , typename T , typename... R>
RET ReadVariant_helper (CSerializable::Ptr &ptr, std::enable_if_t< mrpt::is_shared_ptr< T >::value > *=nullptr)
 
template<typename RET , typename T , typename... R>
RET ReadVariant_helper (CSerializable::Ptr &ptr, std::enable_if_t<!mrpt::is_shared_ptr< T >::value > *=nullptr)
 

Member Typedef Documentation

◆ Ptr

Definition at line 60 of file CArchive.h.

◆ UniquePtr

Definition at line 61 of file CArchive.h.

Constructor & Destructor Documentation

◆ CArchive()

mrpt::serialization::CArchive::CArchive ( )
default

◆ ~CArchive()

virtual mrpt::serialization::CArchive::~CArchive ( )
virtualdefault

Member Function Documentation

◆ internal_ReadObject()

void CArchive::internal_ReadObject ( CSerializable newObj,
const std::string className,
bool  isOldFormat,
int8_t  version 
)
protected

Read the object.

Definition at line 428 of file CArchive.cpp.

References ReadBuffer(), SERIALIZATION_END_FLAG, THROW_EXCEPTION, THROW_EXCEPTION_FMT, and THROW_STACKED_EXCEPTION_CUSTOM_MSG2.

Referenced by ReadObject(), and ReadVariant().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ internal_ReadObjectHeader()

void CArchive::internal_ReadObjectHeader ( std::string className,
bool &  isOldFormat,
int8_t &  version 
)
protected

Read the object Header.

Definition at line 329 of file CArchive.cpp.

References ASSERT_, ReadBuffer(), ReadBufferFixEndianness(), THROW_EXCEPTION, THROW_STACKED_EXCEPTION_CUSTOM_MSG2, and THROW_TYPED_EXCEPTION.

Referenced by ReadObject(), and ReadVariant().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ operator<<() [1/2]

CArchive & CArchive::operator<< ( const CSerializable obj)

Write a CSerializable object to a stream in the binary MRPT format.

Write an object to a stream in the binary MRPT format.

Definition at line 146 of file CArchive.cpp.

References WriteObject().

Here is the call graph for this function:

◆ operator<<() [2/2]

CArchive & CArchive::operator<< ( const CSerializable::Ptr pObj)

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Definition at line 139 of file CArchive.cpp.

References WriteObject().

Here is the call graph for this function:

◆ operator>>() [1/2]

CArchive & CArchive::operator>> ( CSerializable obj)

Reads a CSerializable object from the stream.

Definition at line 158 of file CArchive.cpp.

References ReadObject().

Here is the call graph for this function:

◆ operator>>() [2/2]

CArchive & CArchive::operator>> ( CSerializable::Ptr pObj)

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Definition at line 152 of file CArchive.cpp.

References ReadObject().

Here is the call graph for this function:

◆ read()

virtual size_t mrpt::serialization::CArchive::read ( void buf,
size_t  len 
)
protectedpure virtual

Reads a block of bytes.

Exceptions
std::exceptionOn any error, or if ZERO bytes are read.
Returns
Number of bytes actually read if >0.

Implemented in mrpt::serialization::CArchiveStreamBase< STREAM >, mrpt::serialization::CArchiveStreamBase< std::iostream >, mrpt::serialization::CArchiveStreamBase< const std::vector< uint8_t > >, mrpt::serialization::CArchiveStreamBase< std::ostream >, mrpt::serialization::CArchiveStreamBase< std::vector< uint8_t > >, and mrpt::serialization::CArchiveStreamBase< std::istream >.

Referenced by ReadBuffer().

Here is the caller graph for this function:

◆ ReadAs()

template<typename STORED_TYPE >
STORED_TYPE mrpt::serialization::CArchive::ReadAs ( )
inline

De-serialize a variable and returns it by value.

Definition at line 155 of file CArchive.h.

◆ ReadAsAndCastTo()

template<typename STORED_TYPE , typename CAST_TO_TYPE >
void mrpt::serialization::CArchive::ReadAsAndCastTo ( CAST_TO_TYPE &  read_here)
inline

Read a value from a stream stored in a type different of the target variable, making the conversion via static_cast.

Useful for coding backwards compatible de-serialization blocks

Definition at line 147 of file CArchive.h.

Referenced by mrpt::maps::CPointsMap::TRenderOptions::readFromStream().

Here is the caller graph for this function:

◆ ReadBuffer()

size_t CArchive::ReadBuffer ( void Buffer,
size_t  Count 
)

Reads a block of bytes from the stream into Buffer.

Exceptions
std::exceptionOn any error, or if ZERO bytes are read.
Returns
The amound of bytes actually read.
Note
This method is endianness-dependent.
See also
ReadBufferImmediate ; Important, see: ReadBufferFixEndianness,

Definition at line 25 of file CArchive.cpp.

References ASSERT_, read(), and THROW_EXCEPTION.

Referenced by internal_ReadObject(), internal_ReadObjectHeader(), mrpt::serialization::operator>>(), ReadBufferFixEndianness(), and receiveMessage().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ ReadBufferFixEndianness()

template<typename T >
size_t mrpt::serialization::CArchive::ReadBufferFixEndianness ( T *  ptr,
size_t  ElementCount 
)
inline

Reads a sequence of elemental datatypes, taking care of reordering their bytes from the MRPT stream standard (little endianness) to the format of the running architecture.

Parameters
ElementCountThe number of elements (not bytes) to read.
ptrA pointer to the first output element in an array (or std::vector<>, etc...).
Returns
The amound of bytes (not elements) actually read (under error situations, the last element may be invalid if the data stream abruptly ends). Example of usage:
uint32_t N;
s >> N;
vector<float> vec(N);
if (N)
s.ReadBufferFixEndianness<float>(&vec[0],N);
Exceptions
std::exceptionOn any error, or if ZERO bytes are read.
See also
ReadBufferFixEndianness, ReadBuffer

Definition at line 94 of file CArchive.h.

References ReadBuffer(), and mrpt::reverseBytesInPlace().

Referenced by internal_ReadObjectHeader(), mrpt::math::operator>>(), ReadPOD(), and triangle_readFromStream().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ ReadObject() [1/3]

CSerializable::Ptr mrpt::serialization::CArchive::ReadObject ( )
inline

Reads an object from stream, its class determined at runtime, and returns a smart pointer to the object.

Exceptions
std::exceptionOn I/O error or undefined class.
CExceptionEOFOn an End-Of-File condition found at a correct place: an EOF that abruptly finishes in the middle of one object raises a plain std::exception instead.

Definition at line 177 of file CArchive.h.

Referenced by operator>>().

Here is the caller graph for this function:

◆ ReadObject() [2/3]

template<typename T >
T::Ptr mrpt::serialization::CArchive::ReadObject ( )
inline

Reads an object from stream, its class determined at runtime, and returns a smart pointer to the object.

This version is similar to std::make_shared<T>.

Exceptions
std::exceptionOn I/O error or undefined class.
CExceptionEOFOn an End-Of-File condition found at a correct place: an EOF that abruptly finishes in the middle of one object raises a plain std::exception instead.

Definition at line 187 of file CArchive.h.

References mrpt::rtti::TRuntimeClassId::createObject(), mrpt::rtti::findRegisteredClass(), mrpt::ptr_cast< CAST_TO >::from(), internal_ReadObject(), internal_ReadObjectHeader(), and THROW_EXCEPTION_FMT.

Here is the call graph for this function:

◆ ReadObject() [3/3]

void CArchive::ReadObject ( CSerializable existingObj)

Reads an object from stream, where its class must be the same as the supplied object, where the loaded object will be stored in.

Exceptions
std::exceptionOn I/O error or different class found.
CExceptionEOFOn an End-Of-File condition found at a correct place: an EOF that abruptly finishes in the middle of one object raises a plain std::exception instead.

Definition at line 473 of file CArchive.cpp.

References ASSERT_, mrpt::rtti::findRegisteredClass(), mrpt::format(), mrpt::serialization::CSerializable::GetRuntimeClass(), internal_ReadObject(), internal_ReadObjectHeader(), THROW_EXCEPTION, and THROW_EXCEPTION_FMT.

Here is the call graph for this function:

◆ ReadPOD()

template<typename T >
T mrpt::serialization::CArchive::ReadPOD ( )
inline

Reads a simple POD type and returns by value.

Useful when stream >> var; cannot be used becuase of errors of misaligned reference binding. Use with macro MRPT_READ_POD to avoid typing the type T yourself.

Note
[New in MRPT 2.0.0]
Write operator s << var; is safe for misaligned variables.

Definition at line 302 of file CArchive.h.

References ReadBufferFixEndianness().

Here is the call graph for this function:

◆ ReadVariant()

template<typename... T>
std::variant<T...> mrpt::serialization::CArchive::ReadVariant ( )
inline

Reads a variant from stream, its class determined at runtime, and returns a variant to the object.

To be compatible with the current polymorphic system this support smart pointer types. For pointer types, This will bind to the first possible pointer type. variant<CSerializable::Ptr, CRenderizable::Ptr>

Exceptions
std::exceptionOn I/O error or undefined class.
CExceptionEOFOn an End-Of-File condition found at a correct place: an EOF that abruptly finishes in the middle of one object raises a plain std::exception instead.

Definition at line 257 of file CArchive.h.

References mrpt::rtti::TRuntimeClassId::createObject(), mrpt::rtti::findRegisteredClass(), mrpt::ptr_cast< CAST_TO >::from(), internal_ReadObject(), internal_ReadObjectHeader(), ReadVariant_helper(), and THROW_EXCEPTION_FMT.

Referenced by mrpt::serialization::operator>>().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ ReadVariant_helper() [1/3]

template<typename RET >
RET mrpt::serialization::CArchive::ReadVariant_helper ( CSerializable::Ptr ptr)
inlineprivate

Definition at line 219 of file CArchive.h.

Referenced by ReadVariant(), and ReadVariant_helper().

Here is the caller graph for this function:

◆ ReadVariant_helper() [2/3]

template<typename RET , typename T , typename... R>
RET mrpt::serialization::CArchive::ReadVariant_helper ( CSerializable::Ptr ptr,
std::enable_if_t< mrpt::is_shared_ptr< T >::value > *  = nullptr 
)
inlineprivate

Definition at line 226 of file CArchive.h.

References IS_CLASS, R, and ReadVariant_helper().

Here is the call graph for this function:

◆ ReadVariant_helper() [3/3]

template<typename RET , typename T , typename... R>
RET mrpt::serialization::CArchive::ReadVariant_helper ( CSerializable::Ptr ptr,
std::enable_if_t<!mrpt::is_shared_ptr< T >::value > *  = nullptr 
)
inlineprivate

Definition at line 236 of file CArchive.h.

References IS_CLASS, R, and ReadVariant_helper().

Here is the call graph for this function:

◆ receiveMessage()

bool CArchive::receiveMessage ( CMessage msg)

Tries to receive a message from the device.

Exceptions
std::exceptionOn communication errors
Returns
True if successful, false if there is no new data from the device (but communications seem to work fine)
See also
The frame format is described in sendMessage()

Definition at line 555 of file CArchive.cpp.

References mrpt::serialization::CMessage::content, MAKEWORD16B, mrpt::system::os::memcpy(), MRPT_END, MRPT_START, ReadBuffer(), and mrpt::serialization::CMessage::type.

Here is the call graph for this function:

◆ sendMessage()

void CArchive::sendMessage ( const CMessage msg)

Send a message to the device.

Note that only the low byte from the "type" field will be used.

For frames of size < 255 the frame format is an array of bytes in this order:

<START_FLAG> <HEADER> <LENGTH> <BODY> <END_FLAG>
<START_FLAG> = 0x69
<HEADER> = A header byte
<LENGHT> = Number of bytes of BODY
<BODY> = N x bytes
<END_FLAG> = 0X96
Total length = <LENGTH> + 4

For frames of size > 255 the frame format is an array of bytes in this order:

<START_FLAG> <HEADER> <HIBYTE(LENGTH)> <LOBYTE(LENGTH)> <BODY>
<END_FLAG>
<START_FLAG> = 0x79
<HEADER> = A header byte
<LENGHT> = Number of bytes of BODY
<BODY> = N x bytes
<END_FLAG> = 0X96
Total length = <LENGTH> + 5
Exceptions
std::exceptionOn communication errors

Definition at line 521 of file CArchive.cpp.

References mrpt::serialization::CMessage::content, mrpt::system::os::memcpy(), MRPT_END, MRPT_START, mrpt::serialization::CMessage::type, and WriteBuffer().

Here is the call graph for this function:

◆ write()

virtual size_t mrpt::serialization::CArchive::write ( const void buf,
size_t  len 
)
protectedpure virtual

Writes a block of bytes.

Exceptions
std::exceptionOn any error
Returns
Number of bytes actually written.

Implemented in mrpt::serialization::CArchiveStreamBase< STREAM >, mrpt::serialization::CArchiveStreamBase< std::iostream >, mrpt::serialization::CArchiveStreamBase< const std::vector< uint8_t > >, mrpt::serialization::CArchiveStreamBase< std::ostream >, mrpt::serialization::CArchiveStreamBase< std::vector< uint8_t > >, and mrpt::serialization::CArchiveStreamBase< std::istream >.

Referenced by WriteBuffer().

Here is the caller graph for this function:

◆ WriteAs()

template<typename TYPE_TO_STORE , typename TYPE_FROM_ACTUAL >
void mrpt::serialization::CArchive::WriteAs ( const TYPE_FROM_ACTUAL &  value)
inline

Definition at line 162 of file CArchive.h.

Referenced by mrpt::math::operator<<().

Here is the caller graph for this function:

◆ WriteBuffer()

void CArchive::WriteBuffer ( const void Buffer,
size_t  Count 
)

Writes a block of bytes to the stream from Buffer.

Exceptions
std::exceptionOn any error
See also
Important, see: WriteBufferFixEndianness
Note
This method is endianness-dependent.

Definition at line 49 of file CArchive.cpp.

References ASSERT_, THROW_EXCEPTION, and write().

Referenced by sendMessage(), WriteBufferFixEndianness(), and WriteObject().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ WriteBufferFixEndianness()

template<typename T >
void mrpt::serialization::CArchive::WriteBufferFixEndianness ( const T *  ptr,
size_t  ElementCount 
)
inline

Writes a sequence of elemental datatypes, taking care of reordering their bytes from the running architecture to MRPT stream standard (little endianness).

Parameters
ElementCountThe number of elements (not bytes) to write.
ptrA pointer to the first input element in an array (or std::vector<>, etc...). Example of usage:
vector<float> vec = ...
uint32_t N = vec.size();
s << N
if (N)
s.WriteBufferFixEndianness<float>(&vec[0],N);
Exceptions
std::exceptionOn any error
See also
WriteBuffer

Definition at line 133 of file CArchive.h.

References WriteBuffer().

Referenced by mrpt::math::operator<<(), and triangle_writeToStream().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ WriteObject() [1/2]

void CArchive::WriteObject ( const CSerializable o)

Writes an object to the stream.

Definition at line 101 of file CArchive.cpp.

References mrpt::rtti::TRuntimeClassId::className, mrpt::serialization::CSerializable::GetRuntimeClass(), MRPT_END, MRPT_START, SERIALIZATION_END_FLAG, mrpt::serialization::CSerializable::serializeGetVersion(), mrpt::serialization::CSerializable::serializeTo(), and WriteBuffer().

Referenced by operator<<(), and WriteVariant().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ WriteObject() [2/2]

void mrpt::serialization::CArchive::WriteObject ( const CSerializable o)
inline

Definition at line 169 of file CArchive.h.

References WriteObject().

Referenced by WriteObject().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ WriteVariant()

template<typename T >
void mrpt::serialization::CArchive::WriteVariant ( t)
inline

Writes a Variant to the stream.

Definition at line 288 of file CArchive.h.

References WriteObject().

Here is the call graph for this function:



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: ce444d842 Fri Dec 6 19:35:10 2019 +0100 at vie dic 6 19:45:12 CET 2019