Coverage Report

Created: 2026-07-25 06:47

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
104k
    {
1665
104k
        return deserialize(reinterpret_cast<int32_t&>(ulong_t));
1666
104k
    }
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
     * @param map_t The variable that will store the map read from the buffer.
2038
     * @return Reference to the eprosima::fastcdr::Cdr object.
2039
     * @exception exception::NotEnoughMemoryException This exception is thrown when trying to deserialize a position that exceeds the internal memory size.
2040
     */
2041
    template<class _K, class _T, typename std::enable_if<!std::is_enum<_T>::value &&
2042
            !std::is_arithmetic<_T>::value>::type* = nullptr>
2043
    Cdr& deserialize(
2044
            std::map<_K, _T>& map_t)
2045
    {
2046
        state state_before_error(*this);
2047
2048
        if (CdrVersion::XCDRv2 == cdr_version_)
2049
        {
2050
            uint32_t dheader {0};
2051
            deserialize(dheader);
2052
2053
            if (((end_ - offset_) < dheader) || (dheader < 4))
2054
            {
2055
                set_state(state_before_error);
2056
                throw exception::NotEnoughMemoryException(
2057
                          exception::NotEnoughMemoryException::NOT_ENOUGH_MEMORY_MESSAGE_DEFAULT);
2058
            }
2059
2060
            auto last_offset = offset_;
2061
            last_offset += dheader;
2062
2063
            uint32_t map_length {0};
2064
            deserialize(map_length);
2065
2066
            map_t.clear();
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_t.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
            map_t.clear();
2100
2101
            try
2102
            {
2103
                for (uint32_t i = 0; i < sequence_length; ++i)
2104
                {
2105
                    _K key;
2106
                    _T value;
2107
                    deserialize(key);
2108
                    deserialize(value);
2109
                    map_t.emplace(std::pair<_K, _T>(std::move(key), std::move(value)));
2110
                }
2111
            }
2112
            catch (exception::Exception& ex)
2113
            {
2114
                set_state(state_before_error);
2115
                ex.raise();
2116
            }
2117
        }
2118
2119
        return *this;
2120
    }
2121
2122
    /*!
2123
     * @brief This function template deserializes a map of primitive.
2124
     * @param map_t The variable that will store the map read from the buffer.
2125
     * @return Reference to the eprosima::fastcdr::Cdr object.
2126
     * @exception exception::NotEnoughMemoryException This exception is thrown when trying to deserialize a position that exceeds the internal memory size.
2127
     */
2128
    template<class _K, class _T, typename std::enable_if<std::is_enum<_T>::value ||
2129
            std::is_arithmetic<_T>::value>::type* = nullptr>
2130
    Cdr& deserialize(
2131
            std::map<_K, _T>& map_t)
2132
    {
2133
        uint32_t sequence_length = 0;
2134
        state state_(*this);
2135
2136
        deserialize(sequence_length);
2137
2138
        try
2139
        {
2140
            for (uint32_t i = 0; i < sequence_length; ++i)
2141
            {
2142
                _K key;
2143
                _T value;
2144
                deserialize(key);
2145
                deserialize(value);
2146
                map_t.emplace(std::pair<_K, _T>(std::move(key), std::move(value)));
2147
            }
2148
        }
2149
        catch (exception::Exception& ex)
2150
        {
2151
            set_state(state_);
2152
            ex.raise();
2153
        }
2154
2155
        return *this;
2156
    }
2157
2158
    /*!
2159
     * @brief Decodes a bitset from the buffer.
2160
     * @param[out] value Reference to the variable where the bitset will be stored after decoding from the buffer.
2161
     * @return Reference to the eprosima::fastcdr::Cdr object.
2162
     * @exception exception::NotEnoughMemoryException This exception is thrown when trying to decode from a buffer
2163
     * position that exceeds the internal memory size.
2164
     */
2165
    template<size_t N, typename std::enable_if < (N < 9) > ::type* = nullptr>
2166
    Cdr& deserialize(
2167
            std::bitset<N>& value)
2168
    {
2169
        uint8_t decode_value {0};
2170
        deserialize(decode_value);
2171
        value = decode_value;
2172
        return *this;
2173
    }
2174
2175
    template<size_t N, typename std::enable_if < (8 < N && N < 17) > ::type* = nullptr>
2176
    Cdr& deserialize(
2177
            std::bitset<N>& value)
2178
    {
2179
        uint16_t decode_value {0};
2180
        deserialize(decode_value);
2181
        value = decode_value;
2182
        return *this;
2183
    }
2184
2185
    template<size_t N, typename std::enable_if < (16 < N && N < 33) > ::type* = nullptr>
2186
    Cdr& deserialize(
2187
            std::bitset<N>& value)
2188
    {
2189
        uint32_t decode_value {0};
2190
        deserialize(decode_value);
2191
        value = decode_value;
2192
        return *this;
2193
    }
2194
2195
    template<size_t N, typename std::enable_if < (32 < N && N < 65) > ::type* = nullptr>
2196
    Cdr& deserialize(
2197
            std::bitset<N>& value)
2198
    {
2199
        uint64_t decode_value {0};
2200
        deserialize(decode_value);
2201
        value = decode_value;
2202
        return *this;
2203
    }
2204
2205
    /*!
2206
     * @brief Decodes an array of a type not managed by this encoder from the buffer.
2207
     *
2208
     * To do that, the encoder expects a function `deserialize` to be provided by the type.
2209
     *
2210
     * @param[out] value Reference to the variable where the array will be stored after decoding from the buffer.
2211
     * @param[in] num_elements Number of the elements in the array.
2212
     * @return Reference to the eprosima::fastcdr::Cdr object.
2213
     * @exception exception::NotEnoughMemoryException This exception is thrown when trying to decode from a buffer
2214
     * position that exceeds the internal memory size.
2215
     */
2216
    template<class _T>
2217
    Cdr& deserialize_array(
2218
            _T* value,
2219
            size_t num_elements)
2220
0
    {
2221
0
        for (size_t count = 0; count < num_elements; ++count)
2222
0
        {
2223
0
            deserialize(value[count]);
2224
0
        }
2225
0
        return *this;
2226
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)
2227
2228
    /*!
2229
     * @brief This function template deserializes an array of non-basic objects with a different endianness.
2230
     * @param type_t The variable that will store the array of objects read from the buffer.
2231
     * @param num_elements Number of the elements in the array.
2232
     * @param endianness Endianness that will be used in the deserialization of this value.
2233
     * @return Reference to the eprosima::fastcdr::Cdr object.
2234
     * @exception exception::NotEnoughMemoryException This exception is thrown when trying to deserialize a position that exceeds the internal memory size.
2235
     */
2236
    template<class _T>
2237
    Cdr& deserialize_array(
2238
            _T* type_t,
2239
            size_t num_elements,
2240
            Endianness endianness)
2241
    {
2242
        bool aux_swap = swap_bytes_;
2243
        swap_bytes_ = (swap_bytes_ && (static_cast<Endianness>(endianness_) == endianness)) ||
2244
                (!swap_bytes_ && (static_cast<Endianness>(endianness_) != endianness));
2245
2246
        try
2247
        {
2248
            deserialize_array(type_t, num_elements);
2249
            swap_bytes_ = aux_swap;
2250
        }
2251
        catch (exception::Exception& ex)
2252
        {
2253
            swap_bytes_ = aux_swap;
2254
            ex.raise();
2255
        }
2256
2257
        return *this;
2258
    }
2259
2260
    /*!
2261
     * @brief This function deserializes an array of octets.
2262
     * @param octet_t The variable that will store the array of octets read from the buffer.
2263
     * @param num_elements Number of the elements in the array.
2264
     * @return Reference to the eprosima::fastcdr::Cdr object.
2265
     * @exception exception::NotEnoughMemoryException This exception is thrown when trying to deserialize a position that exceeds the internal memory size.
2266
     */
2267
    TEMPLATE_SPEC
2268
    Cdr& deserialize_array(
2269
            uint8_t* octet_t,
2270
            size_t num_elements)
2271
0
    {
2272
0
        return deserialize_array(reinterpret_cast<char*>(octet_t), num_elements);
2273
0
    }
2274
2275
    /*!
2276
     * @brief This function deserializes an array of characters.
2277
     * @param char_t The variable that will store the array of characters read from the buffer.
2278
     * @param num_elements Number of the elements in the array.
2279
     * @return Reference to the eprosima::fastcdr::Cdr object.
2280
     * @exception exception::NotEnoughMemoryException This exception is thrown when trying to deserialize a position that exceeds the internal memory size.
2281
     */
2282
    Cdr_DllAPI Cdr& deserialize_array(
2283
            char* char_t,
2284
            size_t num_elements);
2285
2286
    /*!
2287
     * @brief This function deserializes an array of int8_t.
2288
     * @param int8 The variable that will store the array of int8_t read from the buffer.
2289
     * @param num_elements Number of the elements in the array.
2290
     * @return Reference to the eprosima::fastcdr::Cdr object.
2291
     * @exception exception::NotEnoughMemoryException This exception is thrown when trying to deserialize a position that exceeds the internal memory size.
2292
     */
2293
    TEMPLATE_SPEC
2294
    Cdr& deserialize_array(
2295
            int8_t* int8,
2296
            size_t num_elements)
2297
0
    {
2298
0
        return deserialize_array(reinterpret_cast<char*>(int8), num_elements);
2299
0
    }
