| Fast CDR Version 2.3.5 Fast CDR

Loading...
Searching...
No Matches
FastBuffer.h
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 {
42 class Cdr_DllAPI _FastBuffer_iterator
43{
44 public:
45
50 _FastBuffer_iterator() = default;
51
58 explicit _FastBuffer_iterator(
59 char* buffer,
60 size_t index)
61 : buffer_(buffer)
62 , current_position_(&buffer_[index])
63 {
64 }
65
72 inline
73 void operator <<(
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
101 void operator <<(
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
216 bool operator ==(
217 const _FastBuffer_iterator& other_iterator) const
218 {
219 return other_iterator.current_position_ == current_position_;
220 }
221
222 bool operator !=(
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
243 class Cdr_DllAPI FastBuffer
244{
245 public:
246
247 typedef _FastBuffer_iterator iterator;
248
254 FastBuffer() = default;
255
263 FastBuffer(
264 char* const buffer,
265 const size_t bufferSize);
266
268 FastBuffer(
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
317 iterator begin()
318 {
319 return (iterator(buffer_, 0));
320 }
321
326 inline
327 iterator end()
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
This class implements the iterator used to go through a FastBuffer.
Definition FastBuffer.h:43
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_iterator::_FastBuffer_iterator
_FastBuffer_iterator()=default
Default constructor.
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
_FastBuffer_iterator(char *buffer, size_t index)
Constructor.
Definition FastBuffer.h:58
eprosima::fastcdr::FastBuffer::~FastBuffer
virtual ~FastBuffer()
Default destructor.
eprosima::fastcdr::FastBuffer::reserve
bool reserve(size_t size)
This function reserves memory for the internal raw buffer.
eprosima::fastcdr::FastBuffer::FastBuffer
FastBuffer()=default
This constructor creates an internal stream and assigns it to the eprosima::fastcdr::FastBuffers obje...
eprosima::fastcdr::FastBuffer::FastBuffer
FastBuffer(char *const buffer, const size_t bufferSize)
This constructor assigns the user's stream of bytes to the eprosima::fastcdr::FastBuffers object.
eprosima::fastcdr::FastBuffer::iterator
_FastBuffer_iterator iterator
Definition FastBuffer.h:247
eprosima::fastcdr::FastBuffer::resize
bool resize(size_t min_size_inc)
This function resizes the raw buffer.
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
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::FastBuffer
FastBuffer(FastBuffer &&fbuffer)
Move constructor.
Definition FastBuffer.h:268
eprosima::fastcdr::FastBuffer::end
iterator end()
This function returns a iterator that points to the end of the stream.
Definition FastBuffer.h:327
eprosima::fastcdr::FastBuffer::begin
iterator begin()
This function returns a iterator that points to the begining of the stream.
Definition FastBuffer.h:317
eprosima::fastcdr
Definition Cdr.h:50
eprosima
Definition Cdr.h:49