MRPT  1.9.9
List of all members | Public Types | Public Member Functions | Protected Member Functions | Protected Attributes
mrpt::io::CMemoryStream Class Reference

Detailed Description

This CStream derived class allow using a memory buffer as a CStream.

This class is useful for storing any required set of variables or objects, and then read them to other objects, or storing them to a file, for example.

See also
CStream

Definition at line 26 of file io/CMemoryStream.h.

#include <mrpt/io/CMemoryStream.h>

Inheritance diagram for mrpt::io::CMemoryStream:

Public Types

enum  TSeekOrigin { sFromBeginning = 0, sFromCurrent = 1, sFromEnd = 2 }
 Used in CStream::Seek. More...
 

Public Member Functions

size_t Read (void *Buffer, size_t Count) override
 Introduces a pure virtual method responsible for reading from the stream. More...
 
size_t Write (const void *Buffer, size_t Count) override
 Introduces a pure virtual method responsible for writing to the stream. More...
 
 CMemoryStream ()=default
 Default constructor. More...
 
 CMemoryStream (const void *data, const uint64_t nBytesInData)
 Constructor to initilize the data in the stream from a block of memory (which is copied), and sets the current stream position at the beginning of the data. More...
 
void assignMemoryNotOwn (const void *data, const uint64_t nBytesInData)
 Initilize the data in the stream from a block of memory which is NEITHER OWNED NOR COPIED by the object, so it must exist during the whole live of the object. More...
 
 ~CMemoryStream () override
 Destructor. More...
 
void Clear ()
 Clears the memory buffer. More...
 
void changeSize (uint64_t newSize)
 Change size. More...
 
uint64_t Seek (int64_t Offset, CStream::TSeekOrigin Origin=sFromBeginning) override
 Introduces a pure virtual method for moving to a specified position in the streamed resource. More...
 
uint64_t getTotalBytesCount () const override
 Returns the total size of the internal buffer. More...
 
uint64_t getPosition () const override
 Method for getting the current cursor position, where 0 is the first byte and TotalBytesCount-1 the last one. More...
 
voidgetRawBufferData ()
 Method for getting a pointer to the raw stored data. More...
 
const voidgetRawBufferData () const
 
bool saveBufferToFile (const std::string &file_name)
 Saves the entire buffer to a file. More...
 
bool loadBufferFromFile (const std::string &file_name)
 Loads the entire buffer from a file *. More...
 
void setAllocBlockSize (uint64_t alloc_block_size)
 Change the size of the additional memory block that is reserved whenever the current block runs too short (default=0x10000 bytes) More...
 
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, without waiting for more extra bytes to arrive (just those already enqued in the stream). More...
 