2300
2301
    /*!
2302
     * @brief This function deserializes an array of unsigned shorts.
2303
     * @param ushort_t The variable that will store the array of unsigned shorts read from the buffer.
2304
     * @param num_elements Number of the elements in the array.
2305
     * @return Reference to the eprosima::fastcdr::Cdr object.
2306
     * @exception exception::NotEnoughMemoryException This exception is thrown when trying to deserialize a position that exceeds the internal memory size.
2307
     */
2308
    TEMPLATE_SPEC
2309
    Cdr& deserialize_array(
2310
            uint16_t* ushort_t,
2311
            size_t num_elements)
2312
0
    {
2313
0
        return deserialize_array(reinterpret_cast<int16_t*>(ushort_t), num_elements);
2314
0
    }
2315
2316
    /*!
2317
     * @brief This function deserializes an array of shorts.
2318
     * @param short_t The variable that will store the array of shorts read from the buffer.
2319
     * @param num_elements Number of the elements in the array.
2320
     * @return Reference to the eprosima::fastcdr::Cdr object.
2321
     * @exception exception::NotEnoughMemoryException This exception is thrown when trying to deserialize a position that exceeds the internal memory size.
2322
     */
2323
    Cdr_DllAPI Cdr& deserialize_array(
2324
            int16_t* short_t,
2325
            size_t num_elements);
2326
2327
    /*!
2328
     * @brief This function deserializes an array of unsigned longs.
2329
     * @param ulong_t The variable that will store the array of unsigned longs read from the buffer.
2330
     * @param num_elements Number of the elements in the array.
2331
     * @return Reference to the eprosima::fastcdr::Cdr object.
2332
     * @exception exception::NotEnoughMemoryException This exception is thrown when trying to deserialize a position that exceeds the internal memory size.
2333
     */
2334
    TEMPLATE_SPEC
2335
    Cdr& deserialize_array(
2336
            uint32_t* ulong_t,
2337
            size_t num_elements)
2338
0
    {
2339
0
        return deserialize_array(reinterpret_cast<int32_t*>(ulong_t), num_elements);
2340
0
    }
2341
2342
    /*!
2343
     * @brief This function deserializes an array of longs.
2344
     * @param long_t The variable that will store the array of longs read from the buffer.
2345
     * @param num_elements Number of the elements in the array.
2346
     * @return Reference to the eprosima::fastcdr::Cdr object.
2347
     * @exception exception::NotEnoughMemoryException This exception is thrown when trying to deserialize a position that exceeds the internal memory size.
2348
     */
2349
    Cdr_DllAPI Cdr& deserialize_array(
2350
            int32_t* long_t,
2351
            size_t num_elements);
2352
2353
    /*!
2354
     * @brief This function deserializes an array of wide-chars.
2355
     * @param wchar The variable that will store the array of wide-chars read from the buffer.
2356
     * @param num_elements Number of the elements in the array.
2357
     * @return Reference to the eprosima::fastcdr::Cdr object.
2358
     * @exception exception::NotEnoughMemoryException This exception is thrown when trying to deserialize a position that exceeds the internal memory size.
2359
     */
2360
    Cdr_DllAPI Cdr& deserialize_array(
2361
            wchar_t* wchar,
2362
            size_t num_elements);
2363
2364
    /*!
2365
     * @brief This function deserializes an array of unsigned long longs.
2366
     * @param ulonglong_t The variable that will store the array of unsigned long longs read from the buffer.
2367
     * @param num_elements Number of the elements in the array.
2368
     * @return Reference to the eprosima::fastcdr::Cdr object.
2369
     * @exception exception::NotEnoughMemoryException This exception is thrown when trying to deserialize a position that exceeds the internal memory size.
2370
     */
2371
    TEMPLATE_SPEC
2372
    Cdr& deserialize_array(
2373
            uint64_t* ulonglong_t,
2374
            size_t num_elements)
2375
0
    {
2376
0
        return deserialize_array(reinterpret_cast<int64_t*>(ulonglong_t), num_elements);
2377
0
    }
2378
2379
    /*!
2380
     * @brief This function deserializes an array of long longs.
2381
     * @param longlong_t The variable that will store the array of long longs read from the buffer.
2382
     * @param num_elements Number of the elements in the array.
2383
     * @return Reference to the eprosima::fastcdr::Cdr object.
2384
     * @exception exception::NotEnoughMemoryException This exception is thrown when trying to deserialize a position that exceeds the internal memory size.
2385
     */
2386
    Cdr_DllAPI Cdr& deserialize_array(
2387
            int64_t* longlong_t,
2388
            size_t num_elements);
2389
2390
    /*!
2391
     * @brief This function deserializes an array of floats.
2392
     * @param float_t The variable that will store the array of floats read from the buffer.
2393
     * @param num_elements Number of the elements in the array.
2394
     * @return Reference to the eprosima::fastcdr::Cdr object.
2395
     * @exception exception::NotEnoughMemoryException This exception is thrown when trying to deserialize a position that exceeds the internal memory size.
2396
     */
2397
    Cdr_DllAPI Cdr& deserialize_array(
2398
            float* float_t,
2399
            size_t num_elements);
2400
2401
    /*!
2402
     * @brief This function deserializes an array of doubles.
2403
     * @param double_t The variable that will store the array of doubles read from the buffer.
2404
     * @param num_elements Number of the elements in the array.
2405
     * @return Reference to the eprosima::fastcdr::Cdr object.
2406
     * @exception exception::NotEnoughMemoryException This exception is thrown when trying to deserialize a position that exceeds the internal memory size.
2407
     */
2408
    Cdr_DllAPI Cdr& deserialize_array(
2409
            double* double_t,
2410
            size_t num_elements);
2411
2412
    /*!
2413
     * @brief This function deserializes an array of long doubles.
2414
     * @param ldouble_t The variable that will store the array of long doubles read from the buffer.
2415
     * @param num_elements Number of the elements in the array.
2416
     * @return Reference to the eprosima::fastcdr::Cdr object.
2417
     * @exception exception::NotEnoughMemoryException This exception is thrown when trying to deserialize a position that exceeds the internal memory size.
2418
     * @note Due to internal representation differences, WIN32 and *NIX like systems are not compatible.
2419
     */
2420
    Cdr_DllAPI Cdr& deserialize_array(
2421
            long double* ldouble_t,
2422
            size_t num_elements);
2423
2424
    /*!
2425
     * @brief This function deserializes an array of booleans.
2426
     * @param bool_t The variable that will store the array of booleans read from the buffer.
2427
     * @param num_elements Number of the elements in the array.
2428
     * @return Reference to the eprosima::fastcdr::Cdr object.
2429
     * @exception exception::NotEnoughMemoryException This exception is thrown when trying to deserialize a position that exceeds the internal memory size.
2430
     */
2431
    Cdr_DllAPI Cdr& deserialize_array(
2432
            bool* bool_t,
2433
            size_t num_elements);
2434
2435
    /*!
2436
     * @brief Decodes an array of primitives on a std::vector.
2437
     *
2438
     * std::vector must have allocated the number of element of the array.
2439
     *
2440
     * @param[out] value Reference to the std::vector where the array will be stored after decoding from the buffer.
2441
     * @return Reference to the eprosima::fastcdr::Cdr object.
2442
     * @exception exception::NotEnoughMemoryException This exception is thrown when trying to decode from a buffer
2443
     * position that exceeds the internal memory size.
2444
     */
2445
    template<class _T, typename std::enable_if<std::is_enum<_T>::value ||
2446
            std::is_arithmetic<_T>::value>::type* = nullptr>
2447
    Cdr& deserialize_array(
2448
            std::vector<_T>& value)
2449
    {
2450
        deserialize_array(value.data(), value.size());
2451
2452
        return *this;
2453
    }
2454
2455
    /*!
2456
     * @brief Decodes an array of non-primitives on a std::vector.
2457
     *
2458
     * std::vector must have allocated the number of element of the array.
2459
     *
2460
     * @param[out] value Reference to the std::vector where the array will be stored after decoding from the buffer.
2461
     * @return Reference to the eprosima::fastcdr::Cdr object.
2462
     * @exception exception::NotEnoughMemoryException This exception is thrown when trying to decode from a buffer
2463
     * position that exceeds the internal memory size.
2464
     */
2465
    template<class _T, typename std::enable_if<!std::is_enum<_T>::value &&
2466
            !std::is_arithmetic<_T>::value>::type* = nullptr>
2467
    Cdr& deserialize_array(
2468
            std::vector<_T>& value)
2469
    {
2470
        state state_before_error(*this);
2471
2472
        if (CdrVersion::XCDRv2 == cdr_version_)
2473
        {
2474
            uint32_t dheader {0};
2475
            deserialize(dheader);
2476
2477
            if ((end_ - offset_) < dheader)
2478
            {
2479
                set_state(state_before_error);
2480
                throw exception::NotEnoughMemoryException(
2481
                          exception::NotEnoughMemoryException::NOT_ENOUGH_MEMORY_MESSAGE_DEFAULT);
2482
            }
2483
2484
            uint32_t count {0};
2485
            auto last_offset = offset_;
2486
            last_offset += dheader;
2487
            try
2488
            {
2489
                while (last_offset - offset_ > 0 && count < value.size())
2490
                {
2491
                    deserialize_array(&value.data()[count], 1);
2492
                    ++count;
2493
                }
2494
            }
2495
            catch (exception::Exception& ex)
2496
            {
2497
                set_state(state_before_error);
2498
                ex.raise();
2499
            }
2500
2501
            if (offset_ != last_offset)
2502
            {
2503
                set_state(state_before_error);
2504
                throw exception::BadParamException("Member size greater than size specified by DHEADER");
2505
            }
2506
        }
2507
        else
2508
        {
2509
            return deserialize_array(value.data(), value.size());
2510
        }
2511
2512
        return *this;
2513
    }
