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  ---------------------------------------------------------------*/
18 namespace mrpt
19 {
20 namespace 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
mrpt::utils::CMemoryStream::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),...
mrpt::utils::CMemoryStream::m_size
uint64_t m_size
Definition: CMemoryStream.h:37
mrpt::utils::CMemoryStream::Clear
void Clear()
Clears the memory buffer.
mrpt::utils::CMemoryStream::resize
void resize(uint64_t newSize)
Resizes the internal buffer size.
mrpt
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
Definition: CParticleFilter.h:17
CStream.h
mrpt::utils::CStream
This base class is used to provide a unified interface to files,memory buffers,..Please see the deriv...
Definition: CStream.h:39
mrpt::utils::CMemoryStream::Write
size_t Write(const void *Buffer, size_t Count) MRPT_OVERRIDE
Introduces a pure virtual method responsible for writing to the stream.
mrpt::utils::CMemoryStream::getTotalBytesCount
uint64_t getTotalBytesCount() MRPT_OVERRIDE
Returns the total size of the internal buffer
mrpt::utils::CMemoryStream::m_read_only
bool m_read_only
If the memory block does not belong to the object.
Definition: CMemoryStream.h:39
mrpt::utils::CMemoryStream::m_memory
void_ptr_noncopy m_memory
Internal data.
Definition: CMemoryStream.h:36
mrpt::utils::CMemoryStream::m_alloc_block_size
uint64_t m_alloc_block_size
Definition: CMemoryStream.h:38
safe_pointers.h
mrpt::utils::CMemoryStream::Seek
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.
mrpt::utils::CStream::TSeekOrigin
TSeekOrigin
Used in CStream::Seek.
Definition: CStream.h:43
mrpt::utils::CMemoryStream::Read
size_t Read(void *Buffer, size_t Count) MRPT_OVERRIDE
Introduces a pure virtual method responsible for reading from the stream.
ASSERT_
#define ASSERT_(f)
Definition: mrpt_macros.h:261
mrpt::utils::CMemoryStream::CMemoryStream
CMemoryStream()
Default constructor.
mrpt::utils::non_copiable_ptr_basic< void >
mrpt::utils::CMemoryStream::assignMemoryNotOwn
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...
MRPT_OVERRIDE
#define MRPT_OVERRIDE
C++11 "override" for virtuals:
Definition: mrpt_macros.h:28
mrpt::utils::CMemoryStream
This CStream derived class allow using a memory buffer as a CStream.
Definition: CMemoryStream.h:30
mrpt::utils::CMemoryStream::changeSize
void changeSize(uint64_t newSize)
Change size. This would be rarely used. Use ">>" operators for writing to stream.
mrpt::utils::CMemoryStream::~CMemoryStream
virtual ~CMemoryStream()
Destructor.



Page generated by Doxygen 1.8.20 for MRPT 1.4.0 SVN: at Thu Aug 27 02:40:23 UTC 2020