pjmsg_mcap_wrapper
Loading...
Searching...
No Matches
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
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 {
39/*!
40 * @brief This class implements the iterator used to go through a FastBuffer.
41 */
43{
44public:
45
46 /*!
47 * @brief Default constructor.
48 * The iterator points any position.
49 */
51
52 /*!
53 * @brief Constructor.
54 * The iterator points to the indicated position.
55 * @param buffer Pointer to the raw buffer.
56 * @param index Position of the raw buffer where the iterator will point.
57 */
59 char* buffer,
60 size_t index)
61 : buffer_(buffer)
62 , current_position_(&buffer_[index])
63 {
64 }
65
66 /*!
67 * @brief This operator changes the iterator's raw buffer.
68 * This operator makes the iterator point to the same position but in another raw buffer.
69 * The new raw buffer is the same than the source iterator's.
70 * @param iterator The source iterator. The iterator will use the source iterator's raw buffer after this operation.
71 */
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
81 /*!
82 * @brief This operator changes the position where the iterator points.
83 * This operator takes the index of the source iterator, but the iterator continues using its raw buffer.
84 * @param iterator The source iterator. The iterator will use the source's iterator index to point to its own raw buffer.
85 */
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
94 /*!
95 * @brief This operator copies a data in the raw buffer.
96 * The copy uses the size of the data type.
97 * @param data Data to be copied. Cannot be NULL.
98 */
99 template<typename _T>
100 inline
101 void operator <<(
102 const _T& data)
103 {
104 memcpy(current_position_, &data, sizeof(_T));
105 }
106
107 /*!
108 * @brief This operator copies data from the raw buffer to a variable.
109 * The copy uses the size of the data type.
110 * @param data Data to be filled.
111 */
112 template<typename _T>
113 inline
114 void operator >>(
115 _T& data)
116 {
117 memcpy(&data, current_position_, sizeof(_T));
118 }
119
120 /*!
121 * @brief This function copies a buffer into the raw buffer.
122 * @param src The source buffer.
123 * @param size The number of bytes to be copied.
124 */
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
136 /*!
137 * @brief This function copies from the raw buffer to a external buffer.
138 * @param dst The destination buffer.
139 * @param size The size of bytes to be copied.
140 */
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
152 /*!
153 * @brief This function increments the position where the iterator points.
154 * @param num_bytes Number of bytes the iterator moves the position.
155 */
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
170 /*!
171 * @brief This operator returns the subtraction of the current interator's position and the source iterator's position.
172 * @param it Source iterator whose position is subtracted to the current iterator's position.
173 * @return The result of subtract the current iterator's position and the source iterator's position.
174 */
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
182 /*!
183 * @brief This function increments the iterator in one the position.
184 * @return The current iterator.
185 */
186 inline
188 {
189 ++current_position_;
190 return *this;
191 }
192
193 /*!
194 * @brief This function increments the iterator in one the position.
195 * @return The current iterator.
196 */
197 inline
199 int)
200 {
201 _FastBuffer_iterator tmp = *this;
202 ++*this;
203 return tmp;
204 }
205
206 /*!
207 * @brief This function returns the current position in the raw buffer.
208 * @return The current position in the raw buffer.
209 */
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
230 //! Pointer to the raw buffer.
231 char* buffer_ {nullptr};
232
233 //! Current position in the raw buffer.
234 char* current_position_ {nullptr};
235};
236
237/*!
238 * @brief This class represents a stream of bytes that contains (or will contain)
239 * serialized data. This class is used by the serializers to serialize
240 * or deserialize using their representation.
241 * @ingroup FASTCDRAPIREFERENCE
242 */
244{
245public:
246
248
249 /*!
250 * @brief This constructor creates an internal stream and assigns it to the eprosima::fastcdr::FastBuffers object.
251 * The user can obtain this internal stream using the function eprosima::fastcdr::FastBuffers::getBuffer(). Be careful because this internal stream
252 * is deleted in the destruction of the eprosima::fastcdr::FastBuffers object.
253 */
254 FastBuffer() = default;
255
256 /*!
257 * @brief This constructor assigns the user's stream of bytes to the eprosima::fastcdr::FastBuffers object.
258 * The user's stream will be used to serialize.
259 *
260 * @param buffer The user's buffer that will be used. This buffer is not deallocated in the object's destruction. Cannot be NULL.
261 * @param bufferSize The length of user's buffer.
262 */
264 char* const buffer,
265 const size_t bufferSize);
266
267 //! Move constructor
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
279 //! Move assignment
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
289 /*!
290 * @brief Default destructor.
291 */
292 virtual ~FastBuffer();
293
294 /*!
295 * @brief This function returns the stream that the eprosima::fastcdr::FastBuffers uses to serialize data.
296 * @return The stream used by eprosima::fastcdr::FastBuffers to serialize data.
297 */
298 inline char* getBuffer() const
299 {
300 return buffer_;
301 }
302
303 /*!
304 * @brief This function returns the size of the allocated memory of the stream that the eprosima::fastcdr::FastBuffers uses to serialize data.
305 * @return The size of the allocated memory of the stream used by the eprosima::fastcdr::FastBuffers to serialize data.
306 */
307 inline size_t getBufferSize() const
308 {
309 return size_;
310 }
311
312 /*!
313 * @brief This function returns a iterator that points to the begining of the stream.
314 * @return The new iterator.
315 */
316 inline
318 {
319 return (iterator(buffer_, 0));
320 }
321
322 /*!
323 * @brief This function returns a iterator that points to the end of the stream.
324 * @return The new iterator.
325 */
326 inline
328 {
329 return (iterator(buffer_, size_));
330 }
331
332 /*!
333 * @brief This function reserves memory for the internal raw buffer. It will only do so if the buffer is not yet allocated and is not externally set.
334 * @param size The size of the memory to be allocated.
335 * @return True if the allocation suceeded. False if the raw buffer was set externally or is already allocated.
336 */
337 bool reserve(
338 size_t size);
339
340 /*!
341 * @brief This function resizes the raw buffer. It will call the user's defined function for this purpose.
342 * @param min_size_inc The minimun growth expected of the current raw buffer.
343 * @return True if the operation works. False if it does not.
344 */
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
356 //! @brief Pointer to the stream of bytes that contains the serialized data.
357 char* buffer_ { nullptr };
358
359 //! @brief The total size of the user's buffer.
360 size_t size_ { 0 };
361
362 //! @brief This variable indicates if the managed buffer is internal or is from the user.
363 bool m_internalBuffer { true };
364};
365} //namespace fastcdr
366} //namespace eprosima
367
368#endif // _FASTCDR_FASTCDRBUFFER_H_
uint32_t size_to_uint32(size_t val)
Definition FastBuffer.h:25
This class represents a stream of bytes that contains (or will contain) serialized data....
Definition FastBuffer.h:244
FastBuffer(FastBuffer &&fbuffer)
Move constructor.
Definition FastBuffer.h:268
FastBuffer()=default
This constructor creates an internal stream and assigns it to the eprosima::fastcdr::FastBuffers obje...
FastBuffer(const FastBuffer &)=delete
iterator begin()
This function returns a iterator that points to the begining of the stream.
Definition FastBuffer.h:317
char * getBuffer() const
This function returns the stream that the eprosima::fastcdr::FastBuffers uses to serialize data.
Definition FastBuffer.h:298
_FastBuffer_iterator iterator
Definition FastBuffer.h:247
iterator end()
This function returns a iterator that points to the end of the stream.
Definition FastBuffer.h:327
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
This class implements the iterator used to go through a FastBuffer.
Definition FastBuffer.h:43
_FastBuffer_iterator()=default
Default constructor. The iterator points any position.
void memcopy(const void *src, const size_t size)
This function copies a buffer into the raw buffer.
Definition FastBuffer.h:126
char * current_position_
Current position in the raw buffer.
Definition FastBuffer.h:234
_FastBuffer_iterator(char *buffer, size_t index)
Constructor. The iterator points to the indicated position.
Definition FastBuffer.h:58
void rmemcopy(void *dst, const size_t size)
This function copies from the raw buffer to a external buffer.
Definition FastBuffer.h:142
char * buffer_
Pointer to the raw buffer.
Definition FastBuffer.h:231
#define Cdr_DllAPI
Definition fastcdr_dll.h:51
Definition Cdr.h:49
T swap(T... args)