Coverage Report

Created: 2025-06-13 06:46

/src/Fast-CDR/include/fastcdr/Cdr.h
Line
Count
Source (jump to first uncovered line)
1
// Copyright 2016 Proyectos y Sistemas de Mantenimiento SL (eProsima).
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
//     http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14
15
#ifndef _FASTCDR_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"
33
#include "cdr/fixed_size_string.hpp"
34
#include "detail/container_recursive_inspector.hpp"
35
#include "exceptions/BadParamException.h"
36
#include "exceptions/Exception.h"
37
#include "exceptions/NotEnoughMemoryException.h"
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
49
namespace eprosima {
50
namespace fastcdr {
51
52
class Cdr;
53
54
template<class _T>
55
extern void serialize(
56
        Cdr&,
57
        const _T&);
58
59
template<class _T>
60
extern 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
 */
68
class Cdr
69
{
70
public:
71
72
    /*!
73
     * @brief This enumeration represents endianness types.
74
     */
75
    typedef enum : uint8_t
76
    {
77
        //! @brief Big endianness.
78
        BIG_ENDIANNESS = 0x0,
79
        //! @brief Little endianness.
80
        LITTLE_ENDIANNESS = 0x1
81
    } Endianness;
82
83
    //! Default endianess in the system.
84
    Cdr_DllAPI static const Endianness DEFAULT_ENDIAN;
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.
93
        SHORT_HEADER,
94
        //! Initially a long member header is allocated and cannot be changed.
95
        LONG_HEADER,
96
        //! Initially a short member header is allocated but can be changed to the longer version.
97
        AUTO_WITH_SHORT_HEADER_BY_DEFAULT,
98
        //! Initially a long member header is allocated but can be changed to the shorter version.
99
        AUTO_WITH_LONG_HEADER_BY_DEFAULT
100
    } XCdrHeaderSelection;
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.
112
        Cdr_DllAPI state(
113
                const Cdr& cdr);
114
115
        //! Copy constructor.
116
        Cdr_DllAPI state(
117
                const state& state);
118
119
120
        //! Compares two states.
121
        Cdr_DllAPI bool operator ==(
122
                const state& other_state) const;
123
124
    private:
125
126
        state& operator =(
127
                const state& state) = delete;
128
129
        //! The position in the buffer when the state was created.
130
        const FastBuffer::iterator offset_;
131
132
        //! The position from the alignment is calculated, when the state was created.
133
        const FastBuffer::iterator origin_;
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.
142
        MemberId next_member_id_;
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.
148
        XCdrHeaderSelection header_selection_ {XCdrHeaderSelection::AUTO_WITH_SHORT_HEADER_BY_DEFAULT};
149
150
        //! Not related with the state. Used by encoding algorithms to store the allocated member header version.
151
        XCdrHeaderSelection header_serialized_ {XCdrHeaderSelection::SHORT_HEADER};
152
153
        //! Not related with the state. Used by encoding algorithms to store the previous encoding algorithm.
154
        EncodingAlgorithmFlag previous_encoding_ {EncodingAlgorithmFlag::PLAIN_CDR2};
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
     */
165
    Cdr_DllAPI Cdr(
166
            FastBuffer& cdr_buffer,
167
            const Endianness endianness = DEFAULT_ENDIAN,
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
     */
179
    Cdr_DllAPI Cdr& read_encapsulation();
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
     */
187
    Cdr_DllAPI Cdr& serialize_encapsulation();
188
189
    /*!
190
     * @brief Retrieves the CdrVersion used by the instance.
191
     * @return Configured CdrVersion.
192
     */
193
    Cdr_DllAPI CdrVersion get_cdr_version() const;
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
     */
200
    Cdr_DllAPI EncodingAlgorithmFlag get_encoding_flag() const;
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
     */
209
    Cdr_DllAPI bool set_encoding_flag(
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
     */
216
    Cdr_DllAPI std::array<uint8_t, 2> get_dds_cdr_options() const;
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
     */
222
    Cdr_DllAPI void set_dds_cdr_options(
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
     */
229
    Cdr_DllAPI void change_endianness(
230
            Endianness endianness);
231
232
    /*!
233
     * @brief This function returns the current endianness used by the CDR type.
234
     * @return The endianness.
235
     */
236
    Cdr_DllAPI Endianness endianness() const;
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
     */
255
    Cdr_DllAPI char* get_buffer_pointer();
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
     */
261
    Cdr_DllAPI char* get_current_position();
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
     */
267
    Cdr_DllAPI size_t get_serialized_data_length() const;
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
0
    {
279
0
        return (data_size - (current_alignment % data_size)) & (data_size - 1);
280
0
    }
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
     */
292
    Cdr_DllAPI void set_state(
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
     */
300
    Cdr_DllAPI bool move_alignment_forward(
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
0
    {
308
0
        origin_ = offset_;
309
0
        last_data_size_ = 0;
310
0
    }
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>
326
    inline Cdr& operator <<(
327
            const _T& value)
328
0
    {
329
0
        if (MEMBER_ID_INVALID == next_member_id_)
330
0
        {
331
0
            serialize(value);
332
0
        }
333
0
        else
334
0
        {
335
0
            serialize_member(next_member_id_, value);
336
337
0
        }
338
339
0
        return *this;
340
0
    }
Unexecuted instantiation: eprosima::fastcdr::Cdr& eprosima::fastcdr::Cdr::operator<< <unsigned char>(unsigned char const&)
Unexecuted instantiation: eprosima::fastcdr::Cdr& eprosima::fastcdr::Cdr::operator<< <int>(int const&)
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>
356
    inline Cdr& operator >>(
357
            _T& value)
358
0
    {
359
0
        if (MEMBER_ID_INVALID == next_member_id_)
360
0
        {
361
0
            deserialize(value);
362
0
        }
363
0
        else
364
0
        {
365
0
            deserialize_member(value);
366
0
        }
367
0
        return *this;
368
0
    }
Unexecuted instantiation: eprosima::fastcdr::Cdr& eprosima::fastcdr::Cdr::operator>><unsigned char>(unsigned char&)
Unexecuted instantiation: eprosima::fastcdr::Cdr& eprosima::fastcdr::Cdr::operator>><unsigned int>(unsigned int&)
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>
381
    Cdr& serialize(
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>
397
    Cdr& serialize(
398
            const _T& value,
399
            Endianness endianness)
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,
429
            typename std::enable_if<std::is_same<typename std::underlying_type<_T>::type,
430
            int32_t>::value>::type* = nullptr>
431
    Cdr& serialize(
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,
447
            typename std::enable_if<std::is_same<typename std::underlying_type<_T>::type,
448
            uint32_t>::value>::type* = nullptr>
449
    Cdr& serialize(
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,
465
            typename std::enable_if<std::is_same<typename std::underlying_type<_T>::type,
466
            int16_t>::value>::type* = nullptr>
467
    Cdr& serialize(
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,
483
            typename std::enable_if<std::is_same<typename std::underlying_type<_T>::type,
484
            uint16_t>::value>::type* = nullptr>
485
    Cdr& serialize(
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,
501
            typename std::enable_if<std::is_same<typename std::underlying_type<_T>::type,
502
            int8_t>::value>::type* = nullptr>
503
    Cdr& serialize(
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,
519
            typename std::enable_if<std::is_same<typename std::underlying_type<_T>::type,
520
            uint8_t>::value>::type* = nullptr>
521
    Cdr& serialize(
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
     */
533
    Cdr_DllAPI Cdr& serialize(
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
     */
542
    Cdr_DllAPI Cdr& serialize(
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
     */
551
    Cdr_DllAPI Cdr& serialize(
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
     */
560
    Cdr_DllAPI Cdr& serialize(
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
     */
569
    Cdr_DllAPI Cdr& serialize(
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
     */
578
    Cdr_DllAPI Cdr& serialize(
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
     */
587
    Cdr_DllAPI Cdr& serialize(
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
     */
596
    Cdr_DllAPI Cdr& serialize(
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
     */
605
    Cdr_DllAPI Cdr& serialize(
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
     */
614
    Cdr_DllAPI Cdr& serialize(
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
     */
623
    Cdr_DllAPI Cdr& serialize(
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
     */
632
    Cdr_DllAPI Cdr& serialize(
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
     */
642
    Cdr_DllAPI Cdr& serialize(
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
     */
651
    Cdr_DllAPI Cdr& serialize(
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
     */
660
    Cdr_DllAPI Cdr& serialize(
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
     */
669
    Cdr_DllAPI Cdr& serialize(
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
     */
678
    Cdr_DllAPI Cdr& serialize(
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
689
    Cdr& serialize(
690
            const std::string& string_t)
691
0
    {
692
0
        // Check there are no null characters in the string.
693
0
        const char* c_str = string_t.c_str();
694
0
        const auto str_len = strlen(c_str);
695
0
        if (string_t.size() > str_len)
696
0
        {
697
0
            throw exception::BadParamException("The string contains null characters");
698
0
        }
699
0
700
0
        return serialize_sequence(c_str, str_len + 1);
701
0
    }
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
710
    Cdr& serialize(
711
            const std::wstring& string_t)
712
0
    {
713
0
        return serialize(string_t.c_str());
714
0
    }
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>
724
    Cdr& serialize(
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>
737
    Cdr& serialize(
738
            const std::array<_T, _Size>& array_t)
739
0
    {
740
0
        if (!is_multi_array_primitive(&array_t))
741
0
        {
742
0
            Cdr::state dheader_state {allocate_xcdrv2_dheader()};
743
744
0
            serialize_array(array_t.data(), array_t.size());
745
746
0
            set_xcdrv2_dheader(dheader_state);
747
0
        }
748
0
        else
749
0
        {
750
0
            serialize_array(array_t.data(), array_t.size());
751
0
        }
752
753
0
        return *this;
754
0
    }
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>
764
    Cdr& serialize(
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>
794
    Cdr& serialize(
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
811
        if (CdrVersion::XCDRv2 == cdr_version_)
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
826
    Cdr& serialize(
827
            const std::vector<bool>& vector_t)
828
0
    {
829
0
        return serialize_bool_sequence(vector_t);
830
0
    }
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>
840
    Cdr& serialize(
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>
874
    Cdr& serialize(
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>
946
    Cdr& serialize_array(
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>
966
    Cdr& serialize_array(
967
            const _T* type_t,
968
            size_t num_elements,
969
            Endianness endianness)
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
997
    Cdr& serialize_array(
998
            const uint8_t* octet_t,
999
            size_t num_elements)
1000
0
    {
1001
0
        return serialize_array(reinterpret_cast<const char*>(octet_t), num_elements);
1002
0
    }
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
     */
1011
    Cdr_DllAPI Cdr& serialize_array(
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
1023
    Cdr& serialize_array(
1024
            const int8_t* int8,
1025
            size_t num_elements)
1026
0
    {
1027
0
        return serialize_array(reinterpret_cast<const char*>(int8), num_elements);
1028
0
    }
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
1038
    Cdr& serialize_array(
1039
            const uint16_t* ushort_t,
1040
            size_t num_elements)
1041
0
    {
1042
0
        return serialize_array(reinterpret_cast<const int16_t*>(ushort_t), num_elements);
1043
0
    }
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
     */
1052
    Cdr_DllAPI Cdr& serialize_array(
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
1064
    Cdr& serialize_array(
1065
            const uint32_t* ulong_t,
1066
            size_t num_elements)
1067
0
    {
1068
0
        return serialize_array(reinterpret_cast<const int32_t*>(ulong_t), num_elements);
1069
0
    }
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
     */
1078
    Cdr_DllAPI Cdr& serialize_array(
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
     */
1089
    Cdr_DllAPI Cdr& serialize_array(
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
1101
    Cdr& serialize_array(
1102
            const uint64_t* ulonglong_t,
1103
            size_t num_elements)
1104
0
    {
1105
0
        return serialize_array(reinterpret_cast<const int64_t*>(ulonglong_t), num_elements);
1106
0
    }
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
     */
1115
    Cdr_DllAPI Cdr& serialize_array(
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
     */
1126
    Cdr_DllAPI Cdr& serialize_array(
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
     */
1137
    Cdr_DllAPI Cdr& serialize_array(
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
     */
1149
    Cdr_DllAPI Cdr& serialize_array(
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
     */
1160
    Cdr_DllAPI Cdr& serialize_array(
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
1172
    Cdr& serialize_array(
1173
            const std::string* string_t,
1174
            size_t num_elements)
1175
0
    {
1176
0
        for (size_t count = 0; count < num_elements; ++count)
1177
0
        {
1178
0
            serialize(string_t[count].c_str());
1179
0
        }
1180
0
        return *this;
1181
0
    }
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
1191
    Cdr& serialize_array(
1192
            const std::wstring* string_t,
1193
            size_t num_elements)
1194
0
    {
1195
0
        for (size_t count = 0; count < num_elements; ++count)
1196
0
        {
1197
0
            serialize(string_t[count].c_str());
1198
0
        }
1199
0
        return *this;
1200
0
    }
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>
1211
    Cdr& serialize_array(
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>
1231
    Cdr& serialize_array(
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>
1248
    Cdr& serialize_array(
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>
1269
    Cdr& serialize_array(
1270
            const std::vector<_T>& value,
1271
            Endianness endianness)
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
1299
    Cdr& serialize_array(
1300
            const std::vector<bool>& value)
1301
0
    {
1302
0
        serialize_bool_array(value);
1303
0
1304
0
        return *this;
1305
0
    }
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>
1316
    Cdr& serialize_sequence(
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>
1348
    Cdr& serialize_sequence(
1349
            const _T* sequence_t,
1350
            size_t num_elements)
1351
0
    {
1352
0
        state state_before_error(*this);
1353
0
1354
0
        serialize(static_cast<int32_t>(num_elements));
1355
0
1356
0
        try
1357
0
        {
1358
0
            serialize_array(sequence_t, num_elements);
1359
0
        }
1360
0
        catch (exception::Exception& ex)
1361
0
        {
1362
0
            set_state(state_before_error);
1363
0
            ex.raise();
1364
0
        }
1365
0
1366
0
        if (CdrVersion::XCDRv2 == cdr_version_)
1367
0
        {
1368
0
            serialized_member_size_ = get_serialized_member_size<_T>();
1369
0
        }
1370
0
1371
0
        return *this;
1372
0
    }
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>
1383
    Cdr& serialize_sequence(
1384
            const _T* sequence_t,
1385
            size_t num_elements,
1386
            Endianness endianness)
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>
1417
    Cdr& deserialize(
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>
1433
    Cdr& deserialize(
1434
            _T& value,
1435
            Endianness endianness)
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,
1464
            typename std::enable_if<std::is_same<typename std::underlying_type<_T>::type,
1465
            int32_t>::value>::type* = nullptr>
1466
    Cdr& deserialize(
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,
1484
            typename std::enable_if<std::is_same<typename std::underlying_type<_T>::type,
1485
            uint32_t>::value>::type* = nullptr>
1486
    Cdr& deserialize(
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,
1504
            typename std::enable_if<std::is_same<typename std::underlying_type<_T>::type,
1505
            int16_t>::value>::type* = nullptr>
1506
    Cdr& deserialize(
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,
1524
            typename std::enable_if<std::is_same<typename std::underlying_type<_T>::type,
1525
            uint16_t>::value>::type* = nullptr>
1526
    Cdr& deserialize(
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,
1544
            typename std::enable_if<std::is_same<typename std::underlying_type<_T>::type,
1545
            int8_t>::value>::type* = nullptr>
1546
    Cdr& deserialize(
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,
1564
            typename std::enable_if<std::is_same<typename std::underlying_type<_T>::type,
1565
            uint8_t>::value>::type* = nullptr>
1566
    Cdr& deserialize(
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
1582
    Cdr& deserialize(
1583
            uint8_t& octet_t)
1584
0
    {
1585
0
        return deserialize(reinterpret_cast<char&>(octet_t));
1586
0
    }
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
     */
1594
    Cdr_DllAPI Cdr& deserialize(
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
1604
    Cdr& deserialize(
1605
            int8_t& int8)
1606
0
    {
1607
0
        return deserialize(reinterpret_cast<char&>(int8));
1608
0
    }
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
1617
    Cdr& deserialize(
1618
            uint16_t& ushort_t)
1619
0
    {
1620
0
        return deserialize(reinterpret_cast<int16_t&>(ushort_t));
1621
0
    }
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
     */
1629
    Cdr_DllAPI Cdr& deserialize(
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
1639
    Cdr& deserialize(
1640
            uint32_t& ulong_t)
1641
0
    {
1642
0
        return deserialize(reinterpret_cast<int32_t&>(ulong_t));
1643
0
    }
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
     */
1651
    Cdr_DllAPI Cdr& deserialize(
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
1661
    Cdr& deserialize(
1662
            wchar_t& wchar)
1663
0
    {
1664
0
        uint16_t ret;
1665
0
        deserialize(ret);
1666
0
        wchar = static_cast<wchar_t>(ret);
1667
0
        return *this;
1668
0
    }
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
1677
    Cdr& deserialize(
1678
            uint64_t& ulonglong_t)
1679
0
    {
1680
0
        return deserialize(reinterpret_cast<int64_t&>(ulonglong_t));
1681
0
    }
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
     */
1689
    Cdr_DllAPI Cdr& deserialize(
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
     */
1698
    Cdr_DllAPI Cdr& deserialize(
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
     */
1707
    Cdr_DllAPI Cdr& deserialize(
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
     */
1717
    Cdr_DllAPI Cdr& deserialize(
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
     */
1727
    Cdr_DllAPI Cdr& deserialize(
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
     */
1738
    Cdr_DllAPI Cdr& deserialize(
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
     */
1749
    Cdr_DllAPI Cdr& deserialize(
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
1759
    Cdr& deserialize(
1760
            std::string& string_t)
1761
0
    {
1762
0
        uint32_t length = 0;
1763
0
        const char* str = read_string(length);
1764
0
        string_t.assign(str, length);
1765
0
        return *this;
1766
0
    }
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
1775
    Cdr& deserialize(
1776
            std::wstring& string_t)
1777
0
    {
1778
0
        uint32_t length = 0;
1779
0
        string_t = read_wstring(length);
1780
0
        return *this;
1781
0
    }
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>
1791
    Cdr& deserialize(
1792
            fixed_string<MAX_CHARS>& value)
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>
1807
    Cdr& deserialize(
1808
            std::array<_T, _Size>& array_t)
1809
0
    {
1810
0
        if (CdrVersion::XCDRv2 == cdr_version_ && !is_multi_array_primitive(&array_t))
1811
0
        {
1812
0
            uint32_t dheader {0};
1813
0
            deserialize(dheader);
1814
1815
0
            uint32_t count {0};
1816
0
            auto offset = offset_;
1817
0
            while (offset_ - offset < dheader && count < _Size)
1818
0
            {
1819
0
                deserialize_array(&array_t.data()[count], 1);
1820
0
                ++count;
1821
0
            }
1822
1823
0
            if (offset_ - offset != dheader)
1824
0
            {
1825
0
                throw exception::BadParamException("Member size greater than size specified by DHEADER");
1826
0
            }
1827
0
        }
1828
0
        else
1829
0
        {
1830
0
            return deserialize_array(array_t.data(), array_t.size());
1831
0
        }
1832
1833
0
        return *this;
1834
0
    }
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>
1844
    Cdr& deserialize(
1845
            std::vector<_T>& vector_t)
1846
    {
1847
        uint32_t sequence_length {0};
1848
1849
        if (CdrVersion::XCDRv2 == cdr_version_)
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);
1895
                throw exception::NotEnoughMemoryException(
1896
                          exception::NotEnoughMemoryException::NOT_ENOUGH_MEMORY_MESSAGE_DEFAULT);
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>
1922
    Cdr& deserialize(
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);
1939
            throw exception::NotEnoughMemoryException(
1940
                      exception::NotEnoughMemoryException::NOT_ENOUGH_MEMORY_MESSAGE_DEFAULT);
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
1964
    Cdr& deserialize(
1965
            std::vector<bool>& vector_t)
1966
0
    {
1967
0
        return deserialize_bool_sequence(vector_t);
1968
0
    }
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>
1978
    Cdr& deserialize(
1979
            std::map<_K, _T>& map_t)
1980
    {
1981
        if (CdrVersion::XCDRv2 == cdr_version_)
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>
2047
    Cdr& deserialize(
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>
2134
    Cdr& deserialize_array(
2135
            _T* value,
2136
            size_t num_elements)
2137
0
    {
2138
0
        for (size_t count = 0; count < num_elements; ++count)
2139
0
        {
2140
0
            deserialize(value[count]);
2141
0
        }
2142
0
        return *this;
2143
0
    }
Unexecuted instantiation: eprosima::fastcdr::Cdr& eprosima::fastcdr::Cdr::deserialize_array<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > >(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >*, unsigned long)
Unexecuted instantiation: eprosima::fastcdr::Cdr& eprosima::fastcdr::Cdr::deserialize_array<std::__1::basic_string<wchar_t, std::__1::char_traits<wchar_t>, std::__1::allocator<wchar_t> > >(std::__1::basic_string<wchar_t, std::__1::char_traits<wchar_t>, std::__1::allocator<wchar_t> >*, unsigned long)
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>
2154
    Cdr& deserialize_array(
2155
            _T* type_t,
2156
            size_t num_elements,
2157
            Endianness endianness)
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
2185
    Cdr& deserialize_array(
2186
            uint8_t* octet_t,
2187
            size_t num_elements)
2188
0
    {
2189
0
        return deserialize_array(reinterpret_cast<char*>(octet_t), num_elements);
2190
0
    }
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
     */
2199
    Cdr_DllAPI Cdr& deserialize_array(
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
2211
    Cdr& deserialize_array(
2212
            int8_t* int8,
2213
            size_t num_elements)
2214
0
    {
2215
0
        return deserialize_array(reinterpret_cast<char*>(int8), num_elements);
2216
0
    }
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
2226
    Cdr& deserialize_array(
2227
            uint16_t* ushort_t,
2228
            size_t num_elements)
2229
0
    {
2230
0
        return deserialize_array(reinterpret_cast<int16_t*>(ushort_t), num_elements);
2231
0
    }
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
     */
2240
    Cdr_DllAPI Cdr& deserialize_array(
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
2252
    Cdr& deserialize_array(
2253
            uint32_t* ulong_t,
2254
            size_t num_elements)
2255
0
    {
2256
0
        return deserialize_array(reinterpret_cast<int32_t*>(ulong_t), num_elements);
2257
0
    }
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
     */
2266
    Cdr_DllAPI Cdr& deserialize_array(
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
     */
2277
    Cdr_DllAPI Cdr& deserialize_array(
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
2289
    Cdr& deserialize_array(
2290
            uint64_t* ulonglong_t,
2291
            size_t num_elements)
2292
0
    {
2293
0
        return deserialize_array(reinterpret_cast<int64_t*>(ulonglong_t), num_elements);
2294
0
    }
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
     */
2303
    Cdr_DllAPI Cdr& deserialize_array(
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
     */
2314
    Cdr_DllAPI Cdr& deserialize_array(
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
     */
2325
    Cdr_DllAPI Cdr& deserialize_array(
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
     */
2337
    Cdr_DllAPI Cdr& deserialize_array(
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
     */
2348
    Cdr_DllAPI Cdr& deserialize_array(
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>
2364
    Cdr& deserialize_array(
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>
2384
    Cdr& deserialize_array(
2385
            std::vector<_T>& value)
2386
    {
2387
        if (CdrVersion::XCDRv2 == cdr_version_)
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>
2425
    Cdr& deserialize_array(
2426
            std::vector<_T>& value,
2427
            Endianness endianness)
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
2458
    Cdr& deserialize_array(
2459
            std::vector<bool>& value)
2460
0
    {
2461
0
        deserialize_bool_array(value);
2462
0
2463
0
        return *this;
2464
0
    }
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>
2477
    Cdr& deserialize_sequence(
2478
            _T*& sequence_t,
2479
            size_t& num_elements)
2480
    {
2481
        uint32_t sequence_length {0};
2482
2483
        if (CdrVersion::XCDRv2 == cdr_version_)
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);
2524
                throw exception::NotEnoughMemoryException(
2525
                          exception::NotEnoughMemoryException::NOT_ENOUGH_MEMORY_MESSAGE_DEFAULT);
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>
2557
    Cdr& deserialize_sequence(
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>
2594
    Cdr& deserialize_sequence(
2595
            _T*& sequence_t,
2596
            size_t& num_elements,
2597
            Endianness endianness)
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
2627
    Cdr& deserialize_sequence(
2628
            std::string*& sequence_t,
2629
            size_t& num_elements)
2630
0
    {
2631
0
        return deserialize_string_sequence(sequence_t, num_elements);
2632
0
    }
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
2644
    Cdr& deserialize_sequence(
2645
            std::wstring*& sequence_t,
2646
            size_t& num_elements)
2647
0
    {
2648
0
        return deserialize_wstring_sequence(sequence_t, num_elements);
2649
0
    }
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>
2666
    Cdr& serialize_member(
2667
            const MemberId& member_id,
2668
            const _T& member_value,
2669
            XCdrHeaderSelection header_selection = XCdrHeaderSelection::AUTO_WITH_SHORT_HEADER_BY_DEFAULT)
2670
0
    {
2671
0
        Cdr::state current_state(*this);
2672
0
        (this->*begin_serialize_member_)(member_id, true, current_state, header_selection);
2673
0
        serialize(member_value);
2674
0
        return (this->*end_serialize_member_)(current_state);
2675
0
    }
Unexecuted instantiation: eprosima::fastcdr::Cdr& eprosima::fastcdr::Cdr::serialize_member<unsigned char>(eprosima::fastcdr::MemberId const&, unsigned char const&, eprosima::fastcdr::Cdr::XCdrHeaderSelection)
Unexecuted instantiation: eprosima::fastcdr::Cdr& eprosima::fastcdr::Cdr::serialize_member<int>(eprosima::fastcdr::MemberId const&, int const&, eprosima::fastcdr::Cdr::XCdrHeaderSelection)
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>
2688
    Cdr& serialize_member(
2689
            const MemberId& member_id,
2690
            const optional<_T>& member_value,
2691
            XCdrHeaderSelection header_selection = XCdrHeaderSelection::AUTO_WITH_SHORT_HEADER_BY_DEFAULT)
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>
2707
    Cdr& deserialize_member(
2708
            _T& member_value)
2709
0
    {
2710
0
        return deserialize(member_value);
2711
0
    }
Unexecuted instantiation: eprosima::fastcdr::Cdr& eprosima::fastcdr::Cdr::deserialize_member<unsigned char>(unsigned char&)
Unexecuted instantiation: eprosima::fastcdr::Cdr& eprosima::fastcdr::Cdr::deserialize_member<unsigned int>(unsigned int&)
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>
2721
    Cdr& deserialize_member(
2722
            optional<_T>& member_value)
2723
    {
2724
        if (EncodingAlgorithmFlag::PLAIN_CDR == current_encoding_)
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
            {
2736
                throw exception::BadParamException(
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
     */
2755
    Cdr_DllAPI Cdr& begin_serialize_type(
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
     */
2766
    Cdr_DllAPI Cdr& end_serialize_type(
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
     */
2777
    Cdr_DllAPI Cdr& deserialize_type(
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>
2789
    Cdr& serialize(
2790
            const optional<_T>& value)
2791
    {
2792
        if (CdrVersion::XCDRv2 == cdr_version_ && EncodingAlgorithmFlag::PL_CDR2 != current_encoding_)
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>
2813
    Cdr& serialize(
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
     */
2832
    Cdr_DllAPI Cdr& operator <<(
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>
2843
    Cdr& deserialize(
2844
            optional<_T>& value)
2845
    {
2846
        bool is_present = true;
2847
        if (CdrVersion::XCDRv2 == cdr_version_ && EncodingAlgorithmFlag::PL_CDR2 != current_encoding_)
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>
2868
    Cdr& deserialize(
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>
2894
    Cdr& deserialize(
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;
2903
        if (CdrVersion::XCDRv2 == cdr_version_ && EncodingAlgorithmFlag::PL_CDR2 != current_encoding_)
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
     */
2920
    Cdr_DllAPI state allocate_xcdrv2_dheader();
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
     */
2928
    Cdr_DllAPI void set_xcdrv2_dheader(
2929
            const state& state);
2930
2931
private:
2932
2933
    Cdr(
2934
            const Cdr&) = delete;
2935
2936
    Cdr& operator =(
2937
            const Cdr&) = delete;
2938
2939
    Cdr_DllAPI Cdr& serialize_bool_array(
2940
            const std::vector<bool>& vector_t);
2941
2942
    Cdr_DllAPI Cdr& serialize_bool_sequence(
2943
            const std::vector<bool>& vector_t);
2944
2945
    Cdr_DllAPI Cdr& deserialize_bool_array(
2946
            std::vector<bool>& vector_t);
2947
2948
    Cdr_DllAPI Cdr& deserialize_bool_sequence(
2949
            std::vector<bool>& vector_t);
2950
2951
    Cdr_DllAPI Cdr& deserialize_string_sequence(
2952
            std::string*& sequence_t,
2953
            size_t& num_elements);
2954
2955
    Cdr_DllAPI Cdr& deserialize_wstring_sequence(
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>
2967
    Cdr& serialize_array(
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>
2982
    Cdr& deserialize_array(
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>
2998
    Cdr& deserialize_array(
2999
            std::array<_T, _Size>* array_t,
3000
            size_t num_elements,
3001
            Endianness endianness)
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
0
    {
3015
0
        return data_size > last_data_size_ ? (data_size - ((offset_ - origin_) % data_size)) & (data_size - 1) : 0;
3016
0
    }
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
0
    {
3025
0
        offset_ += align;
3026
0
        last_data_size_ = 0;
3027
0
    }
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);
3039
    Cdr_DllAPI const std::wstring read_wstring(
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
     */
3052
    void xcdr1_serialize_short_member_header(
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
     */
3064
    void xcdr1_end_short_member_header(
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
     */
3074
    void xcdr1_serialize_long_member_header(
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
     */
3084
    void xcdr1_end_long_member_header(
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
     */
3097
    void xcdr1_change_to_short_member_header(
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
     */
3108
    void xcdr1_change_to_long_member_header(
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
     */
3120
    Cdr_DllAPI bool xcdr1_deserialize_member_header(
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
     */
3131
    void xcdr2_serialize_short_member_header(
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
     */
3143
    void xcdr2_end_short_member_header(
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
     */
3154
    void xcdr2_serialize_long_member_header(
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
     */
3165
    void xcdr2_end_long_member_header(
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
     */
3178
    void xcdr2_change_to_short_member_header(
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
     */
3190
    void xcdr2_change_to_long_member_header(
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
     */
3202
    void xcdr2_shrink_to_long_member_header(
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
     */
3214
    void xcdr2_deserialize_member_header(
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
     */
3234
    Cdr& xcdr1_begin_serialize_member(
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
     */
3250
    Cdr& xcdr1_end_serialize_member(
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
     */
3268
    Cdr& xcdr1_begin_serialize_opt_member(
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
     */
3284
    Cdr& xcdr1_end_serialize_opt_member(
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
     */
3303
    Cdr& xcdr2_begin_serialize_member(
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
     */
3319
    Cdr& xcdr2_end_serialize_member(
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
     */
3331
    Cdr& xcdr1_begin_serialize_type(
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
     */
3343
    Cdr& xcdr1_end_serialize_type(
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
     */
3359
    Cdr& xcdr2_begin_serialize_type(
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
     */
3372
    Cdr& xcdr2_end_serialize_type(
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
     */
3387
    Cdr& xcdr1_deserialize_type(
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
     */
3404
    Cdr& xcdr2_deserialize_type(
3405
            EncodingAlgorithmFlag type_encoding,
3406
            std::function<bool (Cdr&, const MemberId&)> functor);
3407
3408
    Cdr& cdr_begin_serialize_member(
3409
            const MemberId& member_id,
3410
            bool is_present,
3411
            Cdr::state& current_state,
3412
            XCdrHeaderSelection header_selection);
3413
3414
    Cdr& cdr_end_serialize_member(
3415
            const Cdr::state& current_state);
3416
3417
    Cdr& cdr_begin_serialize_type(
3418
            Cdr::state& current_state,
3419
            EncodingAlgorithmFlag type_encoding);
3420
3421
    Cdr& cdr_end_serialize_type(
3422
            const Cdr::state& current_state);
3423
3424
    Cdr& cdr_deserialize_type(
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
3433
    using begin_serialize_member_functor = Cdr& (Cdr::*)(
3434
        const MemberId&,
3435
        bool,
3436
        Cdr::state&,
3437
        XCdrHeaderSelection);
3438
    begin_serialize_member_functor begin_serialize_member_ { nullptr };
3439
3440
    using end_serialize_member_functor = Cdr& (Cdr::*)(
3441
        const Cdr::state&);
3442
    end_serialize_member_functor end_serialize_member_ { nullptr };
3443
3444
    using begin_serialize_opt_member_functor = Cdr& (Cdr::*)(
3445
        const MemberId&,
3446
        bool,
3447
        Cdr::state&,
3448
        XCdrHeaderSelection);
3449
    begin_serialize_opt_member_functor begin_serialize_opt_member_ { nullptr };
3450
3451
    using end_serialize_memberopt__functor = Cdr& (Cdr::*)(
3452
        const Cdr::state&);
3453
    end_serialize_member_functor end_serialize_opt_member_ { nullptr };
3454
3455
    using begin_serialize_type_functor = Cdr& (Cdr::*)(
3456
        Cdr::state&,
3457
        EncodingAlgorithmFlag);
3458
    begin_serialize_type_functor begin_serialize_type_ { nullptr };
3459
3460
    using end_serialize_type_functor = Cdr& (Cdr::*)(
3461
        const Cdr::state&);
3462
    end_serialize_type_functor end_serialize_type_ { nullptr };
3463
3464
    using deserialize_type_functor = Cdr& (Cdr::*)(
3465
        EncodingAlgorithmFlag,
3466
        std::function<bool (Cdr&, const MemberId&)>);
3467
    deserialize_type_functor deserialize_type_ { nullptr };
3468
3469
    //! @brief Reference to the buffer that will be serialized/deserialized.
3470
    FastBuffer& cdr_buffer_;
3471
3472
    //! @brief The type of CDR that will be use in serialization/deserialization.
3473
    CdrVersion cdr_version_ {CdrVersion::XCDRv2};
3474
3475
    //! @brief Stores the main encoding algorithm.
3476
    EncodingAlgorithmFlag encoding_flag_ {EncodingAlgorithmFlag::PLAIN_CDR2};
3477
3478
    //! @brief Stores the current encoding algorithm.
3479
    EncodingAlgorithmFlag current_encoding_ {EncodingAlgorithmFlag::PLAIN_CDR2};
3480
3481
    //! @brief This attribute stores the option flags when the CDR type is DDS_CDR;
3482
    std::array<uint8_t, 2> options_{{0}};
3483
3484
    //! @brief The endianness that will be applied over the buffer.
3485
    uint8_t endianness_ {Endianness::LITTLE_ENDIANNESS};
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.
3491
    size_t last_data_size_ {0};
3492
3493
    //! @brief The current position in the serialization/deserialization process.
3494
    FastBuffer::iterator offset_;
3495
3496
    //! @brief The position from where the alignment is calculated.
3497
    FastBuffer::iterator origin_;
3498
3499
    //! @brief The last position in the buffer;
3500
    FastBuffer::iterator end_;
3501
3502
    //! Next member identifier to be processed.
3503
    MemberId next_member_id_;
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
     */
3512
    enum SerializedMemberSizeForNextInt
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.
3518
    }
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.
3523
    state initial_state_;
3524
3525
    //! Whether the encapsulation was serialized.
3526
    bool encapsulation_serialized_ {false};
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>
3537
    constexpr SerializedMemberSizeForNextInt get_serialized_member_size() const
3538
0
    {
3539
0
        return (1 == sizeof(_T) ? SERIALIZED_MEMBER_SIZE :
3540
0
               (4 == sizeof(_T) ? SERIALIZED_MEMBER_SIZE_4 :
3541
0
               (8 == sizeof(_T) ? SERIALIZED_MEMBER_SIZE_8 :  NO_SERIALIZED_MEMBER_SIZE)));
3542
0
    }
3543
3544
};
3545
3546
}            //namespace fastcdr
3547
}        //namespace eprosima
3548
3549
#endif // _CDR_CDR_H_