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 
50  _FastBuffer_iterator() = default;
51 
59  char* buffer,
60  size_t index)
61  : buffer_(buffer)
62  , current_position_(&buffer_[index])
63  {
64  }
65 
72  inline
74  const _FastBuffer_iterator& iterator)
75  {
76  ptrdiff_t diff = current_position_ - buffer_;
77  buffer_ = iterator.buffer_;
78  current_position_ = buffer_ + diff;
79  }
80 
86  inline
87  void operator >>(
88  const _FastBuffer_iterator& iterator)
89  {
90  ptrdiff_t diff = iterator.current_position_ - iterator.buffer_;
91  current_position_ = buffer_ + diff;
92  }
93 
99  template<typename _T>
100  inline
102  const _T& data)
103  {
104  memcpy(current_position_, &data, sizeof(_T));
105  }
106 
112  template<typename _T>
113  inline
114  void operator >>(
115  _T& data)
116  {
117  memcpy(&data, current_position_, sizeof(_T));
118  }
119 
125  inline
126  void memcopy(
127  const void* src,
128  const size_t size)
129  {
130  if (size > 0)
131  {
132  memcpy(current_position_, src, size);
133  }
134  }
135 
141  inline
142  void rmemcopy(
143  void* dst,
144  const size_t size)
145  {
146  if (size > 0)
147  {
148  memcpy(dst, current_position_, size);
149  }
150  }
151 
156  inline
157  void operator +=(
158  size_t num_bytes)
159  {
160  current_position_ += num_bytes;
161  }
162 
163  inline
164  void operator -=(
165  size_t num_bytes)
166  {
167  current_position_ -= num_bytes;
168  }
169 
175  inline
176  size_t operator -(
177  const _FastBuffer_iterator& it) const
178  {
179  return static_cast<size_t>(current_position_ - it.current_position_);
180  }
181 
186  inline
187  _FastBuffer_iterator operator ++()
188  {
189  ++current_position_;
190  return *this;
191  }
192 
197  inline
198  _FastBuffer_iterator operator ++(
199  int)
200  {
201  _FastBuffer_iterator tmp = *this;
202  ++*this;
203  return tmp;
204  }
205 
210  inline
211  char* operator &()
212  {
213  return current_position_;
214  }
215 
217  const _FastBuffer_iterator& other_iterator) const
218  {
219  return other_iterator.current_position_ == current_position_;
220  }
221 
223  const _FastBuffer_iterator& other_iterator) const
224  {
225  return !(other_iterator == *this);
226  }
227 
228 private:
229 
231  char* buffer_ {nullptr};
232 
234  char* current_position_ {nullptr};
235 };
236 
244 {
245 public:
246 
248 
254  FastBuffer() = default;
255 
263  FastBuffer(
264  char* const buffer,
265  const size_t bufferSize);
266 
269  FastBuffer&& fbuffer)
270  : buffer_(nullptr)
271  , size_(0)
272  , m_internalBuffer(true)
273  {
274  std::swap(buffer_, fbuffer.buffer_);
275  std::swap(size_, fbuffer.size_);
276  std::swap(m_internalBuffer, fbuffer.m_internalBuffer);
277  }
278 
280  FastBuffer& operator =(
281  FastBuffer&& fbuffer)
282  {
283  std::swap(buffer_, fbuffer.buffer_);
284  std::swap(size_, fbuffer.size_);
285  std::swap(m_internalBuffer, fbuffer.m_internalBuffer);
286  return *this;
287  }
288 
292  virtual ~FastBuffer();
293 
298  inline char* getBuffer() const
299  {
300  return buffer_;
301  }
302 
307  inline size_t getBufferSize() const
308  {
309  return size_;
310  }
311 
316  inline
318  {
319  return (iterator(buffer_, 0));
320  }
321 
326  inline
328  {
329  return (iterator(buffer_, size_));
330  }
331 
337  bool reserve(
338  size_t size);
339 
345  bool resize(
346  size_t min_size_inc);
347 
348 private:
349 
350  FastBuffer(
351  const FastBuffer&) = delete;
352 
353  FastBuffer& operator =(
354  const FastBuffer&) = delete;
355 
357  char* buffer_ { nullptr };
358 
360  size_t size_ { 0 };
361 
363  bool m_internalBuffer { true };
364 };
365 } //namespace fastcdr
366 } //namespace eprosima
367 
368 #endif // _FASTCDR_FASTCDRBUFFER_H_
eprosima::fastcdr::_FastBuffer_iterator::memcopy
void memcopy(const void *src, const size_t size)
This function copies a buffer into the raw buffer.
Definition: FastBuffer.h:126
eprosima::fastcdr::FastBuffer::FastBuffer
FastBuffer(FastBuffer &&fbuffer)
Move constructor.
Definition: FastBuffer.h:268
sol::detail::reserve
void reserve(T &, std::size_t)
Definition: sol.hpp:11364
eprosima::fastcdr::_FastBuffer_iterator::buffer_
char * buffer_
Pointer to the raw buffer.
Definition: FastBuffer.h:231
eprosima::fastcdr::_FastBuffer_iterator::rmemcopy
void rmemcopy(void *dst, const size_t size)
This function copies from the raw buffer to a external buffer.
Definition: FastBuffer.h:142
eprosima::fastcdr::FastBuffer::iterator
_FastBuffer_iterator iterator
Definition: FastBuffer.h:247
eprosima::fastcdr::FastBuffer::getBufferSize
size_t getBufferSize() const
This function returns the size of the allocated memory of the stream that the eprosima::fastcdr::Fast...
Definition: FastBuffer.h:307
eprosima::fastcdr::FastBuffer::begin
iterator begin()
This function returns a iterator that points to the begining of the stream.
Definition: FastBuffer.h:317
nonstd::ring_span_lite::detail::operator-
ring_iterator< RS, C > operator-(ring_iterator< RS, C > it, int i) nsrs_noexcept
Definition: ring_span.hpp:1010
nonstd::span_lite::size
span_constexpr std::size_t size(span< T, Extent > const &spn)
Definition: span.hpp:1554
sol::operator&
constexpr automagic_flags operator&(automagic_flags left, automagic_flags right) noexcept
Definition: sol.hpp:8673
size_to_uint32
uint32_t size_to_uint32(size_t val)
Definition: FastBuffer.h:25
operator==
bool operator==(QwtEventPattern::MousePattern b1, QwtEventPattern::MousePattern b2)
Compare operator.
Definition: qwt_event_pattern.h:228
std::swap
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
sol::operator!=
constexpr bool operator!=(const optional< T > &lhs, const optional< U > &rhs)
\group relop
Definition: sol.hpp:6020
Cdr_DllAPI
#define Cdr_DllAPI
Definition: fastcdr_dll.h:51
fastcdr_dll.h
eprosima::fastcdr::FastBuffer::getBuffer
char * getBuffer() const
This function returns the stream that the eprosima::fastcdr::FastBuffers uses to serialize data.
Definition: FastBuffer.h:298
mcap::internal::operator<<
std::ostream & operator<<(std::ostream &out, const Interval< Scalar, Value > &i)
Definition: intervaltree.hpp:37
mqtt_test.data
dictionary data
Definition: mqtt_test.py:22
eprosima::fastcdr::FastBuffer
This class represents a stream of bytes that contains (or will contain) serialized data....
Definition: FastBuffer.h:243
eprosima::fastcdr::_FastBuffer_iterator::_FastBuffer_iterator
_FastBuffer_iterator(char *buffer, size_t index)
Constructor. The iterator points to the indicated position.
Definition: FastBuffer.h:58
dst
char * dst
Definition: lz4.h:792
eprosima
Definition: fixed_size_string.hpp:32
eprosima::fastcdr::_FastBuffer_iterator::current_position_
char * current_position_
Current position in the raw buffer.
Definition: FastBuffer.h:234
eprosima::fastcdr::FastBuffer::end
iterator end()
This function returns a iterator that points to the end of the stream.
Definition: FastBuffer.h:327
nullptr
#define nullptr
Definition: backward.hpp:386
eprosima::fastcdr::_FastBuffer_iterator
This class implements the iterator used to go through a FastBuffer.
Definition: FastBuffer.h:42


plotjuggler
Author(s): Davide Faconti
autogenerated on Sun Aug 11 2024 02:24:22