Fast CDR  Version 1.1.0
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 : 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
77 void operator <<(
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
105 void operator <<(
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
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
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
185 {
186 ++m_currentPosition;
187 return *this;
188 }
189
194 inline
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
213private:
214
216 char* m_buffer;
217
219 char* m_currentPosition;
220};
221
228class Cdr_DllAPI FastBuffer
229{
230public:
231
233
240
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
303 {
304 return (iterator(m_buffer, 0));
305 }
306
311 inline
313 {
314 return (iterator(m_buffer, m_bufferSize));
315 }
316
323 size_t size);
324
330 bool resize(
331 size_t minSizeInc);
332
333private:
334
336 const FastBuffer&) = delete;
337
338 FastBuffer& operator =(
339 const FastBuffer&) = delete;
340
342 char* m_buffer;
343
345 size_t m_bufferSize;
346
348 bool m_internalBuffer;
349};
350} //namespace fastcdr
351} //namespace eprosima
352
353#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:130
_FastBuffer_iterator()
Default constructor.
Definition FastBuffer.h:50
void rmemcopy(void *dst, const size_t size)
This function copies from the raw buffer to a external buffer.
Definition FastBuffer.h:146
_FastBuffer_iterator(char *buffer, size_t index)
Constructor.
Definition FastBuffer.h:62
This class represents a stream of bytes that contains (or will contain) serialized data.
Definition FastBuffer.h:229
virtual ~FastBuffer()
Default destructor.
bool resize(size_t minSizeInc)
This function resizes the raw buffer.
bool reserve(size_t size)
This function reserves memory for the internal raw buffer.
FastBuffer(char *const buffer, const size_t bufferSize)
This constructor assigns the user's stream of bytes to the eprosima::fastcdr::FastBuffers object.
FastBuffer()
This constructor creates an internal stream and assigns it to the eprosima::fastcdr::FastBuffers obje...
_FastBuffer_iterator iterator
Definition FastBuffer.h:232
char * getBuffer() const
This function returns the stream that the eprosima::fastcdr::FastBuffers uses to serialize data.
Definition FastBuffer.h:283
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
FastBuffer(FastBuffer &&fbuffer)
Move constructor.
Definition FastBuffer.h:253
iterator end()
This function returns a iterator that points to the end of the stream.
Definition FastBuffer.h:312
iterator begin()
This function returns a iterator that points to the begining of the stream.
Definition FastBuffer.h:302
Definition Cdr.h:35