MRPT  2.0.3
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
mrpt::io::CMemoryStream::Seek
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.
Definition: CMemoryStream.cpp:126
mrpt::io::CStream::TSeekOrigin
TSeekOrigin
Used in CStream::Seek.
Definition: io/CStream.h:32
mrpt::opengl::internal::data
static struct FontData data
Definition: gltext.cpp:144
mrpt::io::CMemoryStream::m_size
uint64_t m_size
Definition: io/CMemoryStream.h:35
mrpt::io::internal::TFreeFnDataForZMQ::do_free
bool do_free
Definition: io/CMemoryStream.h:102
mrpt::io::internal::TFreeFnDataForZMQ
Definition: io/CMemoryStream.h:99
mrpt::io::CMemoryStream::clear
void clear()
Clears the memory buffer.
Definition: CMemoryStream.cpp:148
mrpt::io::CMemoryStream::m_position
uint64_t m_position
Definition: io/CMemoryStream.h:35
mrpt
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
Definition: BaseAppDataSource.h:15
ASSERT_
#define ASSERT_(f)
Defines an assertion mechanism.
Definition: exceptions.h:120
mrpt::io::CMemoryStream::m_read_only
bool m_read_only
If the memory block does not belong to the object.
Definition: io/CMemoryStream.h:38
mrpt::io::CMemoryStream::~CMemoryStream
~CMemoryStream() override
Destructor.
Definition: CMemoryStream.cpp:47
mrpt::io::internal::TFreeFnDataForZMQ::buf
CMemoryStream * buf
Definition: io/CMemoryStream.h:101
mrpt::io::CMemoryStream::m_memory
void_ptr_noncopy m_memory
Internal data.
Definition: io/CMemoryStream.h:34
mrpt::io::CStream::sFromBeginning
@ sFromBeginning
Definition: io/CStream.h:34
mrpt::io::CMemoryStream::getPosition
uint64_t getPosition() const override
Method for getting the current cursor position, where 0 is the first byte and TotalBytesCount-1 the l...
Definition: CMemoryStream.cpp:147
mrpt::io::internal::free_fn_for_zmq
void free_fn_for_zmq(void *data, void *hint)
Used in mrpt_send_to_zmq().
Definition: CMemoryStream.cpp:208
mrpt::io::CMemoryStream::resize
void resize(uint64_t newSize)
Resizes the internal buffer size.
Definition: CMemoryStream.cpp:59
mrpt::io::CMemoryStream
This CStream derived class allow using a memory buffer as a CStream.
Definition: io/CMemoryStream.h:26
CStream.h
mrpt::io::CMemoryStream::Write
size_t Write(const void *Buffer, size_t Count) override
Introduces a pure virtual method responsible for writing to the stream.
Definition: CMemoryStream.cpp:103
mrpt::io::CMemoryStream::getTotalBytesCount
uint64_t getTotalBytesCount() const override
Returns the total size of the internal buffer
Definition: CMemoryStream.cpp:146
mrpt::io::CMemoryStream::getRawBufferData
void * getRawBufferData()
Method for getting a pointer to the raw stored data.
Definition: CMemoryStream.cpp:164
mrpt::io::CMemoryStream::m_bytesWritten
uint64_t m_bytesWritten
Definition: io/CMemoryStream.h:35
safe_pointers.h
mrpt::io::CMemoryStream::CMemoryStream
CMemoryStream()=default
Default constructor.
mrpt::io::CMemoryStream::m_alloc_block_size
uint64_t m_alloc_block_size
Definition: io/CMemoryStream.h:36
mrpt::io::CMemoryStream::loadBufferFromFile
bool loadBufferFromFile(const std::string &file_name)
Loads the entire buffer from a file *.
Definition: CMemoryStream.cpp:183
mrpt::io::CMemoryStream::Read
size_t Read(void *Buffer, size_t Count) override
Introduces a pure virtual method responsible for reading from the stream.
Definition: CMemoryStream.cpp:86
mrpt::io::CMemoryStream::assignMemoryNotOwn
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...
Definition: CMemoryStream.cpp:36
mrpt::io::CMemoryStream::setAllocBlockSize
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: io/CMemoryStream.h:90
mrpt::io::internal::TFreeFnDataForZMQ::TFreeFnDataForZMQ
TFreeFnDataForZMQ()=default
mrpt::io::CMemoryStream::saveBufferToFile
bool saveBufferToFile(const std::string &file_name)
Saves the entire buffer to a file.
Definition: CMemoryStream.cpp:166
mrpt::non_copiable_ptr_basic< void >
mrpt::io::CStream
This base class is used to provide a unified interface to files,memory buffers,..Please see the deriv...
Definition: io/CStream.h:28



Page generated by Doxygen 1.8.17 for MRPT 2.0.3 at Fri May 29 13:06:46 UTC 2020