MRPT
2.0.4
|
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 | |
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... | |
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.
|
default |
Default constructor.
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.
Definition at line 22 of file CMemoryStream.cpp.
References ASSERT_, mrpt::opengl::internal::data, mrpt::non_copiable_ptr_basic< T >::get(), m_bytesWritten, m_memory, mrpt::system::os::memcpy(), MRPT_END, MRPT_START, and resize().
|
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.
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(), mrpt::serialization::mrpt_recv_from_zmq_buf(), and mrpt::img::CImage::serializeFrom().
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().
|
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 147 of file CMemoryStream.cpp.
References m_position.
Referenced by TEST().
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::serializeTo().
const void * CMemoryStream::getRawBufferData | ( | ) | const |
Definition at line 165 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 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().
bool CMemoryStream::loadBufferFromFile | ( | const std::string & | file_name | ) |
Loads the entire buffer from a file *.
Definition at line 183 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 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().
|
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 86 of file CMemoryStream.cpp.
References mrpt::non_copiable_ptr_basic< T >::get(), m_bytesWritten, m_memory, m_position, and mrpt::system::os::memcpy().
Referenced by TEST().
|
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().
|
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 clear(), CMemoryStream(), loadBufferFromFile(), and Write().
bool CMemoryStream::saveBufferToFile | ( | const std::string & | file_name | ) |
Saves the entire buffer to a file.
Definition at line 166 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 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().
|
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 90 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 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().
Referenced by TEST().
|
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(), Read(), 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(), Write(), and ~CMemoryStream().
|
protected |
Definition at line 35 of file io/CMemoryStream.h.
Referenced by assignMemoryNotOwn(), clear(), getPosition(), loadBufferFromFile(), Read(), resize(), Seek(), Write(), and ~CMemoryStream().
|
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(), resize(), Seek(), Write(), and ~CMemoryStream().
Page generated by Doxygen 1.8.14 for MRPT 2.0.4 Git: 33de1d0ad Sat Jun 20 11:02:42 2020 +0200 at sáb jun 20 17:35:17 CEST 2020 |