/src/Fast-CDR/include/fastcdr/CdrSizeCalculator.hpp
Line | Count | Source (jump to first uncovered line) |
1 | | // Copyright 2023 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_CDRSIZECALCULATOR_HPP_ |
16 | | #define _FASTCDR_CDRSIZECALCULATOR_HPP_ |
17 | | |
18 | | #include <array> |
19 | | #include <bitset> |
20 | | #include <cstdint> |
21 | | #include <limits> |
22 | | #include <map> |
23 | | #include <vector> |
24 | | |
25 | | #include "fastcdr_dll.h" |
26 | | |
27 | | #include "CdrEncoding.hpp" |
28 | | #include "cdr/fixed_size_string.hpp" |
29 | | #include "detail/container_recursive_inspector.hpp" |
30 | | #include "exceptions/BadParamException.h" |
31 | | #include "xcdr/external.hpp" |
32 | | #include "xcdr/MemberId.hpp" |
33 | | #include "xcdr/optional.hpp" |
34 | | |
35 | | namespace eprosima { |
36 | | namespace fastcdr { |
37 | | |
38 | | class CdrSizeCalculator; |
39 | | |
40 | | template<class _T> |
41 | | extern size_t calculate_serialized_size( |
42 | | CdrSizeCalculator&, |
43 | | const _T&, |
44 | | size_t&); |
45 | | |
46 | | /*! |
47 | | * @brief This class offers an interface to calculate the encoded size of a type serialized using a support encoding |
48 | | * algorithm. |
49 | | * @ingroup FASTCDRAPIREFERENCE |
50 | | */ |
51 | | class CdrSizeCalculator |
52 | | { |
53 | | public: |
54 | | |
55 | | /*! |
56 | | * @brief Constructor. |
57 | | * @param[in] cdr_version Represents the version of the encoding algorithm that will be used for the encoding. |
58 | | * The default value is CdrVersion::XCDRv2. |
59 | | */ |
60 | | Cdr_DllAPI CdrSizeCalculator( |
61 | | CdrVersion cdr_version); |
62 | | |
63 | | /*! |
64 | | * @brief Constructor. |
65 | | * @param[in] cdr_version Represents the version of the encoding algorithm that will be used for the encoding. |
66 | | * The default value is CdrVersion::XCDRv2. |
67 | | * @param[in] encoding Represents the initial encoding. |
68 | | */ |
69 | | Cdr_DllAPI CdrSizeCalculator( |
70 | | CdrVersion cdr_version, |
71 | | EncodingAlgorithmFlag encoding); |
72 | | |
73 | | /*! |
74 | | * @brief Retrieves the version of the encoding algorithm used by the instance. |
75 | | * @return Configured CdrVersion. |
76 | | */ |
77 | | Cdr_DllAPI CdrVersion get_cdr_version() const; |
78 | | |
79 | | /*! |
80 | | * @brief Retrieves the current encoding algorithm used by the instance. |
81 | | * @return Configured EncodingAlgorithmFlag. |
82 | | */ |
83 | | Cdr_DllAPI EncodingAlgorithmFlag get_encoding() const; |
84 | | |
85 | | /*! |
86 | | * @brief Generic template which calculates the encoded size of an instance of an unknown type. |
87 | | * @tparam _T Instance's type. |
88 | | * @param[in] data Reference to the instance. |
89 | | * @param[inout] current_alignment Current alignment in the encoding. |
90 | | * @return Encoded size of the instance. |
91 | | */ |
92 | | template<class _T, typename std::enable_if<!std::is_enum<_T>::value>::type* = nullptr, typename = void> |
93 | | size_t calculate_serialized_size( |
94 | | const _T& data, |
95 | | size_t& current_alignment) |
96 | | { |
97 | | return eprosima::fastcdr::calculate_serialized_size(*this, data, current_alignment); |
98 | | } |
99 | | |
100 | | /*! |
101 | | * @brief Template which calculates the encoded size of an instance of an enumeration of 32bits. |
102 | | * @tparam _T Instance's type. |
103 | | * @param[in] data Reference to the instance. |
104 | | * @param[inout] current_alignment Current alignment in the encoding. |
105 | | * @return Encoded size of the instance. |
106 | | */ |
107 | | template<class _T, |
108 | | typename std::enable_if<std::is_enum<_T>::value>::type* = nullptr, |
109 | | typename std::enable_if<std::is_same<typename std::underlying_type<_T>::type, |
110 | | int32_t>::value>::type* = nullptr> |
111 | | size_t calculate_serialized_size( |
112 | | const _T& data, |
113 | | size_t& current_alignment) |
114 | | { |
115 | | return calculate_serialized_size(static_cast<int32_t>(data), current_alignment); |
116 | | } |
117 | | |
118 | | /*! |
119 | | * @brief Template which calculates the encoded size of an instance of an enumeration of unsigned 32bits. |
120 | | * @tparam _T Instance's type. |
121 | | * @param[in] data Reference to the instance. |
122 | | * @param[inout] current_alignment Current alignment in the encoding. |
123 | | * @return Encoded size of the instance. |
124 | | */ |
125 | | template<class _T, |
126 | | typename std::enable_if<std::is_enum<_T>::value>::type* = nullptr, |
127 | | typename std::enable_if<std::is_same<typename std::underlying_type<_T>::type, |
128 | | uint32_t>::value>::type* = nullptr> |
129 | | size_t calculate_serialized_size( |
130 | | const _T& data, |
131 | | size_t& current_alignment) |
132 | | { |
133 | | return calculate_serialized_size(static_cast<uint32_t>(data), current_alignment); |
134 | | } |
135 | | |
136 | | /*! |
137 | | * @brief Template which calculates the encoded size of an instance of an enumeration of 16bits. |
138 | | * @tparam _T Instance's type. |
139 | | * @param[in] data Reference to the instance. |
140 | | * @param[inout] current_alignment Current alignment in the encoding. |
141 | | * @return Encoded size of the instance. |
142 | | */ |
143 | | template<class _T, |
144 | | typename std::enable_if<std::is_enum<_T>::value>::type* = nullptr, |
145 | | typename std::enable_if<std::is_same<typename std::underlying_type<_T>::type, |
146 | | int16_t>::value>::type* = nullptr> |
147 | | size_t calculate_serialized_size( |
148 | | const _T& data, |
149 | | size_t& current_alignment) |
150 | | { |
151 | | return calculate_serialized_size(static_cast<int16_t>(data), current_alignment); |
152 | | } |
153 | | |
154 | | /*! |
155 | | * @brief Template which calculates the encoded size of an instance of an enumeration of unsigned 16bits. |
156 | | * @tparam _T Instance's type. |
157 | | * @param[in] data Reference to the instance. |
158 | | * @param[inout] current_alignment Current alignment in the encoding. |
159 | | * @return Encoded size of the instance. |
160 | | */ |
161 | | template<class _T, |
162 | | typename std::enable_if<std::is_enum<_T>::value>::type* = nullptr, |
163 | | typename std::enable_if<std::is_same<typename std::underlying_type<_T>::type, |
164 | | uint16_t>::value>::type* = nullptr> |
165 | | size_t calculate_serialized_size( |
166 | | const _T& data, |
167 | | size_t& current_alignment) |
168 | | { |
169 | | return calculate_serialized_size(static_cast<uint16_t>(data), current_alignment); |
170 | | } |
171 | | |
172 | | /*! |
173 | | * @brief Template which calculates the encoded size of an instance of an enumeration of 8bits. |
174 | | * @tparam _T Instance's type. |
175 | | * @param[in] data Reference to the instance. |
176 | | * @param[inout] current_alignment Current alignment in the encoding. |
177 | | * @return Encoded size of the instance. |
178 | | */ |
179 | | template<class _T, |
180 | | typename std::enable_if<std::is_enum<_T>::value>::type* = nullptr, |
181 | | typename std::enable_if<std::is_same<typename std::underlying_type<_T>::type, |
182 | | int8_t>::value>::type* = nullptr> |
183 | | size_t calculate_serialized_size( |
184 | | const _T& data, |
185 | | size_t& current_alignment) |
186 | | { |
187 | | return calculate_serialized_size(static_cast<int8_t>(data), current_alignment); |
188 | | } |
189 | | |
190 | | /*! |
191 | | * @brief Template which calculates the encoded size of an instance of an enumeration of unsigned 8bits. |
192 | | * @tparam _T Instance's type. |
193 | | * @param[in] data Reference to the instance. |
194 | | * @param[inout] current_alignment Current alignment in the encoding. |
195 | | * @return Encoded size of the instance. |
196 | | */ |
197 | | template<class _T, |
198 | | typename std::enable_if<std::is_enum<_T>::value>::type* = nullptr, |
199 | | typename std::enable_if<std::is_same<typename std::underlying_type<_T>::type, |
200 | | uint8_t>::value>::type* = nullptr> |
201 | | size_t calculate_serialized_size( |
202 | | const _T& data, |
203 | | size_t& current_alignment) |
204 | | { |
205 | | return calculate_serialized_size(static_cast<uint8_t>(data), current_alignment); |
206 | | } |
207 | | |
208 | | /*! |
209 | | * @brief Specific template which calculates the encoded size of an instance of an int8_t. |
210 | | * @param[in] data Reference to the instance. |
211 | | * @param[inout] current_alignment Current alignment in the encoding. |
212 | | * @return Encoded size of the instance. |
213 | | */ |
214 | | TEMPLATE_SPEC |
215 | | size_t calculate_serialized_size( |
216 | | const int8_t& data, |
217 | | size_t& current_alignment) |
218 | 0 | { |
219 | 0 | static_cast<void>(data); |
220 | 0 | ++current_alignment; |
221 | 0 | return 1; |
222 | 0 | } |
223 | | |
224 | | /*! |
225 | | * @brief Specific template which calculates the encoded size of an instance of an uint8_t. |
226 | | * @param[in] data Reference to the instance. |
227 | | * @param[inout] current_alignment Current alignment in the encoding. |
228 | | * @return Encoded size of the instance. |
229 | | */ |
230 | | TEMPLATE_SPEC |
231 | | size_t calculate_serialized_size( |
232 | | const uint8_t& data, |
233 | | size_t& current_alignment) |
234 | 0 | { |
235 | 0 | static_cast<void>(data); |
236 | 0 | ++current_alignment; |
237 | 0 | return 1; |
238 | 0 | } |
239 | | |
240 | | /*! |
241 | | * @brief Specific template which calculates the encoded size of an instance of a char. |
242 | | * @param[in] data Reference to the instance. |
243 | | * @param[inout] current_alignment Current alignment in the encoding. |
244 | | * @return Encoded size of the instance. |
245 | | */ |
246 | | TEMPLATE_SPEC |
247 | | size_t calculate_serialized_size( |
248 | | const char& data, |
249 | | size_t& current_alignment) |
250 | 0 | { |
251 | 0 | static_cast<void>(data); |
252 | 0 | ++current_alignment; |
253 | 0 | return 1; |
254 | 0 | } |
255 | | |
256 | | /*! |
257 | | * @brief Specific template which calculates the encoded size of an instance of a bool. |
258 | | * @param[in] data Reference to the instance. |
259 | | * @param[inout] current_alignment Current alignment in the encoding. |
260 | | * @return Encoded size of the instance. |
261 | | */ |
262 | | TEMPLATE_SPEC |
263 | | size_t calculate_serialized_size( |
264 | | const bool& data, |
265 | | size_t& current_alignment) |
266 | 0 | { |
267 | 0 | static_cast<void>(data); |
268 | 0 | ++current_alignment; |
269 | 0 | return 1; |
270 | 0 | } |
271 | | |
272 | | /*! |
273 | | * @brief Specific template which calculates the encoded size of an instance of a wchar. |
274 | | * @param[in] data Reference to the instance. |
275 | | * @param[inout] current_alignment Current alignment in the encoding. |
276 | | * @return Encoded size of the instance. |
277 | | */ |
278 | | TEMPLATE_SPEC |
279 | | size_t calculate_serialized_size( |
280 | | const wchar_t& data, |
281 | | size_t& current_alignment) |
282 | 0 | { |
283 | 0 | static_cast<void>(data); |
284 | 0 | size_t calculated_size {2 + alignment(current_alignment, 2)}; |
285 | 0 | current_alignment += calculated_size; |
286 | 0 | return calculated_size; |
287 | 0 | } |
288 | | |
289 | | /*! |
290 | | * @brief Specific template which calculates the encoded size of an instance of a int16_t. |
291 | | * @param[in] data Reference to the instance. |
292 | | * @param[inout] current_alignment Current alignment in the encoding. |
293 | | * @return Encoded size of the instance. |
294 | | */ |
295 | | TEMPLATE_SPEC |
296 | | size_t calculate_serialized_size( |
297 | | const int16_t& data, |
298 | | size_t& current_alignment) |
299 | 0 | { |
300 | 0 | static_cast<void>(data); |
301 | 0 | size_t calculated_size {2 + alignment(current_alignment, 2)}; |
302 | 0 | current_alignment += calculated_size; |
303 | 0 | return calculated_size; |
304 | 0 | } |
305 | | |
306 | | /*! |
307 | | * @brief Specific template which calculates the encoded size of an instance of a uint16_t. |
308 | | * @param[in] data Reference to the instance. |
309 | | * @param[inout] current_alignment Current alignment in the encoding. |
310 | | * @return Encoded size of the instance. |
311 | | */ |
312 | | TEMPLATE_SPEC |
313 | | size_t calculate_serialized_size( |
314 | | const uint16_t& data, |
315 | | size_t& current_alignment) |
316 | 0 | { |
317 | 0 | static_cast<void>(data); |
318 | 0 | size_t calculated_size {2 + alignment(current_alignment, 2)}; |
319 | 0 | current_alignment += calculated_size; |
320 | 0 | return calculated_size; |
321 | 0 | } |
322 | | |
323 | | /*! |
324 | | * @brief Specific template which calculates the encoded size of an instance of a int32_t. |
325 | | * @param[in] data Reference to the instance. |
326 | | * @param[inout] current_alignment Current alignment in the encoding. |
327 | | * @return Encoded size of the instance. |
328 | | */ |
329 | | TEMPLATE_SPEC |
330 | | size_t calculate_serialized_size( |
331 | | const int32_t& data, |
332 | | size_t& current_alignment) |
333 | 0 | { |
334 | 0 | static_cast<void>(data); |
335 | 0 | size_t calculated_size {4 + alignment(current_alignment, 4)}; |
336 | 0 | current_alignment += calculated_size; |
337 | 0 | return calculated_size; |
338 | 0 | } |
339 | | |
340 | | /*! |
341 | | * @brief Specific template which calculates the encoded size of an instance of a uint32_t. |
342 | | * @param[in] data Reference to the instance. |
343 | | * @param[inout] current_alignment Current alignment in the encoding. |
344 | | * @return Encoded size of the instance. |
345 | | */ |
346 | | TEMPLATE_SPEC |
347 | | size_t calculate_serialized_size( |
348 | | const uint32_t& data, |
349 | | size_t& current_alignment) |
350 | 0 | { |
351 | 0 | static_cast<void>(data); |
352 | 0 | size_t calculated_size {4 + alignment(current_alignment, 4)}; |
353 | 0 | current_alignment += calculated_size; |
354 | 0 | return calculated_size; |
355 | 0 | } |
356 | | |
357 | | /*! |
358 | | * @brief Specific template which calculates the encoded size of an instance of a int64_t. |
359 | | * @param[in] data Reference to the instance. |
360 | | * @param[inout] current_alignment Current alignment in the encoding. |
361 | | * @return Encoded size of the instance. |
362 | | */ |
363 | | TEMPLATE_SPEC |
364 | | size_t calculate_serialized_size( |
365 | | const int64_t& data, |
366 | | size_t& current_alignment) |
367 | 0 | { |
368 | 0 | static_cast<void>(data); |
369 | 0 | size_t calculated_size {8 + alignment(current_alignment, align64_)}; |
370 | 0 | current_alignment += calculated_size; |
371 | 0 | return calculated_size; |
372 | 0 | } |
373 | | |
374 | | /*! |
375 | | * @brief Specific template which calculates the encoded size of an instance of a uint64_t. |
376 | | * @param[in] data Reference to the instance. |
377 | | * @param[inout] current_alignment Current alignment in the encoding. |
378 | | * @return Encoded size of the instance. |
379 | | */ |
380 | | TEMPLATE_SPEC |
381 | | size_t calculate_serialized_size( |
382 | | const uint64_t& data, |
383 | | size_t& current_alignment) |
384 | 0 | { |
385 | 0 | static_cast<void>(data); |
386 | 0 | size_t calculated_size {8 + alignment(current_alignment, align64_)}; |
387 | 0 | current_alignment += calculated_size; |
388 | 0 | return calculated_size; |
389 | 0 | } |
390 | | |
391 | | /*! |
392 | | * @brief Specific template which calculates the encoded size of an instance of a float. |
393 | | * @param[in] data Reference to the instance. |
394 | | * @param[inout] current_alignment Current alignment in the encoding. |
395 | | * @return Encoded size of the instance. |
396 | | */ |
397 | | TEMPLATE_SPEC |
398 | | size_t calculate_serialized_size( |
399 | | const float& data, |
400 | | size_t& current_alignment) |
401 | 0 | { |
402 | 0 | static_cast<void>(data); |
403 | 0 | size_t calculated_size {4 + alignment(current_alignment, 4)}; |
404 | 0 | current_alignment += calculated_size; |
405 | 0 | return calculated_size; |
406 | 0 | } |
407 | | |
408 | | /*! |
409 | | * @brief Specific template which calculates the encoded size of an instance of a double. |
410 | | * @param[in] data Reference to the instance. |
411 | | * @param[inout] current_alignment Current alignment in the encoding. |
412 | | * @return Encoded size of the instance. |
413 | | */ |
414 | | TEMPLATE_SPEC |
415 | | size_t calculate_serialized_size( |
416 | | const double& data, |
417 | | size_t& current_alignment) |
418 | 0 | { |
419 | 0 | static_cast<void>(data); |
420 | 0 | size_t calculated_size {8 + alignment(current_alignment, align64_)}; |
421 | 0 | current_alignment += calculated_size; |
422 | 0 | return calculated_size; |
423 | 0 | } |
424 | | |
425 | | /*! |
426 | | * @brief Specific template which calculates the encoded size of an instance of a long double. |
427 | | * @param[in] data Reference to the instance. |
428 | | * @param[inout] current_alignment Current alignment in the encoding. |
429 | | * @return Encoded size of the instance. |
430 | | */ |
431 | | TEMPLATE_SPEC |
432 | | size_t calculate_serialized_size( |
433 | | const long double& data, |
434 | | size_t& current_alignment) |
435 | 0 | { |
436 | 0 | static_cast<void>(data); |
437 | 0 | size_t calculated_size {16 + alignment(current_alignment, align64_)}; |
438 | 0 | current_alignment += calculated_size; |
439 | 0 | return calculated_size; |
440 | 0 | } |
441 | | |
442 | | /*! |
443 | | * @brief Specific template which calculates the encoded size of an instance of a std::string. |
444 | | * @param[in] data Reference to the instance. |
445 | | * @param[inout] current_alignment Current alignment in the encoding. |
446 | | * @return Encoded size of the instance. |
447 | | */ |
448 | | TEMPLATE_SPEC |
449 | | size_t calculate_serialized_size( |
450 | | const std::string& data, |
451 | | size_t& current_alignment) |
452 | 0 | { |
453 | 0 | size_t calculated_size {4 + alignment(current_alignment, 4) + data.size() + 1}; |
454 | 0 | current_alignment += calculated_size; |
455 | 0 | serialized_member_size_ = SERIALIZED_MEMBER_SIZE; |
456 | 0 |
|
457 | 0 | return calculated_size; |
458 | 0 | } |
459 | | |
460 | | /*! |
461 | | * @brief Specific template which calculates the encoded size of an instance of a std::wstring. |
462 | | * @param[in] data Reference to the instance. |
463 | | * @param[inout] current_alignment Current alignment in the encoding. |
464 | | * @return Encoded size of the instance. |
465 | | */ |
466 | | TEMPLATE_SPEC |
467 | | size_t calculate_serialized_size( |
468 | | const std::wstring& data, |
469 | | size_t& current_alignment) |
470 | 0 | { |
471 | 0 | size_t calculated_size {4 + alignment(current_alignment, 4) + data.size() * 2}; |
472 | 0 | current_alignment += calculated_size; |
473 | 0 |
|
474 | 0 | return calculated_size; |
475 | 0 | } |
476 | | |
477 | | /*! |
478 | | * @brief Specific template which calculates the encoded size of an instance of a fixed_string. |
479 | | * @param[in] data Reference to the instance. |
480 | | * @param[inout] current_alignment Current alignment in the encoding. |
481 | | * @return Encoded size of the instance. |
482 | | */ |
483 | | template <size_t MAX_CHARS> |
484 | | size_t calculate_serialized_size( |
485 | | const fixed_string<MAX_CHARS>& data, |
486 | | size_t& current_alignment) |
487 | | { |
488 | | size_t calculated_size {4 + alignment(current_alignment, 4) + data.size() + 1}; |
489 | | current_alignment += calculated_size; |
490 | | serialized_member_size_ = SERIALIZED_MEMBER_SIZE; |
491 | | |
492 | | return calculated_size; |
493 | | } |
494 | | |
495 | | /*! |
496 | | * @brief Specific template which calculates the encoded size of an instance of a sequence of non-primitives. |
497 | | * @param[in] data Reference to the instance. |
498 | | * @param[inout] current_alignment Current alignment in the encoding. |
499 | | * @return Encoded size of the instance. |
500 | | */ |
501 | | template<class _T, typename std::enable_if<!std::is_enum<_T>::value && |
502 | | !std::is_arithmetic<_T>::value>::type* = nullptr> |
503 | | size_t calculate_serialized_size( |
504 | | const std::vector<_T>& data, |
505 | | size_t& current_alignment) |
506 | | { |
507 | | size_t initial_alignment {current_alignment}; |
508 | | |
509 | | if (CdrVersion::XCDRv2 == cdr_version_) |
510 | | { |
511 | | // DHEADER |
512 | | current_alignment += 4 + alignment(current_alignment, 4); |
513 | | } |
514 | | |
515 | | current_alignment += 4 + alignment(current_alignment, 4); |
516 | | |
517 | | size_t calculated_size {current_alignment - initial_alignment}; |
518 | | calculated_size += calculate_array_serialized_size(data.data(), data.size(), current_alignment); |
519 | | |
520 | | if (CdrVersion::XCDRv2 == cdr_version_) |
521 | | { |
522 | | // Inform DHEADER can be joined with NEXTINT |
523 | | serialized_member_size_ = SERIALIZED_MEMBER_SIZE; |
524 | | } |
525 | | |
526 | | return calculated_size; |
527 | | } |
528 | | |
529 | | /*! |
530 | | * @brief Specific template which calculates the encoded size of an instance of a sequence of primitives. |
531 | | * @param[in] data Reference to the instance. |
532 | | * @param[inout] current_alignment Current alignment in the encoding. |
533 | | * @return Encoded size of the instance. |
534 | | */ |
535 | | template<class _T, typename std::enable_if<std::is_enum<_T>::value || |
536 | | std::is_arithmetic<_T>::value>::type* = nullptr> |
537 | | size_t calculate_serialized_size( |
538 | | const std::vector<_T>& data, |
539 | | size_t& current_alignment) |
540 | | { |
541 | | size_t initial_alignment {current_alignment}; |
542 | | |
543 | | current_alignment += 4 + alignment(current_alignment, 4); |
544 | | |
545 | | size_t calculated_size {current_alignment - initial_alignment}; |
546 | | calculated_size += calculate_array_serialized_size(data.data(), data.size(), current_alignment); |
547 | | |
548 | | if (CdrVersion::XCDRv2 == cdr_version_) |
549 | | { |
550 | | serialized_member_size_ = get_serialized_member_size<_T>(); |
551 | | } |
552 | | |
553 | | return calculated_size; |
554 | | } |
555 | | |
556 | | /*! |
557 | | * @brief Specific template which calculates the encoded size of an instance of a sequence of bool. |
558 | | * @param[in] data Reference to the instance. |
559 | | * @param[inout] current_alignment Current alignment in the encoding. |
560 | | * @return Encoded size of the instance. |
561 | | */ |
562 | | TEMPLATE_SPEC |
563 | | size_t calculate_serialized_size( |
564 | | const std::vector<bool>& data, |
565 | | size_t& current_alignment) |
566 | 0 | { |
567 | 0 | size_t calculated_size {data.size() + 4 + alignment(current_alignment, 4)}; |
568 | 0 | current_alignment += calculated_size; |
569 | 0 |
|
570 | 0 | return calculated_size; |
571 | 0 | } |
572 | | |
573 | | /*! |
574 | | * @brief Specific template which calculates the encoded size of an instance of an array. |
575 | | * @param[in] data Reference to the instance. |
576 | | * @param[inout] current_alignment Current alignment in the encoding. |
577 | | * @return Encoded size of the instance. |
578 | | */ |
579 | | template<class _T, size_t _Size> |
580 | | size_t calculate_serialized_size( |
581 | | const std::array<_T, _Size>& data, |
582 | | size_t& current_alignment) |
583 | | { |
584 | | size_t initial_alignment {current_alignment}; |
585 | | |
586 | | if (CdrVersion::XCDRv2 == cdr_version_ && |
587 | | !is_multi_array_primitive(&data)) |
588 | | { |
589 | | // DHEADER |
590 | | current_alignment += 4 + alignment(current_alignment, 4); |
591 | | } |
592 | | |
593 | | size_t calculated_size {current_alignment - initial_alignment}; |
594 | | calculated_size += calculate_array_serialized_size(data.data(), data.size(), current_alignment); |
595 | | |
596 | | if (CdrVersion::XCDRv2 == cdr_version_ && |
597 | | !is_multi_array_primitive(&data)) |
598 | | { |
599 | | // Inform DHEADER can be joined with NEXTINT |
600 | | serialized_member_size_ = SERIALIZED_MEMBER_SIZE; |
601 | | } |
602 | | |
603 | | return calculated_size; |
604 | | } |
605 | | |
606 | | /*! |
607 | | * @brief Specific template which calculates the encoded size of an instance of a map of non-primitives. |
608 | | * @param[in] data Reference to the instance. |
609 | | * @param[inout] current_alignment Current alignment in the encoding. |
610 | | * @return Encoded size of the instance. |
611 | | */ |
612 | | template<class _K, class _V, typename std::enable_if<!std::is_enum<_V>::value && |
613 | | !std::is_arithmetic<_V>::value>::type* = nullptr> |
614 | | size_t calculate_serialized_size( |
615 | | const std::map<_K, _V>& data, |
616 | | size_t& current_alignment) |
617 | | { |
618 | | size_t initial_alignment {current_alignment}; |
619 | | |
620 | | if (CdrVersion::XCDRv2 == cdr_version_) |
621 | | { |
622 | | // DHEADER |
623 | | current_alignment += 4 + alignment(current_alignment, 4); |
624 | | } |
625 | | |
626 | | current_alignment += 4 + alignment(current_alignment, 4); |
627 | | |
628 | | size_t calculated_size {current_alignment - initial_alignment}; |
629 | | for (auto it = data.begin(); it != data.end(); ++it) |
630 | | { |
631 | | calculated_size += calculate_serialized_size(it->first, current_alignment); |
632 | | calculated_size += calculate_serialized_size(it->second, current_alignment); |
633 | | } |
634 | | |
635 | | if (CdrVersion::XCDRv2 == cdr_version_) |
636 | | { |
637 | | // Inform DHEADER can be joined with NEXTINT |
638 | | serialized_member_size_ = SERIALIZED_MEMBER_SIZE; |
639 | | } |
640 | | |
641 | | return calculated_size; |
642 | | } |
643 | | |
644 | | /*! |
645 | | * @brief Specific template which calculates the encoded size of an instance of a map of primitives. |
646 | | * @param[in] data Reference to the instance. |
647 | | * @param[inout] current_alignment Current alignment in the encoding. |
648 | | * @return Encoded size of the instance. |
649 | | */ |
650 | | template<class _K, class _V, typename std::enable_if<std::is_enum<_V>::value || |
651 | | std::is_arithmetic<_V>::value>::type* = nullptr> |
652 | | size_t calculate_serialized_size( |
653 | | const std::map<_K, _V>& data, |
654 | | size_t& current_alignment) |
655 | | { |
656 | | size_t initial_alignment {current_alignment}; |
657 | | |
658 | | current_alignment += 4 + alignment(current_alignment, 4); |
659 | | |
660 | | size_t calculated_size {current_alignment - initial_alignment}; |
661 | | for (auto it = data.begin(); it != data.end(); ++it) |
662 | | { |
663 | | calculated_size += calculate_serialized_size(it->first, current_alignment); |
664 | | calculated_size += calculate_serialized_size(it->second, current_alignment); |
665 | | } |
666 | | |
667 | | return calculated_size; |
668 | | } |
669 | | |
670 | | /*! |
671 | | * @brief Specific template which calculates the encoded size of an instance of a bitset of 8bits. |
672 | | * @param[in] data Reference to the instance. |
673 | | * @param[inout] current_alignment Current alignment in the encoding. |
674 | | * @return Encoded size of the instance. |
675 | | */ |
676 | | template<size_t N, typename std::enable_if < (N < 9) > ::type* = nullptr> |
677 | | size_t calculate_serialized_size( |
678 | | const std::bitset<N>& data, |
679 | | size_t& current_alignment) |
680 | | { |
681 | | static_cast<void>(data); |
682 | | ++current_alignment; |
683 | | return 1; |
684 | | } |
685 | | |
686 | | /*! |
687 | | * @brief Specific template which calculates the encoded size of an instance of a bitset of 16bits. |
688 | | * @param[in] data Reference to the instance. |
689 | | * @param[inout] current_alignment Current alignment in the encoding. |
690 | | * @return Encoded size of the instance. |
691 | | */ |
692 | | template<size_t N, typename std::enable_if < (8 < N && N < 17) > ::type* = nullptr> |
693 | | size_t calculate_serialized_size( |
694 | | const std::bitset<N>& data, |
695 | | size_t& current_alignment) |
696 | | { |
697 | | static_cast<void>(data); |
698 | | size_t calculated_size {2 + alignment(current_alignment, 2)}; |
699 | | current_alignment += calculated_size; |
700 | | return calculated_size; |
701 | | } |
702 | | |
703 | | /*! |
704 | | * @brief Specific template which calculates the encoded size of an instance of a bitset of 32bits. |
705 | | * @param[in] data Reference to the instance. |
706 | | * @param[inout] current_alignment Current alignment in the encoding. |
707 | | * @return Encoded size of the instance. |
708 | | */ |
709 | | template<size_t N, typename std::enable_if < (16 < N && N < 33) > ::type* = nullptr> |
710 | | size_t calculate_serialized_size( |
711 | | const std::bitset<N>& data, |
712 | | size_t& current_alignment) |
713 | | { |
714 | | static_cast<void>(data); |
715 | | size_t calculated_size {4 + alignment(current_alignment, 4)}; |
716 | | current_alignment += calculated_size; |
717 | | return calculated_size; |
718 | | } |
719 | | |
720 | | /*! |
721 | | * @brief Specific template which calculates the encoded size of an instance of a bitset of 64bits. |
722 | | * @param[in] data Reference to the instance. |
723 | | * @param[inout] current_alignment Current alignment in the encoding. |
724 | | * @return Encoded size of the instance. |
725 | | */ |
726 | | template<size_t N, typename std::enable_if < (32 < N && N < 65) > ::type* = nullptr> |
727 | | size_t calculate_serialized_size( |
728 | | const std::bitset<N>& data, |
729 | | size_t& current_alignment) |
730 | | { |
731 | | static_cast<void>(data); |
732 | | size_t calculated_size {8 + alignment(current_alignment, align64_)}; |
733 | | current_alignment += calculated_size; |
734 | | return calculated_size; |
735 | | } |
736 | | |
737 | | /*! |
738 | | * @brief Specific template which calculates the encoded size of an instance of an optional type. |
739 | | * @param[in] data Reference to the instance. |
740 | | * @param[inout] current_alignment Current alignment in the encoding. |
741 | | * @return Encoded size of the instance. |
742 | | */ |
743 | | template<class _T> |
744 | | size_t calculate_serialized_size( |
745 | | const optional<_T>& data, |
746 | | size_t& current_alignment) |
747 | | { |
748 | | size_t initial_alignment = current_alignment; |
749 | | |
750 | | if (CdrVersion::XCDRv2 == cdr_version_ && |
751 | | EncodingAlgorithmFlag::PL_CDR2 != current_encoding_) |
752 | | { |
753 | | // Take into account the boolean is_present; |
754 | | ++current_alignment; |
755 | | } |
756 | | |
757 | | size_t calculated_size {current_alignment - initial_alignment}; |
758 | | |
759 | | if (data.has_value()) |
760 | | { |
761 | | calculated_size += calculate_serialized_size(data.value(), current_alignment); |
762 | | } |
763 | | |
764 | | return calculated_size; |
765 | | } |
766 | | |
767 | | /*! |
768 | | * @brief Specific template which calculates the encoded size of an instance of an external type. |
769 | | * @param[in] data Reference to the instance. |
770 | | * @param[inout] current_alignment Current alignment in the encoding. |
771 | | * @exception exception::BadParamException This exception is thrown when the external is null. |
772 | | * @return Encoded size of the instance. |
773 | | */ |
774 | | template<class _T> |
775 | | size_t calculate_serialized_size( |
776 | | const external<_T>& data, |
777 | | size_t& current_alignment) |
778 | | { |
779 | | if (!data) |
780 | | { |
781 | | throw exception::BadParamException("External member is null"); |
782 | | } |
783 | | |
784 | | return calculate_serialized_size(*data, current_alignment); |
785 | | } |
786 | | |
787 | | /*! |
788 | | * @brief Specific template which calculates the encoded size of an instance of an array of unknown type. |
789 | | * @tparam _T Array's type. |
790 | | * @param[in] data Reference to the array's instance. |
791 | | * @param[in] num_elements Number of elements in the array. |
792 | | * @param[inout] current_alignment Current alignment in the encoding. |
793 | | * @return Encoded size of the instance. |
794 | | */ |
795 | | template<class _T> |
796 | | size_t calculate_array_serialized_size( |
797 | | const _T* data, |
798 | | size_t num_elements, |
799 | | size_t& current_alignment) |
800 | | { |
801 | | size_t calculated_size {0}; |
802 | | |
803 | | for (size_t count = 0; count < num_elements; ++count) |
804 | | { |
805 | | calculated_size += calculate_serialized_size(data[count], current_alignment); |
806 | | } |
807 | | |
808 | | return calculated_size; |
809 | | } |
810 | | |
811 | | /*! |
812 | | * @brief Specific template which calculates the encoded size of an instance of an array of int8_t. |
813 | | * @param[in] data Reference to the array's instance. |
814 | | * @param[in] num_elements Number of elements in the array. |
815 | | * @param[inout] current_alignment Current alignment in the encoding. |
816 | | * @return Encoded size of the instance. |
817 | | */ |
818 | | TEMPLATE_SPEC |
819 | | size_t calculate_array_serialized_size( |
820 | | const int8_t* data, |
821 | | size_t num_elements, |
822 | | size_t& current_alignment) |
823 | 0 | { |
824 | 0 | static_cast<void>(data); |
825 | 0 | current_alignment += num_elements; |
826 | 0 | return num_elements; |
827 | 0 | } |
828 | | |
829 | | /*! |
830 | | * @brief Specific template which calculates the encoded size of an instance of an array of uint8_t. |
831 | | * @param[in] data Reference to the array's instance. |
832 | | * @param[in] num_elements Number of elements in the array. |
833 | | * @param[inout] current_alignment Current alignment in the encoding. |
834 | | * @return Encoded size of the instance. |
835 | | */ |
836 | | TEMPLATE_SPEC |
837 | | size_t calculate_array_serialized_size( |
838 | | const uint8_t* data, |
839 | | size_t num_elements, |
840 | | size_t& current_alignment) |
841 | 0 | { |
842 | 0 | static_cast<void>(data); |
843 | 0 | current_alignment += num_elements; |
844 | 0 | return num_elements; |
845 | 0 | } |
846 | | |
847 | | /*! |
848 | | * @brief Specific template which calculates the encoded size of an instance of an array of char. |
849 | | * @param[in] data Reference to the array's instance. |
850 | | * @param[in] num_elements Number of elements in the array. |
851 | | * @param[inout] current_alignment Current alignment in the encoding. |
852 | | * @return Encoded size of the instance. |
853 | | */ |
854 | | TEMPLATE_SPEC |
855 | | size_t calculate_array_serialized_size( |
856 | | const char* data, |
857 | | size_t num_elements, |
858 | | size_t& current_alignment) |
859 | 0 | { |
860 | 0 | static_cast<void>(data); |
861 | 0 | current_alignment += num_elements; |
862 | 0 | return num_elements; |
863 | 0 | } |
864 | | |
865 | | /*! |
866 | | * @brief Specific template which calculates the encoded size of an instance of an array of wchar. |
867 | | * @param[in] data Reference to the array's instance. |
868 | | * @param[in] num_elements Number of elements in the array. |
869 | | * @param[inout] current_alignment Current alignment in the encoding. |
870 | | * @return Encoded size of the instance. |
871 | | */ |
872 | | TEMPLATE_SPEC |
873 | | size_t calculate_array_serialized_size( |
874 | | const wchar_t* data, |
875 | | size_t num_elements, |
876 | | size_t& current_alignment) |
877 | 0 | { |
878 | 0 | static_cast<void>(data); |
879 | 0 | size_t calculated_size {num_elements* 2 + alignment(current_alignment, 2)}; |
880 | 0 | current_alignment += calculated_size; |
881 | 0 | return calculated_size; |
882 | 0 | } |
883 | | |
884 | | /*! |
885 | | * @brief Specific template which calculates the encoded size of an instance of an array of int16_t. |
886 | | * @param[in] data Reference to the array's instance. |
887 | | * @param[in] num_elements Number of elements in the array. |
888 | | * @param[inout] current_alignment Current alignment in the encoding. |
889 | | * @return Encoded size of the instance. |
890 | | */ |
891 | | TEMPLATE_SPEC |
892 | | size_t calculate_array_serialized_size( |
893 | | const int16_t* data, |
894 | | size_t num_elements, |
895 | | size_t& current_alignment) |
896 | 0 | { |
897 | 0 | static_cast<void>(data); |
898 | 0 | size_t calculated_size {num_elements* 2 + alignment(current_alignment, 2)}; |
899 | 0 | current_alignment += calculated_size; |
900 | 0 | return calculated_size; |
901 | 0 | } |
902 | | |
903 | | /*! |
904 | | * @brief Specific template which calculates the encoded size of an instance of an array of uint16_t. |
905 | | * @param[in] data Reference to the array's instance. |
906 | | * @param[in] num_elements Number of elements in the array. |
907 | | * @param[inout] current_alignment Current alignment in the encoding. |
908 | | * @return Encoded size of the instance. |
909 | | */ |
910 | | TEMPLATE_SPEC |
911 | | size_t calculate_array_serialized_size( |
912 | | const uint16_t* data, |
913 | | size_t num_elements, |
914 | | size_t& current_alignment) |
915 | 0 | { |
916 | 0 | static_cast<void>(data); |
917 | 0 | size_t calculated_size {num_elements* 2 + alignment(current_alignment, 2)}; |
918 | 0 | current_alignment += calculated_size; |
919 | 0 | return calculated_size; |
920 | 0 | } |
921 | | |
922 | | /*! |
923 | | * @brief Specific template which calculates the encoded size of an instance of an array of int32_t. |
924 | | * @param[in] data Reference to the array's instance. |
925 | | * @param[in] num_elements Number of elements in the array. |
926 | | * @param[inout] current_alignment Current alignment in the encoding. |
927 | | * @return Encoded size of the instance. |
928 | | */ |
929 | | TEMPLATE_SPEC |
930 | | size_t calculate_array_serialized_size( |
931 | | const int32_t* data, |
932 | | size_t num_elements, |
933 | | size_t& current_alignment) |
934 | 0 | { |
935 | 0 | static_cast<void>(data); |
936 | 0 | size_t calculated_size {num_elements* 4 + alignment(current_alignment, 4)}; |
937 | 0 | current_alignment += calculated_size; |
938 | 0 | return calculated_size; |
939 | 0 | } |
940 | | |
941 | | /*! |
942 | | * @brief Specific template which calculates the encoded size of an instance of an array of uint32_t. |
943 | | * @param[in] data Reference to the array's instance. |
944 | | * @param[in] num_elements Number of elements in the array. |
945 | | * @param[inout] current_alignment Current alignment in the encoding. |
946 | | * @return Encoded size of the instance. |
947 | | */ |
948 | | TEMPLATE_SPEC |
949 | | size_t calculate_array_serialized_size( |
950 | | const uint32_t* data, |
951 | | size_t num_elements, |
952 | | size_t& current_alignment) |
953 | 0 | { |
954 | 0 | static_cast<void>(data); |
955 | 0 | size_t calculated_size {num_elements* 4 + alignment(current_alignment, 4)}; |
956 | 0 | current_alignment += calculated_size; |
957 | 0 | return calculated_size; |
958 | 0 | } |
959 | | |
960 | | /*! |
961 | | * @brief Specific template which calculates the encoded size of an instance of an array of int64_t. |
962 | | * @param[in] data Reference to the array's instance. |
963 | | * @param[in] num_elements Number of elements in the array. |
964 | | * @param[inout] current_alignment Current alignment in the encoding. |
965 | | * @return Encoded size of the instance. |
966 | | */ |
967 | | TEMPLATE_SPEC |
968 | | size_t calculate_array_serialized_size( |
969 | | const int64_t* data, |
970 | | size_t num_elements, |
971 | | size_t& current_alignment) |
972 | 0 | { |
973 | 0 | static_cast<void>(data); |
974 | 0 | size_t calculated_size {num_elements* 8 + alignment(current_alignment, align64_)}; |
975 | 0 | current_alignment += calculated_size; |
976 | 0 | return calculated_size; |
977 | 0 | } |
978 | | |
979 | | /*! |
980 | | * @brief Specific template which calculates the encoded size of an instance of an array of uint64_t. |
981 | | * @param[in] data Reference to the array's instance. |
982 | | * @param[in] num_elements Number of elements in the array. |
983 | | * @param[inout] current_alignment Current alignment in the encoding. |
984 | | * @return Encoded size of the instance. |
985 | | */ |
986 | | TEMPLATE_SPEC |
987 | | size_t calculate_array_serialized_size( |
988 | | const uint64_t* data, |
989 | | size_t num_elements, |
990 | | size_t& current_alignment) |
991 | 0 | { |
992 | 0 | static_cast<void>(data); |
993 | 0 | size_t calculated_size {num_elements* 8 + alignment(current_alignment, align64_)}; |
994 | 0 | current_alignment += calculated_size; |
995 | 0 | return calculated_size; |
996 | 0 | } |
997 | | |
998 | | /*! |
999 | | * @brief Specific template which calculates the encoded size of an instance of an array of float. |
1000 | | * @param[in] data Reference to the array's instance. |
1001 | | * @param[in] num_elements Number of elements in the array. |
1002 | | * @param[inout] current_alignment Current alignment in the encoding. |
1003 | | * @return Encoded size of the instance. |
1004 | | */ |
1005 | | TEMPLATE_SPEC |
1006 | | size_t calculate_array_serialized_size( |
1007 | | const float* data, |
1008 | | size_t num_elements, |
1009 | | size_t& current_alignment) |
1010 | 0 | { |
1011 | 0 | static_cast<void>(data); |
1012 | 0 | size_t calculated_size {num_elements* 4 + alignment(current_alignment, 4)}; |
1013 | 0 | current_alignment += calculated_size; |
1014 | 0 | return calculated_size; |
1015 | 0 | } |
1016 | | |
1017 | | /*! |
1018 | | * @brief Specific template which calculates the encoded size of an instance of an array of double. |
1019 | | * @param[in] data Reference to the array's instance. |
1020 | | * @param[in] num_elements Number of elements in the array. |
1021 | | * @param[inout] current_alignment Current alignment in the encoding. |
1022 | | * @return Encoded size of the instance. |
1023 | | */ |
1024 | | TEMPLATE_SPEC |
1025 | | size_t calculate_array_serialized_size( |
1026 | | const double* data, |
1027 | | size_t num_elements, |
1028 | | size_t& current_alignment) |
1029 | 0 | { |
1030 | 0 | static_cast<void>(data); |
1031 | 0 | size_t calculated_size {num_elements* 8 + alignment(current_alignment, align64_)}; |
1032 | 0 | current_alignment += calculated_size; |
1033 | 0 | return calculated_size; |
1034 | 0 | } |
1035 | | |
1036 | | /*! |
1037 | | * @brief Specific template which calculates the encoded size of an instance of an array of long double. |
1038 | | * @param[in] data Reference to the array's instance. |
1039 | | * @param[in] num_elements Number of elements in the array. |
1040 | | * @param[inout] current_alignment Current alignment in the encoding. |
1041 | | * @return Encoded size of the instance. |
1042 | | */ |
1043 | | TEMPLATE_SPEC |
1044 | | size_t calculate_array_serialized_size( |
1045 | | const long double* data, |
1046 | | size_t num_elements, |
1047 | | size_t& current_alignment) |
1048 | 0 | { |
1049 | 0 | static_cast<void>(data); |
1050 | 0 | size_t calculated_size {num_elements* 16 + alignment(current_alignment, align64_)}; |
1051 | 0 | current_alignment += calculated_size; |
1052 | 0 | return calculated_size; |
1053 | 0 | } |
1054 | | |
1055 | | /*! |
1056 | | * @brief Specific template which calculates the encoded size of an instance of a multi-dimensional array. |
1057 | | * @param[in] data Reference to the array's instance. |
1058 | | * @param[in] num_elements Number of elements in the array. |
1059 | | * @param[inout] current_alignment Current alignment in the encoding. |
1060 | | * @return Encoded size of the instance. |
1061 | | */ |
1062 | | template<class _T, size_t _N> |
1063 | | size_t calculate_array_serialized_size( |
1064 | | const std::array<_T, _N>* data, |
1065 | | size_t num_elements, |
1066 | | size_t& current_alignment) |
1067 | | { |
1068 | | return calculate_array_serialized_size(data->data(), num_elements * data->size(), current_alignment); |
1069 | | } |
1070 | | |
1071 | | /*! |
1072 | | * @brief Specific template which calculates the encoded size of an std::vector of primitives as an array. |
1073 | | * @param[in] data Reference to the instance. |
1074 | | * @param[inout] current_alignment Current alignment in the encoding. |
1075 | | * @return Encoded size of the instance. |
1076 | | */ |
1077 | | template<class _T, typename std::enable_if<std::is_enum<_T>::value || |
1078 | | std::is_arithmetic<_T>::value>::type* = nullptr> |
1079 | | size_t calculate_array_serialized_size( |
1080 | | const std::vector<_T>& data, |
1081 | | size_t& current_alignment) |
1082 | | { |
1083 | | return calculate_array_serialized_size(data.data(), data.size(), current_alignment); |
1084 | | } |
1085 | | |
1086 | | /*! |
1087 | | * @brief Specific template which calculates the encoded size of an std::vector of non-primitives as an array. |
1088 | | * @param[in] data Reference to the instance. |
1089 | | * @param[inout] current_alignment Current alignment in the encoding. |
1090 | | * @return Encoded size of the instance. |
1091 | | */ |
1092 | | template<class _T, typename std::enable_if<!std::is_enum<_T>::value && |
1093 | | !std::is_arithmetic<_T>::value>::type* = nullptr> |
1094 | | size_t calculate_array_serialized_size( |
1095 | | const std::vector<_T>& data, |
1096 | | size_t& current_alignment) |
1097 | | { |
1098 | | size_t initial_alignment {current_alignment}; |
1099 | | |
1100 | | if (CdrVersion::XCDRv2 == cdr_version_) |
1101 | | { |
1102 | | // DHEADER |
1103 | | current_alignment += 4 + alignment(current_alignment, 4); |
1104 | | } |
1105 | | |
1106 | | size_t calculated_size {current_alignment - initial_alignment}; |
1107 | | calculated_size += calculate_array_serialized_size(data.data(), data.size(), current_alignment); |
1108 | | |
1109 | | if (CdrVersion::XCDRv2 == cdr_version_) |
1110 | | { |
1111 | | // Inform DHEADER can be joined with NEXTINT |
1112 | | serialized_member_size_ = SERIALIZED_MEMBER_SIZE; |
1113 | | } |
1114 | | |
1115 | | return calculated_size; |
1116 | | } |
1117 | | |
1118 | | /*! |
1119 | | * @brief Specific template which calculates the encoded size of an std::vector of bool as an array. |
1120 | | * @param[in] data Reference to the instance. |
1121 | | * @param[inout] current_alignment Current alignment in the encoding. |
1122 | | * @return Encoded size of the instance. |
1123 | | */ |
1124 | | TEMPLATE_SPEC |
1125 | | size_t calculate_array_serialized_size( |
1126 | | const std::vector<bool>& data, |
1127 | | size_t& current_alignment) |
1128 | 0 | { |
1129 | 0 | current_alignment += data.size(); |
1130 | 0 | return data.size(); |
1131 | 0 | } |
1132 | | |
1133 | | /*! |
1134 | | * @brief Generic template which calculates the encoded size of the constructed type's member of a unknown type. |
1135 | | * @tparam _T Member's type. |
1136 | | * @param[in] id Member's identifier. |
1137 | | * @param[in] data Reference to the member's instance. |
1138 | | * @param[inout] current_alignment Current alignment in the encoding. |
1139 | | * @return Encoded size of the member's instance. |
1140 | | */ |
1141 | | template<class _T> |
1142 | | size_t calculate_member_serialized_size( |
1143 | | const MemberId& id, |
1144 | | const _T& data, |
1145 | | size_t& current_alignment) |
1146 | | { |
1147 | | size_t initial_alignment {current_alignment}; |
1148 | | |
1149 | | if (EncodingAlgorithmFlag::PL_CDR == current_encoding_ || |
1150 | | EncodingAlgorithmFlag::PL_CDR2 == current_encoding_) |
1151 | | { |
1152 | | // Align to 4 for the XCDR header before calculating the data serialized size. |
1153 | | current_alignment += alignment(current_alignment, 4); |
1154 | | } |
1155 | | |
1156 | | size_t prev_size {current_alignment - initial_alignment}; |
1157 | | size_t extra_size {0}; |
1158 | | |
1159 | | if (EncodingAlgorithmFlag::PL_CDR == current_encoding_) |
1160 | | { |
1161 | | current_alignment = 0; |
1162 | | } |
1163 | | |
1164 | | size_t calculated_size {calculate_serialized_size(data, current_alignment)}; |
1165 | | |
1166 | | if (CdrVersion::XCDRv2 == cdr_version_ && EncodingAlgorithmFlag::PL_CDR2 == current_encoding_ && |
1167 | | 0 < calculated_size) |
1168 | | { |
1169 | | |
1170 | | if (8 < calculated_size || |
1171 | | (1 != calculated_size && 2 != calculated_size && 4 != calculated_size && |
1172 | | 8 != calculated_size)) |
1173 | | { |
1174 | | extra_size = 8; // Long EMHEADER. |
1175 | | if (NO_SERIALIZED_MEMBER_SIZE != serialized_member_size_) |
1176 | | { |
1177 | | calculated_size -= 4; // Join NEXTINT and DHEADER. |
1178 | | } |
1179 | | } |
1180 | | else |
1181 | | { |
1182 | | extra_size = 4; // EMHEADER; |
1183 | | } |
1184 | | } |
1185 | | else if (CdrVersion::XCDRv1 == cdr_version_ && EncodingAlgorithmFlag::PL_CDR == current_encoding_ && |
1186 | | 0 < calculated_size) |
1187 | | { |
1188 | | extra_size = 4; // ShortMemberHeader |
1189 | | |
1190 | | if (0x3F00 < id.id || calculated_size > std::numeric_limits<uint16_t>::max()) |
1191 | | { |
1192 | | extra_size += 8; // LongMemberHeader |
1193 | | } |
1194 | | |
1195 | | } |
1196 | | |
1197 | | calculated_size += prev_size + extra_size; |
1198 | | if (EncodingAlgorithmFlag::PL_CDR != current_encoding_) |
1199 | | { |
1200 | | current_alignment += extra_size; |
1201 | | } |
1202 | | |
1203 | | serialized_member_size_ = NO_SERIALIZED_MEMBER_SIZE; |
1204 | | |
1205 | | return calculated_size; |
1206 | | } |
1207 | | |
1208 | | /*! |
1209 | | * @brief Generic template which calculates the encoded size of the constructed type's member of type optional. |
1210 | | * @tparam _T Member's optional type. |
1211 | | * @param[in] id Member's identifier. |
1212 | | * @param[in] data Reference to the member's instance. |
1213 | | * @param[inout] current_alignment Current alignment in the encoding. |
1214 | | * @return Encoded size of the member's instance. |
1215 | | */ |
1216 | | template<class _T> |
1217 | | size_t calculate_member_serialized_size( |
1218 | | const MemberId& id, |
1219 | | const optional<_T>& data, |
1220 | | size_t& current_alignment) |
1221 | | { |
1222 | | size_t initial_alignment = current_alignment; |
1223 | | |
1224 | | if (CdrVersion::XCDRv2 != cdr_version_ || |
1225 | | EncodingAlgorithmFlag::PL_CDR2 == current_encoding_) |
1226 | | { |
1227 | | if (data.has_value() || EncodingAlgorithmFlag::PLAIN_CDR == current_encoding_) |
1228 | | { |
1229 | | // Align to 4 for the XCDR header before calculating the data serialized size. |
1230 | | current_alignment += alignment(current_alignment, 4); |
1231 | | } |
1232 | | } |
1233 | | |
1234 | | size_t prev_size = {current_alignment - initial_alignment}; |
1235 | | size_t extra_size {0}; |
1236 | | |
1237 | | if (CdrVersion::XCDRv1 == cdr_version_ && |
1238 | | (data.has_value() || EncodingAlgorithmFlag::PLAIN_CDR == current_encoding_)) |
1239 | | { |
1240 | | current_alignment = 0; |
1241 | | } |
1242 | | |
1243 | | size_t calculated_size {calculate_serialized_size(data, current_alignment)}; |
1244 | | |
1245 | | if (CdrVersion::XCDRv2 == cdr_version_ && EncodingAlgorithmFlag::PL_CDR2 == current_encoding_ && |
1246 | | 0 < calculated_size) |
1247 | | { |
1248 | | if (8 < calculated_size) |
1249 | | { |
1250 | | extra_size = 8; // Long EMHEADER. |
1251 | | if (NO_SERIALIZED_MEMBER_SIZE != serialized_member_size_) |
1252 | | { |
1253 | | extra_size -= 4; // Join NEXTINT and DHEADER. |
1254 | | } |
1255 | | } |
1256 | | else |
1257 | | { |
1258 | | extra_size = 4; // EMHEADER; |
1259 | | } |
1260 | | } |
1261 | | else if (CdrVersion::XCDRv1 == cdr_version_ && |
1262 | | (0 < calculated_size || EncodingAlgorithmFlag::PLAIN_CDR == current_encoding_)) |
1263 | | { |
1264 | | extra_size = 4; // ShortMemberHeader |
1265 | | |
1266 | | if (0x3F00 < id.id || calculated_size > std::numeric_limits<uint16_t>::max()) |
1267 | | { |
1268 | | extra_size += 8; // LongMemberHeader |
1269 | | } |
1270 | | |
1271 | | } |
1272 | | |
1273 | | calculated_size += prev_size + extra_size; |
1274 | | if (CdrVersion::XCDRv1 != cdr_version_) |
1275 | | { |
1276 | | current_alignment += extra_size; |
1277 | | } |
1278 | | |
1279 | | |
1280 | | return calculated_size; |
1281 | | } |
1282 | | |
1283 | | /*! |
1284 | | * @brief Indicates a new constructed type will be calculated. |
1285 | | * @param[in] new_encoding New encoding algorithm used for the constructed type. |
1286 | | * @param[inout] current_alignment Current alignment in the encoding. |
1287 | | * @return If new encoding algorithm encodes a header, return the encoded size of it. |
1288 | | */ |
1289 | | Cdr_DllAPI size_t begin_calculate_type_serialized_size( |
1290 | | EncodingAlgorithmFlag new_encoding, |
1291 | | size_t& current_alignment); |
1292 | | |
1293 | | /*! |
1294 | | * @brief Indicates the ending of a constructed type. |
1295 | | * @param[in] new_encoding New encoding algorithm used after the constructed type. |
1296 | | * @param[inout] current_alignment Current alignment in the encoding. |
1297 | | * @return If current encoding algorithm encodes a final mark, return the encoded size of it. |
1298 | | */ |
1299 | | Cdr_DllAPI size_t end_calculate_type_serialized_size( |
1300 | | EncodingAlgorithmFlag new_encoding, |
1301 | | size_t& current_alignment); |
1302 | | |
1303 | | private: |
1304 | | |
1305 | | CdrSizeCalculator() = delete; |
1306 | | |
1307 | | CdrVersion cdr_version_ {CdrVersion::XCDRv2}; |
1308 | | |
1309 | | EncodingAlgorithmFlag current_encoding_ {EncodingAlgorithmFlag::PLAIN_CDR2}; |
1310 | | |
1311 | | enum SerializedMemberSizeForNextInt |
1312 | | { |
1313 | | NO_SERIALIZED_MEMBER_SIZE, |
1314 | | SERIALIZED_MEMBER_SIZE, |
1315 | | SERIALIZED_MEMBER_SIZE_4, |
1316 | | SERIALIZED_MEMBER_SIZE_8 |
1317 | | } |
1318 | | //! Specifies if a DHEADER was serialized. Used to calculate XCDRv2 member headers. |
1319 | | serialized_member_size_ {NO_SERIALIZED_MEMBER_SIZE}; |
1320 | | |
1321 | | //! Align for types equal or greater than 64bits. |
1322 | | size_t align64_ {4}; |
1323 | | |
1324 | | inline size_t alignment( |
1325 | | size_t current_alignment, |
1326 | | size_t data_size) const |
1327 | 0 | { |
1328 | 0 | return (data_size - (current_alignment % data_size)) & (data_size - 1); |
1329 | 0 | } |
1330 | | |
1331 | | template<class _T, typename std::enable_if<std::is_enum<_T>::value || |
1332 | | std::is_arithmetic<_T>::value>::type* = nullptr> |
1333 | | constexpr SerializedMemberSizeForNextInt get_serialized_member_size() const |
1334 | | { |
1335 | | return (1 == sizeof(_T) ? SERIALIZED_MEMBER_SIZE : |
1336 | | (4 == sizeof(_T) ? SERIALIZED_MEMBER_SIZE_4 : |
1337 | | (8 == sizeof(_T) ? SERIALIZED_MEMBER_SIZE_8 : NO_SERIALIZED_MEMBER_SIZE))); |
1338 | | } |
1339 | | |
1340 | | }; |
1341 | | |
1342 | | } // namespace fastcdr |
1343 | | } // namespace eprosima |
1344 | | |
1345 | | #endif // _FASTCDR_CDRSIZECALCULATOR_HPP_ |