MRPT
1.9.9
|
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.
Definition at line 26 of file io/CMemoryStream.h.
#include <mrpt/io/CMemoryStream.h>
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 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... | |
virtual | ~CMemoryStream () |
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... | |
void * | getRawBufferData () |
Method for getting a pointer to the raw stored data. More... | |
const void * | getRawBufferData () 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... | |
|
inherited |
Used in CStream::Seek.
Enumerator | |
---|---|
sFromBeginning | |
sFromCurrent | |
sFromEnd |
Definition at line 32 of file io/CStream.h.
|
inline |
Default constructor.
Definition at line 44 of file io/CMemoryStream.h.
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.
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().
|
virtual |
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::serialization::mrpt_recv_from_zmq_buf().
Change size.
This would be rarely used. Use ">>" operators for writing to stream
Definition at line 160 of file CMemoryStream.cpp.
References resize().
Referenced by mrpt::img::CImage::serializeFrom().
void CMemoryStream::Clear | ( | ) |
Clears the memory buffer.
Definition at line 142 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().
|
inherited |
Reads from the stream until a '
' character is found ('' characters are ignored).
Definition at line 69 of file CStream.cpp.
|
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 141 of file CMemoryStream.cpp.
References m_position.
void * CMemoryStream::getRawBufferData | ( | ) |
Method for getting a pointer to the raw stored data.
The lenght in bytes is given by getTotalBytesCount
Definition at line 158 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().
const void * CMemoryStream::getRawBufferData | ( | ) | const |
Definition at line 159 of file CMemoryStream.cpp.
References mrpt::non_copiable_ptr_basic< T >::get(), and m_memory.
|
overridevirtual |
Returns the total size of the internal buffer.
Implements mrpt::io::CStream.
Definition at line 140 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().
bool CMemoryStream::loadBufferFromFile | ( | const std::string & | file_name | ) |
Loads the entire buffer from a file *.
Definition at line 178 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().
|
virtualinherited |
Writes a string to the stream in a textual form.
Definition at line 30 of file CStream.cpp.
References MRPT_END, MRPT_START, and vsnprintf.
Referenced by mrpt::hmtslam::CTopLCDetector_GridMatching::computeTopologicalObservationModel(), and mrpt::io::CStream::printf_vector().
|
inlineinherited |
Prints a vector in the format [A,B,C,...] using CStream::printf, and the fmt string for each vector element T
.
CONTAINER_TYPE | can 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().
|
overridevirtual |
Introduces a pure virtual method responsible for reading from the stream.
Implements mrpt::io::CStream.
Definition at line 83 of file CMemoryStream.cpp.
References mrpt::non_copiable_ptr_basic< T >::get(), m_memory, m_position, m_size, mrpt::system::os::memcpy(), and min.
|
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.
std::exception | On 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().
Resizes the internal buffer size.
Definition at line 56 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(), Write(), and ~CMemoryStream().
bool CMemoryStream::saveBufferToFile | ( | const std::string & | file_name | ) |
Saves the entire buffer to a file.
Definition at line 161 of file CMemoryStream.cpp.
References mrpt::non_copiable_ptr_basic< T >::get(), getTotalBytesCount(), m_memory, and mrpt::io::CFileOutputStream::Write().
|
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:
Implements mrpt::io::CStream.
Definition at line 120 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().
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.
|
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 98 of file CMemoryStream.cpp.
References 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().
|
protected |
Definition at line 36 of file io/CMemoryStream.h.
Referenced by setAllocBlockSize(), and Write().
|
protected |
Definition at line 35 of file io/CMemoryStream.h.
Referenced by assignMemoryNotOwn(), Clear(), CMemoryStream(), getTotalBytesCount(), loadBufferFromFile(), resize(), Seek(), and Write().
|
protected |
Internal data.
Definition at line 34 of file io/CMemoryStream.h.
Referenced by assignMemoryNotOwn(), Clear(), CMemoryStream(), getRawBufferData(), loadBufferFromFile(), Read(), resize(), saveBufferToFile(), and Write().
|
protected |
Definition at line 35 of file io/CMemoryStream.h.
Referenced by assignMemoryNotOwn(), Clear(), getPosition(), loadBufferFromFile(), Read(), resize(), Seek(), and Write().
|
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().
|
protected |
Definition at line 35 of file io/CMemoryStream.h.
Referenced by assignMemoryNotOwn(), Clear(), Read(), resize(), Seek(), and Write().
Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: 7d5e6d718 Fri Aug 24 01:51:28 2018 +0200 at lun nov 2 08:35:50 CET 2020 |