2514
2515
    /*!
2516
     * @brief Decodes an array of non-primitives on a std::vector with a different endianness.
2517
     *
2518
     * std::vector must have allocated the number of element of the array.
2519
     *
2520
     * @param[out] value Reference to the std::vector where the array will be stored after decoding from the buffer.
2521
     * @param[in] endianness Endianness that will be used in the serialization of this value.
2522
     * @return Reference to the eprosima::fastcdr::Cdr object.
2523
     * @exception exception::NotEnoughMemoryException This exception is thrown when trying to decode from a buffer
2524
     * position that exceeds the internal memory size.
2525
     */
2526
    template<class _T>
2527
    Cdr& deserialize_array(
2528
            std::vector<_T>& value,
2529
            Endianness endianness)
2530
    {
2531
        bool aux_swap = swap_bytes_;
2532
        swap_bytes_ = (swap_bytes_ && (static_cast<Endianness>(endianness_) == endianness)) ||
2533
                (!swap_bytes_ && (static_cast<Endianness>(endianness_) != endianness));
2534
2535
        try
2536
        {
2537
            deserialize_array(value);
2538
            swap_bytes_ = aux_swap;
2539
        }
2540
        catch (exception::Exception& ex)
2541
        {
2542
            swap_bytes_ = aux_swap;
2543
            ex.raise();
2544
        }
2545
2546
        return *this;
2547
    }
2548
2549
    /*!
2550
     * @brief Decodes an array of booleans on a std::vector.
2551
     *
2552
     * std::vector must have allocated the number of element of the array.
2553
     *
2554
     * @param[out] value Reference to the std::vector where the array will be stored after decoding from the buffer.
2555
     * @return Reference to the eprosima::fastcdr::Cdr object.
2556
     * @exception exception::NotEnoughMemoryException This exception is thrown when trying to encode into a buffer
2557
     * position that exceeds the internal memory size.
2558
     */
2559
    TEMPLATE_SPEC
2560
    Cdr& deserialize_array(
2561
            std::vector<bool>& value)
2562
0
    {
2563
0
        deserialize_bool_array(value);
2564
0
2565
0
        return *this;
2566
0
    }
2567
2568
    /*!
2569
     * @brief This function template deserializes a raw sequence of non-primitives.
2570
     * This function allocates memory to store the sequence. The user pointer will be set to point this allocated memory.
2571
     * The user will have to free this allocated memory using free()
2572
     * @param sequence_t The pointer that will store the sequence read from the buffer.
2573
     * @param num_elements This variable return the number of elements of the sequence.
2574
     * @return Reference to the eprosima::fastcdr::Cdr object.
2575
     * @exception exception::NotEnoughMemoryException This exception is thrown when trying to deserialize a position that exceeds the internal memory size.
2576
     */
2577
    template<class _T, typename std::enable_if<!std::is_enum<_T>::value &&
2578
            !std::is_arithmetic<_T>::value>::type* = nullptr>
2579
    Cdr& deserialize_sequence(
2580
            _T*& sequence_t,
2581
            size_t& num_elements)
2582
    {
2583
        uint32_t sequence_length {0};
2584
        state state_before_error(*this);
2585
2586
        if (CdrVersion::XCDRv2 == cdr_version_)
2587
        {
2588
            uint32_t dheader {0};
2589
            deserialize(dheader);
2590
2591
            if (((end_ - offset_) < dheader) || (dheader < 4))
2592
            {
2593
                set_state(state_before_error);
2594
                throw exception::NotEnoughMemoryException(
2595
                          exception::NotEnoughMemoryException::NOT_ENOUGH_MEMORY_MESSAGE_DEFAULT);
2596
            }
2597
2598
            auto last_offset = offset_;
2599
            last_offset += dheader;
2600
2601
            deserialize(sequence_length);
2602
            if (0 == sequence_length)
2603
            {
2604
                sequence_t = NULL;
2605
                num_elements = 0;
2606
                return *this;
2607
            }
2608
2609
            if ((last_offset - offset_) < sequence_length)
2610
            {
2611
                set_state(state_before_error);
2612
                throw exception::NotEnoughMemoryException(
2613
                          exception::NotEnoughMemoryException::NOT_ENOUGH_MEMORY_MESSAGE_DEFAULT);
2614
            }
2615
2616
            try
2617
            {
2618
                sequence_t = reinterpret_cast<_T*>(calloc(sequence_length, sizeof(_T)));
2619
2620
                uint32_t count {0};
2621
                while (last_offset - offset_ > 0 && count < sequence_length)
2622
                {
2623
                    deserialize(sequence_t[count]);
2624
                    ++count;
2625
                }
2626
2627
                if (last_offset - offset_ != 0)
2628
                {
2629
                    throw exception::BadParamException("Member size greater than size specified by DHEADER");
2630
                }
2631
            }
2632
            catch (exception::Exception& ex)
2633
            {
2634
                free(sequence_t);
2635
                sequence_t = NULL;
2636
                set_state(state_before_error);
2637
                ex.raise();
2638
            }
2639
        }
2640
        else
2641
        {
2642
            deserialize(sequence_length);
2643
2644
            if ((end_ - offset_) < sequence_length)
2645
            {
2646
                set_state(state_before_error);
2647
                throw exception::NotEnoughMemoryException(
2648
                          exception::NotEnoughMemoryException::NOT_ENOUGH_MEMORY_MESSAGE_DEFAULT);
2649
            }
2650
2651
            try
2652
            {
2653
                sequence_t = reinterpret_cast<_T*>(calloc(sequence_length, sizeof(_T)));
2654
                deserialize_array(sequence_t, sequence_length);
2655
            }
2656
            catch (exception::Exception& ex)
2657
            {
2658
                free(sequence_t);
2659
                sequence_t = NULL;
2660
                set_state(state_before_error);
2661
                ex.raise();
2662
            }
2663
        }
2664
2665
        num_elements = sequence_length;
2666
        return *this;
2667
    }
2668
2669
    /*!
2670
     * @brief This function template deserializes a raw sequence of primitives.
2671
     * This function allocates memory to store the sequence. The user pointer will be set to point this allocated memory.
2672
     * The user will have to free this allocated memory using free()
2673
     * @param sequence_t The pointer that will store the sequence read from the buffer.
2674
     * @param num_elements This variable return the number of elements of the sequence.
2675
     * @return Reference to the eprosima::fastcdr::Cdr object.
2676
     * @exception exception::NotEnoughMemoryException This exception is thrown when trying to deserialize a position that exceeds the internal memory size.
2677
     */
2678
    template<class _T, typename std::enable_if<std::is_enum<_T>::value ||
2679
            std::is_arithmetic<_T>::value>::type* = nullptr>
2680
    Cdr& deserialize_sequence(
2681
            _T*& sequence_t,
2682
            size_t& num_elements)
2683
    {
2684
        uint32_t sequence_length = 0;
2685
        state state_before_error(*this);
2686
2687
        deserialize(sequence_length);
2688
2689
        try
2690
        {
2691
            sequence_t = reinterpret_cast<_T*>(calloc(sequence_length, sizeof(_T)));
2692
            deserialize_array(sequence_t, sequence_length);
2693
        }
2694
        catch (exception::Exception& ex)
2695
        {
2696
            free(sequence_t);
2697
            sequence_t = NULL;
2698
            set_state(state_before_error);
2699
            ex.raise();
2700
        }
2701
2702
        num_elements = sequence_length;
2703
        return *this;
2704
    }
2705
2706
    /*!
2707
     * @brief This function template deserializes a raw sequence with a different endianness.
2708
     * This function allocates memory to store the sequence. The user pointer will be set to point this allocated memory.
2709
     * The user will have to free this allocated memory using free()
2710
     * @param sequence_t The pointer that will store the sequence read from the buffer.
2711
     * @param num_elements This variable return the number of elements of the sequence.
2712
     * @param endianness Endianness that will be used in the deserialization of this value.
2713
     * @return Reference to the eprosima::fastcdr::Cdr object.
2714
     * @exception exception::NotEnoughMemoryException This exception is thrown when trying to deserialize a position that exceeds the internal memory size.
2715
     */
2716
    template<class _T>
2717
    Cdr& deserialize_sequence(
2718
            _T*& sequence_t,
2719
            size_t& num_elements,
2720
            Endianness endianness)
2721
    {
2722
        bool aux_swap = swap_bytes_;
2723
        swap_bytes_ = (swap_bytes_ && (static_cast<Endianness>(endianness_) == endianness)) ||
2724
                (!swap_bytes_ && (static_cast<Endianness>(endianness_) != endianness));
2725
2726
        try
2727
        {
2728
            deserialize_sequence(sequence_t, num_elements);
2729
            swap_bytes_ = aux_swap;
2730
        }
2731
        catch (exception::Exception& ex)
2732
        {
2733
            swap_bytes_ = aux_swap;
2734
            ex.raise();
2735
        }
2736
2737
        return *this;
2738
    }
2739
2740
    /*!
2741
     * @brief This function template deserializes a string sequence.
2742
     * This function allocates memory to store the sequence. The user pointer will be set to point this allocated memory.
2743
     * The user will have to free this allocated memory using free()
2744
     * @param sequence_t The pointer that will store the sequence read from the buffer.
2745
     * @param num_elements This variable return the number of elements of the sequence.
2746
     * @return Reference to the eprosima::fastcdr::Cdr object.
2747
     * @exception exception::NotEnoughMemoryException This exception is thrown when trying to deserialize a position that exceeds the internal memory size.
2748
     */
2749
    TEMPLATE_SPEC
