/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() |
51 | | : m_buffer(NULL) |
52 | | , m_currentPosition(NULL) |
53 | 0 | { |
54 | 0 | } |
55 | | |
56 | | /*! |
57 | | * @brief Constructor. |
58 | | * The iterator points to the indicated position. |
59 | | * @param buffer Pointer to the raw buffer. |
60 | | * @param index Position of the raw buffer where the iterator will point. |
61 | | */ |
62 | | explicit _FastBuffer_iterator( |
63 | | char* buffer, |
64 | | size_t index) |
65 | | : m_buffer(buffer) |
66 | | , m_currentPosition(&m_buffer[index]) |
67 | 0 | { |
68 | 0 | } |
69 | | |
70 | | /*! |
71 | | * @brief This operator changes the iterator's raw buffer. |
72 | | * This operator makes the iterator point to the same position but in another raw buffer. |
73 | | * The new raw buffer is the same than the source iterator's. |
74 | | * @param iterator The source iterator. The iterator will use the source iterator's raw buffer after this operation. |
75 | | */ |
76 | | inline |
77 | | void operator <<( |
78 | | const _FastBuffer_iterator& iterator) |
79 | 0 | { |
80 | 0 | ptrdiff_t diff = m_currentPosition - m_buffer; |
81 | 0 | m_buffer = iterator.m_buffer; |
82 | 0 | m_currentPosition = m_buffer + diff; |
83 | 0 | } |
84 | | |
85 | | /*! |
86 | | * @brief This operator changes the position where the iterator points. |
87 | | * This operator takes the index of the source iterator, but the iterator continues using its raw buffer. |
88 | | * @param iterator The source iterator. The iterator will use the source's iterator index to point to its own raw buffer. |
89 | | */ |
90 | | inline |
91 | | void operator >>( |
92 | | const _FastBuffer_iterator& iterator) |
93 | 0 | { |
94 | 0 | ptrdiff_t diff = iterator.m_currentPosition - iterator.m_buffer; |
95 | 0 | m_currentPosition = m_buffer + diff; |
96 | 0 | } |
97 | | |
98 | | /*! |
99 | | * @brief This operator copies a data in the raw buffer. |
100 | | * The copy uses the size of the data type. |
101 | | * @param data Data to be copied. Cannot be NULL. |
102 | | */ |
103 | | template<typename _T> |
104 | | inline |
105 | | void operator <<( |
106 | | const _T& data) |
107 | 0 | { |
108 | 0 | memcpy(m_currentPosition, &data, sizeof(_T)); |
109 | 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&) |
110 | | |
111 | | /*! |
112 | | * @brief This operator copies data from the raw buffer to a variable. |
113 | | * The copy uses the size of the data type. |
114 | | * @param data Data to be filled. |
115 | | */ |
116 | | template<typename _T> |
117 | | inline |
118 | | void operator >>( |
119 | | _T& data) |
120 | 0 | { |
121 | 0 | memcpy(&data, m_currentPosition, sizeof(_T)); |
122 | 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&) |
123 | | |
124 | | /*! |
125 | | * @brief This function copies a buffer into the raw buffer. |
126 | | * @param src The source buffer. |
127 | | * @param size The number of bytes to be copied. |
128 | | */ |
129 | | inline |
130 | | void memcopy( |
131 | | const void* src, |
132 | | const size_t size) |
133 | 0 | { |
134 | 0 | if (size > 0) |
135 | 0 | { |
136 | 0 | memcpy(m_currentPosition, src, size); |
137 | 0 | } |
138 | 0 | } |
139 | | |
140 | | /*! |
141 | | * @brief This function copies from the raw buffer to a external buffer. |
142 | | * @param dst The destination buffer. |
143 | | * @param size The size of bytes to be copied. |
144 | | */ |
145 | | inline |
146 | | void rmemcopy( |
147 | | void* dst, |
148 | | const size_t size) |
149 | 0 | { |
150 | 0 | if (size > 0) |
151 | 0 | { |
152 | 0 | memcpy(dst, m_currentPosition, size); |
153 | 0 | } |
154 | 0 | } |
155 | | |
156 | | /*! |
157 | | * @brief This function increments the position where the iterator points. |
158 | | * @param numBytes Number of bytes the iterator moves the position. |
159 | | */ |
160 | | inline |
161 | | void operator +=( |
162 | | size_t numBytes) |
163 | 0 | { |
164 | 0 | m_currentPosition += numBytes; |
165 | 0 | } |
166 | | |
167 | | /*! |
168 | | * @brief This operator returns the subtraction of the current interator's position and the source iterator's position. |
169 | | * @param it Source iterator whose position is subtracted to the current iterator's position. |
170 | | * @return The result of subtract the current iterator's position and the source iterator's position. |
171 | | */ |
172 | | inline |
173 | | size_t operator -( |
174 | | const _FastBuffer_iterator& it) const |
175 | 0 | { |
176 | 0 | return static_cast<size_t>(m_currentPosition - it.m_currentPosition); |
177 | 0 | } |
178 | | |
179 | | /*! |
180 | | * @brief This function increments the iterator in one the position. |
181 | | * @return The current iterator. |
182 | | */ |
183 | | inline |
184 | | _FastBuffer_iterator operator ++() |
185 | 0 | { |
186 | 0 | ++m_currentPosition; |
187 | 0 | return *this; |
188 | 0 | } |
189 | | |
190 | | /*! |
191 | | * @brief This function increments the iterator in one the position. |
192 | | * @return The current iterator. |
193 | | */ |
194 | | inline |
195 | | _FastBuffer_iterator operator ++( |
196 | | int) |
197 | 0 | { |
198 | 0 | _FastBuffer_iterator tmp = *this; |
199 | 0 | ++*this; |
200 | 0 | return tmp; |
201 | 0 | } |
202 | | |
203 | | /*! |
204 | | * @brief This function returns the current position in the raw buffer. |
205 | | * @return The current position in the raw buffer. |
206 | | */ |
207 | | inline |
208 | | char* operator &() |
209 | 0 | { |
210 | 0 | return m_currentPosition; |
211 | 0 | } |
212 | | |
213 | | private: |
214 | | |
215 | | //! Pointer to the raw buffer. |
216 | | char* m_buffer; |
217 | | |
218 | | //! Current position in the raw buffer. |
219 | | char* m_currentPosition; |
220 | | }; |
221 | | |
222 | | /*! |
223 | | * @brief This class represents a stream of bytes that contains (or will contain) |
224 | | * serialized data. This class is used by the serializers to serialize |
225 | | * or deserialize using their representation. |
226 | | * @ingroup FASTCDRAPIREFERENCE |
227 | | */ |
228 | | class Cdr_DllAPI FastBuffer |
229 | | { |
230 | | public: |
231 | | |
232 | | typedef _FastBuffer_iterator iterator; |
233 | | |
234 | | /*! |
235 | | * @brief This constructor creates an internal stream and assigns it to the eprosima::fastcdr::FastBuffers object. |
236 | | * The user can obtain this internal stream using the function eprosima::fastcdr::FastBuffers::getBuffer(). Be careful because this internal stream |
237 | | * is deleted in the destruction of the eprosima::fastcdr::FastBuffers object. |
238 | | */ |
239 | | FastBuffer(); |
240 | | |
241 | | /*! |
242 | | * @brief This constructor assigns the user's stream of bytes to the eprosima::fastcdr::FastBuffers object. |
243 | | * The user's stream will be used to serialize. |
244 | | * |
245 | | * @param buffer The user's buffer that will be used. This buffer is not deallocated in the object's destruction. Cannot be NULL. |
246 | | * @param bufferSize The length of user's buffer. |
247 | | */ |
248 | | FastBuffer( |
249 | | char* const buffer, |
250 | | const size_t bufferSize); |
251 | | |
252 | | //! Move constructor |
253 | | FastBuffer( |
254 | | FastBuffer&& fbuffer) |
255 | | : m_buffer(nullptr) |
256 | | , m_bufferSize(0) |
257 | | , m_internalBuffer(true) |
258 | 0 | { |
259 | 0 | std::swap(m_buffer, fbuffer.m_buffer); |
260 | 0 | std::swap(m_bufferSize, fbuffer.m_bufferSize); |
261 | 0 | std::swap(m_internalBuffer, fbuffer.m_internalBuffer); |
262 | 0 | } |
263 | | |
264 | | //! Move assignment |
265 | | FastBuffer& operator =( |
266 | | FastBuffer&& fbuffer) |
267 | 0 | { |
268 | 0 | std::swap(m_buffer, fbuffer.m_buffer); |
269 | 0 | std::swap(m_bufferSize, fbuffer.m_bufferSize); |
270 | 0 | std::swap(m_internalBuffer, fbuffer.m_internalBuffer); |
271 | 0 | return *this; |
272 | 0 | } |
273 | | |
274 | | /*! |
275 | | * @brief Default destructor. |
276 | | */ |
277 | | virtual ~FastBuffer(); |
278 | | |
279 | | /*! |
280 | | * @brief This function returns the stream that the eprosima::fastcdr::FastBuffers uses to serialize data. |
281 | | * @return The stream used by eprosima::fastcdr::FastBuffers to serialize data. |
282 | | */ |
283 | | inline char* getBuffer() const |
284 | 0 | { |
285 | 0 | return m_buffer; |
286 | 0 | } |
287 | | |
288 | | /*! |
289 | | * @brief This function returns the size of the allocated memory of the stream that the eprosima::fastcdr::FastBuffers uses to serialize data. |
290 | | * @return The size of the allocated memory of the stream used by the eprosima::fastcdr::FastBuffers to serialize data. |
291 | | */ |
292 | | inline size_t getBufferSize() const |
293 | 0 | { |
294 | 0 | return m_bufferSize; |
295 | 0 | } |
296 | | |
297 | | /*! |
298 | | * @brief This function returns a iterator that points to the begining of the stream. |
299 | | * @return The new iterator. |
300 | | */ |
301 | | inline |
302 | | iterator begin() |
303 | 0 | { |
304 | 0 | return (iterator(m_buffer, 0)); |
305 | 0 | } |
306 | | |
307 | | /*! |
308 | | * @brief This function returns a iterator that points to the end of the stream. |
309 | | * @return The new iterator. |
310 | | */ |
311 | | inline |
312 | | iterator end() |
313 | 0 | { |
314 | 0 | return (iterator(m_buffer, m_bufferSize)); |
315 | 0 | } |
316 | | |
317 | | /*! |
318 | | * @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. |
319 | | * @param size The size of the memory to be allocated. |
320 | | * @return True if the allocation suceeded. False if the raw buffer was set externally or is already allocated. |
321 | | */ |
322 | | bool reserve( |
323 | | size_t size); |
324 | | |
325 | | /*! |
326 | | * @brief This function resizes the raw buffer. It will call the user's defined function for this purpose. |
327 | | * @param minSizeInc The minimun growth expected of the current raw buffer. |
328 | | * @return True if the operation works. False if it does not. |
329 | | */ |
330 | | bool resize( |
331 | | size_t minSizeInc); |
332 | | |
333 | | private: |
334 | | |
335 | | FastBuffer( |
336 | | const FastBuffer&) = delete; |
337 | | |
338 | | FastBuffer& operator =( |
339 | | const FastBuffer&) = delete; |
340 | | |
341 | | //! @brief Pointer to the stream of bytes that contains the serialized data. |
342 | | char* m_buffer; |
343 | | |
344 | | //! @brief The total size of the user's buffer. |
345 | | size_t m_bufferSize; |
346 | | |
347 | | //! @brief This variable indicates if the managed buffer is internal or is from the user. |
348 | | bool m_internalBuffer; |
349 | | }; |
350 | | } //namespace fastcdr |
351 | | } //namespace eprosima |
352 | | |
353 | | #endif // _FASTCDR_FASTCDRBUFFER_H_ |