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 */
62  uint64_t getTotalBytesCount() MRPT_OVERRIDE;
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
TSeekOrigin
Used in CStream::Seek.
Definition: CStream.h:42
#define MRPT_OVERRIDE
C++11 "override" for virtuals:
STL namespace.
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:39
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
#define ASSERT_(f)
void_ptr_noncopy m_memory
Internal data.
Definition: CMemoryStream.h:36



Page generated by Doxygen 1.8.11 for MRPT 1.4.0 SVN:Unversioned directory at Tue Jun 28 11:46:25 UTC 2016