/src/Fast-CDR/include/fastcdr/FastBuffer.h
Line | Count | Source (jump to first uncovered line) |
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 | 0 | { |
28 | 0 | #if defined(_WIN32) || !defined(FASTCDR_ARM32) |
29 | | // On 64 bit platforms and all Windows architectures (because of C4267), explicitly cast. |
30 | 0 | 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 | 0 | } |
36 | | |
37 | | namespace eprosima { |
38 | | namespace fastcdr { |
39 | | /*! |
40 | | * @brief This class implements the iterator used to go through a FastBuffer. |
41 | | */ |
42 | | class Cdr_DllAPI _FastBuffer_iterator |
43 | | { |
44 | | public: |
45 | | |
46 | | /*! |
47 | | * @brief Default constructor. |
48 | | * The iterator points any position. |
49 | | */ |
50 | | _FastBuffer_iterator() = default; |
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 | | */ |
58 | | explicit _FastBuffer_iterator( |
59 | | char* buffer, |
60 | | size_t index) |
61 | 0 | : buffer_(buffer) |
62 | 0 | , current_position_(&buffer_[index]) |
63 | 0 | { |
64 | 0 | } |
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 | 0 | { |
76 | 0 | ptrdiff_t diff = current_position_ - buffer_; |
77 | 0 | buffer_ = iterator.buffer_; |
78 | 0 | current_position_ = buffer_ + diff; |
79 | 0 | } |
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 | 0 | { |
90 | 0 | ptrdiff_t diff = iterator.current_position_ - iterator.buffer_; |
91 | 0 | current_position_ = buffer_ + diff; |
92 | 0 | } |
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 | 0 | { |
104 | 0 | memcpy(current_position_, &data, sizeof(_T)); |
105 | 0 | } Unexecuted instantiation: void eprosima::fastcdr::_FastBuffer_iterator::operator<< <char>(char const&) Unexecuted instantiation: void eprosima::fastcdr::_FastBuffer_iterator::operator<< <short>(short const&) Unexecuted instantiation: void eprosima::fastcdr::_FastBuffer_iterator::operator<< <int>(int const&) Unexecuted instantiation: void eprosima::fastcdr::_FastBuffer_iterator::operator<< <long>(long const&) Unexecuted instantiation: void eprosima::fastcdr::_FastBuffer_iterator::operator<< <float>(float const&) Unexecuted instantiation: void eprosima::fastcdr::_FastBuffer_iterator::operator<< <double>(double const&) Unexecuted instantiation: void eprosima::fastcdr::_FastBuffer_iterator::operator<< <long double>(long double const&) Unexecuted instantiation: void eprosima::fastcdr::_FastBuffer_iterator::operator<< <unsigned char>(unsigned char const&) |
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 | 0 | { |
117 | 0 | memcpy(&data, current_position_, sizeof(_T)); |
118 | 0 | } Unexecuted instantiation: void eprosima::fastcdr::_FastBuffer_iterator::operator>><char>(char&) Unexecuted instantiation: void eprosima::fastcdr::_FastBuffer_iterator::operator>><short>(short&) Unexecuted instantiation: void eprosima::fastcdr::_FastBuffer_iterator::operator>><int>(int&) Unexecuted instantiation: void eprosima::fastcdr::_FastBuffer_iterator::operator>><long>(long&) Unexecuted instantiation: void eprosima::fastcdr::_FastBuffer_iterator::operator>><float>(float&) Unexecuted instantiation: void eprosima::fastcdr::_FastBuffer_iterator::operator>><double>(double&) Unexecuted instantiation: void eprosima::fastcdr::_FastBuffer_iterator::operator>><long double>(long double&) Unexecuted instantiation: void eprosima::fastcdr::_FastBuffer_iterator::operator>><unsigned char>(unsigned char&) |
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 |
126 | | void memcopy( |
127 | | const void* src, |
128 | | const size_t size) |
129 | 0 | { |
130 | 0 | if (size > 0) |
131 | 0 | { |
132 | 0 | memcpy(current_position_, src, size); |
133 | 0 | } |
134 | 0 | } |
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 |
142 | | void rmemcopy( |
143 | | void* dst, |
144 | | const size_t size) |
145 | 0 | { |
146 | 0 | if (size > 0) |
147 | 0 | { |
148 | 0 | memcpy(dst, current_position_, size); |
149 | 0 | } |
150 | 0 | } |
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 | 0 | { |
160 | 0 | current_position_ += num_bytes; |
161 | 0 | } |
162 | | |
163 | | inline |
164 | | void operator -=( |
165 | | size_t num_bytes) |
166 | 0 | { |
167 | 0 | current_position_ -= num_bytes; |
168 | 0 | } |
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 | 0 | { |
179 | 0 | return static_cast<size_t>(current_position_ - it.current_position_); |
180 | 0 | } |
181 | | |
182 | | /*! |
183 | | * @brief This function increments the iterator in one the position. |
184 | | * @return The current iterator. |
185 | | */ |
186 | | inline |
187 | | _FastBuffer_iterator operator ++() |
188 | 0 | { |
189 | 0 | ++current_position_; |
190 | 0 | return *this; |
191 | 0 | } |
192 | | |
193 | | /*! |
194 | | * @brief This function increments the iterator in one the position. |
195 | | * @return The current iterator. |
196 | | */ |
197 | | inline |
198 | | _FastBuffer_iterator operator ++( |
199 | | int) |
200 | 0 | { |
201 | 0 | _FastBuffer_iterator tmp = *this; |
202 | 0 | ++*this; |
203 | 0 | return tmp; |
204 | 0 | } |
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 | 0 | { |
213 | 0 | return current_position_; |
214 | 0 | } |
215 | | |
216 | | bool operator ==( |
217 | | const _FastBuffer_iterator& other_iterator) const |
218 | 0 | { |
219 | 0 | return other_iterator.current_position_ == current_position_; |
220 | 0 | } |
221 | | |
222 | | bool operator !=( |
223 | | const _FastBuffer_iterator& other_iterator) const |
224 | 0 | { |
225 | 0 | return !(other_iterator == *this); |
226 | 0 | } |
227 | | |
228 | | private: |
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 | | */ |
243 | | class Cdr_DllAPI FastBuffer |
244 | | { |
245 | | public: |
246 | | |
247 | | typedef _FastBuffer_iterator iterator; |
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 | | */ |
263 | | FastBuffer( |
264 | | char* const buffer, |
265 | | const size_t bufferSize); |
266 | | |
267 | | //! Move constructor |
268 | | FastBuffer( |
269 | | FastBuffer&& fbuffer) |
270 | | : buffer_(nullptr) |
271 | | , size_(0) |
272 | | , m_internalBuffer(true) |
273 | 0 | { |
274 | 0 | std::swap(buffer_, fbuffer.buffer_); |
275 | 0 | std::swap(size_, fbuffer.size_); |
276 | 0 | std::swap(m_internalBuffer, fbuffer.m_internalBuffer); |
277 | 0 | } |
278 | | |
279 | | //! Move assignment |
280 | | FastBuffer& operator =( |
281 | | FastBuffer&& fbuffer) |
282 | 0 | { |
283 | 0 | std::swap(buffer_, fbuffer.buffer_); |
284 | 0 | std::swap(size_, fbuffer.size_); |
285 | 0 | std::swap(m_internalBuffer, fbuffer.m_internalBuffer); |
286 | 0 | return *this; |
287 | 0 | } |
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 | 0 | { |
300 | 0 | return buffer_; |
301 | 0 | } |
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 | 0 | { |
309 | 0 | return size_; |
310 | 0 | } |
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 |
317 | | iterator begin() |
318 | 0 | { |
319 | 0 | return (iterator(buffer_, 0)); |
320 | 0 | } |
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 |
327 | | iterator end() |
328 | 0 | { |
329 | 0 | return (iterator(buffer_, size_)); |
330 | 0 | } |
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 | | |
348 | | private: |
349 | | |
350 | | FastBuffer( |
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_ |