Fast CDR  Version 2.2.4
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
25inline 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
37namespace eprosima {
38namespace fastcdr {
42class Cdr_DllAPI _FastBuffer_iterator
43{
44public:
45
51
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
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
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
188 {
189 ++current_position_;
190 return *this;
191 }
192
197 inline
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
228private:
229
231 char* buffer_ {nullptr};
232
234 char* current_position_ {nullptr};
235};
236
243class Cdr_DllAPI FastBuffer
244{
245public:
246
248
254 FastBuffer() = default;
255
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
338 size_t size);
339
345 bool resize(
346 size_t min_size_inc);
347
348private:
349
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_
This class implements the iterator used to go through a FastBuffer.
Definition FastBuffer.h:43
void memcopy(const void *src, const size_t size)
This function copies a buffer into the raw buffer.
Definition FastBuffer.h:126
_FastBuffer_iterator()=default
Default constructor.
void rmemcopy(void *dst, const size_t size)
This function copies from the raw buffer to a external buffer.
Definition FastBuffer.h:142
_FastBuffer_iterator(char *buffer, size_t index)
Constructor.
Definition FastBuffer.h:58
This class represents a stream of bytes that contains (or will contain) serialized data.
Definition FastBuffer.h:244
virtual ~FastBuffer()
Default destructor.
bool reserve(size_t size)
This function reserves memory for the internal raw buffer.
FastBuffer()=default
This constructor creates an internal stream and assigns it to the eprosima::fastcdr::FastBuffers obje...
FastBuffer(char *const buffer, const size_t bufferSize)
This constructor assigns the user's stream of bytes to the eprosima::fastcdr::FastBuffers object.
_FastBuffer_iterator iterator
Definition FastBuffer.h:247
bool resize(size_t min_size_inc)
This function resizes the raw buffer.
char * getBuffer() const
This function returns the stream that the eprosima::fastcdr::FastBuffers uses to serialize data.
Definition FastBuffer.h:298
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
FastBuffer(FastBuffer &&fbuffer)
Move constructor.
Definition FastBuffer.h:268
iterator end()
This function returns a iterator that points to the end of the stream.
Definition FastBuffer.h:327
iterator begin()
This function returns a iterator that points to the begining of the stream.
Definition FastBuffer.h:317
Definition Cdr.h:48