MRPT  2.0.1
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-2020, 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 */
35  uint64_t m_size{0}, m_position{0}, m_bytesWritten{0};
36  uint64_t m_alloc_block_size{0x1000};
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  // See docs in base class
67  uint64_t Seek(
68  int64_t Offset, CStream::TSeekOrigin Origin = sFromBeginning) override;
69  /** Returns the total size of the internal buffer */
70  uint64_t getTotalBytesCount() const override;
71  /** Method for getting the current cursor position, where 0 is the first
72  * byte and TotalBytesCount-1 the last one */
73  uint64_t getPosition() const override;
74 
75  /** Method for getting a pointer to the raw stored data. The lenght in bytes
76  * is given by getTotalBytesCount */
77  void* getRawBufferData();
78  const void* getRawBufferData() const;
79 
80  /** Saves the entire buffer to a file \return true on success, false on
81  * error */
82  bool saveBufferToFile(const std::string& file_name);
83 
84  /** Loads the entire buffer from a file * \return true on success, false on
85  * error */
86  bool loadBufferFromFile(const std::string& file_name);
87 
88  /** Change the size of the additional memory block that is reserved whenever
89  * the current block runs too short (default=0x10000 bytes) */
90  void setAllocBlockSize(uint64_t alloc_block_size)
91  {
92  ASSERT_(alloc_block_size > 0);
93  m_alloc_block_size = alloc_block_size;
94  }
95 }; // End of class def.
96 
97 namespace internal
98 {
100 {
101  CMemoryStream* buf{nullptr};
102  bool do_free{true};
103  TFreeFnDataForZMQ() = default;
104 };
105 /** Used in mrpt_send_to_zmq(). `hint` points to a `TFreeFnDataForZMQ` struct,
106  * to be freed here. */
107 void free_fn_for_zmq(void* data, void* hint);
108 } // namespace internal
109 } // namespace io
110 } // 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 * 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.
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_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.
void resize(uint64_t newSize)
Resizes the internal buffer size.
void clear()
Clears the memory buffer.
bool m_read_only
If the memory block does not belong to the object.
static struct FontData data
Definition: gltext.cpp:144



Page generated by Doxygen 1.8.14 for MRPT 2.0.1 Git: 0fef1a6d7 Fri Apr 3 23:00:21 2020 +0200 at vie abr 3 23:20:28 CEST 2020