Main MRPT website > C++ reference for MRPT 1.9.9
io/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-2018, 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 #pragma once
10 
11 #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 */
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  virtual ~CMemoryStream();
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 {
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 } // namespace internal
113 } // namespace io
114 } // 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:120
mrpt::io::CStream::TSeekOrigin
TSeekOrigin
Used in CStream::Seek.
Definition: io/CStream.h:34
mrpt::io::CMemoryStream::changeSize
void changeSize(uint64_t newSize)
Change size.
Definition: CMemoryStream.cpp:160
mrpt::io::CMemoryStream::m_size
uint64_t m_size
Definition: io/CMemoryStream.h:35
mrpt::io::CMemoryStream::CMemoryStream
CMemoryStream()
Default constructor.
Definition: io/CMemoryStream.h:44
mrpt::io::internal::TFreeFnDataForZMQ::do_free
bool do_free
Definition: io/CMemoryStream.h:106
mrpt::io::internal::TFreeFnDataForZMQ
Definition: io/CMemoryStream.h:103
mrpt::io::CMemoryStream::m_position
uint64_t m_position
Definition: io/CMemoryStream.h:35
int64_t
__int64 int64_t
Definition: rptypes.h:49
mrpt
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
Definition: CKalmanFilterCapable.h:30
ASSERT_
#define ASSERT_(f)
Defines an assertion mechanism.
Definition: exceptions.h:113
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::internal::TFreeFnDataForZMQ::TFreeFnDataForZMQ
TFreeFnDataForZMQ()
Definition: io/CMemoryStream.h:107
data
GLsizei GLsizei GLenum GLenum const GLvoid * data
Definition: glext.h:3547
mrpt::io::internal::TFreeFnDataForZMQ::buf
CMemoryStream * buf
Definition: io/CMemoryStream.h:105
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:36
uint64_t
unsigned __int64 uint64_t
Definition: rptypes.h:50
mrpt::io::CMemoryStream::Clear
void Clear()
Clears the memory buffer.
Definition: CMemoryStream.cpp:142
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:141
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:203
mrpt::io::CMemoryStream::resize
void resize(uint64_t newSize)
Resizes the internal buffer size.
Definition: CMemoryStream.cpp:56
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:98
mrpt::io::CMemoryStream::getTotalBytesCount
uint64_t getTotalBytesCount() const override
Returns the total size of the internal buffer
Definition: CMemoryStream.cpp:140
mrpt::io::CMemoryStream::getRawBufferData
void * getRawBufferData()
Method for getting a pointer to the raw stored data.
Definition: CMemoryStream.cpp:158
mrpt::io::CMemoryStream::m_bytesWritten
uint64_t m_bytesWritten
Definition: io/CMemoryStream.h:35
safe_pointers.h
mrpt::io::CMemoryStream::~CMemoryStream
virtual ~CMemoryStream()
Destructor.
Definition: CMemoryStream.cpp:47
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:178
string
GLsizei const GLchar ** string
Definition: glext.h:4101
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:83
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:94
mrpt::io::CMemoryStream::saveBufferToFile
bool saveBufferToFile(const std::string &file_name)
Saves the entire buffer to a file.
Definition: CMemoryStream.cpp:161
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:30



Page generated by Doxygen 1.8.17 for MRPT 1.9.9 Git: ad3a9d8ae Tue May 1 23:10:22 2018 -0700 at miƩ 12 jul 2023 10:03:34 CEST