Main MRPT website > C++ reference for MRPT 1.9.9
CMemoryStream.cpp
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 
10 #include "base-precomp.h" // Precompiled headers
11 
15 #include <mrpt/system/os.h>
16 #include <cstring>
17 
18 using namespace mrpt::utils;
19 using namespace std;
20 
21 /*---------------------------------------------------------------
22  Constructor
23  ---------------------------------------------------------------*/
25  : m_memory(nullptr),
26  m_size(0),
27  m_position(0),
28  m_bytesWritten(0),
29  m_alloc_block_size(0x1000),
30  m_read_only(false)
31 {
32 }
33 
34 /*---------------------------------------------------------------
35  Constructor with data
36  ---------------------------------------------------------------*/
37 CMemoryStream::CMemoryStream(const void* data, const uint64_t nBytesInData)
38  : m_memory(nullptr),
39  m_size(0),
40  m_position(0),
41  m_bytesWritten(0),
42  m_alloc_block_size(0x1000),
43  m_read_only(false)
44 {
46  ASSERT_(data != nullptr);
47 
48  // Set data:
49  resize(nBytesInData);
50  memcpy(m_memory.get(), data, nBytesInData);
51 
52  m_bytesWritten = nBytesInData;
53 
54  MRPT_END
55 }
56 
57 /*---------------------------------------------------------------
58  assignMemoryNotOwn
59  ---------------------------------------------------------------*/
61  const void* data, const uint64_t nBytesInData)
62 {
63  this->Clear();
64  m_memory.set(data);
65  m_size = nBytesInData;
66  m_position = 0;
67  m_bytesWritten = 0;
68  m_read_only = true;
69 }
70 
71 /*---------------------------------------------------------------
72  Destructor
73  ---------------------------------------------------------------*/
75 {
76  if (!m_read_only)
77  {
78  // Free memory buffer:
79  resize(0);
80  }
81 }
82 
83 /*---------------------------------------------------------------
84  resize
85  ---------------------------------------------------------------*/
87 {
88  if (m_read_only)
90  "[CMemoryStream::resize] Cannot change memory block size since it "
91  "was set with 'assign'")
92 
93  if (!newSize)
94  { // Free buffer:
95  if (m_memory.get()) free(m_memory.get());
96  m_memory = nullptr;
97  m_size = 0;
98  m_position = 0;
99  }
100  else
101  { // Resize:
102  m_memory.set(realloc(m_memory.get(), newSize));
103 
104  // Check for non-memory errors??
105  if (newSize) ASSERT_(m_memory.get());
106 
107  m_size = newSize;
108  }
109 
111 }
112 
113 /*---------------------------------------------------------------
114  Read
115  Reads bytes from the stream into Buffer
116  ---------------------------------------------------------------*/
117 size_t CMemoryStream::Read(void* Buffer, size_t Count)
118 {
119  // Enought bytes?
120  long maxAvail = (((long)m_size)) - ((long)m_position);
121  size_t nToRead = (size_t)min(((long)Count), maxAvail);
122 
123  // Copy the memory block:
124  if (nToRead > 0)
125  memcpy(Buffer, ((char*)m_memory.get()) + m_position, nToRead);
126 
127  // Update cursor position:
128  m_position += nToRead;
129  return nToRead;
130 }
131 
132 /*---------------------------------------------------------------
133  Write
134  Writes a block of bytes to the stream.
135  ---------------------------------------------------------------*/
136 size_t CMemoryStream::Write(const void* Buffer, size_t Count)
137 {
138  // Enought space in current bufer?
139  size_t requiredSize = m_position + Count;
140 
141  if (requiredSize >= m_size)
142  {
143  // Incrent the size of reserved memory:
144  resize(requiredSize + m_alloc_block_size);
145  }
146 
147  // Copy the memory block:
148  memcpy(((char*)m_memory.get()) + m_position, Buffer, Count);
149 
150  // New cursor position:
151  m_position = requiredSize;
152 
154 
155  return Count;
156 }
157 
158 /*---------------------------------------------------------------
159  Seek
160  Method for moving to a specified position in the streamed resource.
161  See documentation of CStream::Seek
162  ---------------------------------------------------------------*/
164 {
165  switch (Origin)
166  {
167  case sFromBeginning:
168  m_position = Offset;
169  break;
170  case sFromCurrent:
171  m_position += Offset;
172  break;
173  case sFromEnd:
174  m_position = m_bytesWritten - 1 + Origin;
175  break;
176  };
177 
178  if (m_position >= m_size) m_position = m_size - 1;
179 
180  return m_position;
181 }
182 
183 /*---------------------------------------------------------------
184  getTotalBytesCount
185  ---------------------------------------------------------------*/
187 /*---------------------------------------------------------------
188  getPosition
189  ---------------------------------------------------------------*/
191 /*---------------------------------------------------------------
192  Clear
193  ---------------------------------------------------------------*/
195 {
196  if (!m_read_only)
197  {
198  resize(0);
199  }
200  else
201  {
202  m_memory = 0;
203  m_size = 0;
204  m_position = 0;
205  m_bytesWritten = 0;
206  m_read_only = false;
207  }
208 }
209 
210 /*---------------------------------------------------------------
211  getRawBufferData
212  Method for getting a pointer to the raw stored data. The
213  lenght in bytes is given by getTotalBytesCount
214  ---------------------------------------------------------------*/
216 /*---------------------------------------------------------------
217  changeSize
218 Change size. This would be rarely used
219  Use ">>" operators for writing to stream.
220  ---------------------------------------------------------------*/
221 void CMemoryStream::changeSize(uint64_t newSize) { resize(newSize); }
222 /*---------------------------------------------------------------
223  saveBufferToFile
224  ---------------------------------------------------------------*/
226 {
227  try
228  {
229  CFileOutputStream fo(file_name);
231  return true;
232  }
233  catch (...)
234  {
235  return false;
236  }
237 }
238 
239 /*---------------------------------------------------------------
240  loadBufferFromFile
241  ---------------------------------------------------------------*/
243 {
244  try
245  {
246  CFileInputStream fi(file_name);
247  uint64_t N = fi.getTotalBytesCount();
248 
249  // Read into the buffer:
250  Clear();
251  resize(N + 100);
252  uint64_t N_read = fi.ReadBuffer(m_memory.get(), N);
253 
254  m_position = N_read;
256 
257  return N_read == N;
258  }
259  catch (...)
260  {
261  return false;
262  }
263 }
264 
265 // Used in mrpt_send_to_zmq(). `hint` points to a `TFreeFnDataForZMQ` struct, to
266 // be freed here.
267 void mrpt::utils::internal::free_fn_for_zmq(void* /* data*/, void* hint)
268 {
270  reinterpret_cast<mrpt::utils::internal::TFreeFnDataForZMQ*>(hint);
271  if (fd->do_free) delete fd->buf;
272  delete fd;
273 }
size_t ReadBuffer(void *Buffer, size_t Count)
Reads a block of bytes from the stream into Buffer.
Definition: CStream.cpp:40
size_t Write(const void *Buffer, size_t Count) override
Introduces a pure virtual method responsible for writing to the stream.
Classes for serialization, sockets, ini-file manipulation, streams, list of properties-values, timewatch, extensions to STL.
#define min(a, b)
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...
#define THROW_EXCEPTION(msg)
void WriteBuffer(const void *Buffer, size_t Count)
Writes a block of bytes to the stream from Buffer.
Definition: CStream.cpp:64
size_t Read(void *Buffer, size_t Count) override
Introduces a pure virtual method responsible for reading from the stream.
STL namespace.
void resize(uint64_t newSize)
Resizes the internal buffer size.
#define MRPT_END
virtual ~CMemoryStream()
Destructor.
void Clear()
Clears the memory buffer.
This CStream derived class allow using a file as a write-only, binary stream.
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
size_t m_size
Number of elements accessed with write access so far.
Definition: ts_hash_map.h:181
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.
#define MRPT_START
unsigned __int64 uint64_t
Definition: rptypes.h:50
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)
This CStream derived class allow using a file as a read-only, binary stream.
void set(const T *p)
This method can change the pointer, since the change is made explicitly, not through copy operators t...
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
uint64_t getTotalBytesCount() override
Method for getting the total number of bytes in the buffer.
void memcpy(void *dest, size_t destSize, const void *src, size_t copyCount) noexcept
An OS and compiler independent version of "memcpy".
Definition: os.cpp:355



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