2750
    Cdr& deserialize_sequence(
2751
            std::string*& sequence_t,
2752
            size_t& num_elements)
2753
0
    {
2754
0
        return deserialize_string_sequence(sequence_t, num_elements);
2755
0
    }
2756
2757
    /*!
2758
     * @brief This function template deserializes a wide-string sequence.
2759
     * This function allocates memory to store the sequence. The user pointer will be set to point this allocated memory.
2760
     * The user will have to free this allocated memory using free()
2761
     * @param sequence_t The pointer that will store the sequence read from the buffer.
2762
     * @param num_elements This variable return the number of elements of the sequence.
2763
     * @return Reference to the eprosima::fastcdr::Cdr object.
2764
     * @exception exception::NotEnoughMemoryException This exception is thrown when trying to deserialize a position that exceeds the internal memory size.
2765
     */
2766
    TEMPLATE_SPEC
2767
    Cdr& deserialize_sequence(
2768
            std::wstring*& sequence_t,
2769
            size_t& num_elements)
2770
0
    {
2771
0
        return deserialize_wstring_sequence(sequence_t, num_elements);
2772
0
    }
2773
2774
    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
2775
    /// XCDR extensions
2776
    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
2777
2778
    /*!
2779
     * @brief Encodes a member of a type according to the encoding algorithm used.
2780
     * @param[in] member_id Member identifier.
2781
     * @param[in] member_value Member value.
2782
     * @param[in] header_selection Selects which member header will be used to allocate space.
2783
     * Default: XCdrHeaderSelection::AUTO_WITH_SHORT_HEADER_BY_DEFAULT.
2784
     * @return Reference to the eprosima::fastcdr::Cdr object.
2785
     * @exception exception::NotEnoughMemoryException This exception is thrown when trying to encode into a buffer
2786
     * position that exceeds the internal memory size.
2787
     */
2788
    template<class _T>
2789
    Cdr& serialize_member(
2790
            const MemberId& member_id,
2791
            const _T& member_value,
2792
            XCdrHeaderSelection header_selection = XCdrHeaderSelection::AUTO_WITH_SHORT_HEADER_BY_DEFAULT)
2793
0
    {
2794
0
        Cdr::state current_state(*this);
2795
0
        (this->*begin_serialize_member_)(member_id, true, current_state, header_selection);
2796
0
        serialize(member_value);
2797
0
        return (this->*end_serialize_member_)(current_state);
2798
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)
2799
2800
    /*!
2801
     * @brief Encodes an optional member of a type according to the encoding algorithm used.
2802
     * @param[in] member_id Member identifier.
2803
     * @param[in] member_value Optional member value.
2804
     * @param[in] header_selection Selects which member header will be used to allocate space.
2805
     * Default: XCdrHeaderSelection::AUTO_WITH_SHORT_HEADER_BY_DEFAULT.
2806
     * @return Reference to the eprosima::fastcdr::Cdr object.
2807
     * @exception exception::NotEnoughMemoryException This exception is thrown when trying to encode into a buffer
2808
     * position that exceeds the internal memory size.
2809
     */
2810
    template<class _T>
2811
    Cdr& serialize_member(
2812
            const MemberId& member_id,
2813
            const optional<_T>& member_value,
2814
            XCdrHeaderSelection header_selection = XCdrHeaderSelection::AUTO_WITH_SHORT_HEADER_BY_DEFAULT)
2815
    {
2816
        Cdr::state current_state(*this);
2817
        (this->*begin_serialize_opt_member_)(member_id, member_value.has_value(), current_state, header_selection);
2818
        serialize(member_value);
2819
        return (this->*end_serialize_opt_member_)(current_state);
2820
    }
2821
2822
    /*!
2823
     * @brief Decodes a member of a type according to the encoding algorithm used.
2824
     * @param[out] member_value A reference of the variable where the member value will be stored.
2825
     * @return Reference to the eprosima::fastcdr::Cdr object.
2826
     * @exception exception::NotEnoughMemoryException This exception is thrown when trying to decode from a buffer
2827
     * position that exceeds the internal memory size.
2828
     */
2829
    template<class _T>
2830
    Cdr& deserialize_member(
2831
            _T& member_value)
2832
0
    {
2833
0
        return deserialize(member_value);
2834
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&)
2835
2836
    /*!
2837
     * @brief Decodes an optional member of a type according to the encoding algorithm used.
2838
     * @param[out] member_value A reference of the variable where the optional member value will be stored.
2839
     * @return Reference to the eprosima::fastcdr::Cdr object.
2840
     * @exception exception::NotEnoughMemoryException This exception is thrown when trying to decode from a buffer
2841
     * position that exceeds the internal memory size.
2842
     */
2843
    template<class _T>
2844
    Cdr& deserialize_member(
2845
            optional<_T>& member_value)
2846
    {
2847
        if (EncodingAlgorithmFlag::PLAIN_CDR == current_encoding_)
2848
        {
2849
            Cdr::state current_state(*this);
2850
            MemberId member_id;
2851
            xcdr1_deserialize_member_header(member_id, current_state);
2852
            auto prev_offset = offset_;
2853
            member_value.reset(0 < current_state.member_size_);
2854
            if (0 < current_state.member_size_)
2855
            {
2856
                deserialize(member_value);
2857
            }
2858
            size_t member_size {current_state.member_size_};
2859
            size_t diff {offset_ - prev_offset};
2860
            if (member_size < diff)
2861
            {
2862
                throw exception::BadParamException(
2863
                          "Member size provided by member header is lower than real decoded member size");
2864
            }
2865
2866
            // Skip unused bytes
2867
            offset_ += (member_size - diff);
2868
        }
2869
        else
2870
        {
2871
            deserialize(member_value);
2872
        }
2873
        return *this;
2874
    }
2875
2876
    /*!
2877
     * @brief Decodes an optional member of an external according to the encoding algorithm used.
2878
     * @param[out] member_value A reference of the variable where the optional member value will be stored.
2879
     * @return Reference to the eprosima::fastcdr::Cdr object.
2880
     * @exception exception::NotEnoughMemoryException This exception is thrown when trying to decode from a buffer
2881
     * position that exceeds the internal memory size.
2882
     */
2883
    template<class _T>
2884
    Cdr& deserialize_member(
2885
            optional<external<_T>>& member_value)
2886
    {
2887
        if (member_value.has_value() && member_value.value().is_locked())
2888
        {
2889
            throw exception::BadParamException("External member is locked");
2890
        }
2891
2892
        if (EncodingAlgorithmFlag::PLAIN_CDR == current_encoding_)
2893
        {
2894
            Cdr::state current_state(*this);
2895
            MemberId member_id;
2896
            xcdr1_deserialize_member_header(member_id, current_state);
2897
            auto prev_offset = offset_;
2898
            member_value.reset(0 < current_state.member_size_);
2899
            if (0 < current_state.member_size_)
2900
            {
2901
                deserialize(member_value);
2902
            }
2903
            size_t member_size {current_state.member_size_};
2904
            size_t diff {offset_ - prev_offset};
2905
            if (member_size < diff)
2906
            {
2907
                throw exception::BadParamException(
2908
                          "Member size provided by member header is lower than real decoded member size");
2909
            }
2910
2911
            // Skip unused bytes
2912
            offset_ += (member_size - diff);
2913
        }
2914
        else
2915
        {
2916
            deserialize(member_value);
2917
        }
2918
        return *this;
2919
    }
2920
2921
    /*!
2922
     * @brief Tells to the encoder a new type and its members starts to be encoded.
2923
     * @param[in,out] current_state State of the encoder previous of calling this function.
2924
     * @param[in] type_encoding The encoding algorithm used to encode the type and its members.
2925
     * @return Reference to the eprosima::fastcdr::Cdr object.
2926
     * @exception exception::NotEnoughMemoryException This exception is thrown when trying to decode from a buffer
2927
     * position that exceeds the internal memory size.
2928
     */
2929
    Cdr_DllAPI Cdr& begin_serialize_type(
2930
            Cdr::state& current_state,
2931
            EncodingAlgorithmFlag type_encoding);
2932
2933
    /*!
2934
     * @brief Tells to the encoder the encoding of the type finishes.
2935
     * @param[in] current_state State of the encoder previous of calling the function begin_serialize_type.
2936
     * @return Reference to the eprosima::fastcdr::Cdr object.
2937
     * @exception exception::NotEnoughMemoryException This exception is thrown when trying to decode from a buffer
2938
     * position that exceeds the internal memory size.
2939
     */
2940
    Cdr_DllAPI Cdr& end_serialize_type(
2941
            Cdr::state& current_state);
2942
2943
    /*!
2944
     * @brief Tells to the encoder a new type and its members starts to be decoded.
2945
     * @param[in] type_encoding The encoding algorithm used to decode the type and its members.
2946
     * @param[in] functor Functor called each time a member has to be decoded.
2947
     * @return Reference to the eprosima::fastcdr::Cdr object.
2948
     * @exception exception::NotEnoughMemoryException This exception is thrown when trying to decode from a buffer
2949
     * position that exceeds the internal memory size.
2950
     */
2951
    Cdr_DllAPI Cdr& deserialize_type(
2952
            EncodingAlgorithmFlag type_encoding,
2953
            std::function<bool (Cdr&, const MemberId&)> functor);
2954
2955
    /*!
2956
     * @brief Encodes an optional in the buffer.
2957
     * @param[in] value A reference to the optional which will be encoded in the buffer.
2958
     * @return Reference to the eprosima::fastcdr::Cdr object.
2959
     * @exception exception::NotEnoughMemoryException This exception is thrown when trying to encode into a buffer
2960
     * position that exceeds the internal memory size.
2961
     */
