MRPT  1.9.9
io/CMemoryStream.h
Go to the documentation of this file.
1 /* +------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | https://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2019, Individual contributors, see AUTHORS file |
6  | See: https://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See: https://www.mrpt.org/License |
8  +------------------------------------------------------------------------+ */
9 #pragma once
10 
12 #include <mrpt/io/CStream.h>
13 
14 namespace mrpt
15 {
16 namespace io
17 {
18 /** This CStream derived class allow using a memory buffer as a CStream.
19  * This class is useful for storing any required set of variables or objects,
20  * and then read them to other objects, or storing them to a file, for
21  * example.
22  *
23  * \sa CStream
24  * \ingroup mrpt_io_grp
25  */
26 class CMemoryStream : public CStream
27 {
28  public:
29  size_t Read(void* Buffer, size_t Count) override;
30  size_t Write(const void* Buffer, size_t Count) override;
31 
32  protected:
33  /** Internal data */
37  /** If the memory block does not belong to the object. */
38  bool m_read_only{false};
39  /** Resizes the internal buffer size. */
40  void resize(uint64_t newSize);
41 
42  public:
43  /** Default constructor */
44  CMemoryStream() = default;
45  /** Constructor to initilize the data in the stream from a block of memory
46  * (which is copied), and sets the current stream position at the beginning
47  * of the data.
48  * \sa assignMemoryNotOwn */
49  CMemoryStream(const void* data, const uint64_t nBytesInData);
50 
51  /** Initilize the data in the stream from a block of memory which is NEITHER
52  * OWNED NOR COPIED by the object, so it must exist during the whole live of
53  * the object.
54  * After assigning a block of data with this method, the object becomes
55  * "read-only", so further attempts to change the size of the buffer will
56  * raise an exception.
57  * This method resets the write and read positions to the beginning. */
58  void assignMemoryNotOwn(const void* data, const uint64_t nBytesInData);
59 
60  /** Destructor */
61  ~CMemoryStream() override;
62 
63  /** Clears the memory buffer. */
64  void Clear();
65 
66  /** Change size. This would be rarely used. Use ">>" operators for writing
67  * to stream \sa Stream */
68  void changeSize(uint64_t newSize);
69 
70  // See docs in base class
71  uint64_t Seek(
72  int64_t Offset, CStream::TSeekOrigin Origin = sFromBeginning) override;
73  /** Returns the total size of the internal buffer */
74  uint64_t getTotalBytesCount() const override;
75  /** Method for getting the current cursor position, where 0 is the first
76  * byte and TotalBytesCount-1 the last one */
77  uint64_t getPosition() const override;
78 
79  /** Method for getting a pointer to the raw stored data. The lenght in bytes
80  * is given by getTotalBytesCount */
81  void* getRawBufferData();
82  const void* getRawBufferData() const;
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 {
105  CMemoryStream* buf{nullptr};
106  bool do_free{true};
107  TFreeFnDataForZMQ() = default;
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 } // namespace internal
113 } // namespace io
114 } // namespace mrpt
TSeekOrigin
Used in CStream::Seek.
Definition: io/CStream.h:32
bool loadBufferFromFile(const std::string &file_name)
Loads the entire buffer from a file *.
void free_fn_for_zmq(void *data, void *hint)
Used in mrpt_send_to_zmq().
This base class is used to provide a unified interface to files,memory buffers,..Please see the deriv...
Definition: io/CStream.h:28
uint64_t getTotalBytesCount() const override
Returns the total size of the internal buffer.
bool saveBufferToFile(const std::string &file_name)
Saves the entire buffer to a file.
#define ASSERT_(f)
Defines an assertion mechanism.
Definition: exceptions.h:120
void Clear()
Clears the memory buffer.
__int64 int64_t
Definition: rptypes.h:52
void * getRawBufferData()
Method for getting a pointer to the raw stored data.
This CStream derived class allow using a memory buffer as a CStream.
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...
uint64_t Seek(int64_t Offset, CStream::TSeekOrigin Origin=sFromBeginning) override
Introduces a pure virtual method for moving to a specified position in the streamed resource...
uint64_t getPosition() const override
Method for getting the current cursor position, where 0 is the first byte and TotalBytesCount-1 the l...
CMemoryStream()=default
Default constructor.
~CMemoryStream() override
Destructor.
GLsizei const GLchar ** string
Definition: glext.h:4116
unsigned __int64 uint64_t
Definition: rptypes.h:53
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
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...
size_t Read(void *Buffer, size_t Count) override
Introduces a pure virtual method responsible for reading from the stream.
void changeSize(uint64_t newSize)
Change size.
void_ptr_noncopy m_memory
Internal data.
size_t Write(const void *Buffer, size_t Count) override
Introduces a pure virtual method responsible for writing to the stream.
GLsizei GLsizei GLenum GLenum const GLvoid * data
Definition: glext.h:3550
void resize(uint64_t newSize)
Resizes the internal buffer size.
bool m_read_only
If the memory block does not belong to the object.



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: 8fe78517f Sun Jul 14 19:43:28 2019 +0200 at lun oct 28 02:10:00 CET 2019