Main MRPT website > C++ reference for MRPT 1.5.7
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-2017, 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 namespace mrpt
16 {
17 namespace utils
18 {
19  /** This CStream derived class allow using a memory buffer as a CStream.
20  * This class is useful for storing any required set of variables or objects,
21  * and then read them to other objects, or storing them to a file, for example.
22  *
23  * \sa CStream
24  * \ingroup mrpt_base_grp
25  */
27  {
28  protected:
29  size_t Read(void *Buffer, size_t Count) MRPT_OVERRIDE;
30  size_t Write(const void *Buffer, size_t Count) MRPT_OVERRIDE;
31 
32  /** Internal data */
34  uint64_t m_size, m_position, m_bytesWritten;
36  bool m_read_only; //!< If the memory block does not belong to the object.
37  void resize(uint64_t newSize); //!< Resizes the internal buffer size.
38  public:
39  CMemoryStream(); //!< Default constructor
40 
41  /** 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.
42  * \sa assignMemoryNotOwn */
43  CMemoryStream( const void *data, const uint64_t nBytesInData );
44 
45  /** 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.
46  * 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.
47  * This method resets the write and read positions to the beginning. */
48  void assignMemoryNotOwn( const void *data, const uint64_t nBytesInData );
49 
50  virtual ~CMemoryStream(); //!< Destructor
51 
52  void Clear(); //!< Clears the memory buffer.
53 
54  void changeSize( uint64_t newSize ); //!< Change size. This would be rarely used. Use ">>" operators for writing to stream \sa Stream
55 
56  // See docs in base class
57  uint64_t Seek(int64_t Offset, CStream::TSeekOrigin Origin = sFromBeginning) MRPT_OVERRIDE;
58  /** Returns the total size of the internal buffer */
59  uint64_t getTotalBytesCount() MRPT_OVERRIDE;
60  /** Method for getting the current cursor position, where 0 is the first byte and TotalBytesCount-1 the last one */
61  uint64_t getPosition() MRPT_OVERRIDE;
62 
63  /** Method for getting a pointer to the raw stored data. The lenght in bytes is given by getTotalBytesCount */
64  void* getRawBufferData();
65 
66  /** Saves the entire buffer to a file \return true on success, false on error */
67  bool saveBufferToFile( const std::string &file_name );
68 
69  /** Loads the entire buffer from a file * \return true on success, false on error */
70  bool loadBufferFromFile( const std::string &file_name );
71 
72  /** Change the size of the additional memory block that is reserved whenever the current block runs too short (default=0x10000 bytes) */
73  void setAllocBlockSize( uint64_t alloc_block_size )
74  {
75  ASSERT_(alloc_block_size>0)
76  m_alloc_block_size = alloc_block_size;
77  }
78  }; // End of class def.
79 
80  namespace internal {
82  {
84  bool do_free;
85  TFreeFnDataForZMQ() : buf(NULL), do_free(true) { }
86  };
87  void BASE_IMPEXP free_fn_for_zmq(void *data, void *hint); //!< Used in mrpt_send_to_zmq(). `hint` points to a `TFreeFnDataForZMQ` struct, to be freed here.
88  }
89  } // End of namespace
90 } // end of namespace
91 #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
__int64 int64_t
Definition: rptypes.h:51
This CStream derived class allow using a memory buffer as a CStream.
Definition: CMemoryStream.h:26
void BASE_IMPEXP free_fn_for_zmq(void *data, void *hint)
Used in mrpt_send_to_zmq(). hint points to a TFreeFnDataForZMQ struct, to be freed here...
bool m_read_only
If the memory block does not belong to the object.
Definition: CMemoryStream.h:36
unsigned __int64 uint64_t
Definition: rptypes.h:52
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:33
GLsizei GLsizei GLenum GLenum const GLvoid * data
Definition: glext.h:3520



Page generated by Doxygen 1.8.14 for MRPT 1.5.7 Git: 5902e14cc Wed Apr 24 15:04:01 2019 +0200 at lun oct 28 01:39:17 CET 2019