2962
    template<class _T>
2963
    Cdr& serialize(
2964
            const optional<_T>& value)
2965
    {
2966
        if (CdrVersion::XCDRv2 == cdr_version_ && EncodingAlgorithmFlag::PL_CDR2 != current_encoding_)
2967
        {
2968
            serialize(value.has_value());
2969
        }
2970
2971
        if (value.has_value())
2972
        {
2973
            serialize(*value);
2974
        }
2975
        return *this;
2976
    }
2977
2978
    /*!
2979
     * @brief Encodes an external in the buffer.
2980
     * @param[in] value A reference to the external which will be encoded in the buffer.
2981
     * @return Reference to the eprosima::fastcdr::Cdr object.
2982
     * @exception exception::BadParamException This exception is thrown when external is null.
2983
     * @exception exception::NotEnoughMemoryException This exception is thrown when trying to encode into a buffer
2984
     * position that exceeds the internal memory size.
2985
     */
2986
    template<class _T>
2987
    Cdr& serialize(
2988
            const external<_T>& value)
2989
    {
2990
        if (!value)
2991
        {
2992
            throw exception::BadParamException("External member is null");
2993
        }
2994
2995
        serialize(*value);
2996
        return *this;
2997
    }
2998
2999
    /*!
3000
     * @brief Tells the encoder the member identifier for the next member to be encoded.
3001
     * @param[in] member_id Member identifier.
3002
     * @return Reference to the eprosima::fastcdr::Cdr object.
3003
     * @exception exception::BadParamException This exception is thrown when a member id is already set without being
3004
     * encoded.
3005
     */
3006
    Cdr_DllAPI Cdr& operator <<(
3007
            const MemberId& member_id);
3008
3009
    /*!
3010
     * @brief Decodes an optional from the buffer.
3011
     * @param[out] value A reference to the variable where the optional will be stored.
3012
     * @return Reference to the eprosima::fastcdr::Cdr object.
3013
     * @exception exception::NotEnoughMemoryException This exception is thrown when trying to decode from a buffer
3014
     * position that exceeds the internal memory size.
3015
     */
3016
    template<class _T>
3017
    Cdr& deserialize(
3018
            optional<_T>& value)
3019
    {
3020
        bool is_present = true;
3021
        if (CdrVersion::XCDRv2 == cdr_version_ && EncodingAlgorithmFlag::PL_CDR2 != current_encoding_)
3022
        {
3023
            deserialize(is_present);
3024
        }
3025
        value.reset(is_present);
3026
        if (is_present)
3027
        {
3028
            deserialize(*value);
3029
        }
3030
        return *this;
3031
    }
3032
3033
    /*!
3034
     * @brief Decodes an external from the buffer.
3035
     * @param[out] value A reference to the variable where the external will be stored.
3036
     * @return Reference to the eprosima::fastcdr::Cdr object.
3037
     * @exception exception::BadParamException This exception is thrown when the external is locked.
3038
     * @exception exception::NotEnoughMemoryException This exception is thrown when trying to decode from a buffer
3039
     * position that exceeds the internal memory size.
3040
     */
3041
    template<class _T>
3042
    Cdr& deserialize(
3043
            external<_T>& value)
3044
    {
3045
        if (value.is_locked())
3046
        {
3047
            throw exception::BadParamException("External member is locked");
3048
        }
3049
3050
        if (!value)
3051
        {
3052
            value = external<_T>{new typename external<_T>::type()};
3053
        }
3054
3055
        deserialize(*value);
3056
        return *this;
3057
    }
3058
3059
    /*!
3060
     * @brief Decodes an optional of an external from the buffer.
3061
     * @param[out] value A reference to the variable where the optional will be stored.
3062
     * @return Reference to the eprosima::fastcdr::Cdr object.
3063
     * @exception exception::BadParamException This exception is thrown when the external is locked.
3064
     * @exception exception::NotEnoughMemoryException This exception is thrown when trying to decode from a buffer
3065
     * position that exceeds the internal memory size.
3066
     */
3067
    template<class _T>
3068
    Cdr& deserialize(
3069
            optional<external<_T>>& value)
3070
    {
3071
        if (value.has_value() && value.value().is_locked())
3072
        {
3073
            throw exception::BadParamException("External member is locked");
3074
        }
3075
3076
        bool is_present = true;
3077
        if (CdrVersion::XCDRv2 == cdr_version_ && EncodingAlgorithmFlag::PL_CDR2 != current_encoding_)
3078
        {
3079
            deserialize(is_present);
3080
        }
3081
        value.reset(is_present);
3082
        if (is_present)
3083
        {
3084
            deserialize(*value);
3085
        }
3086
        return *this;
3087
    }
3088
3089
    /*!
3090
     * @brief Encodes an empty DHEADER if the encoding version is XCDRv2.
3091
     * After serializing the members's type, @ref set_xcdrv2_dheader must be called to set the correct DHEADER value
3092
     * using the @ref state returned by this function.
3093
     */
3094
    Cdr_DllAPI state allocate_xcdrv2_dheader();
3095
3096
    /*!
3097
     * @brief Uses the @ref state to calculate the member's type size and serialize the value in the previous allocated
3098
     * DHEADER.
3099
     *
3100
     * @param[in] state @ref state used to calculate the member's type size.
3101
     */
3102
    Cdr_DllAPI void set_xcdrv2_dheader(
3103
            const state& state);
3104
3105
private:
3106
3107
    Cdr(
3108
            const Cdr&) = delete;
3109
3110
    Cdr& operator =(
3111
            const Cdr&) = delete;
3112
3113
    Cdr_DllAPI Cdr& serialize_bool_array(
3114
            const std::vector<bool>& vector_t);
3115
3116
    Cdr_DllAPI Cdr& serialize_bool_sequence(
3117
            const std::vector<bool>& vector_t);
3118
3119
    Cdr_DllAPI Cdr& deserialize_bool_array(
3120
            std::vector<bool>& vector_t);
3121
3122
    Cdr_DllAPI Cdr& deserialize_bool_sequence(
3123
            std::vector<bool>& vector_t);
3124
3125
    Cdr_DllAPI Cdr& deserialize_string_sequence(
3126
            std::string*& sequence_t,
3127
            size_t& num_elements);
3128
3129
    Cdr_DllAPI Cdr& deserialize_wstring_sequence(
3130
            std::wstring*& sequence_t,
3131
            size_t& num_elements);
3132
3133
    /*!
3134
     * @brief Serializes the canonical @c uint8_t representation of @p bool_t.
3135
     *
3136
     * When @c FASTCDR_STRICT_BOOL is defined the serialized value is guaranteed to be exactly @c 0 or @c 1.
3137
     *
3138
     * @param[in] bool_t The boolean value to serialize.
3139
     */
3140
    Cdr_DllAPI void serialize_bool(
3141
            bool bool_t);
3142
3143
    /*!
3144
     * @brief This function template detects the content type of the STD container array and serializes the array.
3145
     * @param array_t The array that will be serialized in the buffer.
3146
     * @param num_elements Number of the elements in the array.
3147
     * @return Reference to the eprosima::fastcdr::Cdr object.
3148
     * @exception exception::NotEnoughMemoryException This exception is thrown when trying to serialize a position that exceeds the internal memory size.
3149
     */
3150
    template<class _T, size_t _Size>
3151
    Cdr& serialize_array(
3152
            const std::array<_T, _Size>* array_t,
3153
            size_t num_elements)
3154
    {
3155
        if (num_elements == 0 || array_t == nullptr)
3156
        {
3157
            return *this;
3158
        }
3159
        return serialize_array(array_t->data(), num_elements * array_t->size());
3160
    }
3161
3162
    /*!
3163
     * @brief This function template detects the content type of the STD container array and deserializes the array.
3164
     * @param array_t The variable that will store the array read from the buffer.
3165
     * @param num_elements Number of the elements in the array.
3166
     * @return Reference to the eprosima::fastcdr::Cdr object.
3167
     * @exception exception::NotEnoughMemoryException This exception is thrown when trying to deserialize a position that exceeds the internal memory size.
3168
     */
3169
    template<class _T, size_t _Size>
3170
    Cdr& deserialize_array(
3171
            std::array<_T, _Size>* array_t,
3172
            size_t num_elements)
3173
    {
3174
        if (num_elements == 0 || array_t == nullptr)
3175
        {
3176
            return *this;
3177
        }
3178
        return deserialize_array(array_t->data(), num_elements * array_t->size());
3179
    }
3180
3181
    /*!
3182
     * @brief This function template detects the content type of STD container array and deserializes the array with a different endianness.
3183
     * @param array_t The variable that will store the array read from the buffer.
3184
     * @param num_elements Number of the elements in the array.
3185
     * @param endianness Endianness that will be used in the deserialization of this value.
3186
     * @return Reference to the eprosima::fastcdr::Cdr object.
3187
     * @exception exception::NotEnoughMemoryException This exception is thrown when trying to deserialize a position that exceeds the internal memory size.
3188
     */
3189
    template<class _T, size_t _Size>
3190
    Cdr& deserialize_array(
3191
            std::array<_T, _Size>* array_t,
3192
            size_t num_elements,
3193
            Endianness endianness)
3194
    {
3195
        if (num_elements == 0 || array_t == nullptr)
3196
        {
3197
            return *this;
3198
        }
3199
        return deserialize_array(array_t->data(), num_elements * array_t->size(), endianness);
3200
    }
