pjmsg_mcap_wrapper
Loading...
Searching...
No Matches
Cdr.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_CDR_H_
16#define _FASTCDR_CDR_H_
17
18#include <array>
19#include <bitset>
20#include <cassert>
21#include <cstdint>
22#include <cstring>
23#include <functional>
24#include <map>
25#include <string>
26#include <type_traits>
27#include <utility>
28#include <vector>
29
30#include "fastcdr_dll.h"
31
32#include "CdrEncoding.hpp"
38#include "FastBuffer.h"
39#include "xcdr/external.hpp"
40#include "xcdr/MemberId.hpp"
41#include "xcdr/optional.hpp"
42
43#if !__APPLE__ && !__FreeBSD__ && !__VXWORKS__
44#include <malloc.h>
45#else
46#include <stdlib.h>
47#endif // if !__APPLE__ && !__FreeBSD__ && !__VXWORKS__
48
49namespace eprosima {
50namespace fastcdr {
51
52class Cdr;
53
54template<class _T>
55extern void serialize(
56 Cdr&,
57 const _T&);
58
59template<class _T>
60extern void deserialize(
61 Cdr&,
62 _T&);
63
64/*!
65 * @brief This class offers an interface to serialize/deserialize some basic types using CDR protocol inside an eprosima::fastcdr::FastBuffer.
66 * @ingroup FASTCDRAPIREFERENCE
67 */
68class Cdr
69{
70public:
71
72 /*!
73 * @brief This enumeration represents endianness types.
74 */
75 typedef enum : uint8_t
76 {
77 //! @brief Big endianness.
79 //! @brief Little endianness.
82
83 //! Default endianess in the system.
85
86 /*!
87 * Used to decide, in encoding algorithms where member headers support a short header version and a long header
88 * version, which one will be used.
89 */
90 typedef enum
91 {
92 //! Initially a short member header is allocated and cannot be changed. This option may cause an exception.
94 //! Initially a long member header is allocated and cannot be changed.
96 //! Initially a short member header is allocated but can be changed to the longer version.
98 //! Initially a long member header is allocated but can be changed to the shorter version.
101
102 /*!
103 * @brief This class stores the current state of a CDR serialization.
104 */
105 class state
106 {
107 friend class Cdr;
108
109 public:
110
111 //! Default constructor.
113 const Cdr& cdr);
114
115 //! Copy constructor.
117 const state& state);
118
119
120 //! Compares two states.
122 const state& other_state) const;
123
124 private:
125
127 const state& state) = delete;
128
129 //! The position in the buffer when the state was created.
131
132 //! The position from the alignment is calculated, when the state was created.
134
135 //! This attribute specifies if it is needed to swap the bytes when the state is created.
136 bool swap_bytes_ {false};
137
138 //! Stores the last datasize serialized/deserialized when the state was created.
139 size_t last_data_size_ {0};
140
141 //! Not related with the state. Next member id which will be encoded.
143
144 //! Not related with the state. Used by encoding algorithms to set the encoded member size.
145 uint32_t member_size_ {0};
146
147 //! Not related with the state. Used by encoding algorithms to store the selected member header version.
149
150 //! Not related with the state. Used by encoding algorithms to store the allocated member header version.
152
153 //! Not related with the state. Used by encoding algorithms to store the previous encoding algorithm.
155 };
156
157 /*!
158 * @brief This constructor creates an eprosima::fastcdr::Cdr object that can serialize/deserialize
159 * the assigned buffer.
160 * @param cdr_buffer A reference to the buffer that contains (or will contain) the CDR representation.
161 * @param endianness The initial endianness that will be used. The default value is the endianness of the system.
162 * @param cdr_version Represents the type of encoding algorithm that will be used for the encoding.
163 * The default value is CdrVersion::XCDRv2.
164 */
166 FastBuffer& cdr_buffer,
168 const CdrVersion cdr_version = XCDRv2);
169
170 /*!
171 * @brief This function reads the encapsulation of the CDR stream.
172 * If the CDR stream contains an encapsulation, then this function should be called before starting to deserialize.
173 * CdrVersion and EncodingAlgorithmFlag internal values will be changed to the ones specified by the
174 * encapsulation.
175 * @return Reference to the eprosima::fastcdr::Cdr object.
176 * @exception exception::NotEnoughMemoryException This exception is thrown when trying to deserialize a position that exceeds the internal memory size.
177 * @exception exception::BadParamException This exception is thrown when trying to deserialize an invalid value.
178 */
180
181 /*!
182 * @brief This function writes the encapsulation of the CDR stream.
183 * If the CDR stream should contain an encapsulation, then this function should be called before starting to serialize.
184 * @return Reference to the eprosima::fastcdr::Cdr object.
185 * @exception exception::NotEnoughMemoryException This exception is thrown when trying to serialize a position that exceeds the internal memory size.
186 */
188
189 /*!
190 * @brief Retrieves the CdrVersion used by the instance.
191 * @return Configured CdrVersion.
192 */
194
195 /*!
196 * @brief Returns the EncodingAlgorithmFlag set in the encapsulation when the CDR type is
197 * CdrVersion::DDS_CDR, CdrVersion::XCDRv1 or CdrVersion::XCDRv2.
198 * @return The specified flag in the encapsulation.
199 */
201
202 /*!
203 * @brief Sets the EncodingAlgorithmFlag for the encapsulation when the CDR type is
204 * CdrVersion::DDS_CDR, CdrVersion::XCDRv1 or CdrVersion::XCDRv2.
205 * This function only works when is called before starting the encoding/decoding.
206 * @param[in] encoding_flag Value to be used in the encapsulation.
207 * @return Indicates whether the setting was successful.
208 */
210 EncodingAlgorithmFlag encoding_flag);
211
212 /*!
213 * @brief This function returns the option flags when the CDR type is eprosima::fastcdr::DDS_CDR.
214 * @return The option flags.
215 */
217
218 /*!
219 * @brief This function sets the option flags when the CDR type is eprosima::fastcdr::DDS_CDR.
220 * @param options New value for the option flags.
221 */
223 const std::array<uint8_t, 2>& options);
224
225 /*!
226 * @brief This function sets the current endianness used by the CDR type.
227 * @param endianness The new endianness value.
228 */
231
232 /*!
233 * @brief This function returns the current endianness used by the CDR type.
234 * @return The endianness.
235 */
237
238 /*!
239 * @brief This function skips a number of bytes in the CDR stream buffer.
240 * @param num_bytes The number of bytes that will be jumped.
241 * @return True is returned when it works successfully. Otherwise, false is returned.
242 */
243 Cdr_DllAPI bool jump(
244 size_t num_bytes);
245
246 /*!
247 * @brief This function resets the current position in the buffer to the beginning.
248 */
249 Cdr_DllAPI void reset();
250
251 /*!
252 * @brief This function returns the pointer to the current used buffer.
253 * @return Pointer to the starting position of the buffer.
254 */
256
257 /*!
258 * @brief This function returns the current position in the CDR stream.
259 * @return Pointer to the current position in the buffer.
260 */
262
263 /*!
264 * @brief This function returns the length of the serialized data inside the stream.
265 * @return The length of the serialized data.
266 */
268
269 /*!
270 * @brief Returns the number of bytes needed to align a position to certain data size.
271 * @param current_alignment Position to be aligned.
272 * @param data_size Size of next data to process (should be power of two).
273 * @return Number of required alignment bytes.
274 */
275 inline static size_t alignment(
276 size_t current_alignment,
277 size_t data_size)
278 {
279 return (data_size - (current_alignment % data_size)) & (data_size - 1);
280 }
281
282 /*!
283 * @brief Returns the current state of the CDR serialization process.
284 * @return The current state of the CDR serialization process.
285 */
286 Cdr_DllAPI state get_state() const;
287
288 /*!
289 * @brief Sets a previous state of the CDR serialization process;
290 * @param state Previous state that will be set.
291 */
293 const state& state);
294
295 /*!
296 * @brief This function moves the alignment forward.
297 * @param num_bytes The number of bytes the alignment should advance.
298 * @return True If alignment was moved successfully.
299 */
301 size_t num_bytes);
302
303 /*!
304 * @brief This function resets the alignment to the current position in the buffer.
305 */
306 inline void reset_alignment()
307 {
309 last_data_size_ = 0;
310 }
311
312 /*!
313 * @brief Encodes the value into the buffer.
314 *
315 * If previously a MemberId was set using operator<<, this operator will encode the value as a member of a type
316 * consistent with the set member identifier and according to the encoding algorithm used.
317 *
318 * In other case, the operator will simply encode the value.
319 *
320 * @param[in] value A reference to the value which will be encoded in the buffer.
321 * @return Reference to the eprosima::fastcdr::Cdr object.
322 * @exception exception::NotEnoughMemoryException This exception is thrown when trying to encode into a buffer
323 * position that exceeds the internal memory size.
324 */
325 template<class _T>
327 const _T& value)
328 {
330 {
331 serialize(value);
332 }
333 else
334 {
336
337 }
338
339 return *this;
340 }
341
342 /*!
343 * @brief Decodes the value from the buffer.
344 *
345 * If this operator is called while decoding members of a type, this operator will decode the value as a member
346 * according to the encoding algorithm used.
347 *
348 * In other case, the operator will simply decode the value.
349 *
350 * @param[out] value Reference to the variable where the value will be stored after decoding from the buffer.
351 * @return Reference to the eprosima::fastcdr::Cdr object.
352 * @exception exception::NotEnoughMemoryException This exception is thrown when trying to decode from a position
353 * that exceeds the internal memory size.
354 */
355 template<class _T>
357 _T& value)
358 {
360 {
361 deserialize(value);
362 }
363 else
364 {
365 deserialize_member(value);
366 }
367 return *this;
368 }
369
370 /*!
371 * @brief Encodes the value of a type into the buffer.
372 *
373 * To do that, the encoder expects a function `serialize` to be provided by the type.
374 *
375 * @param[in] value A reference to the value which will be encoded in the buffer.
376 * @return Reference to the eprosima::fastcdr::Cdr object.
377 * @exception exception::NotEnoughMemoryException This exception is thrown when trying to encode into a buffer
378 * position that exceeds the internal memory size.
379 */
380 template<class _T, typename std::enable_if<!std::is_enum<_T>::value>::type* = nullptr, typename = void>
382 const _T& value)
383 {
384 eprosima::fastcdr::serialize(*this, value);
385 return *this;
386 }
387
388 /*!
389 * @brief Encodes the value of a type with a different endianness.
390 * @param[in] value A reference to the value which will be encoded in the buffer.
391 * @param endianness Endianness that will be used in the serialization of this value.
392 * @return Reference to the eprosima::fastcdr::Cdr object.
393 * @exception exception::NotEnoughMemoryException This exception is thrown when trying to encode into a buffer
394 * position that exceeds the internal memory size.
395 */
396 template<class _T>
398 const _T& value,
400 {
401 bool aux_swap = swap_bytes_;
402 swap_bytes_ = (swap_bytes_ && (static_cast<Endianness>(endianness_) == endianness)) ||
403 (!swap_bytes_ && (static_cast<Endianness>(endianness_) != endianness));
404
405 try
406 {
407 serialize(value);
408 swap_bytes_ = aux_swap;
409 }
410 catch (exception::Exception& ex)
411 {
412 swap_bytes_ = aux_swap;
413 ex.raise();
414 }
415
416 return *this;
417 }
418
419 /*!
420 * @brief Encodes the value of a enumerator into the buffer.
421 *
422 * @param[in] value A reference to the value which will be encoded in the buffer.
423 * @return Reference to the eprosima::fastcdr::Cdr object.
424 * @exception exception::NotEnoughMemoryException This exception is thrown when trying to encode into a buffer
425 * position that exceeds the internal memory size.
426 */
427 template<class _T,
428 typename std::enable_if<std::is_enum<_T>::value>::type* = nullptr,
430 int32_t>::value>::type* = nullptr>
432 const _T& value)
433 {
434 return serialize(static_cast<int32_t>(value));
435 }
436
437 /*!
438 * @brief Encodes the value of a enumerator into the buffer.
439 *
440 * @param[in] value A reference to the value which will be encoded in the buffer.
441 * @return Reference to the eprosima::fastcdr::Cdr object.
442 * @exception exception::NotEnoughMemoryException This exception is thrown when trying to encode into a buffer
443 * position that exceeds the internal memory size.
444 */
445 template<class _T,
446 typename std::enable_if<std::is_enum<_T>::value>::type* = nullptr,
448 uint32_t>::value>::type* = nullptr>
450 const _T& value)
451 {
452 return serialize(static_cast<uint32_t>(value));
453 }
454
455 /*!
456 * @brief Encodes the value of a enumerator into the buffer.
457 *
458 * @param[in] value A reference to the value which will be encoded in the buffer.
459 * @return Reference to the eprosima::fastcdr::Cdr object.
460 * @exception exception::NotEnoughMemoryException This exception is thrown when trying to encode into a buffer
461 * position that exceeds the internal memory size.
462 */
463 template<class _T,
464 typename std::enable_if<std::is_enum<_T>::value>::type* = nullptr,
466 int16_t>::value>::type* = nullptr>
468 const _T& value)
469 {
470 return serialize(static_cast<int16_t>(value));
471 }
472
473 /*!
474 * @brief Encodes the value of a enumerator into the buffer.
475 *
476 * @param[in] value A reference to the value which will be encoded in the buffer.
477 * @return Reference to the eprosima::fastcdr::Cdr object.
478 * @exception exception::NotEnoughMemoryException This exception is thrown when trying to encode into a buffer
479 * position that exceeds the internal memory size.
480 */
481 template<class _T,
482 typename std::enable_if<std::is_enum<_T>::value>::type* = nullptr,
484 uint16_t>::value>::type* = nullptr>
486 const _T& value)
487 {
488 return serialize(static_cast<uint16_t>(value));
489 }
490
491 /*!
492 * @brief Encodes the value of a enumerator into the buffer.
493 *
494 * @param[in] value A reference to the value which will be encoded in the buffer.
495 * @return Reference to the eprosima::fastcdr::Cdr object.
496 * @exception exception::NotEnoughMemoryException This exception is thrown when trying to encode into a buffer
497 * position that exceeds the internal memory size.
498 */
499 template<class _T,
500 typename std::enable_if<std::is_enum<_T>::value>::type* = nullptr,
502 int8_t>::value>::type* = nullptr>
504 const _T& value)
505 {
506 return serialize(static_cast<int8_t>(value));
507 }
508
509 /*!
510 * @brief Encodes the value of a enumerator into the buffer.
511 *
512 * @param[in] value A reference to the value which will be encoded in the buffer.
513 * @return Reference to the eprosima::fastcdr::Cdr object.
514 * @exception exception::NotEnoughMemoryException This exception is thrown when trying to encode into a buffer
515 * position that exceeds the internal memory size.
516 */
517 template<class _T,
518 typename std::enable_if<std::is_enum<_T>::value>::type* = nullptr,
520 uint8_t>::value>::type* = nullptr>
522 const _T& value)
523 {
524 return serialize(static_cast<uint8_t>(value));
525 }
526
527 /*!
528 * @brief This function serializes an octet.
529 * @param octet_t The value of the octet that will be serialized in the buffer.
530 * @return Reference to the eprosima::fastcdr::Cdr object.
531 * @exception exception::NotEnoughMemoryException This exception is thrown when trying to serialize a position that exceeds the internal memory size.
532 */
534 const uint8_t& octet_t);
535
536 /*!
537 * @brief This function serializes a character.
538 * @param char_t The value of the character that will be serialized in the buffer.
539 * @return Reference to the eprosima::fastcdr::Cdr object.
540 * @exception exception::NotEnoughMemoryException This exception is thrown when trying to serialize a position that exceeds the internal memory size.
541 */
543 const char char_t);
544
545 /*!
546 * @brief This function serializes an int8_t.
547 * @param int8 The value of the int8_t that will be serialized in the buffer.
548 * @return Reference to the eprosima::fastcdr::Cdr object.
549 * @exception exception::NotEnoughMemoryException This exception is thrown when trying to serialize a position that exceeds the internal memory size.
550 */
552 const int8_t int8);
553
554 /*!
555 * @brief This function serializes an unsigned short.
556 * @param ushort_t The value of the unsigned short that will be serialized in the buffer.
557 * @return Reference to the eprosima::fastcdr::Cdr object.
558 * @exception exception::NotEnoughMemoryException This exception is thrown when trying to serialize a position that exceeds the internal memory size.
559 */
561 const uint16_t ushort_t);
562
563 /*!
564 * @brief This function serializes a short.
565 * @param short_t The value of the short that will be serialized in the buffer.
566 * @return Reference to the eprosima::fastcdr::Cdr object.
567 * @exception exception::NotEnoughMemoryException This exception is thrown when trying to serialize a position that exceeds the internal memory size.
568 */
570 const int16_t short_t);
571
572 /*!
573 * @brief This function serializes an unsigned long.
574 * @param ulong_t The value of the unsigned long that will be serialized in the buffer.
575 * @return Reference to the eprosima::fastcdr::Cdr object.
576 * @exception exception::NotEnoughMemoryException This exception is thrown when trying to serialize a position that exceeds the internal memory size.
577 */
579 const uint32_t ulong_t);
580
581 /*!
582 * @brief This function serializes a long.
583 * @param long_t The value of the long that will be serialized in the buffer.
584 * @return Reference to the eprosima::fastcdr::Cdr object.
585 * @exception exception::NotEnoughMemoryException This exception is thrown when trying to serialize a position that exceeds the internal memory size.
586 */
588 const int32_t long_t);
589
590 /*!
591 * @brief This function serializes a wide-char.
592 * @param wchar The value of the wide-char that will be serialized in the buffer.
593 * @return Reference to the eprosima::fastcdr::Cdr object.
594 * @exception exception::NotEnoughMemoryException This exception is thrown when trying to serialize a position that exceeds the internal memory size.
595 */
597 const wchar_t wchar);
598
599 /*!
600 * @brief This function serializes an unsigned long long.
601 * @param ulonglong_t The value of the unsigned long long that will be serialized in the buffer.
602 * @return Reference to the eprosima::fastcdr::Cdr object.
603 * @exception exception::NotEnoughMemoryException This exception is thrown when trying to serialize a position that exceeds the internal memory size.
604 */
606 const uint64_t ulonglong_t);
607
608 /*!
609 * @brief This function serializes a long long.
610 * @param longlong_t The value of the long long that will be serialized in the buffer.
611 * @return Reference to the eprosima::fastcdr::Cdr object.
612 * @exception exception::NotEnoughMemoryException This exception is thrown when trying to serialize a position that exceeds the internal memory size.
613 */
615 const int64_t longlong_t);
616
617 /*!
618 * @brief This function serializes a float.
619 * @param float_t The value of the float that will be serialized in the buffer.
620 * @return Reference to the eprosima::fastcdr::Cdr object.
621 * @exception exception::NotEnoughMemoryException This exception is thrown when trying to serialize a position that exceeds the internal memory size.
622 */
624 const float float_t);
625
626 /*!
627 * @brief This function serializes a double.
628 * @param double_t The value of the double that will be serialized in the buffer.
629 * @return Reference to the eprosima::fastcdr::Cdr object.
630 * @exception exception::NotEnoughMemoryException This exception is thrown when trying to serialize a position that exceeds the internal memory size.
631 */
633 const double double_t);
634
635 /*!
636 * @brief This function serializes a long double.
637 * @param ldouble_t The value of the long double that will be serialized in the buffer.
638 * @return Reference to the eprosima::fastcdr::Cdr object.
639 * @exception exception::NotEnoughMemoryException This exception is thrown when trying to serialize a position that exceeds the internal memory size.
640 * @note Due to internal representation differences, WIN32 and *NIX like systems are not compatible.
641 */
643 const long double ldouble_t);
644
645 /*!
646 * @brief This function serializes a boolean.
647 * @param bool_t The value of the boolean that will be serialized in the buffer.
648 * @return Reference to the eprosima::fastcdr::Cdr object.
649 * @exception exception::NotEnoughMemoryException This exception is thrown when trying to serialize a position that exceeds the internal memory size.
650 */
652 const bool bool_t);
653
654 /*!
655 * @brief This function serializes a string.
656 * @param string_t The pointer to the string that will be serialized in the buffer.
657 * @return Reference to the eprosima::fastcdr::Cdr object.
658 * @exception exception::NotEnoughMemoryException This exception is thrown when trying to serialize a position that exceeds the internal memory size.
659 */
661 char* string_t);
662
663 /*!
664 * @brief This function serializes a string.
665 * @param string_t The pointer to the string that will be serialized in the buffer.
666 * @return Reference to the eprosima::fastcdr::Cdr object.
667 * @exception exception::NotEnoughMemoryException This exception is thrown when trying to serialize a position that exceeds the internal memory size.
668 */
670 const char* string_t);
671
672 /*!
673 * @brief This function serializes a wstring.
674 * @param string_t The pointer to the wstring that will be serialized in the buffer.
675 * @return Reference to the eprosima::fastcdr::Cdr object.
676 * @exception exception::NotEnoughMemoryException This exception is thrown when trying to serialize a position that exceeds the internal memory size.
677 */
679 const wchar_t* string_t);
680
681 /*!
682 * @brief This function serializes a std::string.
683 * @param string_t The string that will be serialized in the buffer.
684 * @return Reference to the eprosima::fastcdr::Cdr object.
685 * @exception exception::NotEnoughMemoryException This exception is thrown when trying to serialize a position that exceeds the internal memory size.
686 * @exception exception::BadParamException This exception is thrown when trying to serialize a string with null characters.
687 */
688 TEMPLATE_SPEC
690 const std::string& string_t)
691 {
692 // Check there are no null characters in the string.
693 const char* c_str = string_t.c_str();
694 const auto str_len = strlen(c_str);
695 if (string_t.size() > str_len)
696 {
697 throw exception::BadParamException("The string contains null characters");
698 }
699
700 return serialize_sequence(c_str, str_len + 1);
701 }
702
703 /*!
704 * @brief This function serializes a std::wstring.
705 * @param string_t The wstring that will be serialized in the buffer.
706 * @return Reference to the eprosima::fastcdr::Cdr object.
707 * @exception exception::NotEnoughMemoryException This exception is thrown when trying to serialize a position that exceeds the internal memory size.
708 */
709 TEMPLATE_SPEC
711 const std::wstring& string_t)
712 {
713 return serialize(string_t.c_str());
714 }
715
716 /*!
717 * @brief Encodes a eprosima::fastcdr::fixed_string in the buffer.
718 * @param[in] value A reference to the fixed string which will be encoded in the buffer.
719 * @return Reference to the eprosima::fastcdr::Cdr object.
720 * @exception exception::NotEnoughMemoryException This exception is thrown when trying to encode into a buffer
721 * position that exceeds the internal memory size.
722 */
723 template <size_t MAX_CHARS>
725 const fixed_string<MAX_CHARS>& value)
726 {
727 return serialize(value.c_str());
728 }
729
730 /*!
731 * @brief This function template serializes an array.
732 * @param array_t The array that will be serialized in the buffer.
733 * @return Reference to the eprosima::fastcdr::Cdr object.
734 * @exception exception::NotEnoughMemoryException This exception is thrown when trying to serialize a position that exceeds the internal memory size.
735 */
736 template<class _T, size_t _Size>
738 const std::array<_T, _Size>& array_t)
739 {
740 if (!is_multi_array_primitive(&array_t))
741 {
742 Cdr::state dheader_state {allocate_xcdrv2_dheader()};
743
744 serialize_array(array_t.data(), array_t.size());
745
746 set_xcdrv2_dheader(dheader_state);
747 }
748 else
749 {
750 serialize_array(array_t.data(), array_t.size());
751 }
752
753 return *this;
754 }
755
756 /*!
757 * @brief This function template serializes a sequence of non-primitive.
758 * @param vector_t The sequence that will be serialized in the buffer.
759 * @return Reference to the eprosima::fastcdr::Cdr object.
760 * @exception exception::NotEnoughMemoryException This exception is thrown when trying to serialize a position that exceeds the internal memory size.
761 */
762 template<class _T, typename std::enable_if<!std::is_enum<_T>::value &&
763 !std::is_arithmetic<_T>::value>::type* = nullptr>
765 const std::vector<_T>& vector_t)
766 {
767 Cdr::state dheader_state {allocate_xcdrv2_dheader()};
768
769 serialize(static_cast<int32_t>(vector_t.size()));
770
771 try
772 {
773 serialize_array(vector_t.data(), vector_t.size());
774 }
775 catch (exception::Exception& ex)
776 {
777 set_state(dheader_state);
778 ex.raise();
779 }
780
781 set_xcdrv2_dheader(dheader_state);
782
783 return *this;
784 }
785
786 /*!
787 * @brief This function template serializes a sequence of primitive.
788 * @param vector_t The sequence that will be serialized in the buffer.
789 * @return Reference to the eprosima::fastcdr::Cdr object.
790 * @exception exception::NotEnoughMemoryException This exception is thrown when trying to serialize a position that exceeds the internal memory size.
791 */
792 template<class _T, typename std::enable_if<std::is_enum<_T>::value ||
793 std::is_arithmetic<_T>::value>::type* = nullptr>
795 const std::vector<_T>& vector_t)
796 {
797 state state_before_error(*this);
798
799 serialize(static_cast<int32_t>(vector_t.size()));
800
801 try
802 {
803 serialize_array(vector_t.data(), vector_t.size());
804 }
805 catch (exception::Exception& ex)
806 {
807 set_state(state_before_error);
808 ex.raise();
809 }
810
812 {
813 serialized_member_size_ = get_serialized_member_size<_T>();
814 }
815
816 return *this;
817 }
818
819 /*!
820 * @brief This function template serializes a sequence of booleans.
821 * @param vector_t The sequence that will be serialized in the buffer.
822 * @return Reference to the eprosima::fastcdr::Cdr object.
823 * @exception exception::NotEnoughMemoryException This exception is thrown when trying to serialize a position that exceeds the internal memory size.
824 */
825 TEMPLATE_SPEC
827 const std::vector<bool>& vector_t)
828 {
829 return serialize_bool_sequence(vector_t);
830 }
831
832 /*!
833 * @brief This function template serializes a map of non-primitive.
834 * @param map_t The map that will be serialized in the buffer.
835 * @return Reference to the eprosima::fastcdr::Cdr object.
836 * @exception exception::NotEnoughMemoryException This exception is thrown when trying to serialize a position that exceeds the internal memory size.
837 */
838 template<class _K, class _T, typename std::enable_if<!std::is_enum<_T>::value &&
839 !std::is_arithmetic<_T>::value>::type* = nullptr>
841 const std::map<_K, _T>& map_t)
842 {
843 Cdr::state dheader_state {allocate_xcdrv2_dheader()};
844
845 serialize(static_cast<int32_t>(map_t.size()));
846
847 try
848 {
849 for (auto it_pair = map_t.begin(); it_pair != map_t.end(); ++it_pair)
850 {
851 serialize(it_pair->first);
852 serialize(it_pair->second);
853 }
854 }
855 catch (exception::Exception& ex)
856 {
857 set_state(dheader_state);
858 ex.raise();
859 }
860
861 set_xcdrv2_dheader(dheader_state);
862
863 return *this;
864 }
865
866 /*!
867 * @brief This function template serializes a map of primitive.
868 * @param map_t The map that will be serialized in the buffer.
869 * @return Reference to the eprosima::fastcdr::Cdr object.
870 * @exception exception::NotEnoughMemoryException This exception is thrown when trying to serialize a position that exceeds the internal memory size.
871 */
872 template<class _K, class _T, typename std::enable_if<std::is_enum<_T>::value ||
873 std::is_arithmetic<_T>::value>::type* = nullptr>
875 const std::map<_K, _T>& map_t)
876 {
877 state state_(*this);
878
879 serialize(static_cast<int32_t>(map_t.size()));
880
881 try
882 {
883 for (auto it_pair = map_t.begin(); it_pair != map_t.end(); ++it_pair)
884 {
885 serialize(it_pair->first);
886 serialize(it_pair->second);
887 }
888 }
889 catch (exception::Exception& ex)
890 {
891 set_state(state_);
892 ex.raise();
893 }
894
895 return *this;
896 }
897
898 /*!
899 * @brief Encodes the value of a bitset into the buffer.
900 *
901 * @param[in] value A reference to the value which will be encoded in the buffer.
902 * @return Reference to the eprosima::fastcdr::Cdr object.
903 * @exception exception::NotEnoughMemoryException This exception is thrown when trying to encode into a buffer
904 * position that exceeds the internal memory size.
905 */
906 template<size_t N, typename std::enable_if < (N < 9) > ::type* = nullptr>
907 Cdr& serialize(
908 const std::bitset<N>& value)
909 {
910 return serialize(static_cast<uint8_t>(value.to_ulong()));
911 }
912
913 template<size_t N, typename std::enable_if < (8 < N && N < 17) > ::type* = nullptr>
914 Cdr& serialize(
915 const std::bitset<N>& value)
916 {
917 return serialize(static_cast<uint16_t>(value.to_ulong()));
918 }
919
920 template<size_t N, typename std::enable_if < (16 < N && N < 33) > ::type* = nullptr>
921 Cdr& serialize(
922 const std::bitset<N>& value)
923 {
924 return serialize(static_cast<uint32_t>(value.to_ulong()));
925 }
926
927 template<size_t N, typename std::enable_if < (32 < N && N < 65) > ::type* = nullptr>
928 Cdr& serialize(
929 const std::bitset<N>& value)
930 {
931 return serialize(static_cast<uint64_t>(value.to_ullong()));
932 }
933
934 /*!
935 * @brief Encodes an array of a type not managed by this encoder into the buffer.
936 *
937 * To do that, the encoder expects a function `serialize` to be provided by the type.
938 *
939 * @param[in] value Array which will be encoded in the buffer.
940 * @param[in] num_elements Number of the elements in the array.
941 * @return Reference to the eprosima::fastcdr::Cdr object.
942 * @exception exception::NotEnoughMemoryException This exception is thrown when trying to encode into a buffer
943 * position that exceeds the internal memory size.
944 */
945 template<class _T>
947 const _T* value,
948 size_t num_elements)
949 {
950 for (size_t count = 0; count < num_elements; ++count)
951 {
952 serialize(value[count]);
953 }
954 return *this;
955 }
956
957 /*!
958 * @brief This function template serializes an array of non-basic objects with a different endianness.
959 * @param type_t The array of objects that will be serialized in the buffer.
960 * @param num_elements Number of the elements in the array.
961 * @param endianness Endianness that will be used in the serialization of this value.
962 * @return Reference to the eprosima::fastcdr::Cdr object.
963 * @exception exception::NotEnoughMemoryException This exception is thrown when trying to serialize a position that exceeds the internal memory size.
964 */
965 template<class _T>
967 const _T* type_t,
968 size_t num_elements,
970 {
971 bool aux_swap = swap_bytes_;
972 swap_bytes_ = (swap_bytes_ && (static_cast<Endianness>(endianness_) == endianness)) ||
973 (!swap_bytes_ && (static_cast<Endianness>(endianness_) != endianness));
974
975 try
976 {
977 serialize_array(type_t, num_elements);
978 swap_bytes_ = aux_swap;
979 }
980 catch (exception::Exception& ex)
981 {
982 swap_bytes_ = aux_swap;
983 ex.raise();
984 }
985
986 return *this;
987 }
988
989 /*!
990 * @brief This function serializes an array of octets.
991 * @param octet_t The sequence of octets that will be serialized in the buffer.
992 * @param num_elements Number of the elements in the array.
993 * @return Reference to the eprosima::fastcdr::Cdr object.
994 * @exception exception::NotEnoughMemoryException This exception is thrown when trying to serialize a position that exceeds the internal memory size.
995 */
996 TEMPLATE_SPEC
998 const uint8_t* octet_t,
999 size_t num_elements)
1000 {
1001 return serialize_array(reinterpret_cast<const char*>(octet_t), num_elements);
1002 }
1003
1004 /*!
1005 * @brief This function serializes an array of characters.
1006 * @param char_t The array of characters that will be serialized in the buffer.
1007 * @param num_elements Number of the elements in the array.
1008 * @return Reference to the eprosima::fastcdr::Cdr object.
1009 * @exception exception::NotEnoughMemoryException This exception is thrown when trying to serialize a position that exceeds the internal memory size.
1010 */
1012 const char* char_t,
1013 size_t num_elements);
1014
1015 /*!
1016 * @brief This function serializes an array of int8_t.
1017 * @param int8 The sequence of int8_t that will be serialized in the buffer.
1018 * @param num_elements Number of the elements in the array.
1019 * @return Reference to the eprosima::fastcdr::Cdr object.
1020 * @exception exception::NotEnoughMemoryException This exception is thrown when trying to serialize a position that exceeds the internal memory size.
1021 */
1022 TEMPLATE_SPEC
1024 const int8_t* int8,
1025 size_t num_elements)
1026 {
1027 return serialize_array(reinterpret_cast<const char*>(int8), num_elements);
1028 }
1029
1030 /*!
1031 * @brief This function serializes an array of unsigned shorts.
1032 * @param ushort_t The array of unsigned shorts that will be serialized in the buffer.
1033 * @param num_elements Number of the elements in the array.
1034 * @return Reference to the eprosima::fastcdr::Cdr object.
1035 * @exception exception::NotEnoughMemoryException This exception is thrown when trying to serialize a position that exceeds the internal memory size.
1036 */
1037 TEMPLATE_SPEC
1039 const uint16_t* ushort_t,
1040 size_t num_elements)
1041 {
1042 return serialize_array(reinterpret_cast<const int16_t*>(ushort_t), num_elements);
1043 }
1044
1045 /*!
1046 * @brief This function serializes an array of shorts.
1047 * @param short_t The array of shorts that will be serialized in the buffer.
1048 * @param num_elements Number of the elements in the array.
1049 * @return Reference to the eprosima::fastcdr::Cdr object.
1050 * @exception exception::NotEnoughMemoryException This exception is thrown when trying to serialize a position that exceeds the internal memory size.
1051 */
1053 const int16_t* short_t,
1054 size_t num_elements);
1055
1056 /*!
1057 * @brief This function serializes an array of unsigned longs.
1058 * @param ulong_t The array of unsigned longs that will be serialized in the buffer.
1059 * @param num_elements Number of the elements in the array.
1060 * @return Reference to the eprosima::fastcdr::Cdr object.
1061 * @exception exception::NotEnoughMemoryException This exception is thrown when trying to serialize a position that exceeds the internal memory size.
1062 */
1063 TEMPLATE_SPEC
1065 const uint32_t* ulong_t,
1066 size_t num_elements)
1067 {
1068 return serialize_array(reinterpret_cast<const int32_t*>(ulong_t), num_elements);
1069 }
1070
1071 /*!
1072 * @brief This function serializes an array of longs.
1073 * @param long_t The array of longs that will be serialized in the buffer.
1074 * @param num_elements Number of the elements in the array.
1075 * @return Reference to the eprosima::fastcdr::Cdr object.
1076 * @exception exception::NotEnoughMemoryException This exception is thrown when trying to serialize a position that exceeds the internal memory size.
1077 */
1079 const int32_t* long_t,
1080 size_t num_elements);
1081
1082 /*!
1083 * @brief This function serializes an array of wide-chars.
1084 * @param wchar The array of wide-chars that will be serialized in the buffer.
1085 * @param num_elements Number of the elements in the array.
1086 * @return Reference to the eprosima::fastcdr::Cdr object.
1087 * @exception exception::NotEnoughMemoryException This exception is thrown when trying to serialize a position that exceeds the internal memory size.
1088 */
1090 const wchar_t* wchar,
1091 size_t num_elements);
1092
1093 /*!
1094 * @brief This function serializes an array of unsigned long longs.
1095 * @param ulonglong_t The array of unsigned long longs that will be serialized in the buffer.
1096 * @param num_elements Number of the elements in the array.
1097 * @return Reference to the eprosima::fastcdr::Cdr object.
1098 * @exception exception::NotEnoughMemoryException This exception is thrown when trying to serialize a position that exceeds the internal memory size.
1099 */
1100 TEMPLATE_SPEC
1102 const uint64_t* ulonglong_t,
1103 size_t num_elements)
1104 {
1105 return serialize_array(reinterpret_cast<const int64_t*>(ulonglong_t), num_elements);
1106 }
1107
1108 /*!
1109 * @brief This function serializes an array of long longs.
1110 * @param longlong_t The array of long longs that will be serialized in the buffer.
1111 * @param num_elements Number of the elements in the array.
1112 * @return Reference to the eprosima::fastcdr::Cdr object.
1113 * @exception exception::NotEnoughMemoryException This exception is thrown when trying to serialize a position that exceeds the internal memory size.
1114 */
1116 const int64_t* longlong_t,
1117 size_t num_elements);
1118
1119 /*!
1120 * @brief This function serializes an array of floats.
1121 * @param float_t The array of floats that will be serialized in the buffer.
1122 * @param num_elements Number of the elements in the array.
1123 * @return Reference to the eprosima::fastcdr::Cdr object.
1124 * @exception exception::NotEnoughMemoryException This exception is thrown when trying to serialize a position that exceeds the internal memory size.
1125 */
1127 const float* float_t,
1128 size_t num_elements);
1129
1130 /*!
1131 * @brief This function serializes an array of doubles.
1132 * @param double_t The array of doubles that will be serialized in the buffer.
1133 * @param num_elements Number of the elements in the array.
1134 * @return Reference to the eprosima::fastcdr::Cdr object.
1135 * @exception exception::NotEnoughMemoryException This exception is thrown when trying to serialize a position that exceeds the internal memory size.
1136 */
1138 const double* double_t,
1139 size_t num_elements);
1140
1141 /*!
1142 * @brief This function serializes an array of long doubles.
1143 * @param ldouble_t The array of long doubles that will be serialized in the buffer.
1144 * @param num_elements Number of the elements in the array.
1145 * @return Reference to the eprosima::fastcdr::Cdr object.
1146 * @exception exception::NotEnoughMemoryException This exception is thrown when trying to serialize a position that exceeds the internal memory size.
1147 * @note Due to internal representation differences, WIN32 and *NIX like systems are not compatible.
1148 */
1150 const long double* ldouble_t,
1151 size_t num_elements);
1152
1153 /*!
1154 * @brief This function serializes an array of booleans.
1155 * @param bool_t The array of booleans that will be serialized in the buffer.
1156 * @param num_elements Number of the elements in the array.
1157 * @return Reference to the eprosima::fastcdr::Cdr object.
1158 * @exception exception::NotEnoughMemoryException This exception is thrown when trying to serialize a position that exceeds the internal memory size.
1159 */
1161 const bool* bool_t,
1162 size_t num_elements);
1163
1164 /*!
1165 * @brief This function serializes an array of strings.
1166 * @param string_t The array of strings that will be serialized in the buffer.
1167 * @param num_elements Number of the elements in the array.
1168 * @return Reference to the eprosima::fastcdr::Cdr object.
1169 * @exception exception::NotEnoughMemoryException This exception is thrown when trying to serialize a position that exceeds the internal memory size.
1170 */
1171 TEMPLATE_SPEC
1173 const std::string* string_t,
1174 size_t num_elements)
1175 {
1176 for (size_t count = 0; count < num_elements; ++count)
1177 {
1178 serialize(string_t[count].c_str());
1179 }
1180 return *this;
1181 }
1182
1183 /*!
1184 * @brief This function serializes an array of wide-strings.
1185 * @param string_t The array of wide-strings that will be serialized in the buffer.
1186 * @param num_elements Number of the elements in the array.
1187 * @return Reference to the eprosima::fastcdr::Cdr object.
1188 * @exception exception::NotEnoughMemoryException This exception is thrown when trying to serialize a position that exceeds the internal memory size.
1189 */
1190 TEMPLATE_SPEC
1192 const std::wstring* string_t,
1193 size_t num_elements)
1194 {
1195 for (size_t count = 0; count < num_elements; ++count)
1196 {
1197 serialize(string_t[count].c_str());
1198 }
1199 return *this;
1200 }
1201
1202 /*!
1203 * @brief Encodes an array of fixed strings.
1204 * @param[in] value Array of fixed strings which will be encoded in the buffer.
1205 * @param[in] num_elements Number of the elements in the array.
1206 * @return Reference to the eprosima::fastcdr::Cdr object.
1207 * @exception exception::NotEnoughMemoryException This exception is thrown when trying to encode into a buffer
1208 * position that exceeds the internal memory size.
1209 */
1210 template<size_t MAX_CHARS>
1212 const fixed_string<MAX_CHARS>* value,
1213 size_t num_elements)
1214 {
1215 for (size_t count = 0; count < num_elements; ++count)
1216 {
1217 serialize(value[count].c_str());
1218 }
1219 return *this;
1220 }
1221
1222 /*!
1223 * @brief Encodes an std::vector of primitives as an array.
1224 * @param[in] value Reference to a std::vector.
1225 * @return Reference to the eprosima::fastcdr::Cdr object.
1226 * @exception exception::NotEnoughMemoryException This exception is thrown when trying to encode into a buffer
1227 * position that exceeds the internal memory size.
1228 */
1229 template<class _T, typename std::enable_if<std::is_enum<_T>::value ||
1230 std::is_arithmetic<_T>::value>::type* = nullptr>
1232 const std::vector<_T>& value)
1233 {
1234 serialize_array(value.data(), value.size());
1235
1236 return *this;
1237 }
1238
1239 /*!
1240 * @brief Encodes an std::vector of non-primitives as an array.
1241 * @param[in] value Reference to a std::vector.
1242 * @return Reference to the eprosima::fastcdr::Cdr object.
1243 * @exception exception::NotEnoughMemoryException This exception is thrown when trying to encode into a buffer
1244 * position that exceeds the internal memory size.
1245 */
1246 template<class _T, typename std::enable_if<!std::is_enum<_T>::value &&
1247 !std::is_arithmetic<_T>::value>::type* = nullptr>
1249 const std::vector<_T>& value)
1250 {
1251 Cdr::state dheader_state {allocate_xcdrv2_dheader()};
1252
1253 serialize_array(value.data(), value.size());
1254
1255 set_xcdrv2_dheader(dheader_state);
1256
1257 return *this;
1258 }
1259
1260 /*!
1261 * @brief Encodes an std::vector as an array with a different endianness.
1262 * @param[in] value Reference to a std::vector.
1263 * @param[in] endianness Endianness that will be used in the serialization of this value.
1264 * @return Reference to the eprosima::fastcdr::Cdr object.
1265 * @exception exception::NotEnoughMemoryException This exception is thrown when trying to encode into a buffer
1266 * position that exceeds the internal memory size.
1267 */
1268 template<class _T>
1270 const std::vector<_T>& value,
1272 {
1273 bool aux_swap = swap_bytes_;
1274 swap_bytes_ = (swap_bytes_ && (static_cast<Endianness>(endianness_) == endianness)) ||
1275 (!swap_bytes_ && (static_cast<Endianness>(endianness_) != endianness));
1276
1277 try
1278 {
1279 serialize_array(value);
1280 swap_bytes_ = aux_swap;
1281 }
1282 catch (exception::Exception& ex)
1283 {
1284 swap_bytes_ = aux_swap;
1285 ex.raise();
1286 }
1287
1288 return *this;
1289 }
1290
1291 /*!
1292 * @brief Encodes an std::vector of booleans as an array.
1293 * @param[in] value Reference to a std::vector.
1294 * @return Reference to the eprosima::fastcdr::Cdr object.
1295 * @exception exception::NotEnoughMemoryException This exception is thrown when trying to encode into a buffer
1296 * position that exceeds the internal memory size.
1297 */
1298 TEMPLATE_SPEC
1300 const std::vector<bool>& value)
1301 {
1302 serialize_bool_array(value);
1303
1304 return *this;
1305 }
1306
1307 /*!
1308 * @brief This function template serializes a raw sequence of non-primitives
1309 * @param sequence_t Pointer to the sequence that will be serialized in the buffer.
1310 * @param num_elements The number of elements contained in the sequence.
1311 * @return Reference to the eprosima::fastcdr::Cdr object.
1312 * @exception exception::NotEnoughMemoryException This exception is thrown when trying to serialize a position that exceeds the internal memory size.
1313 */
1314 template<class _T, typename std::enable_if<!std::is_enum<_T>::value &&
1315 !std::is_arithmetic<_T>::value>::type* = nullptr>
1317 const _T* sequence_t,
1318 size_t num_elements)
1319 {
1320 Cdr::state dheader_state {allocate_xcdrv2_dheader()};
1321
1322 serialize(static_cast<int32_t>(num_elements));
1323
1324 try
1325 {
1326 serialize_array(sequence_t, num_elements);
1327 }
1328 catch (exception::Exception& ex)
1329 {
1330 set_state(dheader_state);
1331 ex.raise();
1332 }
1333
1334 set_xcdrv2_dheader(dheader_state);
1335
1336 return *this;
1337 }
1338
1339 /*!
1340 * @brief This function template serializes a raw sequence of primitives
1341 * @param sequence_t Pointer to the sequence that will be serialized in the buffer.
1342 * @param num_elements The number of elements contained in the sequence.
1343 * @return Reference to the eprosima::fastcdr::Cdr object.
1344 * @exception exception::NotEnoughMemoryException This exception is thrown when trying to serialize a position that exceeds the internal memory size.
1345 */
1346 template<class _T, typename std::enable_if<std::is_enum<_T>::value ||
1347 std::is_arithmetic<_T>::value>::type* = nullptr>
1349 const _T* sequence_t,
1350 size_t num_elements)
1351 {
1352 state state_before_error(*this);
1353
1354 serialize(static_cast<int32_t>(num_elements));
1355
1356 try
1357 {
1358 serialize_array(sequence_t, num_elements);
1359 }
1360 catch (exception::Exception& ex)
1361 {
1362 set_state(state_before_error);
1363 ex.raise();
1364 }
1365
1367 {
1368 serialized_member_size_ = get_serialized_member_size<_T>();
1369 }
1370
1371 return *this;
1372 }
1373
1374 /*!
1375 * @brief This function template serializes a raw sequence with a different endianness.
1376 * @param sequence_t Pointer to the sequence that will be serialized in the buffer.
1377 * @param num_elements The number of elements contained in the sequence.
1378 * @param endianness Endianness that will be used in the serialization of this value.
1379 * @return Reference to the eprosima::fastcdr::Cdr object.
1380 * @exception exception::NotEnoughMemoryException This exception is thrown when trying to serialize a position that exceeds the internal memory size.
1381 */
1382 template<class _T>
1384 const _T* sequence_t,
1385 size_t num_elements,
1387 {
1388 bool aux_swap = swap_bytes_;
1389 swap_bytes_ = (swap_bytes_ && (static_cast<Endianness>(endianness_) == endianness)) ||
1390 (!swap_bytes_ && (static_cast<Endianness>(endianness_) != endianness));
1391
1392 try
1393 {
1394 serialize_sequence(sequence_t, num_elements);
1395 swap_bytes_ = aux_swap;
1396 }
1397 catch (exception::Exception& ex)
1398 {
1399 swap_bytes_ = aux_swap;
1400 ex.raise();
1401 }
1402
1403 return *this;
1404 }
1405
1406 /*!
1407 * @brief Decodes the value of a type from the buffer.
1408 *
1409 * To do that, the encoder expects a function `deserialize` to be provided by the type.
1410 *
1411 * @param[out] value Reference to the variable where the value will be stored after decoding from the buffer.
1412 * @return Reference to the eprosima::fastcdr::Cdr object.
1413 * @exception exception::NotEnoughMemoryException This exception is thrown when trying to decode from a buffer
1414 * position that exceeds the internal memory size.
1415 */
1416 template<class _T, typename std::enable_if<!std::is_enum<_T>::value>::type* = nullptr, typename = void>
1418 _T& value)
1419 {
1420 eprosima::fastcdr::deserialize(*this, value);
1421 return *this;
1422 }
1423
1424 /*!
1425 * @brief Decodes the value of a type with a different endianness.
1426 * @param[out] value Reference to the variable where the value will be stored after decoding from the buffer.
1427 * @param endianness Endianness that will be used in the deserialization of this value.
1428 * @return Reference to the eprosima::fastcdr::Cdr object.
1429 * @exception exception::NotEnoughMemoryException This exception is thrown when trying to decode from a buffer
1430 * position that exceeds the internal memory size.
1431 */
1432 template<class _T>
1434 _T& value,
1436 {
1437 bool aux_swap = swap_bytes_;
1438 swap_bytes_ = (swap_bytes_ && (static_cast<Endianness>(endianness_) == endianness)) ||
1439 (!swap_bytes_ && (static_cast<Endianness>(endianness_) != endianness));
1440
1441 try
1442 {
1443 deserialize(value);
1444 swap_bytes_ = aux_swap;
1445 }
1446 catch (exception::Exception& ex)
1447 {
1448 swap_bytes_ = aux_swap;
1449 ex.raise();
1450 }
1451
1452 return *this;
1453 }
1454
1455 /*!
1456 * @brief Decodes an enumeration from the buffer.
1457 * @param[out] value Reference to the variable where the enumeration will be stored after decoding from the buffer.
1458 * @return Reference to the eprosima::fastcdr::Cdr object.
1459 * @exception exception::NotEnoughMemoryException This exception is thrown when trying to decode from a buffer
1460 * position that exceeds the internal memory size.
1461 */
1462 template<class _T,
1463 typename std::enable_if<std::is_enum<_T>::value>::type* = nullptr,
1465 int32_t>::value>::type* = nullptr>
1467 _T& value)
1468 {
1469 int32_t decode_value {0};
1470 deserialize(decode_value);
1471 value = static_cast<_T>(decode_value);
1472 return *this;
1473 }
1474
1475 /*!
1476 * @brief Decodes an enumeration from the buffer.
1477 * @param[out] value Reference to the variable where the enumeration will be stored after decoding from the buffer.
1478 * @return Reference to the eprosima::fastcdr::Cdr object.
1479 * @exception exception::NotEnoughMemoryException This exception is thrown when trying to decode from a buffer
1480 * position that exceeds the internal memory size.
1481 */
1482 template<class _T,
1483 typename std::enable_if<std::is_enum<_T>::value>::type* = nullptr,
1485 uint32_t>::value>::type* = nullptr>
1487 _T& value)
1488 {
1489 uint32_t decode_value {0};
1490 deserialize(decode_value);
1491 value = static_cast<_T>(decode_value);
1492 return *this;
1493 }
1494
1495 /*!
1496 * @brief Decodes an enumeration from the buffer.
1497 * @param[out] value Reference to the variable where the enumeration will be stored after decoding from the buffer.
1498 * @return Reference to the eprosima::fastcdr::Cdr object.
1499 * @exception exception::NotEnoughMemoryException This exception is thrown when trying to decode from a buffer
1500 * position that exceeds the internal memory size.
1501 */
1502 template<class _T,
1503 typename std::enable_if<std::is_enum<_T>::value>::type* = nullptr,
1505 int16_t>::value>::type* = nullptr>
1507 _T& value)
1508 {
1509 int16_t decode_value {0};
1510 deserialize(decode_value);
1511 value = static_cast<_T>(decode_value);
1512 return *this;
1513 }
1514
1515 /*!
1516 * @brief Decodes an enumeration from the buffer.
1517 * @param[out] value Reference to the variable where the enumeration will be stored after decoding from the buffer.
1518 * @return Reference to the eprosima::fastcdr::Cdr object.
1519 * @exception exception::NotEnoughMemoryException This exception is thrown when trying to decode from a buffer
1520 * position that exceeds the internal memory size.
1521 */
1522 template<class _T,
1523 typename std::enable_if<std::is_enum<_T>::value>::type* = nullptr,
1525 uint16_t>::value>::type* = nullptr>
1527 _T& value)
1528 {
1529 uint16_t decode_value {0};
1530 deserialize(decode_value);
1531 value = static_cast<_T>(decode_value);
1532 return *this;
1533 }
1534
1535 /*!
1536 * @brief Decodes an enumeration from the buffer.
1537 * @param[out] value Reference to the variable where the enumeration will be stored after decoding from the buffer.
1538 * @return Reference to the eprosima::fastcdr::Cdr object.
1539 * @exception exception::NotEnoughMemoryException This exception is thrown when trying to decode from a buffer
1540 * position that exceeds the internal memory size.
1541 */
1542 template<class _T,
1543 typename std::enable_if<std::is_enum<_T>::value>::type* = nullptr,
1545 int8_t>::value>::type* = nullptr>
1547 _T& value)
1548 {
1549 int8_t decode_value {0};
1550 deserialize(decode_value);
1551 value = static_cast<_T>(decode_value);
1552 return *this;
1553 }
1554
1555 /*!
1556 * @brief Decodes an enumeration from the buffer.
1557 * @param[out] value Reference to the variable where the enumeration will be stored after decoding from the buffer.
1558 * @return Reference to the eprosima::fastcdr::Cdr object.
1559 * @exception exception::NotEnoughMemoryException This exception is thrown when trying to decode from a buffer
1560 * position that exceeds the internal memory size.
1561 */
1562 template<class _T,
1563 typename std::enable_if<std::is_enum<_T>::value>::type* = nullptr,
1565 uint8_t>::value>::type* = nullptr>
1567 _T& value)
1568 {
1569 uint8_t decode_value {0};
1570 deserialize(decode_value);
1571 value = static_cast<_T>(decode_value);
1572 return *this;
1573 }
1574
1575 /*!
1576 * @brief This function deserializes an octet.
1577 * @param octet_t The variable that will store the octet read from the buffer.
1578 * @return Reference to the eprosima::fastcdr::Cdr object.
1579 * @exception exception::NotEnoughMemoryException This exception is thrown when trying to deserialize a position that exceeds the internal memory size.
1580 */
1581 TEMPLATE_SPEC
1583 uint8_t& octet_t)
1584 {
1585 return deserialize(reinterpret_cast<char&>(octet_t));
1586 }
1587
1588 /*!
1589 * @brief This function deserializes a character.
1590 * @param char_t The variable that will store the character read from the buffer.
1591 * @return Reference to the eprosima::fastcdr::Cdr object.
1592 * @exception exception::NotEnoughMemoryException This exception is thrown when trying to deserialize a position that exceeds the internal memory size.
1593 */
1595 char& char_t);
1596
1597 /*!
1598 * @brief This function deserializes an int8_t.
1599 * @param int8 The variable that will store the int8_t read from the buffer.
1600 * @return Reference to the eprosima::fastcdr::Cdr object.
1601 * @exception exception::NotEnoughMemoryException This exception is thrown when trying to deserialize a position that exceeds the internal memory size.
1602 */
1603 TEMPLATE_SPEC
1605 int8_t& int8)
1606 {
1607 return deserialize(reinterpret_cast<char&>(int8));
1608 }
1609
1610 /*!
1611 * @brief This function deserializes an unsigned short.
1612 * @param ushort_t The variable that will store the unsigned short read from the buffer.
1613 * @return Reference to the eprosima::fastcdr::Cdr object.
1614 * @exception exception::NotEnoughMemoryException This exception is thrown when trying to deserialize a position that exceeds the internal memory size.
1615 */
1616 TEMPLATE_SPEC
1618 uint16_t& ushort_t)
1619 {
1620 return deserialize(reinterpret_cast<int16_t&>(ushort_t));
1621 }
1622
1623 /*!
1624 * @brief This function deserializes a short.
1625 * @param short_t The variable that will store the short read from the buffer.
1626 * @return Reference to the eprosima::fastcdr::Cdr object.
1627 * @exception exception::NotEnoughMemoryException This exception is thrown when trying to deserialize a position that exceeds the internal memory size.
1628 */
1630 int16_t& short_t);
1631
1632 /*!
1633 * @brief This function deserializes an unsigned long.
1634 * @param ulong_t The variable that will store the unsigned long read from the buffer.
1635 * @return Reference to the eprosima::fastcdr::Cdr object.
1636 * @exception exception::NotEnoughMemoryException This exception is thrown when trying to deserialize a position that exceeds the internal memory size.
1637 */
1638 TEMPLATE_SPEC
1640 uint32_t& ulong_t)
1641 {
1642 return deserialize(reinterpret_cast<int32_t&>(ulong_t));
1643 }
1644
1645 /*!
1646 * @brief This function deserializes a long.
1647 * @param long_t The variable that will store the long read from the buffer.
1648 * @return Reference to the eprosima::fastcdr::Cdr object.
1649 * @exception exception::NotEnoughMemoryException This exception is thrown when trying to deserialize a position that exceeds the internal memory size.
1650 */
1652 int32_t& long_t);
1653
1654 /*!
1655 * @brief This function deserializes a wide-char.
1656 * @param wchar The variable that will store the wide-char read from the buffer.
1657 * @return Reference to the eprosima::fastcdr::Cdr object.
1658 * @exception exception::NotEnoughMemoryException This exception is thrown when trying to deserialize a position that exceeds the internal memory size.
1659 */
1660 TEMPLATE_SPEC
1662 wchar_t& wchar)
1663 {
1664 uint16_t ret;
1665 deserialize(ret);
1666 wchar = static_cast<wchar_t>(ret);
1667 return *this;
1668 }
1669
1670 /*!
1671 * @brief This function deserializes an unsigned long long.
1672 * @param ulonglong_t The variable that will store the unsigned long long read from the buffer.
1673 * @return Reference to the eprosima::fastcdr::Cdr object.
1674 * @exception exception::NotEnoughMemoryException This exception is thrown when trying to deserialize a position that exceeds the internal memory size.
1675 */
1676 TEMPLATE_SPEC
1678 uint64_t& ulonglong_t)
1679 {
1680 return deserialize(reinterpret_cast<int64_t&>(ulonglong_t));
1681 }
1682
1683 /*!
1684 * @brief This function deserializes a long long.
1685 * @param longlong_t The variable that will store the long long read from the buffer.
1686 * @return Reference to the eprosima::fastcdr::Cdr object.
1687 * @exception exception::NotEnoughMemoryException This exception is thrown when trying to deserialize a position that exceeds the internal memory size.
1688 */
1690 int64_t& longlong_t);
1691
1692 /*!
1693 * @brief This function deserializes a float.
1694 * @param float_t The variable that will store the float read from the buffer.
1695 * @return Reference to the eprosima::fastcdr::Cdr object.
1696 * @exception exception::NotEnoughMemoryException This exception is thrown when trying to deserialize a position that exceeds the internal memory size.
1697 */
1699 float& float_t);
1700
1701 /*!
1702 * @brief This function deserializes a double.
1703 * @param double_t The variable that will store the double read from the buffer.
1704 * @return Reference to the eprosima::fastcdr::Cdr object.
1705 * @exception exception::NotEnoughMemoryException This exception is thrown when trying to deserialize a position that exceeds the internal memory size.
1706 */
1708 double& double_t);
1709
1710 /*!
1711 * @brief This function deserializes a long double.
1712 * @param ldouble_t The variable that will store the long double read from the buffer.
1713 * @return Reference to the eprosima::fastcdr::Cdr object.
1714 * @exception exception::NotEnoughMemoryException This exception is thrown when trying to deserialize a position that exceeds the internal memory size.
1715 * @note Due to internal representation differences, WIN32 and *NIX like systems are not compatible.
1716 */
1718 long double& ldouble_t);
1719
1720 /*!
1721 * @brief This function deserializes a boolean.
1722 * @param bool_t The variable that will store the boolean read from the buffer.
1723 * @return Reference to the eprosima::fastcdr::Cdr object.
1724 * @exception exception::NotEnoughMemoryException This exception is thrown when trying to deserialize a position that exceeds the internal memory size.
1725 * @exception exception::BadParamException This exception is thrown when trying to deserialize an invalid value.
1726 */
1728 bool& bool_t);
1729
1730 /*!
1731 * @brief This function deserializes a string.
1732 * This function allocates memory to store the string. The user pointer will be set to point this allocated memory.
1733 * The user will have to free this allocated memory using free()
1734 * @param string_t The pointer that will point to the string read from the buffer.
1735 * @return Reference to the eprosima::fastcdr::Cdr object.
1736 * @exception exception::NotEnoughMemoryException This exception is thrown when trying to deserialize a position that exceeds the internal memory size.
1737 */
1739 char*& string_t);
1740
1741 /*!
1742 * @brief This function deserializes a wide-string.
1743 * This function allocates memory to store the wide string. The user pointer will be set to point this allocated memory.
1744 * The user will have to free this allocated memory using free()
1745 * @param string_t The pointer that will point to the wide string read from the buffer.
1746 * @return Reference to the eprosima::fastcdr::Cdr object.
1747 * @exception exception::NotEnoughMemoryException This exception is thrown when trying to deserialize a position that exceeds the internal memory size.
1748 */
1750 wchar_t*& string_t);
1751
1752 /*!
1753 * @brief This function deserializes a std::string.
1754 * @param string_t The variable that will store the string read from the buffer.
1755 * @return Reference to the eprosima::fastcdr::Cdr object.
1756 * @exception exception::NotEnoughMemoryException This exception is thrown when trying to deserialize a position that exceeds the internal memory size.
1757 */
1758 TEMPLATE_SPEC
1760 std::string& string_t)
1761 {
1762 uint32_t length = 0;
1763 const char* str = read_string(length);
1764 string_t.assign(str, length);
1765 return *this;
1766 }
1767
1768 /*!
1769 * @brief This function deserializes a std::wstring.
1770 * @param string_t The variable that will store the string read from the buffer.
1771 * @return Reference to the eprosima::fastcdr::Cdr object.
1772 * @exception exception::NotEnoughMemoryException This exception is thrown when trying to deserialize a position that exceeds the internal memory size.
1773 */
1774 TEMPLATE_SPEC
1776 std::wstring& string_t)
1777 {
1778 uint32_t length = 0;
1779 string_t = read_wstring(length);
1780 return *this;
1781 }
1782
1783 /*!
1784 * @brief Decodes a fixed string.
1785 * @param[out] value Reference to the variable where the fixed string will be stored after decoding from the buffer.
1786 * @return Reference to the eprosima::fastcdr::Cdr object.
1787 * @exception exception::NotEnoughMemoryException This exception is thrown when trying to decode from a buffer
1788 * position that exceeds the internal memory size.
1789 */
1790 template <size_t MAX_CHARS>
1793 {
1794 uint32_t length = 0;
1795 const char* str = read_string(length);
1796 value.assign(str, length);
1797 return *this;
1798 }
1799
1800 /*!
1801 * @brief This function template deserializes an array.
1802 * @param array_t The variable that will store the array read from the buffer.
1803 * @return Reference to the eprosima::fastcdr::Cdr object.
1804 * @exception exception::NotEnoughMemoryException This exception is thrown when trying to deserialize a position that exceeds the internal memory size.
1805 */
1806 template<class _T, size_t _Size>
1808 std::array<_T, _Size>& array_t)
1809 {
1811 {
1812 uint32_t dheader {0};
1813 deserialize(dheader);
1814
1815 uint32_t count {0};
1816 auto offset = offset_;
1817 while (offset_ - offset < dheader && count < _Size)
1818 {
1819 deserialize_array(&array_t.data()[count], 1);
1820 ++count;
1821 }
1822
1823 if (offset_ - offset != dheader)
1824 {
1825 throw exception::BadParamException("Member size greater than size specified by DHEADER");
1826 }
1827 }
1828 else
1829 {
1830 return deserialize_array(array_t.data(), array_t.size());
1831 }
1832
1833 return *this;
1834 }
1835
1836 /*!
1837 * @brief This function template deserializes a sequence of non-primitive.
1838 * @param vector_t The variable that will store the sequence read from the buffer.
1839 * @return Reference to the eprosima::fastcdr::Cdr object.
1840 * @exception exception::NotEnoughMemoryException This exception is thrown when trying to deserialize a position that exceeds the internal memory size.
1841 */
1842 template<class _T, typename std::enable_if<!std::is_enum<_T>::value &&
1843 !std::is_arithmetic<_T>::value>::type* = nullptr>
1845 std::vector<_T>& vector_t)
1846 {
1847 uint32_t sequence_length {0};
1848
1850 {
1851 uint32_t dheader {0};
1852 deserialize(dheader);
1853
1854 auto offset = offset_;
1855
1856 deserialize(sequence_length);
1857
1858 if (0 == sequence_length)
1859 {
1860 vector_t.clear();
1861 return *this;
1862 }
1863 else
1864 {
1865 vector_t.resize(sequence_length);
1866 }
1867
1868 uint32_t count {0};
1869 while (offset_ - offset < dheader && count < sequence_length)
1870 {
1871 deserialize(vector_t.data()[count]);
1872 ++count;
1873 }
1874
1875 if (offset_ - offset != dheader)
1876 {
1877 throw exception::BadParamException("Member size differs from the size specified by DHEADER");
1878 }
1879 }
1880 else
1881 {
1882 state state_before_error(*this);
1883
1884 deserialize(sequence_length);
1885
1886 if (sequence_length == 0)
1887 {
1888 vector_t.clear();
1889 return *this;
1890 }
1891
1892 if ((end_ - offset_) < sequence_length)
1893 {
1894 set_state(state_before_error);
1897 }
1898
1899 try
1900 {
1901 vector_t.resize(sequence_length);
1902 return deserialize_array(vector_t.data(), vector_t.size());
1903 }
1904 catch (exception::Exception& ex)
1905 {
1906 set_state(state_before_error);
1907 ex.raise();
1908 }
1909 }
1910
1911 return *this;
1912 }
1913
1914 /*!
1915 * @brief This function template deserializes a sequence of primitive.
1916 * @param vector_t The variable that will store the sequence read from the buffer.
1917 * @return Reference to the eprosima::fastcdr::Cdr object.
1918 * @exception exception::NotEnoughMemoryException This exception is thrown when trying to deserialize a position that exceeds the internal memory size.
1919 */
1920 template<class _T, typename std::enable_if<std::is_enum<_T>::value ||
1921 std::is_arithmetic<_T>::value>::type* = nullptr>
1923 std::vector<_T>& vector_t)
1924 {
1925 uint32_t sequence_length = 0;
1926 state state_before_error(*this);
1927
1928 deserialize(sequence_length);
1929
1930 if (sequence_length == 0)
1931 {
1932 vector_t.clear();
1933 return *this;
1934 }
1935
1936 if ((end_ - offset_) < sequence_length)
1937 {
1938 set_state(state_before_error);
1941 }
1942
1943 try
1944 {
1945 vector_t.resize(sequence_length);
1946 return deserialize_array(vector_t.data(), vector_t.size());
1947 }
1948 catch (exception::Exception& ex)
1949 {
1950 set_state(state_before_error);
1951 ex.raise();
1952 }
1953
1954 return *this;
1955 }
1956
1957 /*!
1958 * @brief This function template deserializes a sequence.
1959 * @param vector_t The variable that will store the sequence read from the buffer.
1960 * @return Reference to the eprosima::fastcdr::Cdr object.
1961 * @exception exception::NotEnoughMemoryException This exception is thrown when trying to deserialize a position that exceeds the internal memory size.
1962 */
1963 TEMPLATE_SPEC
1965 std::vector<bool>& vector_t)
1966 {
1967 return deserialize_bool_sequence(vector_t);
1968 }
1969
1970 /*!
1971 * @brief This function template deserializes a map of non-primitive.
1972 * @param map_t The variable that will store the map read from the buffer.
1973 * @return Reference to the eprosima::fastcdr::Cdr object.
1974 * @exception exception::NotEnoughMemoryException This exception is thrown when trying to deserialize a position that exceeds the internal memory size.
1975 */
1976 template<class _K, class _T, typename std::enable_if<!std::is_enum<_T>::value &&
1977 !std::is_arithmetic<_T>::value>::type* = nullptr>
1979 std::map<_K, _T>& map_t)
1980 {
1982 {
1983 uint32_t dheader {0};
1984 deserialize(dheader);
1985
1986 auto offset = offset_;
1987
1988 uint32_t map_length {0};
1989 deserialize(map_length);
1990
1991 map_t.clear();
1992
1993 uint32_t count {0};
1994 while (offset_ - offset < dheader && count < map_length)
1995 {
1996 _K key;
1997 _T val;
1998 deserialize(key);
1999 deserialize(val);
2000 map_t.emplace(std::pair<_K, _T>(std::move(key), std::move(val)));
2001 ++count;
2002 }
2003
2004 if (offset_ - offset != dheader)
2005 {
2006 throw exception::BadParamException("Member size greater than size specified by DHEADER");
2007 }
2008 }
2009 else
2010 {
2011 uint32_t sequence_length = 0;
2012 state state_(*this);
2013
2014 deserialize(sequence_length);
2015
2016 map_t.clear();
2017
2018 try
2019 {
2020 for (uint32_t i = 0; i < sequence_length; ++i)
2021 {
2022 _K key;
2023 _T value;
2024 deserialize(key);
2025 deserialize(value);
2026 map_t.emplace(std::pair<_K, _T>(std::move(key), std::move(value)));
2027 }
2028 }
2029 catch (exception::Exception& ex)
2030 {
2031 set_state(state_);
2032 ex.raise();
2033 }
2034 }
2035
2036 return *this;
2037 }
2038
2039 /*!
2040 * @brief This function template deserializes a map of primitive.
2041 * @param map_t The variable that will store the map read from the buffer.
2042 * @return Reference to the eprosima::fastcdr::Cdr object.
2043 * @exception exception::NotEnoughMemoryException This exception is thrown when trying to deserialize a position that exceeds the internal memory size.
2044 */
2045 template<class _K, class _T, typename std::enable_if<std::is_enum<_T>::value ||
2046 std::is_arithmetic<_T>::value>::type* = nullptr>
2048 std::map<_K, _T>& map_t)
2049 {
2050 uint32_t sequence_length = 0;
2051 state state_(*this);
2052
2053 deserialize(sequence_length);
2054
2055 try
2056 {
2057 for (uint32_t i = 0; i < sequence_length; ++i)
2058 {
2059 _K key;
2060 _T value;
2061 deserialize(key);
2062 deserialize(value);
2063 map_t.emplace(std::pair<_K, _T>(std::move(key), std::move(value)));
2064 }
2065 }
2066 catch (exception::Exception& ex)
2067 {
2068 set_state(state_);
2069 ex.raise();
2070 }
2071
2072 return *this;
2073 }
2074
2075 /*!
2076 * @brief Decodes a bitset from the buffer.
2077 * @param[out] value Reference to the variable where the bitset will be stored after decoding from the buffer.
2078 * @return Reference to the eprosima::fastcdr::Cdr object.
2079 * @exception exception::NotEnoughMemoryException This exception is thrown when trying to decode from a buffer
2080 * position that exceeds the internal memory size.
2081 */
2082 template<size_t N, typename std::enable_if < (N < 9) > ::type* = nullptr>
2083 Cdr& deserialize(
2084 std::bitset<N>& value)
2085 {
2086 uint8_t decode_value {0};
2087 deserialize(decode_value);
2088 value = decode_value;
2089 return *this;
2090 }
2091
2092 template<size_t N, typename std::enable_if < (8 < N && N < 17) > ::type* = nullptr>
2093 Cdr& deserialize(
2094 std::bitset<N>& value)
2095 {
2096 uint16_t decode_value {0};
2097 deserialize(decode_value);
2098 value = decode_value;
2099 return *this;
2100 }
2101
2102 template<size_t N, typename std::enable_if < (16 < N && N < 33) > ::type* = nullptr>
2103 Cdr& deserialize(
2104 std::bitset<N>& value)
2105 {
2106 uint32_t decode_value {0};
2107 deserialize(decode_value);
2108 value = decode_value;
2109 return *this;
2110 }
2111
2112 template<size_t N, typename std::enable_if < (32 < N && N < 65) > ::type* = nullptr>
2113 Cdr& deserialize(
2114 std::bitset<N>& value)
2115 {
2116 uint64_t decode_value {0};
2117 deserialize(decode_value);
2118 value = decode_value;
2119 return *this;
2120 }
2121
2122 /*!
2123 * @brief Decodes an array of a type not managed by this encoder from the buffer.
2124 *
2125 * To do that, the encoder expects a function `deserialize` to be provided by the type.
2126 *
2127 * @param[out] value Reference to the variable where the array will be stored after decoding from the buffer.
2128 * @param[in] num_elements Number of the elements in the array.
2129 * @return Reference to the eprosima::fastcdr::Cdr object.
2130 * @exception exception::NotEnoughMemoryException This exception is thrown when trying to decode from a buffer
2131 * position that exceeds the internal memory size.
2132 */
2133 template<class _T>
2135 _T* value,
2136 size_t num_elements)
2137 {
2138 for (size_t count = 0; count < num_elements; ++count)
2139 {
2140 deserialize(value[count]);
2141 }
2142 return *this;
2143 }
2144
2145 /*!
2146 * @brief This function template deserializes an array of non-basic objects with a different endianness.
2147 * @param type_t The variable that will store the array of objects read from the buffer.
2148 * @param num_elements Number of the elements in the array.
2149 * @param endianness Endianness that will be used in the deserialization of this value.
2150 * @return Reference to the eprosima::fastcdr::Cdr object.
2151 * @exception exception::NotEnoughMemoryException This exception is thrown when trying to deserialize a position that exceeds the internal memory size.
2152 */
2153 template<class _T>
2155 _T* type_t,
2156 size_t num_elements,
2158 {
2159 bool aux_swap = swap_bytes_;
2160 swap_bytes_ = (swap_bytes_ && (static_cast<Endianness>(endianness_) == endianness)) ||
2161 (!swap_bytes_ && (static_cast<Endianness>(endianness_) != endianness));
2162
2163 try
2164 {
2165 deserialize_array(type_t, num_elements);
2166 swap_bytes_ = aux_swap;
2167 }
2168 catch (exception::Exception& ex)
2169 {
2170 swap_bytes_ = aux_swap;
2171 ex.raise();
2172 }
2173
2174 return *this;
2175 }
2176
2177 /*!
2178 * @brief This function deserializes an array of octets.
2179 * @param octet_t The variable that will store the array of octets read from the buffer.
2180 * @param num_elements Number of the elements in the array.
2181 * @return Reference to the eprosima::fastcdr::Cdr object.
2182 * @exception exception::NotEnoughMemoryException This exception is thrown when trying to deserialize a position that exceeds the internal memory size.
2183 */
2184 TEMPLATE_SPEC
2186 uint8_t* octet_t,
2187 size_t num_elements)
2188 {
2189 return deserialize_array(reinterpret_cast<char*>(octet_t), num_elements);
2190 }
2191
2192 /*!
2193 * @brief This function deserializes an array of characters.
2194 * @param char_t The variable that will store the array of characters read from the buffer.
2195 * @param num_elements Number of the elements in the array.
2196 * @return Reference to the eprosima::fastcdr::Cdr object.
2197 * @exception exception::NotEnoughMemoryException This exception is thrown when trying to deserialize a position that exceeds the internal memory size.
2198 */
2200 char* char_t,
2201 size_t num_elements);
2202
2203 /*!
2204 * @brief This function deserializes an array of int8_t.
2205 * @param int8 The variable that will store the array of int8_t read from the buffer.
2206 * @param num_elements Number of the elements in the array.
2207 * @return Reference to the eprosima::fastcdr::Cdr object.
2208 * @exception exception::NotEnoughMemoryException This exception is thrown when trying to deserialize a position that exceeds the internal memory size.
2209 */
2210 TEMPLATE_SPEC
2212 int8_t* int8,
2213 size_t num_elements)
2214 {
2215 return deserialize_array(reinterpret_cast<char*>(int8), num_elements);
2216 }
2217
2218 /*!
2219 * @brief This function deserializes an array of unsigned shorts.
2220 * @param ushort_t The variable that will store the array of unsigned shorts read from the buffer.
2221 * @param num_elements Number of the elements in the array.
2222 * @return Reference to the eprosima::fastcdr::Cdr object.
2223 * @exception exception::NotEnoughMemoryException This exception is thrown when trying to deserialize a position that exceeds the internal memory size.
2224 */
2225 TEMPLATE_SPEC
2227 uint16_t* ushort_t,
2228 size_t num_elements)
2229 {
2230 return deserialize_array(reinterpret_cast<int16_t*>(ushort_t), num_elements);
2231 }
2232
2233 /*!
2234 * @brief This function deserializes an array of shorts.
2235 * @param short_t The variable that will store the array of shorts read from the buffer.
2236 * @param num_elements Number of the elements in the array.
2237 * @return Reference to the eprosima::fastcdr::Cdr object.
2238 * @exception exception::NotEnoughMemoryException This exception is thrown when trying to deserialize a position that exceeds the internal memory size.
2239 */
2241 int16_t* short_t,
2242 size_t num_elements);
2243
2244 /*!
2245 * @brief This function deserializes an array of unsigned longs.
2246 * @param ulong_t The variable that will store the array of unsigned longs read from the buffer.
2247 * @param num_elements Number of the elements in the array.
2248 * @return Reference to the eprosima::fastcdr::Cdr object.
2249 * @exception exception::NotEnoughMemoryException This exception is thrown when trying to deserialize a position that exceeds the internal memory size.
2250 */
2251 TEMPLATE_SPEC
2253 uint32_t* ulong_t,
2254 size_t num_elements)
2255 {
2256 return deserialize_array(reinterpret_cast<int32_t*>(ulong_t), num_elements);
2257 }
2258
2259 /*!
2260 * @brief This function deserializes an array of longs.
2261 * @param long_t The variable that will store the array of longs read from the buffer.
2262 * @param num_elements Number of the elements in the array.
2263 * @return Reference to the eprosima::fastcdr::Cdr object.
2264 * @exception exception::NotEnoughMemoryException This exception is thrown when trying to deserialize a position that exceeds the internal memory size.
2265 */
2267 int32_t* long_t,
2268 size_t num_elements);
2269
2270 /*!
2271 * @brief This function deserializes an array of wide-chars.
2272 * @param wchar The variable that will store the array of wide-chars read from the buffer.
2273 * @param num_elements Number of the elements in the array.
2274 * @return Reference to the eprosima::fastcdr::Cdr object.
2275 * @exception exception::NotEnoughMemoryException This exception is thrown when trying to deserialize a position that exceeds the internal memory size.
2276 */
2278 wchar_t* wchar,
2279 size_t num_elements);
2280
2281 /*!
2282 * @brief This function deserializes an array of unsigned long longs.
2283 * @param ulonglong_t The variable that will store the array of unsigned long longs read from the buffer.
2284 * @param num_elements Number of the elements in the array.
2285 * @return Reference to the eprosima::fastcdr::Cdr object.
2286 * @exception exception::NotEnoughMemoryException This exception is thrown when trying to deserialize a position that exceeds the internal memory size.
2287 */
2288 TEMPLATE_SPEC
2290 uint64_t* ulonglong_t,
2291 size_t num_elements)
2292 {
2293 return deserialize_array(reinterpret_cast<int64_t*>(ulonglong_t), num_elements);
2294 }
2295
2296 /*!
2297 * @brief This function deserializes an array of long longs.
2298 * @param longlong_t The variable that will store the array of long longs read from the buffer.
2299 * @param num_elements Number of the elements in the array.
2300 * @return Reference to the eprosima::fastcdr::Cdr object.
2301 * @exception exception::NotEnoughMemoryException This exception is thrown when trying to deserialize a position that exceeds the internal memory size.
2302 */
2304 int64_t* longlong_t,
2305 size_t num_elements);
2306
2307 /*!
2308 * @brief This function deserializes an array of floats.
2309 * @param float_t The variable that will store the array of floats read from the buffer.
2310 * @param num_elements Number of the elements in the array.
2311 * @return Reference to the eprosima::fastcdr::Cdr object.
2312 * @exception exception::NotEnoughMemoryException This exception is thrown when trying to deserialize a position that exceeds the internal memory size.
2313 */
2315 float* float_t,
2316 size_t num_elements);
2317
2318 /*!
2319 * @brief This function deserializes an array of doubles.
2320 * @param double_t The variable that will store the array of doubles read from the buffer.
2321 * @param num_elements Number of the elements in the array.
2322 * @return Reference to the eprosima::fastcdr::Cdr object.
2323 * @exception exception::NotEnoughMemoryException This exception is thrown when trying to deserialize a position that exceeds the internal memory size.
2324 */
2326 double* double_t,
2327 size_t num_elements);
2328
2329 /*!
2330 * @brief This function deserializes an array of long doubles.
2331 * @param ldouble_t The variable that will store the array of long doubles read from the buffer.
2332 * @param num_elements Number of the elements in the array.
2333 * @return Reference to the eprosima::fastcdr::Cdr object.
2334 * @exception exception::NotEnoughMemoryException This exception is thrown when trying to deserialize a position that exceeds the internal memory size.
2335 * @note Due to internal representation differences, WIN32 and *NIX like systems are not compatible.
2336 */
2338 long double* ldouble_t,
2339 size_t num_elements);
2340
2341 /*!
2342 * @brief This function deserializes an array of booleans.
2343 * @param bool_t The variable that will store the array of booleans read from the buffer.
2344 * @param num_elements Number of the elements in the array.
2345 * @return Reference to the eprosima::fastcdr::Cdr object.
2346 * @exception exception::NotEnoughMemoryException This exception is thrown when trying to deserialize a position that exceeds the internal memory size.
2347 */
2349 bool* bool_t,
2350 size_t num_elements);
2351
2352 /*!
2353 * @brief Decodes an array of primitives on a std::vector.
2354 *
2355 * std::vector must have allocated the number of element of the array.
2356 *
2357 * @param[out] value Reference to the std::vector where the array will be stored after decoding from the buffer.
2358 * @return Reference to the eprosima::fastcdr::Cdr object.
2359 * @exception exception::NotEnoughMemoryException This exception is thrown when trying to decode from a buffer
2360 * position that exceeds the internal memory size.
2361 */
2362 template<class _T, typename std::enable_if<std::is_enum<_T>::value ||
2363 std::is_arithmetic<_T>::value>::type* = nullptr>
2365 std::vector<_T>& value)
2366 {
2367 deserialize_array(value.data(), value.size());
2368
2369 return *this;
2370 }
2371
2372 /*!
2373 * @brief Decodes an array of non-primitives on a std::vector.
2374 *
2375 * std::vector must have allocated the number of element of the array.
2376 *
2377 * @param[out] value Reference to the std::vector where the array will be stored after decoding from the buffer.
2378 * @return Reference to the eprosima::fastcdr::Cdr object.
2379 * @exception exception::NotEnoughMemoryException This exception is thrown when trying to decode from a buffer
2380 * position that exceeds the internal memory size.
2381 */
2382 template<class _T, typename std::enable_if<!std::is_enum<_T>::value &&
2383 !std::is_arithmetic<_T>::value>::type* = nullptr>
2385 std::vector<_T>& value)
2386 {
2388 {
2389 uint32_t dheader {0};
2390 deserialize(dheader);
2391
2392 uint32_t count {0};
2393 auto offset = offset_;
2394 while (offset_ - offset < dheader && count < value.size())
2395 {
2396 deserialize_array(&value.data()[count], 1);
2397 ++count;
2398 }
2399
2400 if (offset_ - offset != dheader)
2401 {
2402 throw exception::BadParamException("Member size greater than size specified by DHEADER");
2403 }
2404 }
2405 else
2406 {
2407 return deserialize_array(value.data(), value.size());
2408 }
2409
2410 return *this;
2411 }
2412
2413 /*!
2414 * @brief Decodes an array of non-primitives on a std::vector with a different endianness.
2415 *
2416 * std::vector must have allocated the number of element of the array.
2417 *
2418 * @param[out] value Reference to the std::vector where the array will be stored after decoding from the buffer.
2419 * @param[in] endianness Endianness that will be used in the serialization of this value.
2420 * @return Reference to the eprosima::fastcdr::Cdr object.
2421 * @exception exception::NotEnoughMemoryException This exception is thrown when trying to decode from a buffer
2422 * position that exceeds the internal memory size.
2423 */
2424 template<class _T>
2426 std::vector<_T>& value,
2428 {
2429 bool aux_swap = swap_bytes_;
2430 swap_bytes_ = (swap_bytes_ && (static_cast<Endianness>(endianness_) == endianness)) ||
2431 (!swap_bytes_ && (static_cast<Endianness>(endianness_) != endianness));
2432
2433 try
2434 {
2435 deserialize_array(value);
2436 swap_bytes_ = aux_swap;
2437 }
2438 catch (exception::Exception& ex)
2439 {
2440 swap_bytes_ = aux_swap;
2441 ex.raise();
2442 }
2443
2444 return *this;
2445 }
2446
2447 /*!
2448 * @brief Decodes an array of booleans on a std::vector.
2449 *
2450 * std::vector must have allocated the number of element of the array.
2451 *
2452 * @param[out] value Reference to the std::vector where the array will be stored after decoding from the buffer.
2453 * @return Reference to the eprosima::fastcdr::Cdr object.
2454 * @exception exception::NotEnoughMemoryException This exception is thrown when trying to encode into a buffer
2455 * position that exceeds the internal memory size.
2456 */
2457 TEMPLATE_SPEC
2459 std::vector<bool>& value)
2460 {
2462
2463 return *this;
2464 }
2465
2466 /*!
2467 * @brief This function template deserializes a raw sequence of non-primitives.
2468 * This function allocates memory to store the sequence. The user pointer will be set to point this allocated memory.
2469 * The user will have to free this allocated memory using free()
2470 * @param sequence_t The pointer that will store the sequence read from the buffer.
2471 * @param num_elements This variable return the number of elements of the sequence.
2472 * @return Reference to the eprosima::fastcdr::Cdr object.
2473 * @exception exception::NotEnoughMemoryException This exception is thrown when trying to deserialize a position that exceeds the internal memory size.
2474 */
2475 template<class _T, typename std::enable_if<!std::is_enum<_T>::value &&
2476 !std::is_arithmetic<_T>::value>::type* = nullptr>
2478 _T*& sequence_t,
2479 size_t& num_elements)
2480 {
2481 uint32_t sequence_length {0};
2482
2484 {
2485 uint32_t dheader {0};
2486 deserialize(dheader);
2487
2488 auto offset = offset_;
2489
2490 deserialize(sequence_length);
2491
2492 try
2493 {
2494 sequence_t = reinterpret_cast<_T*>(calloc(sequence_length, sizeof(_T)));
2495
2496 uint32_t count {0};
2497 while (offset_ - offset < dheader && count < sequence_length)
2498 {
2499 deserialize(sequence_t[count]);
2500 ++count;
2501 }
2502
2503 if (offset_ - offset != dheader)
2504 {
2505 throw exception::BadParamException("Member size greater than size specified by DHEADER");
2506 }
2507 }
2508 catch (exception::Exception& ex)
2509 {
2510 free(sequence_t);
2511 sequence_t = NULL;
2512 ex.raise();
2513 }
2514 }
2515 else
2516 {
2517 state state_before_error(*this);
2518
2519 deserialize(sequence_length);
2520
2521 if ((end_ - offset_) < sequence_length)
2522 {
2523 set_state(state_before_error);
2526 }
2527
2528 try
2529 {
2530 sequence_t = reinterpret_cast<_T*>(calloc(sequence_length, sizeof(_T)));
2531 deserialize_array(sequence_t, sequence_length);
2532 }
2533 catch (exception::Exception& ex)
2534 {
2535 free(sequence_t);
2536 sequence_t = NULL;
2537 set_state(state_before_error);
2538 ex.raise();
2539 }
2540 }
2541
2542 num_elements = sequence_length;
2543 return *this;
2544 }
2545
2546 /*!
2547 * @brief This function template deserializes a raw sequence of primitives.
2548 * This function allocates memory to store the sequence. The user pointer will be set to point this allocated memory.
2549 * The user will have to free this allocated memory using free()
2550 * @param sequence_t The pointer that will store the sequence read from the buffer.
2551 * @param num_elements This variable return the number of elements of the sequence.
2552 * @return Reference to the eprosima::fastcdr::Cdr object.
2553 * @exception exception::NotEnoughMemoryException This exception is thrown when trying to deserialize a position that exceeds the internal memory size.
2554 */
2555 template<class _T, typename std::enable_if<std::is_enum<_T>::value ||
2556 std::is_arithmetic<_T>::value>::type* = nullptr>
2558 _T*& sequence_t,
2559 size_t& num_elements)
2560 {
2561 uint32_t sequence_length = 0;
2562 state state_before_error(*this);
2563
2564 deserialize(sequence_length);
2565
2566 try
2567 {
2568 sequence_t = reinterpret_cast<_T*>(calloc(sequence_length, sizeof(_T)));
2569 deserialize_array(sequence_t, sequence_length);
2570 }
2571 catch (exception::Exception& ex)
2572 {
2573 free(sequence_t);
2574 sequence_t = NULL;
2575 set_state(state_before_error);
2576 ex.raise();
2577 }
2578
2579 num_elements = sequence_length;
2580 return *this;
2581 }
2582
2583 /*!
2584 * @brief This function template deserializes a raw sequence with a different endianness.
2585 * This function allocates memory to store the sequence. The user pointer will be set to point this allocated memory.
2586 * The user will have to free this allocated memory using free()
2587 * @param sequence_t The pointer that will store the sequence read from the buffer.
2588 * @param num_elements This variable return the number of elements of the sequence.
2589 * @param endianness Endianness that will be used in the deserialization of this value.
2590 * @return Reference to the eprosima::fastcdr::Cdr object.
2591 * @exception exception::NotEnoughMemoryException This exception is thrown when trying to deserialize a position that exceeds the internal memory size.
2592 */
2593 template<class _T>
2595 _T*& sequence_t,
2596 size_t& num_elements,
2598 {
2599 bool aux_swap = swap_bytes_;
2600 swap_bytes_ = (swap_bytes_ && (static_cast<Endianness>(endianness_) == endianness)) ||
2601 (!swap_bytes_ && (static_cast<Endianness>(endianness_) != endianness));
2602
2603 try
2604 {
2605 deserialize_sequence(sequence_t, num_elements);
2606 swap_bytes_ = aux_swap;
2607 }
2608 catch (exception::Exception& ex)
2609 {
2610 swap_bytes_ = aux_swap;
2611 ex.raise();
2612 }
2613
2614 return *this;
2615 }
2616
2617 /*!
2618 * @brief This function template deserializes a string sequence.
2619 * This function allocates memory to store the sequence. The user pointer will be set to point this allocated memory.
2620 * The user will have to free this allocated memory using free()
2621 * @param sequence_t The pointer that will store the sequence read from the buffer.
2622 * @param num_elements This variable return the number of elements of the sequence.
2623 * @return Reference to the eprosima::fastcdr::Cdr object.
2624 * @exception exception::NotEnoughMemoryException This exception is thrown when trying to deserialize a position that exceeds the internal memory size.
2625 */
2626 TEMPLATE_SPEC
2628 std::string*& sequence_t,
2629 size_t& num_elements)
2630 {
2631 return deserialize_string_sequence(sequence_t, num_elements);
2632 }
2633
2634 /*!
2635 * @brief This function template deserializes a wide-string sequence.
2636 * This function allocates memory to store the sequence. The user pointer will be set to point this allocated memory.
2637 * The user will have to free this allocated memory using free()
2638 * @param sequence_t The pointer that will store the sequence read from the buffer.
2639 * @param num_elements This variable return the number of elements of the sequence.
2640 * @return Reference to the eprosima::fastcdr::Cdr object.
2641 * @exception exception::NotEnoughMemoryException This exception is thrown when trying to deserialize a position that exceeds the internal memory size.
2642 */
2643 TEMPLATE_SPEC
2645 std::wstring*& sequence_t,
2646 size_t& num_elements)
2647 {
2648 return deserialize_wstring_sequence(sequence_t, num_elements);
2649 }
2650
2651 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
2652 /// XCDR extensions
2653 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
2654
2655 /*!
2656 * @brief Encodes a member of a type according to the encoding algorithm used.
2657 * @param[in] member_id Member identifier.
2658 * @param[in] member_value Member value.
2659 * @param[in] header_selection Selects which member header will be used to allocate space.
2660 * Default: XCdrHeaderSelection::AUTO_WITH_SHORT_HEADER_BY_DEFAULT.
2661 * @return Reference to the eprosima::fastcdr::Cdr object.
2662 * @exception exception::NotEnoughMemoryException This exception is thrown when trying to encode into a buffer
2663 * position that exceeds the internal memory size.
2664 */
2665 template<class _T>
2667 const MemberId& member_id,
2668 const _T& member_value,
2670 {
2671 Cdr::state current_state(*this);
2672 (this->*begin_serialize_member_)(member_id, true, current_state, header_selection);
2673 serialize(member_value);
2674 return (this->*end_serialize_member_)(current_state);
2675 }
2676
2677 /*!
2678 * @brief Encodes an optional member of a type according to the encoding algorithm used.
2679 * @param[in] member_id Member identifier.
2680 * @param[in] member_value Optional member value.
2681 * @param[in] header_selection Selects which member header will be used to allocate space.
2682 * Default: XCdrHeaderSelection::AUTO_WITH_SHORT_HEADER_BY_DEFAULT.
2683 * @return Reference to the eprosima::fastcdr::Cdr object.
2684 * @exception exception::NotEnoughMemoryException This exception is thrown when trying to encode into a buffer
2685 * position that exceeds the internal memory size.
2686 */
2687 template<class _T>
2689 const MemberId& member_id,
2690 const optional<_T>& member_value,
2692 {
2693 Cdr::state current_state(*this);
2694 (this->*begin_serialize_opt_member_)(member_id, member_value.has_value(), current_state, header_selection);
2695 serialize(member_value);
2696 return (this->*end_serialize_opt_member_)(current_state);
2697 }
2698
2699 /*!
2700 * @brief Decodes a member of a type according to the encoding algorithm used.
2701 * @param[out] member_value A reference of the variable where the member value will be stored.
2702 * @return Reference to the eprosima::fastcdr::Cdr object.
2703 * @exception exception::NotEnoughMemoryException This exception is thrown when trying to decode from a buffer
2704 * position that exceeds the internal memory size.
2705 */
2706 template<class _T>
2708 _T& member_value)
2709 {
2710 return deserialize(member_value);
2711 }
2712
2713 /*!
2714 * @brief Decodes an optional member of a type according to the encoding algorithm used.
2715 * @param[out] member_value A reference of the variable where the optional member value will be stored.
2716 * @return Reference to the eprosima::fastcdr::Cdr object.
2717 * @exception exception::NotEnoughMemoryException This exception is thrown when trying to decode from a buffer
2718 * position that exceeds the internal memory size.
2719 */
2720 template<class _T>
2722 optional<_T>& member_value)
2723 {
2725 {
2726 Cdr::state current_state(*this);
2727 MemberId member_id;
2728 xcdr1_deserialize_member_header(member_id, current_state);
2729 auto prev_offset = offset_;
2730 if (0 < current_state.member_size_)
2731 {
2732 deserialize(member_value);
2733 }
2734 if (current_state.member_size_ != offset_ - prev_offset)
2735 {
2737 "Member size provided by member header is not equal to the real decoded member size");
2738 }
2739 }
2740 else
2741 {
2742 deserialize(member_value);
2743 }
2744 return *this;
2745 }
2746
2747 /*!
2748 * @brief Tells to the encoder a new type and its members starts to be encoded.
2749 * @param[in,out] current_state State of the encoder previous of calling this function.
2750 * @param[in] type_encoding The encoding algorithm used to encode the type and its members.
2751 * @return Reference to the eprosima::fastcdr::Cdr object.
2752 * @exception exception::NotEnoughMemoryException This exception is thrown when trying to decode from a buffer
2753 * position that exceeds the internal memory size.
2754 */
2756 Cdr::state& current_state,
2757 EncodingAlgorithmFlag type_encoding);
2758
2759 /*!
2760 * @brief Tells to the encoder the encoding of the type finishes.
2761 * @param[in] current_state State of the encoder previous of calling the function begin_serialize_type.
2762 * @return Reference to the eprosima::fastcdr::Cdr object.
2763 * @exception exception::NotEnoughMemoryException This exception is thrown when trying to decode from a buffer
2764 * position that exceeds the internal memory size.
2765 */
2767 Cdr::state& current_state);
2768
2769 /*!
2770 * @brief Tells to the encoder a new type and its members starts to be decoded.
2771 * @param[in] type_encoding The encoding algorithm used to decode the type and its members.
2772 * @param[in] functor Functor called each time a member has to be decoded.
2773 * @return Reference to the eprosima::fastcdr::Cdr object.
2774 * @exception exception::NotEnoughMemoryException This exception is thrown when trying to decode from a buffer
2775 * position that exceeds the internal memory size.
2776 */
2778 EncodingAlgorithmFlag type_encoding,
2779 std::function<bool (Cdr&, const MemberId&)> functor);
2780
2781 /*!
2782 * @brief Encodes an optional in the buffer.
2783 * @param[in] value A reference to the optional which will be encoded in the buffer.
2784 * @return Reference to the eprosima::fastcdr::Cdr object.
2785 * @exception exception::NotEnoughMemoryException This exception is thrown when trying to encode into a buffer
2786 * position that exceeds the internal memory size.
2787 */
2788 template<class _T>
2790 const optional<_T>& value)
2791 {
2793 {
2794 serialize(value.has_value());
2795 }
2796
2797 if (value.has_value())
2798 {
2799 serialize(*value);
2800 }
2801 return *this;
2802 }
2803
2804 /*!
2805 * @brief Encodes an external in the buffer.
2806 * @param[in] value A reference to the external which will be encoded in the buffer.
2807 * @return Reference to the eprosima::fastcdr::Cdr object.
2808 * @exception exception::BadParamException This exception is thrown when external is null.
2809 * @exception exception::NotEnoughMemoryException This exception is thrown when trying to encode into a buffer
2810 * position that exceeds the internal memory size.
2811 */
2812 template<class _T>
2814 const external<_T>& value)
2815 {
2816 if (!value)
2817 {
2818 throw exception::BadParamException("External member is null");
2819 }
2820
2821 serialize(*value);
2822 return *this;
2823 }
2824
2825 /*!
2826 * @brief Tells the encoder the member identifier for the next member to be encoded.
2827 * @param[in] member_id Member identifier.
2828 * @return Reference to the eprosima::fastcdr::Cdr object.
2829 * @exception exception::BadParamException This exception is thrown when a member id is already set without being
2830 * encoded.
2831 */
2833 const MemberId& member_id);
2834
2835 /*!
2836 * @brief Decodes an optional from the buffer.
2837 * @param[out] value A reference to the variable where the optional will be stored.
2838 * @return Reference to the eprosima::fastcdr::Cdr object.
2839 * @exception exception::NotEnoughMemoryException This exception is thrown when trying to decode from a buffer
2840 * position that exceeds the internal memory size.
2841 */
2842 template<class _T>
2844 optional<_T>& value)
2845 {
2846 bool is_present = true;
2848 {
2849 deserialize(is_present);
2850 }
2851 value.reset(is_present);
2852 if (is_present)
2853 {
2854 deserialize(*value);
2855 }
2856 return *this;
2857 }
2858
2859 /*!
2860 * @brief Decodes an external from the buffer.
2861 * @param[out] value A reference to the variable where the external will be stored.
2862 * @return Reference to the eprosima::fastcdr::Cdr object.
2863 * @exception exception::BadParamException This exception is thrown when the external is locked.
2864 * @exception exception::NotEnoughMemoryException This exception is thrown when trying to decode from a buffer
2865 * position that exceeds the internal memory size.
2866 */
2867 template<class _T>
2869 external<_T>& value)
2870 {
2871 if (value.is_locked())
2872 {
2873 throw exception::BadParamException("External member is locked");
2874 }
2875
2876 if (!value)
2877 {
2878 value = external<_T>{new typename external<_T>::type()};
2879 }
2880
2881 deserialize(*value);
2882 return *this;
2883 }
2884
2885 /*!
2886 * @brief Decodes an optional of an external from the buffer.
2887 * @param[out] value A reference to the variable where the optional will be stored.
2888 * @return Reference to the eprosima::fastcdr::Cdr object.
2889 * @exception exception::BadParamException This exception is thrown when the external is locked.
2890 * @exception exception::NotEnoughMemoryException This exception is thrown when trying to decode from a buffer
2891 * position that exceeds the internal memory size.
2892 */
2893 template<class _T>
2895 optional<external<_T>>& value)
2896 {
2897 if (value.has_value() && value.value().is_locked())
2898 {
2899 throw exception::BadParamException("External member is locked");
2900 }
2901
2902 bool is_present = true;
2904 {
2905 deserialize(is_present);
2906 }
2907 value.reset(is_present);
2908 if (is_present)
2909 {
2910 deserialize(*value);
2911 }
2912 return *this;
2913 }
2914
2915 /*!
2916 * @brief Encodes an empty DHEADER if the encoding version is XCDRv2.
2917 * After serializing the members's type, @ref set_xcdrv2_dheader must be called to set the correct DHEADER value
2918 * using the @ref state returned by this function.
2919 */
2921
2922 /*!
2923 * @brief Uses the @ref state to calculate the member's type size and serialize the value in the previous allocated
2924 * DHEADER.
2925 *
2926 * @param[in] state @ref state used to calculate the member's type size.
2927 */
2929 const state& state);
2930
2931private:
2932
2934 const Cdr&) = delete;
2935
2937 const Cdr&) = delete;
2938
2940 const std::vector<bool>& vector_t);
2941
2943 const std::vector<bool>& vector_t);
2944
2946 std::vector<bool>& vector_t);
2947
2949 std::vector<bool>& vector_t);
2950
2952 std::string*& sequence_t,
2953 size_t& num_elements);
2954
2956 std::wstring*& sequence_t,
2957 size_t& num_elements);
2958
2959 /*!
2960 * @brief This function template detects the content type of the STD container array and serializes the array.
2961 * @param array_t The array that will be serialized in the buffer.
2962 * @param num_elements Number of the elements in the array.
2963 * @return Reference to the eprosima::fastcdr::Cdr object.
2964 * @exception exception::NotEnoughMemoryException This exception is thrown when trying to serialize a position that exceeds the internal memory size.
2965 */
2966 template<class _T, size_t _Size>
2968 const std::array<_T, _Size>* array_t,
2969 size_t num_elements)
2970 {
2971 return serialize_array(array_t->data(), num_elements * array_t->size());
2972 }
2973
2974 /*!
2975 * @brief This function template detects the content type of the STD container array and deserializes the array.
2976 * @param array_t The variable that will store the array read from the buffer.
2977 * @param num_elements Number of the elements in the array.
2978 * @return Reference to the eprosima::fastcdr::Cdr object.
2979 * @exception exception::NotEnoughMemoryException This exception is thrown when trying to deserialize a position that exceeds the internal memory size.
2980 */
2981 template<class _T, size_t _Size>
2983 std::array<_T, _Size>* array_t,
2984 size_t num_elements)
2985 {
2986 return deserialize_array(array_t->data(), num_elements * array_t->size());
2987 }
2988
2989 /*!
2990 * @brief This function template detects the content type of STD container array and deserializes the array with a different endianness.
2991 * @param array_t The variable that will store the array read from the buffer.
2992 * @param num_elements Number of the elements in the array.
2993 * @param endianness Endianness that will be used in the deserialization of this value.
2994 * @return Reference to the eprosima::fastcdr::Cdr object.
2995 * @exception exception::NotEnoughMemoryException This exception is thrown when trying to deserialize a position that exceeds the internal memory size.
2996 */
2997 template<class _T, size_t _Size>
2999 std::array<_T, _Size>* array_t,
3000 size_t num_elements,
3002 {
3003 return deserialize_array(array_t->data(), num_elements * array_t->size(), endianness);
3004 }
3005
3006 /*!
3007 * @brief Returns the number of bytes needed to align the current position (having as reference the origin) to
3008 * certain data size.
3009 * @param data_size The size of the data that will be serialized.
3010 * @return The size needed for the alignment.
3011 */
3012 inline size_t alignment(
3013 size_t data_size) const
3014 {
3015 return data_size > last_data_size_ ? (data_size - ((offset_ - origin_) % data_size)) & (data_size - 1) : 0;
3016 }
3017
3018 /*!
3019 * @brief This function jumps the number of bytes of the alignment. These bytes should be calculated with the function eprosima::fastcdr::Cdr::alignment.
3020 * @param align The number of bytes to be skipped.
3021 */
3022 inline void make_alignment(
3023 size_t align)
3024 {
3025 offset_ += align;
3026 last_data_size_ = 0;
3027 }
3028
3029 /*!
3030 * @brief This function resizes the internal buffer. It only applies if the FastBuffer object was created with the default constructor.
3031 * @param min_size_inc Minimun size increase for the internal buffer
3032 * @return True if the resize was succesful, false if it was not
3033 */
3034 bool resize(
3035 size_t min_size_inc);
3036
3037 Cdr_DllAPI const char* read_string(
3038 uint32_t& length);
3040 uint32_t& length);
3041
3042 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
3043 /// XCDR extensions
3044 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
3045 /*!
3046 * @brief Encodes a short member header of a member according to XCDRv1.
3047 * @param[in] member_id Member identifier.
3048 * @pre Member identifier less than 0x3F00.
3049 * @exception exception::NotEnoughMemoryException This exception is thrown when trying to encode into a buffer
3050 * position that exceeds the internal memory size.
3051 */
3053 const MemberId& member_id);
3054
3055 /*!
3056 * @brief Finish the encoding of a short member header of a member according to XCDRv1.
3057 * @param[in] member_id Member identifier.
3058 * @pre Member identifier less than 0x3F00.
3059 * @param[in] member_serialized_size Size of the serialized member.
3060 * @pre Serialized size equal or less than 0xFFFF.
3061 * @exception exception::NotEnoughMemoryException This exception is thrown when trying to encode into a buffer
3062 * position that exceeds the internal memory size.
3063 */
3065 const MemberId& member_id,
3066 size_t member_serialized_size);
3067
3068 /*!
3069 * @brief Encodes a long member header of a member according to XCDRv1.
3070 * @param[in] member_id Member identifier.
3071 * @exception exception::NotEnoughMemoryException This exception is thrown when trying to encode into a buffer
3072 * position that exceeds the internal memory size.
3073 */
3075 const MemberId& member_id);
3076
3077 /*!
3078 * @brief Finish the encoding of a long member header of a member according to XCDRv1.
3079 * @param[in] member_id Member identifier.
3080 * @param[in] member_serialized_size Size of the serialized member.
3081 * @exception exception::NotEnoughMemoryException This exception is thrown when trying to encode into a buffer
3082 * position that exceeds the internal memory size.
3083 */
3085 const MemberId& member_id,
3086 size_t member_serialized_size);
3087
3088 /*!
3089 * @brief Changes the previous encoded long header to a short header according to XCDRv1.
3090 * @param[in] member_id Member identifier.
3091 * @pre Member identifier less than 0x3F00.
3092 * @param[in] member_serialized_size Size of the serialized member.
3093 * @pre Serialized size equal or less than 0xFFFF.
3094 * @exception exception::NotEnoughMemoryException This exception is thrown when trying to encode into a buffer
3095 * position that exceeds the internal memory size.
3096 */
3098 const MemberId& member_id,
3099 size_t member_serialized_size);
3100
3101 /*!
3102 * @brief Changes the previous encoded short header to a long header according to XCDRv1.
3103 * @param[in] member_id Member identifier.
3104 * @param[in] member_serialized_size Size of the serialized member.
3105 * @exception exception::NotEnoughMemoryException This exception is thrown when trying to encode into a buffer
3106 * position that exceeds the internal memory size.
3107 */
3109 const MemberId& member_id,
3110 size_t member_serialized_size);
3111
3112 /*!
3113 * @brief Decodes a member header according to XCDRv1.
3114 * @param[out] member_id Member identifier.
3115 * @param[in,out] current_state State of the encoder previous to call this function.
3116 * @exception exception::NotEnoughMemoryException This exception is thrown when trying to decode from a buffer
3117 * position that exceeds the internal memory size.
3118 * @exception exception::BadParamException This exception is thrown when trying to decode an invalid value.
3119 */
3121 MemberId& member_id,
3122 Cdr::state& current_state);
3123
3124 /*!
3125 * @brief Encodes a short member header of a member according to XCDRv2.
3126 * @param[in] member_id Member identifier.
3127 * @pre Member identifier less than 0x10000000.
3128 * @exception exception::NotEnoughMemoryException This exception is thrown when trying to encode into a buffer
3129 * position that exceeds the internal memory size.
3130 */
3132 const MemberId& member_id);
3133
3134 /*!
3135 * @brief Finish the encoding of a short member header of a member according to XCDRv2.
3136 * @param[in] member_id Member identifier.
3137 * @pre Member identifier less than 0x10000000.
3138 * @param[in] member_serialized_size Size of the serialized member.
3139 * @pre Serialized size equal or less than 0x8.
3140 * @exception exception::NotEnoughMemoryException This exception is thrown when trying to encode into a buffer
3141 * position that exceeds the internal memory size.
3142 */
3144 const MemberId& member_id,
3145 size_t member_serialized_size);
3146
3147 /*!
3148 * @brief Encodes a long member header of a member according to XCDRv2.
3149 * @param[in] member_id Member identifier.
3150 * @pre Member identifier less than 0x10000000.
3151 * @exception exception::NotEnoughMemoryException This exception is thrown when trying to encode into a buffer
3152 * position that exceeds the internal memory size.
3153 */
3155 const MemberId& member_id);
3156
3157 /*!
3158 * @brief Finish the encoding of a long member header of a member according to XCDRv2.
3159 * @param[in] member_id Member identifier.
3160 * @pre Member identifier less than 0x10000000.
3161 * @param[in] member_serialized_size Size of the serialized member.
3162 * @exception exception::NotEnoughMemoryException This exception is thrown when trying to encode into a buffer
3163 * position that exceeds the internal memory size.
3164 */
3166 const MemberId& member_id,
3167 size_t member_serialized_size);
3168
3169 /*!
3170 * @brief Changes the previous encoded long header to a short header according to XCDRv2.
3171 * @param[in] member_id Member identifier.
3172 * @pre Member identifier less than 0x10000000.
3173 * @param[in] member_serialized_size Size of the serialized member.
3174 * @pre Serialized size equal or less than 8.
3175 * @exception exception::NotEnoughMemoryException This exception is thrown when trying to encode into a buffer
3176 * position that exceeds the internal memory size.
3177 */
3179 const MemberId& member_id,
3180 size_t member_serialized_size);
3181
3182 /*!
3183 * @brief Changes the previous encoded long header to a short header according to XCDRv2.
3184 * @param[in] member_id Member identifier.
3185 * @pre Member identifier less than 0x10000000.
3186 * @param[in] member_serialized_size Size of the serialized member.
3187 * @exception exception::NotEnoughMemoryException This exception is thrown when trying to encode into a buffer
3188 * position that exceeds the internal memory size.
3189 */
3191 const MemberId& member_id,
3192 size_t member_serialized_size);
3193
3194 /*!
3195 * @brief Join the previous encoded long header with the next DHEADER which was serialized after.
3196 * @param[in] member_id Member identifier.
3197 * @pre Member identifier less than 0x10000000.
3198 * @param[in] offset The last offset of the buffer previous to call this function.
3199 * @exception exception::NotEnoughMemoryException This exception is thrown when trying to encode into a buffer
3200 * position that exceeds the internal memory size.
3201 */
3203 const MemberId& member_id,
3204 const FastBuffer::iterator& offset);
3205
3206 /*!
3207 * @brief Decodes a member header according to XCDRv2.
3208 * @param[out] member_id Member identifier.
3209 * @param[in,out] current_state State of the encoder previous to call this function.
3210 * @exception exception::NotEnoughMemoryException This exception is thrown when trying to decode from a buffer
3211 * position that exceeds the internal memory size.
3212 * @exception exception::BadParamException This exception is thrown when trying to decode an invalid value.
3213 */
3215 MemberId& member_id,
3216 Cdr::state& current_state);
3217
3218 /*!
3219 * @brief Tells to the encoder a member starts to be encoded according to XCDRv1.
3220 * @param[in] member_id Member identifier.
3221 * @pre Member identifier cannot be MEMBER_ID_INVALID and next_member_id_ must be equal to the member identifier or
3222 * MEMBER_ID_INVALID.
3223 * @param[in] is_present If the member is present.
3224 * @pre When XCDRv1, is_present must be always true.
3225 * @param[in,out] current_state State of the encoder previous to call this function.
3226 * @pre Current encoding algorithm must be EncodingAlgorithmFlag::PLAIN_CDR or EncodingAlgorithmFlag::PL_CDR.
3227 * @param[in] header_selection Selects which member header will be used to allocate space.
3228 * @return Reference to the eprosima::fastcdr::Cdr object.
3229 * @exception exception::NotEnoughMemoryException This exception is thrown when trying to encode into a buffer
3230 * position that exceeds the internal memory size.
3231 * @exception exception::BadParamException This exception is thrown when trying to encode a long header when
3232 * header_selection is XCdrHeaderSelection::SHORT_HEADER.
3233 */
3235 const MemberId& member_id,
3236 bool is_present,
3237 Cdr::state& current_state,
3238 XCdrHeaderSelection header_selection);
3239
3240 /*!
3241 * @brief Tells to the encoder to finish the encoding of the member.
3242 * @param[in] current_state State of the encoder previous to call xcdr1_begin_serialize_member function.
3243 * @pre next_member_id_ cannot be MEMBER_ID_INVALID.
3244 * @return Reference to the eprosima::fastcdr::Cdr object.
3245 * @exception exception::NotEnoughMemoryException This exception is thrown when trying to encode into a buffer
3246 * position that exceeds the internal memory size.
3247 * @exception exception::BadParamException This exception is thrown when trying to encode a long header when
3248 * header_selection is XCdrHeaderSelection::SHORT_HEADER.
3249 */
3251 const Cdr::state& current_state);
3252
3253 /*!
3254 * @brief Tells to the encoder a member starts to be encoded according to XCDRv1.
3255 * @param[in] member_id Member identifier.
3256 * @pre Member identifier cannot be MEMBER_ID_INVALID and next_member_id_ must be equal to the member identifier or
3257 * MEMBER_ID_INVALID.
3258 * @param[in] is_present If the member is present.
3259 * @param[in,out] current_state State of the encoder previous to call this function.
3260 * @pre Current encoding algorithm must be EncodingAlgorithmFlag::PLAIN_CDR or EncodingAlgorithmFlag::PL_CDR.
3261 * @param[in] header_selection Selects which member header will be used to allocate space.
3262 * @return Reference to the eprosima::fastcdr::Cdr object.
3263 * @exception exception::NotEnoughMemoryException This exception is thrown when trying to encode into a buffer
3264 * position that exceeds the internal memory size.
3265 * @exception exception::BadParamException This exception is thrown when trying to encode a long header when
3266 * header_selection is XCdrHeaderSelection::SHORT_HEADER.
3267 */
3269 const MemberId& member_id,
3270 bool is_present,
3271 Cdr::state& current_state,
3272 XCdrHeaderSelection header_selection);
3273
3274 /*!
3275 * @brief Tells to the encoder to finish the encoding of the member.
3276 * @param[in] current_state State of the encoder previous to call xcdr1_begin_serialize_opt_member function.
3277 * @pre next_member_id_ cannot be MEMBER_ID_INVALID.
3278 * @return Reference to the eprosima::fastcdr::Cdr object.
3279 * @exception exception::NotEnoughMemoryException This exception is thrown when trying to encode into a buffer
3280 * position that exceeds the internal memory size.
3281 * @exception exception::BadParamException This exception is thrown when trying to encode a long header when
3282 * header_selection is XCdrHeaderSelection::SHORT_HEADER.
3283 */
3285 const Cdr::state& current_state);
3286
3287 /*!
3288 * @brief Tells to the encoder a member starts to be encoded according to XCDRv2.
3289 * @param[in] member_id Member identifier.
3290 * @pre Member identifier cannot be MEMBER_ID_INVALID and next_member_id_ must be equal to the member identifier or
3291 * MEMBER_ID_INVALID.
3292 * @param[in] is_present If the member is present.
3293 * @param[in,out] current_state State of the encoder previous to call this function.
3294 * @pre Current encoding algorithm must be EncodingAlgorithmFlag::PLAIN_CDR2, EncodingAlgorithmFlag::DELIMIT_CDR2 or
3295 * EncodingAlgorithmFlag::PL_CDR2.
3296 * @param[in] header_selection Selects which member header will be used to allocate space.
3297 * @return Reference to the eprosima::fastcdr::Cdr object.
3298 * @exception exception::NotEnoughMemoryException This exception is thrown when trying to encode into a buffer
3299 * position that exceeds the internal memory size.
3300 * @exception exception::BadParamException This exception is thrown when trying to encode member identifier equal or
3301 * greater than 0x10000000.
3302 */
3304 const MemberId& member_id,
3305 bool is_present,
3306 Cdr::state& current_state,
3307 XCdrHeaderSelection header_selection);
3308
3309 /*!
3310 * @brief Tells to the encoder to finish the encoding of the member.
3311 * @param[in] current_state State of the encoder previous to call xcdr2_begin_serialize_member function.
3312 * @pre next_member_id_ cannot be MEMBER_ID_INVALID.
3313 * @return Reference to the eprosima::fastcdr::Cdr object.
3314 * @exception exception::NotEnoughMemoryException This exception is thrown when trying to encode into a buffer
3315 * position that exceeds the internal memory size.
3316 * @exception exception::BadParamException This exception is thrown when trying to encode a long header when
3317 * header_selection is XCdrHeaderSelection::SHORT_HEADER.
3318 */
3320 const Cdr::state& current_state);
3321
3322 /*!
3323 * @brief Tells to the encoder a new type and its members start to be encoded according to XCDRv1.
3324 * @param[in,out] current_state State of the encoder previous to call this function.
3325 * @pre Current encoding algorithm must be EncodingAlgorithmFlag::PLAIN_CDR or EncodingAlgorithmFlag::PL_CDR.
3326 * @param[in] type_encoding Encoding algorithm used to encode the type and its members.
3327 * @pre Type encoding algorithm must be EncodingAlgorithmFlag::PLAIN_CDR or EncodingAlgorithmFlag::PL_CDR.
3328 * @pre If it is the beginning of the whole encoding, current encoding must be equal to type encoding.
3329 * @return Reference to the eprosima::fastcdr::Cdr object.
3330 */
3332 Cdr::state& current_state,
3333 EncodingAlgorithmFlag type_encoding) noexcept;
3334
3335 /*!
3336 * @brief Tells to the encoder to finish the encoding of the type.
3337 * @param[in] current_state State of the encoder previous to call xcdr1_begin_serialize_type function.
3338 * @pre Current encoding algorithm must be EncodingAlgorithmFlag::PLAIN_CDR or EncodingAlgorithmFlag::PL_CDR.
3339 * @return Reference to the eprosima::fastcdr::Cdr object.
3340 * @exception exception::NotEnoughMemoryException This exception is thrown when trying to encode into a buffer
3341 * position that exceeds the internal memory size.
3342 */
3344 const Cdr::state& current_state);
3345
3346 /*!
3347 * @brief Tells to the encoder a new type and its members start to be encoded according to XCDRv2.
3348 * @param[in,out] current_state State of the encoder previous to call this function.
3349 * @pre Current encoding algorithm must be EncodingAlgorithmFlag::PLAIN_CDR2, EncodingAlgorithmFlag::DELIMIT_CDR2 or
3350 * EncodingAlgorithmFlag::PL_CDR2.
3351 * @param[in] type_encoding Encoding algorithm used to encode the type and its members.
3352 * @pre Type encoding algorithm must be EncodingAlgorithmFlag::PLAIN_CDR2, EncodingAlgorithmFlag::DELIMIT_CDR2 or
3353 * EncodingAlgorithmFlag::PL_CDR2.
3354 * @pre If it is the beginning of the whole encoding, current encoding must be equal to type encoding.
3355 * @return Reference to the eprosima::fastcdr::Cdr object.
3356 * @exception exception::NotEnoughMemoryException This exception is thrown when trying to encode into a buffer
3357 * position that exceeds the internal memory size.
3358 */
3360 Cdr::state& current_state,
3361 EncodingAlgorithmFlag type_encoding);
3362
3363 /*!
3364 * @brief Tells to the encoder to finish the encoding of the type.
3365 * @param[in] current_state State of the encoder previous to call xcdr2_begin_serialize_type function.
3366 * @pre Current encoding algorithm must be EncodingAlgorithmFlag::PLAIN_CDR2, EncodingAlgorithmFlag::DELIMIT_CDR2 or
3367 * EncodingAlgorithmFlag::PL_CDR2.
3368 * @return Reference to the eprosima::fastcdr::Cdr object.
3369 * @exception exception::NotEnoughMemoryException This exception is thrown when trying to encode into a buffer
3370 * position that exceeds the internal memory size.
3371 */
3373 const Cdr::state& current_state);
3374
3375 /*!
3376 * @brief Tells to the encoder a new type and its members start to be decoded according to XCDRv1.
3377 * @param[in] type_encoding Encoding algorithm used to encode the type and its members.
3378 * @pre Type encoding algorithm must be EncodingAlgorithmFlag::PLAIN_CDR or EncodingAlgorithmFlag::PL_CDR.
3379 * @pre If it is the beginning of the whole encoding, current encoding must be equal to type encoding.
3380 * @param[in] functor Functor called each time a member has to be decoded.
3381 * @return Reference to the eprosima::fastcdr::Cdr object.
3382 * @exception exception::NotEnoughMemoryException This exception is thrown when trying to encode into a buffer
3383 * position that exceeds the internal memory size.
3384 * @exception exception::BadParamException This exception is thrown when an incorrect behaviour happens when
3385 * trying to decode.
3386 */
3388 EncodingAlgorithmFlag type_encoding,
3389 std::function<bool (Cdr&, const MemberId&)> functor);
3390
3391 /*!
3392 * @brief Tells to the encoder a new type and its members start to be decoded according to XCDRv2.
3393 * @param[in] type_encoding Encoding algorithm used to encode the type and its members.
3394 * @pre Type encoding algorithm must be EncodingAlgorithmFlag::PLAIN_CDR2, EncodingAlgorithmFlag::DELIMIT_CDR2 or
3395 * EncodingAlgorithmFlag::PL_CDR2.
3396 * @pre If it is the beginning of the whole encoding, current encoding must be equal to type encoding.
3397 * @param[in] functor Functor called each time a member has to be decoded.
3398 * @return Reference to the eprosima::fastcdr::Cdr object.
3399 * @exception exception::NotEnoughMemoryException This exception is thrown when trying to encode into a buffer
3400 * position that exceeds the internal memory size.
3401 * @exception exception::BadParamException This exception is thrown when an incorrect behaviour happens when
3402 * trying to decode.
3403 */
3405 EncodingAlgorithmFlag type_encoding,
3406 std::function<bool (Cdr&, const MemberId&)> functor);
3407
3409 const MemberId& member_id,
3410 bool is_present,
3411 Cdr::state& current_state,
3412 XCdrHeaderSelection header_selection);
3413
3415 const Cdr::state& current_state);
3416
3418 Cdr::state& current_state,
3419 EncodingAlgorithmFlag type_encoding);
3420
3422 const Cdr::state& current_state);
3423
3425 EncodingAlgorithmFlag type_encoding,
3426 std::function<bool (Cdr&, const MemberId&)> functor);
3427
3428 /*!
3429 * @brief Resets the internal callbacks depending on the current selected Cdr version.
3430 */
3431 void reset_callbacks();
3432
3434 const MemberId&,
3435 bool,
3436 Cdr::state&,
3439
3441 const Cdr::state&);
3443
3445 const MemberId&,
3446 bool,
3447 Cdr::state&,
3450
3452 const Cdr::state&);
3454
3456 Cdr::state&,
3459
3461 const Cdr::state&);
3463
3466 std::function<bool (Cdr&, const MemberId&)>);
3468
3469 //! @brief Reference to the buffer that will be serialized/deserialized.
3471
3472 //! @brief The type of CDR that will be use in serialization/deserialization.
3474
3475 //! @brief Stores the main encoding algorithm.
3477
3478 //! @brief Stores the current encoding algorithm.
3480
3481 //! @brief This attribute stores the option flags when the CDR type is DDS_CDR;
3483
3484 //! @brief The endianness that will be applied over the buffer.
3486
3487 //! @brief This attribute specifies if it is needed to swap the bytes.
3488 bool swap_bytes_ {false};
3489
3490 //! @brief Stores the last datasize serialized/deserialized. It's used to optimize.
3492
3493 //! @brief The current position in the serialization/deserialization process.
3495
3496 //! @brief The position from where the alignment is calculated.
3498
3499 //! @brief The last position in the buffer;
3501
3502 //! Next member identifier to be processed.
3504
3505 //! Align for types equal or greater than 64bits.
3506 size_t align64_ {4};
3507
3508 /*!
3509 * When serializing a member's type using XCDRv2, this enumerator is used to inform the type was serialized with a
3510 * DHEADER and the algorithm could optimize the XCDRv2 member header.
3511 */
3513 {
3514 NO_SERIALIZED_MEMBER_SIZE, //! Default. No serialized member size in a DHEADER.
3515 SERIALIZED_MEMBER_SIZE, //! Serialized member size in a DHEADER.
3516 SERIALIZED_MEMBER_SIZE_4, //! Serialized member size (which is a multiple of 4) in a DHEADER.
3517 SERIALIZED_MEMBER_SIZE_8 //! Serialized member size (which is a multiple of 8) in a DHEADER.
3519 //! Specifies if a DHEADER was serialized. Used to optimize XCDRv2 member headers.
3520 serialized_member_size_ {NO_SERIALIZED_MEMBER_SIZE};
3521
3522 //! Stores the initial state.
3524
3525 //! Whether the encapsulation was serialized.
3527
3528
3529 uint32_t get_long_lc(
3530 SerializedMemberSizeForNextInt serialized_member_size);
3531
3532 uint32_t get_short_lc(
3533 size_t member_serialized_size);
3534
3535 template<class _T, typename std::enable_if<std::is_enum<_T>::value ||
3536 std::is_arithmetic<_T>::value>::type* = nullptr>
3538 {
3539 return (1 == sizeof(_T) ? SERIALIZED_MEMBER_SIZE :
3540 (4 == sizeof(_T) ? SERIALIZED_MEMBER_SIZE_4 :
3542 }
3543
3544};
3545
3546} //namespace fastcdr
3547} //namespace eprosima
3548
3549#endif // _CDR_CDR_H_
T assign(T... args)
T begin(T... args)
T c_str(T... args)
This class stores the current state of a CDR serialization.
Definition Cdr.h:106
const FastBuffer::iterator origin_
The position from the alignment is calculated, when the state was created.
Definition Cdr.h:133
size_t last_data_size_
Stores the last datasize serialized/deserialized when the state was created.
Definition Cdr.h:139
const FastBuffer::iterator offset_
The position in the buffer when the state was created.
Definition Cdr.h:130
EncodingAlgorithmFlag previous_encoding_
Not related with the state. Used by encoding algorithms to store the previous encoding algorithm.
Definition Cdr.h:154
uint32_t member_size_
Not related with the state. Used by encoding algorithms to set the encoded member size.
Definition Cdr.h:145
state & operator=(const state &state)=delete
bool swap_bytes_
This attribute specifies if it is needed to swap the bytes when the state is created.
Definition Cdr.h:136
XCdrHeaderSelection header_selection_
Not related with the state. Used by encoding algorithms to store the selected member header version.
Definition Cdr.h:148
MemberId next_member_id_
Not related with the state. Next member id which will be encoded.
Definition Cdr.h:142
Cdr_DllAPI bool operator==(const state &other_state) const
Compares two states.
Definition Cdr.cpp:120
XCdrHeaderSelection header_serialized_
Not related with the state. Used by encoding algorithms to store the allocated member header version.
Definition Cdr.h:151
This class offers an interface to serialize/deserialize some basic types using CDR protocol inside an...
Definition Cdr.h:69
TEMPLATE_SPEC Cdr & serialize(const std::vector< bool > &vector_t)
This function template serializes a sequence of booleans.
Definition Cdr.h:826
uint8_t endianness_
The endianness that will be applied over the buffer.
Definition Cdr.h:3485
Cdr & xcdr2_end_serialize_type(const Cdr::state &current_state)
Tells to the encoder to finish the encoding of the type.
Definition Cdr.cpp:3285
end_serialize_member_functor end_serialize_member_
Definition Cdr.h:3442
Cdr & deserialize(std::vector< _T > &vector_t)
This function template deserializes a sequence of non-primitive.
Definition Cdr.h:1844
Cdr & xcdr2_deserialize_type(EncodingAlgorithmFlag type_encoding, std::function< bool(Cdr &, const MemberId &)> functor)
Tells to the encoder a new type and its members start to be decoded according to XCDRv2.
Definition Cdr.cpp:3355
void reset_alignment()
This function resets the alignment to the current position in the buffer.
Definition Cdr.h:306
Cdr_DllAPI std::array< uint8_t, 2 > get_dds_cdr_options() const
This function returns the option flags when the CDR type is eprosima::fastcdr::DDS_CDR.
Definition Cdr.cpp:375
Cdr & xcdr1_end_serialize_member(const Cdr::state &current_state)
Tells to the encoder to finish the encoding of the member.
Definition Cdr.cpp:2847
TEMPLATE_SPEC Cdr & deserialize(std::string &string_t)
This function deserializes a std::string.
Definition Cdr.h:1759
Cdr & serialize_sequence(const _T *sequence_t, size_t num_elements, Endianness endianness)
This function template serializes a raw sequence with a different endianness.
Definition Cdr.h:1383
TEMPLATE_SPEC Cdr & serialize(const std::string &string_t)
This function serializes a std::string.
Definition Cdr.h:689
void xcdr2_change_to_long_member_header(const MemberId &member_id, size_t member_serialized_size)
Changes the previous encoded long header to a short header according to XCDRv2.
Definition Cdr.cpp:2759
Cdr & serialize(const _T &value)
Encodes the value of a type into the buffer.
Definition Cdr.h:381
TEMPLATE_SPEC Cdr & deserialize_array(std::vector< bool > &value)
Decodes an array of booleans on a std::vector.
Definition Cdr.h:2458
void xcdr2_end_short_member_header(const MemberId &member_id, size_t member_serialized_size)
Finish the encoding of a short member header of a member according to XCDRv2.
Definition Cdr.cpp:2707
Cdr_DllAPI bool set_encoding_flag(EncodingAlgorithmFlag encoding_flag)
Sets the EncodingAlgorithmFlag for the encapsulation when the CDR type is CdrVersion::DDS_CDR,...
Definition Cdr.cpp:358
Cdr_DllAPI bool move_alignment_forward(size_t num_bytes)
This function moves the alignment forward.
Definition Cdr.cpp:478
Cdr & deserialize_sequence(_T *&sequence_t, size_t &num_elements)
This function template deserializes a raw sequence of non-primitives. This function allocates memory ...
Definition Cdr.h:2477
std::array< uint8_t, 2 > options_
This attribute stores the option flags when the CDR type is DDS_CDR;.
Definition Cdr.h:3482
static size_t alignment(size_t current_alignment, size_t data_size)
Returns the number of bytes needed to align a position to certain data size.
Definition Cdr.h:275
EncodingAlgorithmFlag encoding_flag_
Stores the main encoding algorithm.
Definition Cdr.h:3476
Cdr & serialize(const std::map< _K, _T > &map_t)
This function template serializes a map of non-primitive.
Definition Cdr.h:840
TEMPLATE_SPEC Cdr & serialize_array(const uint16_t *ushort_t, size_t num_elements)
This function serializes an array of unsigned shorts.
Definition Cdr.h:1038
size_t alignment(size_t data_size) const
Returns the number of bytes needed to align the current position (having as reference the origin) to ...
Definition Cdr.h:3012
Cdr_DllAPI Cdr & deserialize_bool_array(std::vector< bool > &vector_t)
Definition Cdr.cpp:2344
Cdr_DllAPI Cdr & deserialize_bool_sequence(std::vector< bool > &vector_t)
Definition Cdr.cpp:2384
Cdr &(Cdr::*)(const MemberId &, bool, Cdr::state &, XCdrHeaderSelection) begin_serialize_member_functor
Definition Cdr.h:3437
TEMPLATE_SPEC Cdr & serialize_array(const uint64_t *ulonglong_t, size_t num_elements)
This function serializes an array of unsigned long longs.
Definition Cdr.h:1101
Cdr_DllAPI const char * read_string(uint32_t &length)
Definition Cdr.cpp:1735
Cdr_DllAPI const std::wstring read_wstring(uint32_t &length)
Definition Cdr.cpp:1766
Cdr &(Cdr::*)(EncodingAlgorithmFlag, std::function< bool(Cdr &, const MemberId &)>) deserialize_type_functor
Definition Cdr.h:3466
Cdr &(Cdr::*)(const Cdr::state &) end_serialize_type_functor
Definition Cdr.h:3461
Cdr & deserialize(optional< external< _T > > &value)
Decodes an optional of an external from the buffer.
Definition Cdr.h:2894
Cdr & serialize(const _T &value, Endianness endianness)
Encodes the value of a type with a different endianness.
Definition Cdr.h:397
TEMPLATE_SPEC Cdr & serialize_array(const uint8_t *octet_t, size_t num_elements)
This function serializes an array of octets.
Definition Cdr.h:997
Cdr & cdr_deserialize_type(EncodingAlgorithmFlag type_encoding, std::function< bool(Cdr &, const MemberId &)> functor)
Definition Cdr.cpp:3480
Cdr_DllAPI state allocate_xcdrv2_dheader()
Encodes an empty DHEADER if the encoding version is XCDRv2. After serializing the members's type,...
Definition Cdr.cpp:3499
TEMPLATE_SPEC Cdr & serialize(const std::wstring &string_t)
This function serializes a std::wstring.
Definition Cdr.h:710
@ AUTO_WITH_SHORT_HEADER_BY_DEFAULT
Initially a short member header is allocated but can be changed to the longer version.
Definition Cdr.h:97
@ LONG_HEADER
Initially a long member header is allocated and cannot be changed.
Definition Cdr.h:95
@ SHORT_HEADER
Initially a short member header is allocated and cannot be changed. This option may cause an exceptio...
Definition Cdr.h:93
@ AUTO_WITH_LONG_HEADER_BY_DEFAULT
Initially a long member header is allocated but can be changed to the shorter version.
Definition Cdr.h:99
Cdr & serialize_sequence(const _T *sequence_t, size_t num_elements)
This function template serializes a raw sequence of non-primitives.
Definition Cdr.h:1316
size_t align64_
Align for types equal or greater than 64bits.
Definition Cdr.h:3506
TEMPLATE_SPEC Cdr & deserialize(std::wstring &string_t)
This function deserializes a std::wstring.
Definition Cdr.h:1775
Cdr & serialize(const optional< _T > &value)
Encodes an optional in the buffer.
Definition Cdr.h:2789
Cdr & operator=(const Cdr &)=delete
TEMPLATE_SPEC Cdr & deserialize(uint8_t &octet_t)
This function deserializes an octet.
Definition Cdr.h:1582
Cdr & deserialize_array(std::array< _T, _Size > *array_t, size_t num_elements, Endianness endianness)
This function template detects the content type of STD container array and deserializes the array wit...
Definition Cdr.h:2998
Cdr_DllAPI char * get_buffer_pointer()
This function returns the pointer to the current used buffer.
Definition Cdr.cpp:435
Cdr(const Cdr &)=delete
Cdr & serialize_member(const MemberId &member_id, const optional< _T > &member_value, XCdrHeaderSelection header_selection=XCdrHeaderSelection::AUTO_WITH_SHORT_HEADER_BY_DEFAULT)
Encodes an optional member of a type according to the encoding algorithm used.
Definition Cdr.h:2688
Cdr &(Cdr::*)(Cdr::state &, EncodingAlgorithmFlag) begin_serialize_type_functor
Definition Cdr.h:3457
Cdr & xcdr2_begin_serialize_member(const MemberId &member_id, bool is_present, Cdr::state &current_state, XCdrHeaderSelection header_selection)
Tells to the encoder a member starts to be encoded according to XCDRv2.
Definition Cdr.cpp:3041
Cdr_DllAPI Cdr & begin_serialize_type(Cdr::state &current_state, EncodingAlgorithmFlag type_encoding)
Tells to the encoder a new type and its members starts to be encoded.
Definition Cdr.cpp:2234
end_serialize_member_functor end_serialize_opt_member_
Definition Cdr.h:3453
Cdr & serialize_array(const std::vector< _T > &value)
Encodes an std::vector of primitives as an array.
Definition Cdr.h:1231
Cdr & serialize_member(const MemberId &member_id, const _T &member_value, XCdrHeaderSelection header_selection=XCdrHeaderSelection::AUTO_WITH_SHORT_HEADER_BY_DEFAULT)
XCDR extensions.
Definition Cdr.h:2666
Cdr & cdr_end_serialize_member(const Cdr::state &current_state)
Definition Cdr.cpp:3455
Cdr_DllAPI Cdr & serialize_encapsulation()
This function writes the encapsulation of the CDR stream. If the CDR stream should contain an encapsu...
Definition Cdr.cpp:302
bool resize(size_t min_size_inc)
This function resizes the internal buffer. It only applies if the FastBuffer object was created with ...
Definition Cdr.cpp:493
TEMPLATE_SPEC Cdr & serialize_array(const int8_t *int8, size_t num_elements)
This function serializes an array of int8_t.
Definition Cdr.h:1023
@ SERIALIZED_MEMBER_SIZE_4
Serialized member size in a DHEADER.
Definition Cdr.h:3516
@ SERIALIZED_MEMBER_SIZE
Default. No serialized member size in a DHEADER.
Definition Cdr.h:3515
@ SERIALIZED_MEMBER_SIZE_8
Serialized member size (which is a multiple of 4) in a DHEADER.
Definition Cdr.h:3517
Cdr & xcdr1_begin_serialize_opt_member(const MemberId &member_id, bool is_present, Cdr::state &current_state, XCdrHeaderSelection header_selection)
Tells to the encoder a member starts to be encoded according to XCDRv1.
Definition Cdr.cpp:2917
FastBuffer::iterator end_
The last position in the buffer;.
Definition Cdr.h:3500
void xcdr2_deserialize_member_header(MemberId &member_id, Cdr::state &current_state)
Decodes a member header according to XCDRv2.
Definition Cdr.cpp:3181
Cdr & xcdr1_deserialize_type(EncodingAlgorithmFlag type_encoding, std::function< bool(Cdr &, const MemberId &)> functor)
Tells to the encoder a new type and its members start to be decoded according to XCDRv1.
Definition Cdr.cpp:3304
TEMPLATE_SPEC Cdr & deserialize_array(uint64_t *ulonglong_t, size_t num_elements)
This function deserializes an array of unsigned long longs.
Definition Cdr.h:2289
TEMPLATE_SPEC Cdr & deserialize(uint16_t &ushort_t)
This function deserializes an unsigned short.
Definition Cdr.h:1617
static Cdr_DllAPI const Endianness DEFAULT_ENDIAN
Default endianess in the system.
Definition Cdr.h:84
Cdr & xcdr1_begin_serialize_type(Cdr::state &current_state, EncodingAlgorithmFlag type_encoding) noexcept
Tells to the encoder a new type and its members start to be encoded according to XCDRv1.
Definition Cdr.cpp:3231
void xcdr1_end_short_member_header(const MemberId &member_id, size_t member_serialized_size)
Finish the encoding of a short member header of a member according to XCDRv1.
Definition Cdr.cpp:2570
void xcdr2_serialize_long_member_header(const MemberId &member_id)
Encodes a long member header of a member according to XCDRv2.
Definition Cdr.cpp:2719
begin_serialize_member_functor begin_serialize_member_
Definition Cdr.h:3438
void reset_callbacks()
Resets the internal callbacks depending on the current selected Cdr version.
Definition Cdr.cpp:164
Cdr & deserialize_sequence(_T *&sequence_t, size_t &num_elements, Endianness endianness)
This function template deserializes a raw sequence with a different endianness. This function allocat...
Definition Cdr.h:2594
constexpr SerializedMemberSizeForNextInt get_serialized_member_size() const
Definition Cdr.h:3537
Cdr & serialize_array(const fixed_string< MAX_CHARS > *value, size_t num_elements)
Encodes an array of fixed strings.
Definition Cdr.h:1211
Cdr & xcdr1_begin_serialize_member(const MemberId &member_id, bool is_present, Cdr::state &current_state, XCdrHeaderSelection header_selection)
Tells to the encoder a member starts to be encoded according to XCDRv1.
Definition Cdr.cpp:2792
void xcdr1_change_to_short_member_header(const MemberId &member_id, size_t member_serialized_size)
Changes the previous encoded long header to a short header according to XCDRv1.
Definition Cdr.cpp:2609
FastBuffer & cdr_buffer_
Reference to the buffer that will be serialized/deserialized.
Definition Cdr.h:3470
begin_serialize_opt_member_functor begin_serialize_opt_member_
Definition Cdr.h:3449
TEMPLATE_SPEC Cdr & deserialize(uint32_t &ulong_t)
This function deserializes an unsigned long.
Definition Cdr.h:1639
void xcdr1_serialize_short_member_header(const MemberId &member_id)
XCDR extensions.
Definition Cdr.cpp:2555
Cdr & cdr_begin_serialize_type(Cdr::state &current_state, EncodingAlgorithmFlag type_encoding)
Definition Cdr.cpp:3462
Cdr & serialize_array(const _T *value, size_t num_elements)
Encodes an array of a type not managed by this encoder into the buffer.
Definition Cdr.h:946
Cdr & deserialize(std::array< _T, _Size > &array_t)
This function template deserializes an array.
Definition Cdr.h:1807
Cdr & xcdr1_end_serialize_opt_member(const Cdr::state &current_state)
Tells to the encoder to finish the encoding of the member.
Definition Cdr.cpp:2971
Cdr_DllAPI bool jump(size_t num_bytes)
This function skips a number of bytes in the CDR stream buffer.
Definition Cdr.cpp:420
state initial_state_
Stores the initial state.
Definition Cdr.h:3523
TEMPLATE_SPEC Cdr & deserialize(uint64_t &ulonglong_t)
This function deserializes an unsigned long long.
Definition Cdr.h:1677
FastBuffer::iterator origin_
The position from where the alignment is calculated.
Definition Cdr.h:3497
Cdr_DllAPI void set_dds_cdr_options(const std::array< uint8_t, 2 > &options)
This function sets the option flags when the CDR type is eprosima::fastcdr::DDS_CDR.
Definition Cdr.cpp:380
uint32_t get_long_lc(SerializedMemberSizeForNextInt serialized_member_size)
Definition Cdr.cpp:50
size_t last_data_size_
Stores the last datasize serialized/deserialized. It's used to optimize.
Definition Cdr.h:3491
Cdr & deserialize_array(_T *value, size_t num_elements)
Decodes an array of a type not managed by this encoder from the buffer.
Definition Cdr.h:2134
Cdr & serialize(const std::array< _T, _Size > &array_t)
This function template serializes an array.
Definition Cdr.h:737
TEMPLATE_SPEC Cdr & deserialize(int8_t &int8)
This function deserializes an int8_t.
Definition Cdr.h:1604
TEMPLATE_SPEC Cdr & deserialize(wchar_t &wchar)
This function deserializes a wide-char.
Definition Cdr.h:1661
TEMPLATE_SPEC Cdr & serialize_array(const std::vector< bool > &value)
Encodes an std::vector of booleans as an array.
Definition Cdr.h:1299
Cdr & deserialize_array(std::array< _T, _Size > *array_t, size_t num_elements)
This function template detects the content type of the STD container array and deserializes the array...
Definition Cdr.h:2982
Cdr &(Cdr::*)(const Cdr::state &) end_serialize_member_functor
Definition Cdr.h:3441
Cdr & deserialize_array(std::vector< _T > &value)
Decodes an array of primitives on a std::vector.
Definition Cdr.h:2364
begin_serialize_type_functor begin_serialize_type_
Definition Cdr.h:3458
Cdr & deserialize(_T &value)
Decodes the value of a type from the buffer.
Definition Cdr.h:1417
TEMPLATE_SPEC Cdr & deserialize_array(int8_t *int8, size_t num_elements)
This function deserializes an array of int8_t.
Definition Cdr.h:2211
bool encapsulation_serialized_
Whether the encapsulation was serialized.
Definition Cdr.h:3526
Cdr_DllAPI Cdr & serialize_bool_array(const std::vector< bool > &vector_t)
Definition Cdr.cpp:2266
Cdr & cdr_end_serialize_type(const Cdr::state &current_state)
Definition Cdr.cpp:3473
Cdr_DllAPI Cdr & deserialize_type(EncodingAlgorithmFlag type_encoding, std::function< bool(Cdr &, const MemberId &)> functor)
Tells to the encoder a new type and its members starts to be decoded.
Definition Cdr.cpp:2247
Cdr & deserialize(optional< _T > &value)
Decodes an optional from the buffer.
Definition Cdr.h:2843
Cdr & deserialize_array(_T *type_t, size_t num_elements, Endianness endianness)
This function template deserializes an array of non-basic objects with a different endianness.
Definition Cdr.h:2154
Cdr_DllAPI char * get_current_position()
This function returns the current position in the CDR stream.
Definition Cdr.cpp:440
Cdr_DllAPI EncodingAlgorithmFlag get_encoding_flag() const
Returns the EncodingAlgorithmFlag set in the encapsulation when the CDR type is CdrVersion::DDS_CDR,...
Definition Cdr.cpp:353
Cdr & deserialize_member(optional< _T > &member_value)
Decodes an optional member of a type according to the encoding algorithm used.
Definition Cdr.h:2721
Cdr_DllAPI CdrVersion get_cdr_version() const
Retrieves the CdrVersion used by the instance.
Definition Cdr.cpp:348
Cdr & deserialize(std::map< _K, _T > &map_t)
This function template deserializes a map of non-primitive.
Definition Cdr.h:1978
Cdr_DllAPI void change_endianness(Endianness endianness)
This function sets the current endianness used by the CDR type.
Definition Cdr.cpp:405
Cdr & xcdr2_begin_serialize_type(Cdr::state &current_state, EncodingAlgorithmFlag type_encoding)
Tells to the encoder a new type and its members start to be encoded according to XCDRv2.
Definition Cdr.cpp:3263
void xcdr1_end_long_member_header(const MemberId &member_id, size_t member_serialized_size)
Finish the encoding of a long member header of a member according to XCDRv1.
Definition Cdr.cpp:2600
Cdr & serialize(const external< _T > &value)
Encodes an external in the buffer.
Definition Cdr.h:2813
Cdr_DllAPI void reset()
This function resets the current position in the buffer to the beginning.
Definition Cdr.cpp:465
TEMPLATE_SPEC Cdr & serialize_array(const uint32_t *ulong_t, size_t num_elements)
This function serializes an array of unsigned longs.
Definition Cdr.h:1064
void xcdr2_shrink_to_long_member_header(const MemberId &member_id, const FastBuffer::iterator &offset)
Join the previous encoded long header with the next DHEADER which was serialized after.
Definition Cdr.cpp:2780
TEMPLATE_SPEC Cdr & deserialize_sequence(std::wstring *&sequence_t, size_t &num_elements)
This function template deserializes a wide-string sequence. This function allocates memory to store t...
Definition Cdr.h:2644
TEMPLATE_SPEC Cdr & deserialize_array(uint16_t *ushort_t, size_t num_elements)
This function deserializes an array of unsigned shorts.
Definition Cdr.h:2226
Cdr &(Cdr::*)(const MemberId &, bool, Cdr::state &, XCdrHeaderSelection) begin_serialize_opt_member_functor
Definition Cdr.h:3448
Cdr & operator>>(_T &value)
Decodes the value from the buffer.
Definition Cdr.h:356
Cdr &(Cdr::*)(const Cdr::state &) end_serialize_memberopt__functor
Definition Cdr.h:3452
Cdr_DllAPI Cdr & deserialize_wstring_sequence(std::wstring *&sequence_t, size_t &num_elements)
Definition Cdr.cpp:2490
Cdr & serialize(const std::vector< _T > &vector_t)
This function template serializes a sequence of non-primitive.
Definition Cdr.h:764
Endianness
This enumeration represents endianness types.
Definition Cdr.h:76
@ LITTLE_ENDIANNESS
Little endianness.
Definition Cdr.h:80
@ BIG_ENDIANNESS
Big endianness.
Definition Cdr.h:78
Cdr & xcdr1_end_serialize_type(const Cdr::state &current_state)
Tells to the encoder to finish the encoding of the type.
Definition Cdr.cpp:3245
uint32_t get_short_lc(size_t member_serialized_size)
Definition Cdr.cpp:73
TEMPLATE_SPEC Cdr & deserialize(std::vector< bool > &vector_t)
This function template deserializes a sequence.
Definition Cdr.h:1964
enum eprosima::fastcdr::Cdr::SerializedMemberSizeForNextInt NO_SERIALIZED_MEMBER_SIZE
Specifies if a DHEADER was serialized. Used to optimize XCDRv2 member headers.
CdrVersion cdr_version_
The type of CDR that will be use in serialization/deserialization.
Definition Cdr.h:3473
void xcdr1_serialize_long_member_header(const MemberId &member_id)
Encodes a long member header of a member according to XCDRv1.
Definition Cdr.cpp:2583
Cdr & xcdr2_end_serialize_member(const Cdr::state &current_state)
Tells to the encoder to finish the encoding of the member.
Definition Cdr.cpp:3084
Cdr_DllAPI void set_state(const state &state)
Sets a previous state of the CDR serialization process;.
Definition Cdr.cpp:455
void xcdr2_end_long_member_header(const MemberId &member_id, size_t member_serialized_size)
Finish the encoding of a long member header of a member according to XCDRv2.
Definition Cdr.cpp:2730
Cdr & operator<<(const _T &value)
Encodes the value into the buffer.
Definition Cdr.h:326
MemberId next_member_id_
Next member identifier to be processed.
Definition Cdr.h:3503
Cdr & deserialize(external< _T > &value)
Decodes an external from the buffer.
Definition Cdr.h:2868
Cdr & serialize_array(const std::array< _T, _Size > *array_t, size_t num_elements)
This function template detects the content type of the STD container array and serializes the array.
Definition Cdr.h:2967
Cdr_DllAPI Cdr & read_encapsulation()
This function reads the encapsulation of the CDR stream. If the CDR stream contains an encapsulation,...
Definition Cdr.cpp:198
Cdr & serialize_array(const _T *type_t, size_t num_elements, Endianness endianness)
This function template serializes an array of non-basic objects with a different endianness.
Definition Cdr.h:966
TEMPLATE_SPEC Cdr & deserialize_sequence(std::string *&sequence_t, size_t &num_elements)
This function template deserializes a string sequence. This function allocates memory to store the se...
Definition Cdr.h:2627
deserialize_type_functor deserialize_type_
Definition Cdr.h:3467
FastBuffer::iterator offset_
The current position in the serialization/deserialization process.
Definition Cdr.h:3494
Cdr_DllAPI bool xcdr1_deserialize_member_header(MemberId &member_id, Cdr::state &current_state)
Decodes a member header according to XCDRv1.
Definition Cdr.cpp:2647
TEMPLATE_SPEC Cdr & serialize_array(const std::wstring *string_t, size_t num_elements)
This function serializes an array of wide-strings.
Definition Cdr.h:1191
Cdr_DllAPI Cdr & serialize_bool_sequence(const std::vector< bool > &vector_t)
Definition Cdr.cpp:2304
bool swap_bytes_
This attribute specifies if it is needed to swap the bytes.
Definition Cdr.h:3488
EncodingAlgorithmFlag current_encoding_
Stores the current encoding algorithm.
Definition Cdr.h:3479
Cdr & serialize(const fixed_string< MAX_CHARS > &value)
Encodes a eprosima::fastcdr::fixed_string in the buffer.
Definition Cdr.h:724
Cdr_DllAPI size_t get_serialized_data_length() const
This function returns the length of the serialized data inside the stream.
Definition Cdr.cpp:445
TEMPLATE_SPEC Cdr & serialize_array(const std::string *string_t, size_t num_elements)
This function serializes an array of strings.
Definition Cdr.h:1172
Cdr_DllAPI void set_xcdrv2_dheader(const state &state)
Uses the state to calculate the member's type size and serialize the value in the previous allocated ...
Definition Cdr.cpp:3513
TEMPLATE_SPEC Cdr & deserialize_array(uint8_t *octet_t, size_t num_elements)
This function deserializes an array of octets.
Definition Cdr.h:2185
void xcdr2_serialize_short_member_header(const MemberId &member_id)
Encodes a short member header of a member according to XCDRv2.
Definition Cdr.cpp:2698
Cdr & deserialize_array(std::vector< _T > &value, Endianness endianness)
Decodes an array of non-primitives on a std::vector with a different endianness.
Definition Cdr.h:2425
Cdr_DllAPI Cdr & deserialize_string_sequence(std::string *&sequence_t, size_t &num_elements)
Definition Cdr.cpp:2428
Cdr & serialize_array(const std::vector< _T > &value, Endianness endianness)
Encodes an std::vector as an array with a different endianness.
Definition Cdr.h:1269
void make_alignment(size_t align)
This function jumps the number of bytes of the alignment. These bytes should be calculated with the f...
Definition Cdr.h:3022
Cdr & cdr_begin_serialize_member(const MemberId &member_id, bool is_present, Cdr::state &current_state, XCdrHeaderSelection header_selection)
Definition Cdr.cpp:3445
TEMPLATE_SPEC Cdr & deserialize_array(uint32_t *ulong_t, size_t num_elements)
This function deserializes an array of unsigned longs.
Definition Cdr.h:2252
Cdr & deserialize(fixed_string< MAX_CHARS > &value)
Decodes a fixed string.
Definition Cdr.h:1791
void xcdr1_change_to_long_member_header(const MemberId &member_id, size_t member_serialized_size)
Changes the previous encoded short header to a long header according to XCDRv1.
Definition Cdr.cpp:2624
void xcdr2_change_to_short_member_header(const MemberId &member_id, size_t member_serialized_size)
Changes the previous encoded long header to a short header according to XCDRv2.
Definition Cdr.cpp:2746
Cdr_DllAPI Endianness endianness() const
This function returns the current endianness used by the CDR type.
Definition Cdr.cpp:415
Cdr & deserialize(_T &value, Endianness endianness)
Decodes the value of a type with a different endianness.
Definition Cdr.h:1433
Cdr & deserialize_member(_T &member_value)
Decodes a member of a type according to the encoding algorithm used.
Definition Cdr.h:2707
Cdr_DllAPI Cdr & end_serialize_type(Cdr::state &current_state)
Tells to the encoder the encoding of the type finishes.
Definition Cdr.cpp:2241
Cdr_DllAPI state get_state() const
Returns the current state of the CDR serialization process.
Definition Cdr.cpp:450
end_serialize_type_functor end_serialize_type_
Definition Cdr.h:3462
This class represents a stream of bytes that contains (or will contain) serialized data....
Definition FastBuffer.h:244
This class implements the iterator used to go through a FastBuffer.
Definition FastBuffer.h:43
This class is thrown as an exception when an invalid parameter is being serialized.
This abstract class is used to create exceptions.
Definition Exception.h:30
virtual Cdr_DllAPI void raise() const =0
This function throws the object as exception.
This class is thrown as an exception when the buffer's internal memory reachs its size limit.
static Cdr_DllAPI const char *const NOT_ENOUGH_MEMORY_MESSAGE_DEFAULT
Default message used in the library.
This class template manages an external member, a member declared to be external to the storage of a ...
Definition external.hpp:30
bool is_locked() const noexcept
Checks if locked.
Definition external.hpp:168
This class template manages an optional contained value, i.e. a value that may or may not be present.
Definition optional.hpp:47
void reset(bool initial_engaged=false)
Reset the state of the optional.
Definition optional.hpp:109
bool has_value() const
Checks whether the optional contains a value.
Definition optional.hpp:196
T clear(T... args)
T data(T... args)
T emplace(T... args)
T end(T... args)
#define Cdr_DllAPI
Definition fastcdr_dll.h:51
void serialize(Cdr &, const _T &)
EncodingAlgorithmFlag
This enumeration represents the supported XCDR encoding algorithms.
@ PLAIN_CDR
Specifies that the content is PLAIN_CDR.
@ PL_CDR2
Specifies that the content is PL_CDR2.
@ PLAIN_CDR2
Specifies that the content is PLAIN_CDR2.
constexpr bool is_multi_array_primitive(...)
Basis.
static const MemberId MEMBER_ID_INVALID
Definition MemberId.hpp:67
void deserialize(Cdr &, _T &)
CdrVersion
This enumeration represents the kinds of CDR serialization supported by eprosima::fastcdr::CDR.
@ XCDRv2
XCDRv2 encoding defined by standard DDS X-Types 1.3.
Definition Cdr.h:49
T resize(T... args)
T size(T... args)
Template class for non-alloc strings.
fixed_string & assign(const char *c_array, size_t n_chars) noexcept
Assigns from a char array.
const char * c_str() const noexcept
Converts to C string.