Main MRPT website > C++ reference for MRPT 1.9.9
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
22  * example.
23  *
24  * \sa CStream
25  * \ingroup mrpt_base_grp
26  */
27 class CMemoryStream : public CStream
28 {
29  protected:
30  size_t Read(void* Buffer, size_t Count) override;
31  size_t Write(const void* Buffer, size_t Count) override;
32 
33  /** Internal data */
37  /** If the memory block does not belong to the object. */
39  /** Resizes the internal buffer size. */
40  void resize(uint64_t newSize);
41 
42  public:
43  /** Default constructor */
44  CMemoryStream();
45 
46  /** Constructor to initilize the data in the stream from a block of memory
47  * (which is copied), and sets the current stream position at the beginning
48  * of the data.
49  * \sa assignMemoryNotOwn */
50  CMemoryStream(const void* data, const uint64_t nBytesInData);
51 
52  /** Initilize the data in the stream from a block of memory which is NEITHER
53  * OWNED NOR COPIED by the object, so it must exist during the whole live of
54  * the object.
55  * After assigning a block of data with this method, the object becomes
56  * "read-only", so further attempts to change the size of the buffer will
57  * raise an exception.
58  * This method resets the write and read positions to the beginning. */
59  void assignMemoryNotOwn(const void* data, const uint64_t nBytesInData);
60 
61  /** Destructor */
62  virtual ~CMemoryStream();
63 
64  /** Clears the memory buffer. */
65  void Clear();
66 
67  /** Change size. This would be rarely used. Use ">>" operators for writing
68  * to stream \sa Stream */
69  void changeSize(uint64_t newSize);
70 
71  // See docs in base class
72  uint64_t Seek(
73  uint64_t Offset, CStream::TSeekOrigin Origin = sFromBeginning) override;
74  /** Returns the total size of the internal buffer */
75  uint64_t getTotalBytesCount() override;
76  /** Method for getting the current cursor position, where 0 is the first
77  * byte and TotalBytesCount-1 the last one */
78  uint64_t getPosition() override;
79 
80  /** Method for getting a pointer to the raw stored data. The lenght in bytes
81  * is given by getTotalBytesCount */
82  void* getRawBufferData();
83 
84  /** Saves the entire buffer to a file \return true on success, false on
85  * error */
86  bool saveBufferToFile(const std::string& file_name);
87 
88  /** Loads the entire buffer from a file * \return true on success, false on
89  * error */
90  bool loadBufferFromFile(const std::string& file_name);
91 
92  /** Change the size of the additional memory block that is reserved whenever
93  * the current block runs too short (default=0x10000 bytes) */
94  void setAllocBlockSize(uint64_t alloc_block_size)
95  {
96  ASSERT_(alloc_block_size > 0)
97  m_alloc_block_size = alloc_block_size;
98  }
99 }; // End of class def.
100 
101 namespace internal
102 {
104 {
106  bool do_free;
107  TFreeFnDataForZMQ() : buf(nullptr), do_free(true) {}
108 };
109 /** Used in mrpt_send_to_zmq(). `hint` points to a `TFreeFnDataForZMQ` struct,
110  * to be freed here. */
111 void free_fn_for_zmq(void* data, void* hint);
112 }
113 } // End of namespace
114 } // end of namespace
115 #endif
size_t Write(const void *Buffer, size_t Count) override
Introduces a pure virtual method responsible for writing to the stream.
TSeekOrigin
Used in CStream::Seek.
Definition: CStream.h:45
uint64_t Seek(uint64_t Offset, CStream::TSeekOrigin Origin=sFromBeginning) override
Introduces a pure virtual method for moving to a specified position in the streamed resource...
size_t Read(void *Buffer, size_t Count) override
Introduces a pure virtual method responsible for reading from the stream.
void resize(uint64_t newSize)
Resizes the internal buffer size.
This base class is used to provide a unified interface to files,memory buffers,..Please see the deriv...
Definition: CStream.h:41
virtual ~CMemoryStream()
Destructor.
void Clear()
Clears the memory buffer.
This CStream derived class allow using a memory buffer as a CStream.
Definition: CMemoryStream.h:27
uint64_t getPosition() override
Method for getting the current cursor position, where 0 is the first byte and TotalBytesCount-1 the l...
bool saveBufferToFile(const std::string &file_name)
Saves the entire buffer to a file.
GLsizei const GLchar ** string
Definition: glext.h:4101
void free_fn_for_zmq(void *data, void *hint)
Used in mrpt_send_to_zmq().
bool m_read_only
If the memory block does not belong to the object.
Definition: CMemoryStream.h:38
void * getRawBufferData()
Method for getting a pointer to the raw stored data.
unsigned __int64 uint64_t
Definition: rptypes.h:50
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
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...
CMemoryStream()
Default constructor.
void changeSize(uint64_t newSize)
Change size.
#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...
Definition: CMemoryStream.h:94
void_ptr_noncopy m_memory
Internal data.
Definition: CMemoryStream.h:34
uint64_t getTotalBytesCount() override
Returns the total size of the internal buffer.
bool loadBufferFromFile(const std::string &file_name)
Loads the entire buffer from a file *.
GLsizei GLsizei GLenum GLenum const GLvoid * data
Definition: glext.h:3546



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: ae4571287 Thu Nov 23 00:06:53 2017 +0100 at dom oct 27 23:51:55 CET 2019