3201
3202
    /*!
3203
     * @brief Returns the number of bytes needed to align the current position (having as reference the origin) to
3204
     * certain data size.
3205
     * @param data_size The size of the data that will be serialized.
3206
     * @return The size needed for the alignment.
3207
     */
3208
    inline size_t alignment(
3209
            size_t data_size) const
3210
183k
    {
3211
183k
        return data_size > last_data_size_ ? (data_size - ((offset_ - origin_) % data_size)) & (data_size - 1) : 0;
3212
183k
    }
3213
3214
    /*!
3215
     * @brief This function jumps the number of bytes of the alignment. These bytes should be calculated with the function eprosima::fastcdr::Cdr::alignment.
3216
     * @param align The number of bytes to be skipped.
3217
     */
3218
    inline void make_alignment(
3219
            size_t align)
3220
171k
    {
3221
171k
        offset_ += align;
3222
171k
        last_data_size_ = 0;
3223
171k
    }
3224
3225
    /*!
3226
     * @brief This function resizes the internal buffer. It only applies if the FastBuffer object was created with the default constructor.
3227
     * @param min_size_inc Minimun size increase for the internal buffer
3228
     * @return True if the resize was succesful, false if it was not
3229
     */
3230
    bool resize(
3231
            size_t min_size_inc);
3232
3233
    Cdr_DllAPI const char* read_string(
3234
            uint32_t& length);
3235
    Cdr_DllAPI const std::wstring read_wstring(
3236
            uint32_t& length);
3237
3238
    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
3239
    /// XCDR extensions
3240
    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
3241
    /*!
3242
     * @brief Encodes a short member header of a member according to XCDRv1.
3243
     * @param[in] member_id Member identifier.
3244
     * @pre Member identifier less than 0x3F00.
3245
     * @exception exception::NotEnoughMemoryException This exception is thrown when trying to encode into a buffer
3246
     * position that exceeds the internal memory size.
3247
     */
3248
    void xcdr1_serialize_short_member_header(
3249
            const MemberId& member_id);
3250
3251
    /*!
3252
     * @brief Finish the encoding of a short member header of a member according to XCDRv1.
3253
     * @param[in] member_id Member identifier.
3254
     * @pre Member identifier less than 0x3F00.
3255
     * @param[in] member_serialized_size Size of the serialized member.
3256
     * @pre Serialized size equal or less than 0xFFFF.
3257
     * @exception exception::NotEnoughMemoryException This exception is thrown when trying to encode into a buffer
3258
     * position that exceeds the internal memory size.
3259
     */
3260
    void xcdr1_end_short_member_header(
3261
            const MemberId& member_id,
3262
            size_t member_serialized_size);
3263
3264
    /*!
3265
     * @brief Encodes a long member header of a member according to XCDRv1.
3266
     * @param[in] member_id Member identifier.
3267
     * @exception exception::NotEnoughMemoryException This exception is thrown when trying to encode into a buffer
3268
     * position that exceeds the internal memory size.
3269
     */
3270
    void xcdr1_serialize_long_member_header(
3271
            const MemberId& member_id);
3272
3273
    /*!
3274
     * @brief Finish the encoding of a long member header of a member according to XCDRv1.
3275
     * @param[in] member_id Member identifier.
3276
     * @param[in] member_serialized_size Size of the serialized member.
3277
     * @exception exception::NotEnoughMemoryException This exception is thrown when trying to encode into a buffer
3278
     * position that exceeds the internal memory size.
3279
     */
3280
    void xcdr1_end_long_member_header(
3281
            const MemberId& member_id,
3282
            size_t member_serialized_size);
3283
3284
    /*!
3285
     * @brief Changes the previous encoded long header to a short header according to XCDRv1.
3286
     * @param[in] member_id Member identifier.
3287
     * @pre Member identifier less than 0x3F00.
3288
     * @param[in] member_serialized_size Size of the serialized member.
3289
     * @pre Serialized size equal or less than 0xFFFF.
3290
     * @exception exception::NotEnoughMemoryException This exception is thrown when trying to encode into a buffer
3291
     * position that exceeds the internal memory size.
3292
     */
3293
    void xcdr1_change_to_short_member_header(
3294
            const MemberId& member_id,
3295
            size_t member_serialized_size);
3296
3297
    /*!
3298
     * @brief Changes the previous encoded short header to a long header according to XCDRv1.
3299
     * @param[in] member_id Member identifier.
3300
     * @param[in] member_serialized_size Size of the serialized member.
3301
     * @exception exception::NotEnoughMemoryException This exception is thrown when trying to encode into a buffer
3302
     * position that exceeds the internal memory size.
3303
     */
3304
    void xcdr1_change_to_long_member_header(
3305
            const MemberId& member_id,
3306
            size_t member_serialized_size);
3307
3308
    /*!
3309
     * @brief Decodes a member header according to XCDRv1.
3310
     * @param[out] member_id Member identifier.
3311
     * @param[in,out] current_state State of the encoder previous to call this function.
3312
     * @exception exception::NotEnoughMemoryException This exception is thrown when trying to decode from a buffer
3313
     * position that exceeds the internal memory size.
3314
     * @exception exception::BadParamException This exception is thrown when trying to decode an invalid value.
3315
     */
3316
    Cdr_DllAPI bool xcdr1_deserialize_member_header(
3317
            MemberId& member_id,
3318
            Cdr::state& current_state);
3319
3320
    /*!
3321
     * @brief Encodes a short member header of a member according to XCDRv2.
3322
     * @param[in] member_id Member identifier.
3323
     * @pre Member identifier less than 0x10000000.
3324
     * @exception exception::NotEnoughMemoryException This exception is thrown when trying to encode into a buffer
3325
     * position that exceeds the internal memory size.
3326
     */
3327
    void xcdr2_serialize_short_member_header(
3328
            const MemberId& member_id);
3329
3330
    /*!
3331
     * @brief Finish the encoding of a short member header of a member according to XCDRv2.
3332
     * @param[in] member_id Member identifier.
3333
     * @pre Member identifier less than 0x10000000.
3334
     * @param[in] member_serialized_size Size of the serialized member.
3335
     * @pre Serialized size equal or less than 0x8.
3336
     * @exception exception::NotEnoughMemoryException This exception is thrown when trying to encode into a buffer
3337
     * position that exceeds the internal memory size.
3338
     */
3339
    void xcdr2_end_short_member_header(
3340
            const MemberId& member_id,
3341
            size_t member_serialized_size);
3342
3343
    /*!
3344
     * @brief Encodes a long member header of a member according to XCDRv2.
3345
     * @param[in] member_id Member identifier.
3346
     * @pre Member identifier less than 0x10000000.
3347
     * @exception exception::NotEnoughMemoryException This exception is thrown when trying to encode into a buffer
3348
     * position that exceeds the internal memory size.
3349
     */
3350
    void xcdr2_serialize_long_member_header(
3351
            const MemberId& member_id);
3352
3353
    /*!
3354
     * @brief Finish the encoding of a long member header of a member according to XCDRv2.
3355
     * @param[in] member_id Member identifier.
3356
     * @pre Member identifier less than 0x10000000.
3357
     * @param[in] member_serialized_size Size of the serialized member.
3358
     * @exception exception::NotEnoughMemoryException This exception is thrown when trying to encode into a buffer
3359
     * position that exceeds the internal memory size.
3360
     */
3361
    void xcdr2_end_long_member_header(
3362
            const MemberId& member_id,
3363
            size_t member_serialized_size);
3364
3365
    /*!
3366
     * @brief Changes the previous encoded long header to a short header according to XCDRv2.
3367
     * @param[in] member_id Member identifier.
3368
     * @pre Member identifier less than 0x10000000.
3369
     * @param[in] member_serialized_size Size of the serialized member.
3370
     * @pre Serialized size equal or less than 8.
3371
     * @exception exception::NotEnoughMemoryException This exception is thrown when trying to encode into a buffer
3372
     * position that exceeds the internal memory size.
3373
     */
3374
    void xcdr2_change_to_short_member_header(
3375
            const MemberId& member_id,
3376
            size_t member_serialized_size);
3377
3378
    /*!
3379
     * @brief Changes the previous encoded long header to a short header according to XCDRv2.
3380
     * @param[in] member_id Member identifier.
3381
     * @pre Member identifier less than 0x10000000.
3382
     * @param[in] member_serialized_size Size of the serialized member.
3383
     * @exception exception::NotEnoughMemoryException This exception is thrown when trying to encode into a buffer
3384
     * position that exceeds the internal memory size.
3385
     */
3386
    void xcdr2_change_to_long_member_header(
3387
            const MemberId& member_id,
3388
            size_t member_serialized_size);
3389
3390
    /*!
3391
     * @brief Join the previous encoded long header with the next DHEADER which was serialized after.
3392
     * @param[in] member_id Member identifier.
3393
     * @pre Member identifier less than 0x10000000.
3394
     * @param[in] offset The last offset of the buffer previous to call this function.
3395
     * @exception exception::NotEnoughMemoryException This exception is thrown when trying to encode into a buffer
3396
     * position that exceeds the internal memory size.
3397
     */
3398
    void xcdr2_shrink_to_long_member_header(
3399
            const MemberId& member_id,
3400
            const FastBuffer::iterator& offset);
3401
3402
    /*!
3403
     * @brief Decodes a member header according to XCDRv2.
3404
     * @param[out] member_id Member identifier.
3405
     * @param[in,out] current_state State of the encoder previous to call this function.
3406
     * @exception exception::NotEnoughMemoryException This exception is thrown when trying to decode from a buffer
3407
     * position that exceeds the internal memory size.
3408
     * @exception exception::BadParamException This exception is thrown when trying to decode an invalid value.
3409
     */