virtual int printf (const char *fmt,...) MRPT_printf_format_check(2
 Writes a string to the stream in a textual form. More...
 
template<typename CONTAINER_TYPE >
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 element T. More...
 
bool getline (std::string &out_str)
 Reads from the stream until a '
' character is found ('' characters are ignored). More...
 

Protected Member Functions

void resize (uint64_t newSize)
 Resizes the internal buffer size. More...
 

Protected Attributes

void_ptr_noncopy m_memory {nullptr}
 Internal data. More...
 
uint64_t m_size {0}
 
uint64_t m_position {0}
 
uint64_t m_bytesWritten {0}
 
uint64_t m_alloc_block_size {0x1000}
 
bool m_read_only {false}
 If the memory block does not belong to the object. More...
 

Member Enumeration Documentation

◆ TSeekOrigin

Used in CStream::Seek.

Enumerator
sFromBeginning 
sFromCurrent 
sFromEnd 

Definition at line 32 of file io/CStream.h.

Constructor & Destructor Documentation

◆ CMemoryStream() [1/2]

mrpt::io::CMemoryStream::CMemoryStream ( )
default

Default constructor.

◆ CMemoryStream() [2/2]

CMemoryStream::CMemoryStream ( const void data,
const uint64_t  nBytesInData 
)

Constructor to initilize the data in the stream from a block of memory (which is copied), and sets the current stream position at the beginning of the data.

See also
assignMemoryNotOwn

Definition at line 22 of file CMemoryStream.cpp.

References ASSERT_, mrpt::non_copiable_ptr_basic< T >::get(), m_bytesWritten, m_memory, mrpt::system::os::memcpy(), MRPT_END, MRPT_START, and resize().

Here is the call graph for this function:

◆ ~CMemoryStream()

CMemoryStream::~CMemoryStream ( )
override

Destructor.

Definition at line 47 of file CMemoryStream.cpp.

References mrpt::non_copiable_ptr_basic< T >::get(), m_memory, m_position, m_read_only, and m_size.

Here is the call graph for this function:

Member Function Documentation

◆ assignMemoryNotOwn()

void CMemoryStream::assignMemoryNotOwn ( const void data,
const uint64_t  nBytesInData 
)

Initilize the data in the stream from a block of memory which is NEITHER OWNED NOR COPIED by the object, so it must exist during the whole live of the object.

After assigning a block of data with this method, the object becomes "read-only", so further attempts to change the size of the buffer will raise an exception. This method resets the write and read positions to the beginning.

Definition at line 36 of file CMemoryStream.cpp.

References Clear(), m_bytesWritten, m_memory, m_position, m_read_only, m_size, and mrpt::non_copiable_ptr_basic< T >::set().

Referenced by mrpt::obs::stock_observations::exampleImage(), and mrpt::serialization::mrpt_recv_from_zmq_buf().

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

◆ changeSize()

void CMemoryStream::changeSize ( uint64_t  newSize)

Change size.

This would be rarely used. Use ">>" operators for writing to stream

See also
Stream

Definition at line 166 of file CMemoryStream.cpp.

References resize().

Referenced by mrpt::img::CImage::serializeFrom().

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

◆ Clear()

void CMemoryStream::Clear ( )

Clears the memory buffer.

Definition at line 148 of file CMemoryStream.cpp.

References m_bytesWritten, m_memory, m_position, m_read_only, m_size, and resize().

Referenced by assignMemoryNotOwn(), loadBufferFromFile(), and mrpt::serialization::mrpt_recv_from_zmq_buf().

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

◆ getline()

bool CStream::getline ( std::string out_str)
inherited

Reads from the stream until a '
' character is found ('' characters are ignored).

Returns
false on EOF or any other read error.

Definition at line 69 of file CStream.cpp.

◆ getPosition()

uint64_t CMemoryStream::getPosition ( ) const
overridevirtual

Method for getting the current cursor position, where 0 is the first byte and TotalBytesCount-1 the last one.

Implements mrpt::io::CStream.

Definition at line 147 of file CMemoryStream.cpp.

References m_position.

◆ getRawBufferData() [1/2]

void * CMemoryStream::getRawBufferData ( )

Method for getting a pointer to the raw stored data.

The lenght in bytes is given by getTotalBytesCount

Definition at line 164 of file CMemoryStream.cpp.

References mrpt::non_copiable_ptr_basic< T >::get(), and m_memory.

Referenced by mrpt::img::CImage::serializeFrom(), and mrpt::img::CImage::serializeTo().

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

◆ getRawBufferData() [2/2]

const void * CMemoryStream::getRawBufferData ( ) const

Definition at line 165 of file CMemoryStream.cpp.

References mrpt::non_copiable_ptr_basic< T >::get(), and m_memory.

Here is the call graph for this function:

◆ getTotalBytesCount()

uint64_t CMemoryStream::getTotalBytesCount ( ) const
overridevirtual

Returns the total size of the internal buffer.

Implements mrpt::io::CStream.

Definition at line 146 of file CMemoryStream.cpp.

References m_bytesWritten.

Referenced by mrpt::serialization::mrpt_recv_from_zmq_buf(), mrpt::serialization::mrpt_send_to_zmq(), saveBufferToFile(), and mrpt::img::CImage::serializeTo().

Here is the caller graph for this function:

◆ loadBufferFromFile()

bool CMemoryStream::loadBufferFromFile ( const std::string file_name)

Loads the entire buffer from a file *.

Returns
true on success, false on error

Definition at line 184 of file CMemoryStream.cpp.

References Clear(), mrpt::non_copiable_ptr_basic< T >::get(), mrpt::io::CFileInputStream::getTotalBytesCount(), m_bytesWritten, m_memory, m_position, mrpt::io::CFileInputStream::Read(), and resize().

Here is the call graph for this function:

◆ printf()

int CStream::printf ( const char *  fmt,
  ... 
)
virtualinherited

Writes a string to the stream in a textual form.

See also
CStdOutStream

Definition at line 30 of file CStream.cpp.

References MRPT_END, MRPT_START, and mrpt::system::os::vsnprintf().

Referenced by mrpt::hmtslam::CTopLCDetector_GridMatching::computeTopologicalObservationModel(), mrpt::apps::MonteCarloLocalization_Base::do_pf_localization(), mrpt::io::CStream::printf_vector(), mrpt::apps::CGridMapAlignerApp::run(), mrpt::apps::RBPF_SLAM_App_Base::run(), and mrpt::apps::ICP_SLAM_App_Base::run().

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

◆ printf_vector()

template<typename CONTAINER_TYPE >
virtual int void mrpt::io::CStream::printf_vector ( const char *  fmt,
const CONTAINER_TYPE &  V,
char  separator = ',' 
)
inlineinherited

Prints a vector in the format [A,B,C,...] using CStream::printf, and the fmt string for each vector element T.

Template Parameters
CONTAINER_TYPEcan be any vector<T>, deque<T> or alike.

Definition at line 102 of file io/CStream.h.

References mrpt::io::CStream::printf().

Referenced by mrpt::hmtslam::CTopLCDetector_GridMatching::computeTopologicalObservationModel().

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

◆ Read()

size_t CMemoryStream::Read ( void Buffer,
size_t  Count 
)
overridevirtual

Introduces a pure virtual method responsible for reading from the stream.

Implements mrpt::io::CStream.

Definition at line 86 of file CMemoryStream.cpp.

References mrpt::non_copiable_ptr_basic< T >::get(), m_memory, m_position, m_size, and mrpt::system::os::memcpy().

Here is the call graph for this function:

◆ ReadBufferImmediate()

virtual size_t mrpt::io::CStream::ReadBufferImmediate ( void Buffer,
size_t  Count 
)
inlinevirtualinherited

Reads a block of bytes from the stream into Buffer, and returns the amound of bytes actually read, without waiting for more extra bytes to arrive (just those already enqued in the stream).

Note that this method will fallback to ReadBuffer() in most CStream classes but in some hardware-related classes.

Exceptions
std::exceptionOn any error, or if ZERO bytes are read.

Reimplemented in mrpt::comms::CInterfaceFTDI.

Definition at line 62 of file io/CStream.h.

References mrpt::io::CStream::Read().

Here is the call graph for this function:

◆ resize()

void CMemoryStream::resize ( uint64_t  newSize)
protected

Resizes the internal buffer size.

Definition at line 59 of file CMemoryStream.cpp.

References ASSERT_, mrpt::non_copiable_ptr_basic< T >::get(), m_bytesWritten, m_memory, m_position, m_read_only, m_size, mrpt::non_copiable_ptr_basic< T >::set(), and THROW_EXCEPTION.

Referenced by changeSize(), Clear(), CMemoryStream(), loadBufferFromFile(), and Write().

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

◆ saveBufferToFile()

bool CMemoryStream::saveBufferToFile ( const std::string file_name)

Saves the entire buffer to a file.

Returns
true on success, false on error

Definition at line 167 of file CMemoryStream.cpp.

References mrpt::non_copiable_ptr_basic< T >::get(), getTotalBytesCount(), m_memory, and mrpt::io::CFileOutputStream::Write().

Here is the call graph for this function:

◆ Seek()

uint64_t CMemoryStream::Seek ( int64_t  Offset,
CStream::TSeekOrigin  Origin = sFromBeginning 
)
overridevirtual

Introduces a pure virtual method for moving to a specified position in the streamed resource.

he Origin parameter indicates how to interpret the Offset parameter. Origin should be one of the following values:

  • sFromBeginning (Default) Offset is from the beginning of the resource. Seek moves to the position Offset. Offset must be >= 0.
  • sFromCurrent Offset is from the current position in the resource. Seek moves to Position + Offset.
  • sFromEnd Offset is from the end of the resource. Offset must be <= 0 to indicate a number of bytes before the end of the file.
    Returns
    Seek returns the new value of the Position property.

Implements mrpt::io::CStream.

Definition at line 126 of file CMemoryStream.cpp.

References m_bytesWritten, m_position, m_size, mrpt::io::CStream::sFromBeginning, mrpt::io::CStream::sFromCurrent, and mrpt::io::CStream::sFromEnd.

Referenced by mrpt::obs::gnss::gnss_message_ptr::gnss_message_ptr(), mrpt::hwdrivers::CGPSInterface::implement_parser_NOVATEL_OEM6(), mrpt::serialization::mrpt_recv_from_zmq_buf(), mrpt::obs::gnss::gnss_message_ptr::operator=(), mrpt::nav::CAbstractPTGBasedReactive::performNavigationStep(), mrpt::img::CImage::serializeFrom(), TEST(), and GraphTester< my_graph_t >::test_graph_bin_serialization().

Here is the caller graph for this function:

◆ setAllocBlockSize()

void mrpt::io::CMemoryStream::setAllocBlockSize ( uint64_t  alloc_block_size)
inline

Change the size of the additional memory block that is reserved whenever the current block runs too short (default=0x10000 bytes)

Definition at line 94 of file io/CMemoryStream.h.

References ASSERT_, and m_alloc_block_size.

◆ Write()

size_t CMemoryStream::Write ( const void Buffer,
size_t  Count 
)
overridevirtual

Introduces a pure virtual method responsible for writing to the stream.

Write attempts to write up to Count bytes to Buffer, and returns the number of bytes actually written.

Implements mrpt::io::CStream.

Definition at line 103 of file CMemoryStream.cpp.

References ASSERT_, mrpt::non_copiable_ptr_basic< T >::get(), m_alloc_block_size, m_bytesWritten, m_memory, m_position, m_size, mrpt::system::os::memcpy(), and resize().

Here is the call graph for this function:

Member Data Documentation

◆ m_alloc_block_size

uint64_t mrpt::io::CMemoryStream::m_alloc_block_size {0x1000}
protected

Definition at line 36 of file io/CMemoryStream.h.

Referenced by setAllocBlockSize(), and Write().

◆ m_bytesWritten

uint64_t mrpt::io::CMemoryStream::m_bytesWritten {0}
protected

◆ m_memory

void_ptr_noncopy mrpt::io::CMemoryStream::m_memory {nullptr}
protected

◆ m_position

uint64_t mrpt::io::CMemoryStream::m_position {0}
protected

◆ m_read_only

bool mrpt::io::CMemoryStream::m_read_only {false}
protected

If the memory block does not belong to the object.

Definition at line 38 of file io/CMemoryStream.h.

Referenced by assignMemoryNotOwn(), Clear(), resize(), and ~CMemoryStream().

◆ m_size

uint64_t mrpt::io::CMemoryStream::m_size {0}
protected

Definition at line 35 of file io/CMemoryStream.h.

Referenced by assignMemoryNotOwn(), Clear(), Read(), resize(), Seek(), Write(), and ~CMemoryStream().




Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: cb8dd5fc8 Sat Dec 7 21:55:39 2019 +0100 at sáb dic 7 22:00:13 CET 2019