Coverage Report

Created: 2026-09-14 06:15

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