3410
    void xcdr2_deserialize_member_header(
3411
            MemberId& member_id,
3412
            Cdr::state& current_state);
3413
3414
    /*!
3415
     * @brief Tells to the encoder a member starts to be encoded according to XCDRv1.
3416
     * @param[in] member_id Member identifier.
3417
     * @pre Member identifier cannot be MEMBER_ID_INVALID and next_member_id_ must be equal to the member identifier or
3418
     * MEMBER_ID_INVALID.
3419
     * @param[in] is_present If the member is present.
3420
     * @pre When XCDRv1, is_present must be always true.
3421
     * @param[in,out] current_state State of the encoder previous to call this function.
3422
     * @pre Current encoding algorithm must be EncodingAlgorithmFlag::PLAIN_CDR or EncodingAlgorithmFlag::PL_CDR.
3423
     * @param[in] header_selection Selects which member header will be used to allocate space.
3424
     * @return Reference to the eprosima::fastcdr::Cdr object.
3425
     * @exception exception::NotEnoughMemoryException This exception is thrown when trying to encode into a buffer
3426
     * position that exceeds the internal memory size.
3427
     * @exception exception::BadParamException This exception is thrown when trying to encode a long header when
3428
     * header_selection is XCdrHeaderSelection::SHORT_HEADER.
3429
     */
3430
    Cdr& xcdr1_begin_serialize_member(
3431
            const MemberId& member_id,
3432
            bool is_present,
3433
            Cdr::state& current_state,
3434
            XCdrHeaderSelection header_selection);
3435
3436
    /*!
3437
     * @brief Tells to the encoder to finish the encoding of the member.
3438
     * @param[in] current_state State of the encoder previous to call xcdr1_begin_serialize_member function.
3439
     * @pre next_member_id_ cannot be MEMBER_ID_INVALID.
3440
     * @return Reference to the eprosima::fastcdr::Cdr object.
3441
     * @exception exception::NotEnoughMemoryException This exception is thrown when trying to encode into a buffer
3442
     * position that exceeds the internal memory size.
3443
     * @exception exception::BadParamException This exception is thrown when trying to encode a long header when
3444
     * header_selection is XCdrHeaderSelection::SHORT_HEADER.
3445
     */
3446
    Cdr& xcdr1_end_serialize_member(
3447
            const Cdr::state& current_state);
3448
3449
    /*!
3450
     * @brief Tells to the encoder a member starts to be encoded according to XCDRv1.
3451
     * @param[in] member_id Member identifier.
3452
     * @pre Member identifier cannot be MEMBER_ID_INVALID and next_member_id_ must be equal to the member identifier or
3453
     * MEMBER_ID_INVALID.
3454
     * @param[in] is_present If the member is present.
3455
     * @param[in,out] current_state State of the encoder previous to call this function.
3456
     * @pre Current encoding algorithm must be EncodingAlgorithmFlag::PLAIN_CDR or EncodingAlgorithmFlag::PL_CDR.
3457
     * @param[in] header_selection Selects which member header will be used to allocate space.
3458
     * @return Reference to the eprosima::fastcdr::Cdr object.
3459
     * @exception exception::NotEnoughMemoryException This exception is thrown when trying to encode into a buffer
3460
     * position that exceeds the internal memory size.
3461
     * @exception exception::BadParamException This exception is thrown when trying to encode a long header when
3462
     * header_selection is XCdrHeaderSelection::SHORT_HEADER.
3463
     */
3464
    Cdr& xcdr1_begin_serialize_opt_member(
3465
            const MemberId& member_id,
3466
            bool is_present,
3467
            Cdr::state& current_state,
3468
            XCdrHeaderSelection header_selection);
3469
3470
    /*!
3471
     * @brief Tells to the encoder to finish the encoding of the member.
3472
     * @param[in] current_state State of the encoder previous to call xcdr1_begin_serialize_opt_member function.
3473
     * @pre next_member_id_ cannot be MEMBER_ID_INVALID.
3474
     * @return Reference to the eprosima::fastcdr::Cdr object.
3475
     * @exception exception::NotEnoughMemoryException This exception is thrown when trying to encode into a buffer
3476
     * position that exceeds the internal memory size.
3477
     * @exception exception::BadParamException This exception is thrown when trying to encode a long header when
3478
     * header_selection is XCdrHeaderSelection::SHORT_HEADER.
3479
     */
3480
    Cdr& xcdr1_end_serialize_opt_member(
3481
            const Cdr::state& current_state);
3482
3483
    /*!
3484
     * @brief Tells to the encoder a member starts to be encoded according to XCDRv2.
3485
     * @param[in] member_id Member identifier.
3486
     * @pre Member identifier cannot be MEMBER_ID_INVALID and next_member_id_ must be equal to the member identifier or
3487
     * MEMBER_ID_INVALID.
3488
     * @param[in] is_present If the member is present.
3489
     * @param[in,out] current_state State of the encoder previous to call this function.
3490
     * @pre Current encoding algorithm must be EncodingAlgorithmFlag::PLAIN_CDR2, EncodingAlgorithmFlag::DELIMIT_CDR2 or
3491
     * EncodingAlgorithmFlag::PL_CDR2.
3492
     * @param[in] header_selection Selects which member header will be used to allocate space.
3493
     * @return Reference to the eprosima::fastcdr::Cdr object.
3494
     * @exception exception::NotEnoughMemoryException This exception is thrown when trying to encode into a buffer
3495
     * position that exceeds the internal memory size.
3496
     * @exception exception::BadParamException This exception is thrown when trying to encode member identifier equal or
3497
     * greater than 0x10000000.
3498
     */
3499
    Cdr& xcdr2_begin_serialize_member(
3500
            const MemberId& member_id,
3501
            bool is_present,
3502
            Cdr::state& current_state,
3503
            XCdrHeaderSelection header_selection);
3504
3505
    /*!
3506
     * @brief Tells to the encoder to finish the encoding of the member.
3507
     * @param[in] current_state State of the encoder previous to call xcdr2_begin_serialize_member function.
3508
     * @pre next_member_id_ cannot be MEMBER_ID_INVALID.
3509
     * @return Reference to the eprosima::fastcdr::Cdr object.
3510
     * @exception exception::NotEnoughMemoryException This exception is thrown when trying to encode into a buffer
3511
     * position that exceeds the internal memory size.
3512
     * @exception exception::BadParamException This exception is thrown when trying to encode a long header when
3513
     * header_selection is XCdrHeaderSelection::SHORT_HEADER.
3514
     */
3515
    Cdr& xcdr2_end_serialize_member(
3516
            const Cdr::state& current_state);
3517
3518
    /*!
3519
     * @brief Tells to the encoder a new type and its members start to be encoded according to XCDRv1.
3520
     * @param[in,out] current_state State of the encoder previous to call this function.
3521
     * @pre Current encoding algorithm must be EncodingAlgorithmFlag::PLAIN_CDR or EncodingAlgorithmFlag::PL_CDR.
3522
     * @param[in] type_encoding Encoding algorithm used to encode the type and its members.
3523
     * @pre Type encoding algorithm must be EncodingAlgorithmFlag::PLAIN_CDR or EncodingAlgorithmFlag::PL_CDR.
3524
     * @pre If it is the beginning of the whole encoding, current encoding must be equal to type encoding.
3525
     * @return Reference to the eprosima::fastcdr::Cdr object.
3526
     */
3527
    Cdr& xcdr1_begin_serialize_type(
3528
            Cdr::state& current_state,
3529
            EncodingAlgorithmFlag type_encoding) noexcept;
3530
3531
    /*!
3532
     * @brief Tells to the encoder to finish the encoding of the type.
3533
     * @param[in] current_state State of the encoder previous to call xcdr1_begin_serialize_type function.
3534
     * @pre Current encoding algorithm must be EncodingAlgorithmFlag::PLAIN_CDR or EncodingAlgorithmFlag::PL_CDR.
3535
     * @return Reference to the eprosima::fastcdr::Cdr object.
3536
     * @exception exception::NotEnoughMemoryException This exception is thrown when trying to encode into a buffer
3537
     * position that exceeds the internal memory size.
3538
     */
3539
    Cdr& xcdr1_end_serialize_type(
3540
            const Cdr::state& current_state);
3541
3542
    /*!
3543
     * @brief Tells to the encoder a new type and its members start to be encoded according to XCDRv2.
3544
     * @param[in,out] current_state State of the encoder previous to call this function.
3545
     * @pre Current encoding algorithm must be EncodingAlgorithmFlag::PLAIN_CDR2, EncodingAlgorithmFlag::DELIMIT_CDR2 or
3546
     * EncodingAlgorithmFlag::PL_CDR2.
3547
     * @param[in] type_encoding Encoding algorithm used to encode the type and its members.
3548
     * @pre Type encoding algorithm must be EncodingAlgorithmFlag::PLAIN_CDR2, EncodingAlgorithmFlag::DELIMIT_CDR2 or
3549
     * EncodingAlgorithmFlag::PL_CDR2.
3550
     * @pre If it is the beginning of the whole encoding, current encoding must be equal to type encoding.
3551
     * @return Reference to the eprosima::fastcdr::Cdr object.
3552
     * @exception exception::NotEnoughMemoryException This exception is thrown when trying to encode into a buffer
3553
     * position that exceeds the internal memory size.
3554
     */
3555
    Cdr& xcdr2_begin_serialize_type(
3556
            Cdr::state& current_state,
3557
            EncodingAlgorithmFlag type_encoding);
