-
Notifications
You must be signed in to change notification settings - Fork 67
/
basic_cdr_ser.hpp
386 lines (336 loc) · 11 KB
/
basic_cdr_ser.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
/*
* Copyright(c) 2021 ADLINK Technology Limited and others
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0, or the Eclipse Distribution License
* v. 1.0 which is available at
* http://www.eclipse.org/org/documents/edl-v10.php.
*
* SPDX-License-Identifier: EPL-2.0 OR BSD-3-Clause
*/
#ifndef BASIC_CDR_SERIALIZATION_HPP_
#define BASIC_CDR_SERIALIZATION_HPP_
#include "cdr_stream.hpp"
#include <org/eclipse/cyclonedds/core/type_helpers.hpp>
#include <dds/core/Exception.hpp>
#include <array>
#include <vector>
#include <string>
#include <cstring>
#include <algorithm>
namespace org {
namespace eclipse {
namespace cyclonedds {
namespace core {
namespace cdr {
/**
* @brief
* Implementation of the basic cdr stream.
*
* This type of cdr stream has a maximum alignment of 8 bytes.
*/
class basic_cdr_stream : public cdr_stream {
public:
/**
* @brief
* Constructor.
*
* Basically a pass through for the cdr_stream base class.
*
* @param[in] end The endianness to set for the data stream, default to the local system endianness.
* @param[in] ignore_faults Bitmask for ignoring faults, can be composed of bit fields from the serialization_status enumerator.
*/
basic_cdr_stream(endianness end = native_endianness(), uint64_t ignore_faults = 0x0) : cdr_stream(end, 8, ignore_faults) { ; }
};
/**
* @brief
* Primitive type stream manipulation functions.
*
* These are "endpoints" for write functions, since composit
* (sequence/array/constructed type) functions will decay to these
* calls.
*/
/**
* @brief
* Primitive type read function.
*
* Aligns the stream to the alignment of type T.
* Reads the value from the current position of the stream str into
* toread, will swap bytes if necessary.
* Moves the cursor of the stream by the size of T.
* This function is only enabled for arithmetic types and enums.
*
* @param[in, out] str The stream which is read from.
* @param[out] toread The variable to read into.
*/
template<typename T, std::enable_if_t<std::is_arithmetic<T>::value && !std::is_enum<T>::value, bool> = true >
void read(basic_cdr_stream &str, T& toread)
{
if (str.abort_status())
return;
char *cursor = str.get_cursor();
str.align(sizeof(T), false);
assert(cursor);
transfer_and_swap(*(reinterpret_cast<const T*>(cursor)), toread, str.swap_endianness());
str.incr_position(sizeof(T));
}
/**
* @brief
* Primitive type write function.
*
* Aligns str to the type to be written.
* Writes towrite to str.
* Swaps bytes written to str if the endiannesses do not match up.
* Moves the cursor of str by the size of towrite.
* This function is only enabled for arithmetic types.
*
* @param[in, out] str The stream which is written to.
* @param[in] towrite The variable to write.
*/
template<typename T, std::enable_if_t<std::is_arithmetic<T>::value && !std::is_enum<T>::value, bool> = true >
void write(basic_cdr_stream& str, const T& towrite)
{
if (str.abort_status())
return;
char *cursor = str.get_cursor();
str.align(sizeof(T), true);
assert(cursor);
transfer_and_swap(towrite, *(reinterpret_cast<T*>(cursor)), str.swap_endianness());
str.incr_position(sizeof(T));
}
/**
* @brief
* Primitive type cursor move function.
*
* Used in determining the size of a type when written to the stream.
* Aligns str to the size of toincr.
* Moves the cursor of str by the size of toincr.
* This function is only enabled for arithmetic types.
*
* @param[in, out] str The stream whose cursor is moved.
* @param[in] toincr The variable to move the cursor by, no contents of this variable are used, it is just used to determine the template.
*/
template<typename T, std::enable_if_t<std::is_arithmetic<T>::value && !std::is_enum<T>::value, bool> = true >
void move(basic_cdr_stream& str, const T& toincr)
{
if (str.abort_status())
return;
(void)toincr;
str.align(sizeof(T), false);
str.incr_position(sizeof(T));
}
/**
* @brief
* Primitive type max stream move function.
*
* Used in determining the maximum stream size of a constructed type.
* Moves the cursor to the maximum position it could occupy after
* writing max_sz to the stream.
* Is in essence the same as the primitive type cursor move function,
* but additionally checks for whether the cursor it at the "end",
* which may happen if unbounded members (strings/sequences/...)
* are part of the constructed type.
* This function is only enabled for arithmetic types.
*
* @param[in, out] str The stream whose cursor is moved.
* @param[in] max_sz The variable to move the cursor by, no contents of this variable are used, it is just used to determine the template.
*/
template<typename T, std::enable_if_t<std::is_arithmetic<T>::value && !std::is_enum<T>::value, bool> = true >
void max(basic_cdr_stream& str, const T& max_sz)
{
if (str.abort_status())
return;
if (str.position() == SIZE_MAX)
return;
move(str, max_sz);
}
/**
* @brief
* Enumerated type stream manipulation functions.
* Since enumerated types are represented by a uint32_t in basic CDR streams
* they just loop through to writing uint32_t versions of the enum.
*
* These are "endpoints" for write functions, since compound
* (sequence/array/constructed type) functions will decay to these
* calls.
*/
/**
* @brief
* Reads the value of the enum from the stream.
*
* @param[in, out] str The stream which is read from.
* @param[out] toread The variable to read into.
*/
template<typename T, std::enable_if_t<std::is_enum<T>::value && !std::is_arithmetic<T>::value, bool> = true >
void read(basic_cdr_stream& str, T& toread) {
read(str, *reinterpret_cast<uint32_t*>(&toread));
}
/**
* @brief
* Writes the value of the enum to the stream.
*
* @param [in, out] str The stream which is written to.
* @param [in] towrite The variable to write.
*/
template<typename T, std::enable_if_t<std::is_enum<T>::value && !std::is_arithmetic<T>::value, bool> = true >
void write(basic_cdr_stream& str, const T& towrite) {
write(str, uint32_t(towrite));
}
/**
* @brief
* Moves the cursor of the stream by the size the enum would take up.
*
* @param[in, out] str The stream whose cursor is moved.
* @param[in] toincr The variable to move the cursor by, no contents of this variable are used, it is just used to determine the template.
*/
template<typename T, std::enable_if_t<std::is_enum<T>::value && !std::is_arithmetic<T>::value, bool> = true >
void move(basic_cdr_stream& str, const T& toincr) {
move(str, uint32_t(toincr));
}
/**
* @brief
* Moves the cursor of the stream by the size the enum would take up (maximum size version).
*
* @param[in, out] str The stream whose cursor is moved.
* @param[in] max_sz The variable to move the cursor by, no contents of this variable are used, it is just used to determine the template.
*/
template<typename T, std::enable_if_t<std::is_enum<T>::value && !std::is_arithmetic<T>::value, bool> = true >
void max(basic_cdr_stream& str, const T& max_sz) {
max(str, uint32_t(max_sz));
}
/**
* @brief
* String type stream manipulation functions
*
* These are "endpoints" for write functions, since compound
* (sequence/array/constructed type) functions will decay to these
* calls.
*/
/**
* @brief
* Bounded string read function.
*
* Reads the length from str, but then initializes toread with at most N characters from it.
* It does move the cursor by length read, since that is the number of characters in the stream.
* If N is 0, then the string is taken to be unbounded.
*
* @param[in, out] str The stream to read from.
* @param[out] toread The string to read to.
* @param[in] N The maximum number of characters to read from the stream.
*/
template<typename T>
void read_string(basic_cdr_stream& str, T& toread, size_t N)
{
if (str.abort_status())
return;
uint32_t string_length = 0;
read(str, string_length);
if (N &&
string_length > N &&
str.status(serialization_status::read_bound_exceeded))
return;
auto cursor = str.get_cursor();
toread.assign(cursor, cursor + std::min<size_t>(string_length - 1, N ? N : SIZE_MAX)); //remove 1 for terminating NULL
str.incr_position(string_length);
//aligned to chars
str.alignment(1);
}
/**
* @brief
* Bounded string write function.
*
* Attempts to write the length of towrite to str, where the bound is checked.
* Then writes the contents of towrite to str.
* If N is 0, then the string is taken to be unbounded.
*
* @param[in, out] str The stream to write to.
* @param[in] towrite The string to write.
* @param[in] N The maximum number of characters to write to the stream.
*/
template<typename T>
void write_string(basic_cdr_stream& str, const T& towrite, size_t N)
{
if (str.abort_status())
return;
size_t string_length = towrite.length() + 1; //add 1 extra for terminating NULL
if (N &&
string_length > N) {
if (str.status(serialization_status::write_bound_exceeded))
return;
}
write(str, uint32_t(string_length));
memcpy(str.get_cursor(), towrite.c_str(), string_length);
str.incr_position(string_length);
//aligned to chars
str.alignment(1);
}
/**
* @brief
* Bounded string cursor move function.
*
* Attempts to move the cursor for the length field, where the bound is checked.
* Then moves the cursor for the length of the string.
* If N is 0, then the string is taken to be unbounded.
*
* @param[in, out] str The stream whose cursor is moved.
* @param[in] toincr The string used to move the cursor.
* @param[in] N The maximum number of characters in the string which the stream is moved by.
*/
template<typename T>
void move_string(basic_cdr_stream& str, const T& toincr, size_t N)
{
if (str.abort_status())
return;
size_t string_length = toincr.length() + 1; //add 1 extra for terminating NULL
if (N &&
string_length > N) {
if (str.status(serialization_status::move_bound_exceeded))
return;
}
move(str, uint32_t(string_length));
str.incr_position(string_length);
//aligned to chars
str.alignment(1);
}
/**
* @brief
* Bounded string cursor max move function.
*
* Similar to the string move function, with the additional checks that no move
* is done if the cursor is already at its maximum position, and that the cursor
* is set to its maximum position if the bound is equal to 0 (unbounded).
*
* @param[in, out] str The stream whose cursor is moved.
* @param[in] max_sz The string used to move the cursor.
* @param[in] N The maximum number of characters in the string which the stream is moved by.
*/
template<typename T>
void max_string(basic_cdr_stream& str, const T& max_sz, size_t N)
{
if (str.abort_status())
return;
(void)max_sz;
if (str.position() == SIZE_MAX)
return;
if (N == 0)
{
//unbounded string, theoretical length unlimited
str.position(SIZE_MAX);
}
else
{
//length field
max(str, uint32_t(0));
//bounded string, length maximum N+1 characters
str.incr_position(N + 1);
//aligned to chars
str.alignment(1);
}
}
}
}
}
}
} /* namespace org / eclipse / cyclonedds / core / cdr */
#endif