Main MRPT website > C++ reference for MRPT 1.4.0
CMemoryStream.h
Go to the documentation of this file.
1/* +---------------------------------------------------------------------------+
2 | Mobile Robot Programming Toolkit (MRPT) |
3 | http://www.mrpt.org/ |
4 | |
5 | Copyright (c) 2005-2016, Individual contributors, see AUTHORS file |
6 | See: http://www.mrpt.org/Authors - All rights reserved. |
7 | Released under BSD License. See details in http://www.mrpt.org/License |
8 +---------------------------------------------------------------------------+ */
9#ifndef CMEMORYSTREAM_H
10#define CMEMORYSTREAM_H
11
12#include <mrpt/utils/CStream.h>
14
15/*---------------------------------------------------------------
16 Class
17 ---------------------------------------------------------------*/
18namespace mrpt
19{
20namespace utils
21{
22 /** This CStream derived class allow using a memory buffer as a CStream.
23 * This class is useful for storing any required set of variables or objects,
24 * and then read them to other objects, or storing them to a file, for example.
25 *
26 * \sa CStream
27 * \ingroup mrpt_base_grp
28 */
30 {
31 protected:
32 size_t Read(void *Buffer, size_t Count) MRPT_OVERRIDE;
33 size_t Write(const void *Buffer, size_t Count) MRPT_OVERRIDE;
34
35 /** Internal data */
37 uint64_t m_size, m_position, m_bytesWritten;
39 bool m_read_only; //!< If the memory block does not belong to the object.
40 void resize(uint64_t newSize); //!< Resizes the internal buffer size.
41 public:
42 CMemoryStream(); //!< Default constructor
43
44 /** 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.
45 * \sa assignMemoryNotOwn */
46 CMemoryStream( const void *data, const uint64_t nBytesInData );
47
48 /** 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.
49 * 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.
50 * This method resets the write and read positions to the beginning. */
51 void assignMemoryNotOwn( const void *data, const uint64_t nBytesInData );
52
53 virtual ~CMemoryStream(); //!< Destructor
54
55 void Clear(); //!< Clears the memory buffer.
56
57 void changeSize( uint64_t newSize ); //!< Change size. This would be rarely used. Use ">>" operators for writing to stream \sa Stream
58
59 // See docs in base class
60 uint64_t Seek(uint64_t Offset, CStream::TSeekOrigin Origin = sFromBeginning) MRPT_OVERRIDE;
61 /** Returns the total size of the internal buffer */
63 /** Method for getting the current cursor position, where 0 is the first byte and TotalBytesCount-1 the last one */
64 uint64_t getPosition() MRPT_OVERRIDE;
65
66 /** Method for getting a pointer to the raw stored data. The lenght in bytes is given by getTotalBytesCount */
67 void* getRawBufferData();
68
69 /** Saves the entire buffer to a file \return true on success, false on error */
70 bool saveBufferToFile( const std::string &file_name );
71
72 /** Loads the entire buffer from a file * \return true on success, false on error */
73 bool loadBufferFromFile( const std::string &file_name );
74
75 /** Change the size of the additional memory block that is reserved whenever the current block runs too short (default=0x10000 bytes) */
76 void setAllocBlockSize( uint64_t alloc_block_size )
77 {
78 ASSERT_(alloc_block_size>0)
79 m_alloc_block_size = alloc_block_size;
80 }
81 }; // End of class def.
82 } // End of namespace
83} // end of namespace
84#endif
This CStream derived class allow using a memory buffer as a CStream.
Definition: CMemoryStream.h:30
size_t Read(void *Buffer, size_t Count) MRPT_OVERRIDE
Introduces a pure virtual method responsible for reading from the stream.
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 obje...
bool m_read_only
If the memory block does not belong to the object.
Definition: CMemoryStream.h:39
uint64_t getTotalBytesCount() MRPT_OVERRIDE
Returns the total size of the internal buffer
uint64_t Seek(uint64_t Offset, CStream::TSeekOrigin Origin=sFromBeginning) MRPT_OVERRIDE
Introduces a pure virtual method for moving to a specified position in the streamed resource.
void changeSize(uint64_t newSize)
Change size. This would be rarely used. Use ">>" operators for writing to stream.
void resize(uint64_t newSize)
Resizes the internal buffer size.
void Clear()
Clears the memory buffer.
CMemoryStream(const void *data, const uint64_t nBytesInData)
Constructor to initilize the data in the stream from a block of memory (which is copied),...
size_t Write(const void *Buffer, size_t Count) MRPT_OVERRIDE
Introduces a pure virtual method responsible for writing to the stream.
void_ptr_noncopy m_memory
Internal data.
Definition: CMemoryStream.h:36
CMemoryStream()
Default constructor.
virtual ~CMemoryStream()
Destructor.
This base class is used to provide a unified interface to files,memory buffers,..Please see the deriv...
Definition: CStream.h:39
TSeekOrigin
Used in CStream::Seek.
Definition: CStream.h:43
#define ASSERT_(f)
Definition: mrpt_macros.h:261
#define MRPT_OVERRIDE
C++11 "override" for virtuals:
Definition: mrpt_macros.h:28
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
STL namespace.



Page generated by Doxygen 1.9.6 for MRPT 1.4.0 SVN: at Tue Jan 17 22:40:29 UTC 2023