FastBuffer.h
Go to the documentation of this file.
1 // Copyright 2016 Proyectos y Sistemas de Mantenimiento SL (eProsima).
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #ifndef _FASTCDR_CDRBUFFER_H_
16 #define _FASTCDR_CDRBUFFER_H_
17 
18 #include "fastcdr_dll.h"
19 #include <stdint.h>
20 #include <cstdio>
21 #include <string.h>
22 #include <cstddef>
23 #include <utility>
24 
25 inline uint32_t size_to_uint32(
26  size_t val)
27 {
28  #if defined(_WIN32) || !defined(FASTCDR_ARM32)
29  // On 64 bit platforms and all Windows architectures (because of C4267), explicitly cast.
30  return static_cast<uint32_t>(val);
31  #else
32  // Skip useless cast on 32-bit builds.
33  return val;
34  #endif // if defined(_WIN32) || !defined(FASTCDR_ARM32)
35 }
36 
37 namespace eprosima {
38 namespace fastcdr {
43 {
44 public:
45 
51  : m_buffer(NULL)
52  , m_currentPosition(NULL)
53  {
54  }
55 
63  char* buffer,
64  size_t index)
65  : m_buffer(buffer)
66  , m_currentPosition(&m_buffer[index])
67  {
68  }
69 
76  inline
78  const _FastBuffer_iterator& iterator)
79  {
80  ptrdiff_t diff = m_currentPosition - m_buffer;
81  m_buffer = iterator.m_buffer;
82  m_currentPosition = m_buffer + diff;
83  }
84 
90  inline
91  void operator >>(
92  const _FastBuffer_iterator& iterator)
93  {
94  ptrdiff_t diff = iterator.m_currentPosition - iterator.m_buffer;
95  m_currentPosition = m_buffer + diff;
96  }
97 
103  template<typename _T>
104  inline
106  const _T& data)
107  {
108  memcpy(m_currentPosition, &data, sizeof(_T));
109  }
110 
116  template<typename _T>
117  inline
118  void operator >>(
119  _T& data)
120  {
121  memcpy(&data, m_currentPosition, sizeof(_T));
122  }
123 
129  inline
130  void memcopy(
131  const void* src,
132  const size_t size)
133  {
134  if (size > 0)
135  {
136  memcpy(m_currentPosition, src, size);
137  }
138  }
139 
145  inline
146  void rmemcopy(
147  void* dst,
148  const size_t size)
149  {
150  if (size > 0)
151  {
152  memcpy(dst, m_currentPosition, size);
153  }
154  }
155 
160  inline
161  void operator +=(
162  size_t numBytes)
163  {
164  m_currentPosition += numBytes;
165  }
166 
172  inline
173  size_t operator -(
174  const _FastBuffer_iterator& it) const
175  {
176  return static_cast<size_t>(m_currentPosition - it.m_currentPosition);
177  }
178 
183  inline
184  _FastBuffer_iterator operator ++()
185  {
186  ++m_currentPosition;
187  return *this;
188  }
189 
194  inline
195  _FastBuffer_iterator operator ++(
196  int)
197  {
198  _FastBuffer_iterator tmp = *this;
199  ++*this;
200  return tmp;
201  }
202 
207  inline
208  char* operator &()
209  {
210  return m_currentPosition;
211  }
212 
213 private:
214 
216  char* m_buffer;
217 
220 };
221 
229 {
230 public:
231 
233 
239  FastBuffer();
240 
248  FastBuffer(
249  char* const buffer,
250  const size_t bufferSize);
251 
254  FastBuffer&& fbuffer)
255  : m_buffer(nullptr)
256  , m_bufferSize(0)
257  , m_internalBuffer(true)
258  {
259  std::swap(m_buffer, fbuffer.m_buffer);
260  std::swap(m_bufferSize, fbuffer.m_bufferSize);
261  std::swap(m_internalBuffer, fbuffer.m_internalBuffer);
262  }
263 
265  FastBuffer& operator =(
266  FastBuffer&& fbuffer)
267  {
268  std::swap(m_buffer, fbuffer.m_buffer);
269  std::swap(m_bufferSize, fbuffer.m_bufferSize);
270  std::swap(m_internalBuffer, fbuffer.m_internalBuffer);
271  return *this;
272  }
273 
277  virtual ~FastBuffer();
278 
283  inline char* getBuffer() const
284  {
285  return m_buffer;
286  }
287 
292  inline size_t getBufferSize() const
293  {
294  return m_bufferSize;
295  }
296 
301  inline
302  iterator begin()
303  {
304  return (iterator(m_buffer, 0));
305  }
306 
311  inline
312  iterator end()
313  {
314  return (iterator(m_buffer, m_bufferSize));
315  }
316 
322  bool reserve(
323  size_t size);
324 
330  bool resize(
331  size_t minSizeInc);
332 
333 private:
334 
335  FastBuffer(
336  const FastBuffer&) = delete;
337 
338  FastBuffer& operator =(
339  const FastBuffer&) = delete;
340 
342  char* m_buffer;
343 
345  size_t m_bufferSize;
346 
349 };
350 } //namespace fastcdr
351 } //namespace eprosima
352 
353 #endif // _FASTCDR_FASTCDRBUFFER_H_
constexpr automagic_flags operator &(automagic_flags left, automagic_flags right) noexcept
Definition: sol.hpp:8673
#define nullptr
Definition: backward.hpp:386
_FastBuffer_iterator iterator
Definition: FastBuffer.h:232
size_t getBufferSize() const
This function returns the size of the allocated memory of the stream that the eprosima::fastcdr::Fast...
Definition: FastBuffer.h:292
void reserve(T &, std::size_t)
Definition: sol.hpp:11364
char * dst
Definition: lz4.h:724
bool m_internalBuffer
This variable indicates if the managed buffer is internal or is from the user.
Definition: FastBuffer.h:348
This class implements the iterator used to go through a FastBuffer.
Definition: FastBuffer.h:42
iterator begin()
This function returns a iterator that points to the begining of the stream.
Definition: FastBuffer.h:302
_FastBuffer_iterator(char *buffer, size_t index)
Constructor. The iterator points to the indicated position.
Definition: FastBuffer.h:62
uint32_t size_to_uint32(size_t val)
Definition: FastBuffer.h:25
void rmemcopy(void *dst, const size_t size)
This function copies from the raw buffer to a external buffer.
Definition: FastBuffer.h:146
Definition: core.h:760
std::ostream & operator<<(std::ostream &out, const Interval< Scalar, Value > &i)
NLOHMANN_BASIC_JSON_TPL_DECLARATION void swap(nlohmann::NLOHMANN_BASIC_JSON_TPL &j1, nlohmann::NLOHMANN_BASIC_JSON_TPL &j2) noexcept(//NOLINT(readability-inconsistent-declaration-parameter-name) is_nothrow_move_constructible< nlohmann::NLOHMANN_BASIC_JSON_TPL >::value &&//NOLINT(misc-redundant-expression) is_nothrow_move_assignable< nlohmann::NLOHMANN_BASIC_JSON_TPL >::value)
exchanges the values of two JSON objects
Definition: json.hpp:21884
Definition: Cdr.h:37
char * m_buffer
Pointer to the raw buffer.
Definition: FastBuffer.h:216
size_t m_bufferSize
The total size of the user&#39;s buffer.
Definition: FastBuffer.h:345
FastBuffer(FastBuffer &&fbuffer)
Move constructor.
Definition: FastBuffer.h:253
_FastBuffer_iterator()
Default constructor. The iterator points any position.
Definition: FastBuffer.h:50
char * m_currentPosition
Current position in the raw buffer.
Definition: FastBuffer.h:219
ring_iterator< RS, C > operator-(ring_iterator< RS, C > it, int i) nsrs_noexcept
Definition: ring_span.hpp:957
#define Cdr_DllAPI
Definition: fastcdr_dll.h:51
char * m_buffer
Pointer to the stream of bytes that contains the serialized data.
Definition: FastBuffer.h:342
This class represents a stream of bytes that contains (or will contain) serialized data...
Definition: FastBuffer.h:228
void memcopy(const void *src, const size_t size)
This function copies a buffer into the raw buffer.
Definition: FastBuffer.h:130
span_constexpr std::size_t size(span< T, Extent > const &spn)
Definition: span.hpp:1485
char * getBuffer() const
This function returns the stream that the eprosima::fastcdr::FastBuffers uses to serialize data...
Definition: FastBuffer.h:283
iterator end()
This function returns a iterator that points to the end of the stream.
Definition: FastBuffer.h:312
Definition: format.h:895


plotjuggler
Author(s): Davide Faconti
autogenerated on Mon Jun 19 2023 03:01:02