3558
3559
    /*!
3560
     * @brief Tells to the encoder to finish the encoding of the type.
3561
     * @param[in] current_state State of the encoder previous to call xcdr2_begin_serialize_type function.
3562
     * @pre Current encoding algorithm must be EncodingAlgorithmFlag::PLAIN_CDR2, EncodingAlgorithmFlag::DELIMIT_CDR2 or
3563
     * EncodingAlgorithmFlag::PL_CDR2.
3564
     * @return Reference to the eprosima::fastcdr::Cdr object.
3565
     * @exception exception::NotEnoughMemoryException This exception is thrown when trying to encode into a buffer
3566
     * position that exceeds the internal memory size.
3567
     */
3568
    Cdr& xcdr2_end_serialize_type(
3569
            const Cdr::state& current_state);
3570
3571
    /*!
3572
     * @brief Tells to the encoder a new type and its members start to be decoded according to XCDRv1.
3573
     * @param[in] type_encoding Encoding algorithm used to encode the type and its members.
3574
     * @pre Type encoding algorithm must be EncodingAlgorithmFlag::PLAIN_CDR or EncodingAlgorithmFlag::PL_CDR.
3575
     * @pre If it is the beginning of the whole encoding, current encoding must be equal to type encoding.
3576
     * @param[in] functor Functor called each time a member has to be decoded.
3577
     * @return Reference to the eprosima::fastcdr::Cdr object.
3578
     * @exception exception::NotEnoughMemoryException This exception is thrown when trying to encode into a buffer
3579
     * position that exceeds the internal memory size.
3580
     * @exception exception::BadParamException This exception is thrown when an incorrect behaviour happens when
3581
     * trying to decode.
3582
     */
3583
    Cdr& xcdr1_deserialize_type(
3584
            EncodingAlgorithmFlag type_encoding,
3585
            std::function<bool (Cdr&, const MemberId&)> functor);
3586
3587
    /*!
3588
     * @brief Tells to the encoder a new type and its members start to be decoded according to XCDRv2.
3589
     * @param[in] type_encoding Encoding algorithm used to encode the type and its members.
3590
     * @pre Type encoding algorithm must be EncodingAlgorithmFlag::PLAIN_CDR2, EncodingAlgorithmFlag::DELIMIT_CDR2 or
3591
     * EncodingAlgorithmFlag::PL_CDR2.
3592
     * @pre If it is the beginning of the whole encoding, current encoding must be equal to type encoding.
3593
     * @param[in] functor Functor called each time a member has to be decoded.
3594
     * @return Reference to the eprosima::fastcdr::Cdr object.
3595
     * @exception exception::NotEnoughMemoryException This exception is thrown when trying to encode into a buffer
3596
     * position that exceeds the internal memory size.
3597
     * @exception exception::BadParamException This exception is thrown when an incorrect behaviour happens when
3598
     * trying to decode.
3599
     */
3600
    Cdr& xcdr2_deserialize_type(
3601
            EncodingAlgorithmFlag type_encoding,
3602
            std::function<bool (Cdr&, const MemberId&)> functor);
3603
3604
    Cdr& cdr_begin_serialize_member(
3605
            const MemberId& member_id,
3606
            bool is_present,
3607
            Cdr::state& current_state,
3608
            XCdrHeaderSelection header_selection);
3609
3610
    Cdr& cdr_end_serialize_member(
3611
            const Cdr::state& current_state);
3612
3613
    Cdr& cdr_begin_serialize_type(
3614
            Cdr::state& current_state,
3615
            EncodingAlgorithmFlag type_encoding);
3616
3617
    Cdr& cdr_end_serialize_type(
3618
            const Cdr::state& current_state);
3619
3620
    Cdr& cdr_deserialize_type(
3621
            EncodingAlgorithmFlag type_encoding,
3622
            std::function<bool (Cdr&, const MemberId&)> functor);
3623
3624
    /*!
3625
     * @brief Resets the internal callbacks depending on the current selected Cdr version.
3626
     */
3627
    void reset_callbacks();
3628
3629
    using begin_serialize_member_functor = Cdr& (Cdr::*)(
3630
        const MemberId&,
3631
        bool,
3632
        Cdr::state&,
3633
        XCdrHeaderSelection);
3634
    begin_serialize_member_functor begin_serialize_member_ { nullptr };
3635
3636
    using end_serialize_member_functor = Cdr& (Cdr::*)(
3637
        const Cdr::state&);
3638
    end_serialize_member_functor end_serialize_member_ { nullptr };
3639
3640
    using begin_serialize_opt_member_functor = Cdr& (Cdr::*)(
3641
        const MemberId&,
3642
        bool,
3643
        Cdr::state&,
3644
        XCdrHeaderSelection);
3645
    begin_serialize_opt_member_functor begin_serialize_opt_member_ { nullptr };
3646
3647
    using end_serialize_memberopt__functor = Cdr& (Cdr::*)(
3648
        const Cdr::state&);
3649
    end_serialize_member_functor end_serialize_opt_member_ { nullptr };
3650
3651
    using begin_serialize_type_functor = Cdr& (Cdr::*)(
3652
        Cdr::state&,
3653
        EncodingAlgorithmFlag);
3654
    begin_serialize_type_functor begin_serialize_type_ { nullptr };
3655
3656
    using end_serialize_type_functor = Cdr& (Cdr::*)(
3657
        const Cdr::state&);
3658
    end_serialize_type_functor end_serialize_type_ { nullptr };
3659
3660
    using deserialize_type_functor = Cdr& (Cdr::*)(
3661
        EncodingAlgorithmFlag,
3662
        std::function<bool (Cdr&, const MemberId&)>);
3663
    deserialize_type_functor deserialize_type_ { nullptr };
3664
3665
    //! @brief Reference to the buffer that will be serialized/deserialized.
3666
    FastBuffer& cdr_buffer_;
3667
3668
    //! @brief The type of CDR that will be use in serialization/deserialization.
3669
    CdrVersion cdr_version_ {CdrVersion::XCDRv2};
3670
3671
    //! @brief Stores the main encoding algorithm.
3672
    EncodingAlgorithmFlag encoding_flag_ {EncodingAlgorithmFlag::PLAIN_CDR2};
3673
3674
    //! @brief Stores the current encoding algorithm.
3675
    EncodingAlgorithmFlag current_encoding_ {EncodingAlgorithmFlag::PLAIN_CDR2};
3676
3677
    //! @brief This attribute stores the option flags when the CDR type is DDS_CDR;
3678
    std::array<uint8_t, 2> options_{{0}};
3679
3680
    //! @brief The endianness that will be applied over the buffer.
3681
    uint8_t endianness_ {Endianness::LITTLE_ENDIANNESS};
3682
3683
    //! @brief This attribute specifies if it is needed to swap the bytes.
3684
    bool swap_bytes_ {false};
3685
3686
    //! @brief Stores the last datasize serialized/deserialized. It's used to optimize.
3687
    size_t last_data_size_ {0};
3688
3689
    //! @brief The current position in the serialization/deserialization process.
3690
    FastBuffer::iterator offset_;
3691
3692
    //! @brief The position from where the alignment is calculated.
3693
    FastBuffer::iterator origin_;
3694
3695
    //! @brief The last position in the buffer;
3696
    FastBuffer::iterator end_;
3697
3698
    //! Next member identifier to be processed.
3699
    MemberId next_member_id_;
3700
3701
    //! Align for types equal or greater than 64bits.
3702
    size_t align64_ {4};
3703
3704
    /*!
3705
     * When serializing a member's type using XCDRv2, this enumerator is used to inform the type was serialized with a
3706
     * DHEADER and the algorithm could optimize the XCDRv2 member header.
3707
     */
3708
    enum SerializedMemberSizeForNextInt
3709
    {
3710
        NO_SERIALIZED_MEMBER_SIZE,     //! Default. No serialized member size in a DHEADER.
3711
        SERIALIZED_MEMBER_SIZE,        //! Serialized member size in a DHEADER.
3712
        SERIALIZED_MEMBER_SIZE_4,      //! Serialized member size (which is a multiple of 4) in a DHEADER.
3713
        SERIALIZED_MEMBER_SIZE_8       //! Serialized member size (which is a multiple of 8) in a DHEADER.
3714
    }
3715
    //! Specifies if a DHEADER was serialized. Used to optimize XCDRv2 member headers.
3716
    serialized_member_size_ {NO_SERIALIZED_MEMBER_SIZE};
3717
3718
    //! Stores the initial state.
3719
    state initial_state_;
3720
3721
    //! Whether the encapsulation was serialized.
3722
    bool encapsulation_serialized_ {false};
3723
3724
    //! Custom serialization context.
3725
    std::shared_ptr<CdrContext> context_;
3726
3727
3728
    uint32_t get_long_lc(
3729
            SerializedMemberSizeForNextInt serialized_member_size);
3730
3731
    uint32_t get_short_lc(
3732
            size_t member_serialized_size);
3733
3734
    template<class _T, typename std::enable_if<std::is_enum<_T>::value ||
3735
            std::is_arithmetic<_T>::value>::type* = nullptr>
3736
    constexpr SerializedMemberSizeForNextInt get_serialized_member_size() const
3737
0
    {
3738
0
        return (1 == sizeof(_T) ? SERIALIZED_MEMBER_SIZE :
3739
0
               (4 == sizeof(_T) ? SERIALIZED_MEMBER_SIZE_4 :
3740
0
               (8 == sizeof(_T) ? SERIALIZED_MEMBER_SIZE_8 :  NO_SERIALIZED_MEMBER_SIZE)));
3741
0
    }
3742
3743
};
3744
3745
}            //namespace fastcdr
3746
}        //namespace eprosima
3747
3748
#endif // _CDR_CDR_H_