Main MRPT website > C++ reference
MRPT logo
CMemoryStream.h
Go to the documentation of this file.
1 /* +---------------------------------------------------------------------------+
2  | The Mobile Robot Programming Toolkit (MRPT) C++ library |
3  | |
4  | http://www.mrpt.org/ |
5  | |
6  | Copyright (C) 2005-2012 University of Malaga |
7  | |
8  | This software was written by the Machine Perception and Intelligent |
9  | Robotics Lab, University of Malaga (Spain). |
10  | Contact: Jose-Luis Blanco <jlblanco@ctima.uma.es> |
11  | |
12  | This file is part of the MRPT project. |
13  | |
14  | MRPT is free software: you can redistribute it and/or modify |
15  | it under the terms of the GNU General Public License as published by |
16  | the Free Software Foundation, either version 3 of the License, or |
17  | (at your option) any later version. |
18  | |
19  | MRPT is distributed in the hope that it will be useful, |
20  | but WITHOUT ANY WARRANTY; without even the implied warranty of |
21  | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
22  | GNU General Public License for more details. |
23  | |
24  | You should have received a copy of the GNU General Public License |
25  | along with MRPT. If not, see <http://www.gnu.org/licenses/>. |
26  | |
27  +---------------------------------------------------------------------------+ */
28 #ifndef CMEMORYSTREAM_H
29 #define CMEMORYSTREAM_H
30 
31 #include <mrpt/utils/CStream.h>
33 
34 /*---------------------------------------------------------------
35  Class
36  ---------------------------------------------------------------*/
37 namespace mrpt
38 {
39 namespace utils
40 {
41  /** This CStream derived class allow using a memory buffer as a CStream.
42  * This class is useful for storing any required set of variables or objects,
43  * and then read them to other objects, or storing them to a file, for example.
44  *
45  * \sa CStream
46  * \ingroup mrpt_base_grp
47  */
49  {
50  protected:
51  /** Method responsible for reading from the stream.
52  */
53  size_t Read(void *Buffer, size_t Count);
54 
55  /** Method responsible for writing to the stream.
56  * Write attempts to write up to Count bytes to Buffer, and returns the number of bytes actually written.
57  */
58  size_t Write(const void *Buffer, size_t Count);
59 
60  /** Internal data
61  */
63  uint64_t m_size, m_position, m_bytesWritten;
65  bool m_read_only; //!< If the memory block does not belong to the object.
66 
67  /** Resizes the internal buffer size.
68  */
69  void resize(uint64_t newSize);
70  public:
71  /** Default constructor
72  */
73  CMemoryStream();
74 
75  /** 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.
76  * \sa assignMemoryNotOwn
77  */
78  CMemoryStream( const void *data, const uint64_t nBytesInData );
79 
80  /** 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.
81  * 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.
82  * This method resets the write and read positions to the beginning.
83  */
84  void assignMemoryNotOwn( const void *data, const uint64_t nBytesInData );
85 
86  /** Destructor
87  */
88  virtual ~CMemoryStream();
89 
90  /** Clears the memory buffer.
91  */
92  void Clear();
93 
94  /** Change size. This would be rarely used. Use ">>" operators for writing to stream.
95  * \sa Stream
96  */
97  void changeSize( uint64_t newSize );
98 
99  /** Method for moving to a specified position in the streamed resource.
100  * \sa CStream::Seek
101  */
102  uint64_t Seek(long Offset, CStream::TSeekOrigin Origin = sFromBeginning);
103 
104  /** Returns the total size of the internal buffer.
105  */
106  uint64_t getTotalBytesCount();
107 
108  /** Method for getting the current cursor position, where 0 is the first byte and TotalBytesCount-1 the last one.
109  */
110  uint64_t getPosition();
111 
112  /** Method for getting a pointer to the raw stored data.
113  * The lenght in bytes is given by getTotalBytesCount
114  */
115  void* getRawBufferData();
116 
117 
118  /** Saves the entire buffer to a file
119  * \return true on success, false on error
120  */
121  bool saveBufferToFile( const std::string &file_name );
122 
123  /** Loads the entire buffer from a file
124  * \return true on success, false on error
125  */
126  bool loadBufferFromFile( const std::string &file_name );
127 
128  /** Change the size of the additional memory block that is reserved whenever the current block runs too short (default=0x10000 bytes) */
129  void setAllocBlockSize( uint64_t alloc_block_size )
130  {
131  ASSERT_(alloc_block_size>0)
132  m_alloc_block_size = alloc_block_size;
133  }
134 
135  }; // End of class def.
136 
137  } // End of namespace
138 } // end of namespace
139 #endif



Page generated by Doxygen 1.8.3 for MRPT 0.9.6 SVN: at Fri Feb 15 22:05:02 EST 2013