Main MRPT website > C++ reference for MRPT 1.3.2
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-2015, 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  /** Method responsible for reading from the stream.
33  */
34  size_t Read(void *Buffer, size_t Count);
35 
36  /** Method responsible for writing to the stream.
37  * Write attempts to write up to Count bytes to Buffer, and returns the number of bytes actually written.
38  */
39  size_t Write(const void *Buffer, size_t Count);
40 
41  /** Internal data
42  */
44  uint64_t m_size, m_position, m_bytesWritten;
46  bool m_read_only; //!< If the memory block does not belong to the object.
47 
48  /** Resizes the internal buffer size.
49  */
50  void resize(uint64_t newSize);
51  public:
52  /** Default constructor
53  */
54  CMemoryStream();
55 
56  /** 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.
57  * \sa assignMemoryNotOwn
58  */
59  CMemoryStream( const void *data, const uint64_t nBytesInData );
60 
61  /** 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.
62  * 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.
63  * This method resets the write and read positions to the beginning.
64  */
65  void assignMemoryNotOwn( const void *data, const uint64_t nBytesInData );
66 
67  /** Destructor
68  */
69  virtual ~CMemoryStream();
70 
71  /** Clears the memory buffer.
72  */
73  void Clear();
74 
75  /** Change size. This would be rarely used. Use ">>" operators for writing to stream.
76  * \sa Stream
77  */
78  void changeSize( uint64_t newSize );
79 
80  /** Method for moving to a specified position in the streamed resource.
81  * \sa CStream::Seek
82  */
83  uint64_t Seek(uint64_t Offset, CStream::TSeekOrigin Origin = sFromBeginning);
84 
85  /** Returns the total size of the internal buffer.
86  */
87  uint64_t getTotalBytesCount();
88 
89  /** Method for getting the current cursor position, where 0 is the first byte and TotalBytesCount-1 the last one.
90  */
91  uint64_t getPosition();
92 
93  /** Method for getting a pointer to the raw stored data.
94  * The lenght in bytes is given by getTotalBytesCount
95  */
96  void* getRawBufferData();
97 
98 
99  /** Saves the entire buffer to a file
100  * \return true on success, false on error
101  */
102  bool saveBufferToFile( const std::string &file_name );
103 
104  /** Loads the entire buffer from a file
105  * \return true on success, false on error
106  */
107  bool loadBufferFromFile( const std::string &file_name );
108 
109  /** Change the size of the additional memory block that is reserved whenever the current block runs too short (default=0x10000 bytes) */
110  void setAllocBlockSize( uint64_t alloc_block_size )
111  {
112  ASSERT_(alloc_block_size>0)
113  m_alloc_block_size = alloc_block_size;
114  }
115 
116  }; // End of class def.
117 
118  } // End of namespace
119 } // end of namespace
120 #endif
TSeekOrigin
Used in CStream::Seek.
Definition: CStream.h:42
This base class is used to provide a unified interface to files,memory buffers,..Please see the deriv...
Definition: CStream.h:38
This CStream derived class allow using a memory buffer as a CStream.
Definition: CMemoryStream.h:29
bool m_read_only
If the memory block does not belong to the object.
Definition: CMemoryStream.h:46
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
#define ASSERT_(f)
void setAllocBlockSize(uint64_t alloc_block_size)
Change the size of the additional memory block that is reserved whenever the current block runs too s...
void_ptr_noncopy m_memory
Internal data.
Definition: CMemoryStream.h:43



Page generated by Doxygen 1.8.12 for MRPT 1.3.2 SVN: at Thu Nov 10 13:46:27 UTC 2016