Line | Count | Source |
1 | | #ifndef ZSERIO_ARRAY_TRAITS_H_INC |
2 | | #define ZSERIO_ARRAY_TRAITS_H_INC |
3 | | |
4 | | #include <string> |
5 | | #include <type_traits> |
6 | | #include <vector> |
7 | | |
8 | | #include "zserio/BitPositionUtil.h" |
9 | | #include "zserio/BitSizeOfCalculator.h" |
10 | | #include "zserio/BitStreamReader.h" |
11 | | #include "zserio/BitStreamWriter.h" |
12 | | #include "zserio/DeltaContext.h" |
13 | | #include "zserio/Enums.h" |
14 | | #include "zserio/SizeConvertUtil.h" |
15 | | #include "zserio/Traits.h" |
16 | | #include "zserio/Types.h" |
17 | | |
18 | | namespace zserio |
19 | | { |
20 | | |
21 | | namespace detail |
22 | | { |
23 | | |
24 | | template <typename T> |
25 | | T read_bits(BitStreamReader& in, uint8_t numBits); |
26 | | |
27 | | template <> |
28 | | inline int8_t read_bits<int8_t>(BitStreamReader& in, uint8_t numBits) |
29 | 884 | { |
30 | 884 | return static_cast<int8_t>(in.readSignedBits(numBits)); |
31 | 884 | } |
32 | | |
33 | | template <> |
34 | | inline int16_t read_bits<int16_t>(BitStreamReader& in, uint8_t numBits) |
35 | 602 | { |
36 | 602 | return static_cast<int16_t>(in.readSignedBits(numBits)); |
37 | 602 | } |
38 | | |
39 | | template <> |
40 | | inline int32_t read_bits<int32_t>(BitStreamReader& in, uint8_t numBits) |
41 | 312 | { |
42 | 312 | return in.readSignedBits(numBits); |
43 | 312 | } |
44 | | |
45 | | template <> |
46 | | inline int64_t read_bits<int64_t>(BitStreamReader& in, uint8_t numBits) |
47 | 878 | { |
48 | 878 | return in.readSignedBits64(numBits); |
49 | 878 | } |
50 | | |
51 | | template <> |
52 | | inline uint8_t read_bits<uint8_t>(BitStreamReader& in, uint8_t numBits) |
53 | 2.20k | { |
54 | 2.20k | return static_cast<uint8_t>(in.readBits(numBits)); |
55 | 2.20k | } |
56 | | |
57 | | template <> |
58 | | inline uint16_t read_bits<uint16_t>(BitStreamReader& in, uint8_t numBits) |
59 | 312 | { |
60 | 312 | return static_cast<uint16_t>(in.readBits(numBits)); |
61 | 312 | } |
62 | | |
63 | | template <> |
64 | | inline uint32_t read_bits<uint32_t>(BitStreamReader& in, uint8_t numBits) |
65 | 478 | { |
66 | 478 | return in.readBits(numBits); |
67 | 478 | } |
68 | | |
69 | | template <> |
70 | | inline uint64_t read_bits<uint64_t>(BitStreamReader& in, uint8_t numBits) |
71 | 840 | { |
72 | 840 | return in.readBits64(numBits); |
73 | 840 | } |
74 | | |
75 | | template <typename T> |
76 | | void write_bits(BitStreamWriter& out, T value, uint8_t numBits); |
77 | | |
78 | | template <> |
79 | | inline void write_bits<int8_t>(BitStreamWriter& out, int8_t value, uint8_t numBits) |
80 | 496 | { |
81 | 496 | out.writeSignedBits(static_cast<int32_t>(value), numBits); |
82 | 496 | } |
83 | | |
84 | | template <> |
85 | | inline void write_bits<int16_t>(BitStreamWriter& out, int16_t value, uint8_t numBits) |
86 | 504 | { |
87 | 504 | out.writeSignedBits(static_cast<int32_t>(value), numBits); |
88 | 504 | } |
89 | | |
90 | | template <> |
91 | | inline void write_bits<int32_t>(BitStreamWriter& out, int32_t value, uint8_t numBits) |
92 | 312 | { |
93 | 312 | out.writeSignedBits(value, numBits); |
94 | 312 | } |
95 | | |
96 | | template <> |
97 | | inline void write_bits<int64_t>(BitStreamWriter& out, int64_t value, uint8_t numBits) |
98 | 696 | { |
99 | 696 | out.writeSignedBits64(value, numBits); |
100 | 696 | } |
101 | | |
102 | | template <> |
103 | | inline void write_bits<uint8_t>(BitStreamWriter& out, uint8_t value, uint8_t numBits) |
104 | 1.23k | { |
105 | 1.23k | out.writeBits(static_cast<uint32_t>(value), numBits); |
106 | 1.23k | } |
107 | | |
108 | | template <> |
109 | | inline void write_bits<uint16_t>(BitStreamWriter& out, uint16_t value, uint8_t numBits) |
110 | 312 | { |
111 | 312 | out.writeBits(static_cast<uint32_t>(value), numBits); |
112 | 312 | } |
113 | | |
114 | | template <> |
115 | | inline void write_bits<uint32_t>(BitStreamWriter& out, uint32_t value, uint8_t numBits) |
116 | 376 | { |
117 | 376 | out.writeBits(value, numBits); |
118 | 376 | } |
119 | | |
120 | | template <> |
121 | | inline void write_bits<uint64_t>(BitStreamWriter& out, uint64_t value, uint8_t numBits) |
122 | 632 | { |
123 | 632 | out.writeBits64(value, numBits); |
124 | 632 | } |
125 | | |
126 | | } // namespace detail |
127 | | |
128 | | /** |
129 | | * Array traits for bit field Zserio types. |
130 | | * |
131 | | * These traits are used for all fixed bit fields (int:N or bit:N) or for dynamic bit fields which |
132 | | * have constant bit size available during generation (int<N> or bit<N>). |
133 | | */ |
134 | | template <typename T, uint8_t NUM_BITS> |
135 | | class BitFieldArrayTraits |
136 | | { |
137 | | public: |
138 | | /** Element type. */ |
139 | | using ElementType = T; |
140 | | |
141 | | /** |
142 | | * Calculates bit size of the array element. |
143 | | * |
144 | | * \return Bit size of the array element. |
145 | | */ |
146 | | static size_t bitSizeOf() |
147 | 8.11k | { |
148 | 8.11k | return NUM_BITS; |
149 | 8.11k | } |
150 | | |
151 | | /** |
152 | | * Calculates bit size of the array element. |
153 | | * |
154 | | * \return Bit size of the array element. |
155 | | */ |
156 | | static size_t bitSizeOf(ElementType) |
157 | 7.48k | { |
158 | 7.48k | return bitSizeOf(); |
159 | 7.48k | } |
160 | | |
161 | | /** |
162 | | * Calculates bit size of the array element. |
163 | | * |
164 | | * \return Bit size of the array element. |
165 | | */ |
166 | | static size_t bitSizeOf(size_t, ElementType) |
167 | | { |
168 | | return bitSizeOf(); |
169 | | } |
170 | | |
171 | | /** |
172 | | * Initializes indexed offsets of the single array element. |
173 | | * |
174 | | * \param bitPosition Current bit position. |
175 | | * |
176 | | * \return Updated bit position which points to the first bit after the array element. |
177 | | */ |
178 | | static size_t initializeOffsets(size_t bitPosition, ElementType) |
179 | 768 | { |
180 | 768 | return bitPosition + NUM_BITS; |
181 | 768 | } |
182 | | |
183 | | /** |
184 | | * Reads the single array element. |
185 | | * |
186 | | * \param in Bit stream reader. |
187 | | * |
188 | | * \return Read element. |
189 | | */ |
190 | | static ElementType read(BitStreamReader& in, size_t = 0) |
191 | 2.79k | { |
192 | 2.79k | return detail::read_bits<T>(in, NUM_BITS); |
193 | 2.79k | } |
194 | | |
195 | | /** |
196 | | * Writes the single array element. |
197 | | * |
198 | | * \param out Bit stream writer to use. |
199 | | * \param element Element to write. |
200 | | */ |
201 | | static void write(BitStreamWriter& out, ElementType element) |
202 | 1.92k | { |
203 | 1.92k | detail::write_bits(out, element, NUM_BITS); |
204 | 1.92k | } |
205 | | |
206 | | /** Determines whether the bit size of the single element is constant. */ |
207 | | static constexpr bool IS_BITSIZEOF_CONSTANT = true; |
208 | | }; |
209 | | |
210 | | /** |
211 | | * Array traits for dynamic bit field Zserio types with dynamic bit size (int<bitSize> or bit<bitSize>). |
212 | | * |
213 | | * Used for dynamic bit fields with length known at compile time - i.e. which depends on a constant expression |
214 | | * and doesn't need an owner. |
215 | | */ |
216 | | template <typename T, typename ELEMENT_BIT_SIZE, typename = void> |
217 | | class DynamicBitFieldArrayTraits |
218 | | { |
219 | | public: |
220 | | /** Element type. */ |
221 | | using ElementType = T; |
222 | | |
223 | | /** |
224 | | * Calculates bit size of the array element. |
225 | | * |
226 | | * \return Bit size of the array element. |
227 | | */ |
228 | | static size_t bitSizeOf() |
229 | 1.69k | { |
230 | 1.69k | return ELEMENT_BIT_SIZE::get(); |
231 | 1.69k | } |
232 | | |
233 | | /** |
234 | | * Calculates bit size of the array element. |
235 | | * |
236 | | * \return Bit size of the array element. |
237 | | */ |
238 | | static size_t bitSizeOf(ElementType) |
239 | 960 | { |
240 | 960 | return bitSizeOf(); |
241 | 960 | } |
242 | | |
243 | | /** |
244 | | * Calculates bit size of the array element. |
245 | | * |
246 | | * \return Bit size of the array element. |
247 | | */ |
248 | | static size_t bitSizeOf(size_t, ElementType) |
249 | | { |
250 | | return bitSizeOf(); |
251 | | } |
252 | | |
253 | | /** |
254 | | * Initializes indexed offsets of the single array element. |
255 | | * |
256 | | * \param bitPosition Current bit position. |
257 | | * |
258 | | * \return Updated bit position which points to the first bit after the array element. |
259 | | */ |
260 | | static size_t initializeOffsets(size_t bitPosition, ElementType) |
261 | 384 | { |
262 | 384 | return bitPosition + bitSizeOf(); |
263 | 384 | } |
264 | | |
265 | | /** |
266 | | * Reads the single array element. |
267 | | * |
268 | | * \param in Bit stream reader. |
269 | | * |
270 | | * \return Read element. |
271 | | */ |
272 | | static ElementType read(BitStreamReader& in, size_t = 0) |
273 | 770 | { |
274 | 770 | return detail::read_bits<T>(in, ELEMENT_BIT_SIZE::get()); |
275 | 770 | } |
276 | | |
277 | | /** |
278 | | * Writes the single array element. |
279 | | * |
280 | | * \param out Bit stream writer to use. |
281 | | * \param element Element to write. |
282 | | */ |
283 | | static void write(BitStreamWriter& out, ElementType element) |
284 | 576 | { |
285 | 576 | detail::write_bits(out, element, ELEMENT_BIT_SIZE::get()); |
286 | 576 | } |
287 | | |
288 | | /** Determines whether the bit size of the single element is constant. */ |
289 | | static constexpr bool IS_BITSIZEOF_CONSTANT = true; |
290 | | }; |
291 | | |
292 | | /** |
293 | | * Array traits for dynamic bit field Zserio types with dynamic bit size (int<bitSize> or bit<bitSize>). |
294 | | * |
295 | | * Specialization for dynamic bit fields which length depends on an arbitrary expression - i.e. which may |
296 | | * need an owner. |
297 | | */ |
298 | | template <typename T, typename ELEMENT_BIT_SIZE> |
299 | | class DynamicBitFieldArrayTraits<T, ELEMENT_BIT_SIZE, |
300 | | typename std::enable_if<has_owner_type<ELEMENT_BIT_SIZE>::value>::type> |
301 | | { |
302 | | public: |
303 | | /** Element type. */ |
304 | | using ElementType = T; |
305 | | |
306 | | /** Typedef for the array's owner type. */ |
307 | | using OwnerType = typename ELEMENT_BIT_SIZE::OwnerType; |
308 | | |
309 | | /** |
310 | | * Calculates bit size of the array element. |
311 | | * |
312 | | * \param owner Owner of the current array. |
313 | | * |
314 | | * \return Bit size of the array element. |
315 | | */ |
316 | | static size_t bitSizeOf(const OwnerType& owner) |
317 | 2.04k | { |
318 | 2.04k | return ELEMENT_BIT_SIZE::get(owner); |
319 | 2.04k | } |
320 | | |
321 | | /** |
322 | | * Calculates bit size of the array element. |
323 | | * |
324 | | * \param owner Owner of the current array. |
325 | | * |
326 | | * \return Bit size of the array element. |
327 | | */ |
328 | | static size_t bitSizeOf(const OwnerType& owner, ElementType) |
329 | 960 | { |
330 | 960 | return bitSizeOf(owner); |
331 | 960 | } |
332 | | |
333 | | /** |
334 | | * Calculates bit size of the array element. |
335 | | * |
336 | | * \param owner Owner of the current array. |
337 | | * |
338 | | * \return Bit size of the array element. |
339 | | */ |
340 | | static size_t bitSizeOf(const OwnerType& owner, size_t, ElementType) |
341 | | { |
342 | | return bitSizeOf(owner); |
343 | | } |
344 | | |
345 | | /** |
346 | | * Initializes indexed offsets of the single array element. |
347 | | * |
348 | | * \param owner Owner of the current array. |
349 | | * \param bitPosition Current bit position. |
350 | | * |
351 | | * \return Updated bit position which points to the first bit after the array element. |
352 | | */ |
353 | | static size_t initializeOffsets(const OwnerType& owner, size_t bitPosition, ElementType) |
354 | 624 | { |
355 | 624 | return bitPosition + bitSizeOf(owner); |
356 | 624 | } |
357 | | |
358 | | /** |
359 | | * Reads the single array element. |
360 | | * |
361 | | * \param owner Owner of the current array. |
362 | | * \param in Bit stream reader. |
363 | | * |
364 | | * \return Read element. |
365 | | */ |
366 | | static ElementType read(const OwnerType& owner, BitStreamReader& in, size_t = 0) |
367 | 1.01k | { |
368 | 1.01k | return detail::read_bits<T>(in, ELEMENT_BIT_SIZE::get(owner)); |
369 | 1.01k | } |
370 | | |
371 | | /** |
372 | | * Writes the single array element. |
373 | | * |
374 | | * \param owner Owner of the current array. |
375 | | * \param out Bit stream writer to use. |
376 | | * \param element Element to write. |
377 | | */ |
378 | | static void write(const OwnerType& owner, BitStreamWriter& out, ElementType element) |
379 | 816 | { |
380 | 816 | detail::write_bits(out, element, ELEMENT_BIT_SIZE::get(owner)); |
381 | 816 | } |
382 | | |
383 | | /** Determines whether the bit size of the single element is constant. */ |
384 | | static constexpr bool IS_BITSIZEOF_CONSTANT = true; |
385 | | }; |
386 | | |
387 | | /** |
388 | | * Array traits for fixed integer Zserio types (int16, uint16, int32, uint32, etc...). |
389 | | */ |
390 | | template <typename T> |
391 | | struct StdIntArrayTraits |
392 | | { |
393 | | /** Element type. */ |
394 | | using ElementType = T; |
395 | | |
396 | | /** |
397 | | * Calculates bit size of the array element. |
398 | | * |
399 | | * \return Bit size of the array element. |
400 | | */ |
401 | | static size_t bitSizeOf() |
402 | 4.59k | { |
403 | 4.59k | return NUM_BITS; |
404 | 4.59k | } |
405 | | |
406 | | /** |
407 | | * Calculates bit size of the array element. |
408 | | * |
409 | | * \return Bit size of the array element. |
410 | | */ |
411 | | static size_t bitSizeOf(ElementType) |
412 | 3.74k | { |
413 | 3.74k | return bitSizeOf(); |
414 | 3.74k | } |
415 | | |
416 | | /** |
417 | | * Calculates bit size of the array element. |
418 | | * |
419 | | * \return Bit size of the array element. |
420 | | */ |
421 | | static size_t bitSizeOf(size_t, ElementType) |
422 | | { |
423 | | return bitSizeOf(); |
424 | | } |
425 | | |
426 | | /** |
427 | | * Initializes indexed offsets of the single array element. |
428 | | * |
429 | | * \param bitPosition Current bit position. |
430 | | * |
431 | | * \return Updated bit position which points to the first bit after the array element. |
432 | | */ |
433 | | static size_t initializeOffsets(size_t bitPosition, ElementType) |
434 | 960 | { |
435 | 960 | return bitPosition + NUM_BITS; |
436 | 960 | } |
437 | | |
438 | | /** |
439 | | * Reads the single array element. |
440 | | * |
441 | | * \param in Bit stream reader. |
442 | | * |
443 | | * \return Read element. |
444 | | */ |
445 | | static ElementType read(BitStreamReader& in, size_t = 0) |
446 | 1.93k | { |
447 | 1.93k | return detail::read_bits<T>(in, NUM_BITS); |
448 | 1.93k | } |
449 | | |
450 | | /** |
451 | | * Writes the single array element. |
452 | | * |
453 | | * \param out Bit stream writer to use. |
454 | | * \param element Element to write. |
455 | | */ |
456 | | static void write(BitStreamWriter& out, ElementType element) |
457 | 1.24k | { |
458 | 1.24k | detail::write_bits(out, element, NUM_BITS); |
459 | 1.24k | } |
460 | | |
461 | | /** Determines whether the bit size of the single element is constant. */ |
462 | | static constexpr bool IS_BITSIZEOF_CONSTANT = true; |
463 | | |
464 | | private: |
465 | | static constexpr uint8_t NUM_BITS = sizeof(T) * 8; |
466 | | }; |
467 | | |
468 | | /** |
469 | | * Array traits for variable integer Zserio types (varint16, varuint16, etc...). |
470 | | */ |
471 | | template <typename T> |
472 | | struct VarIntNNArrayTraits; |
473 | | |
474 | | /** |
475 | | * Array traits specialization for Zserio varint16 type. |
476 | | */ |
477 | | template <> |
478 | | struct VarIntNNArrayTraits<int16_t> |
479 | | { |
480 | | /** Element type. */ |
481 | | using ElementType = int16_t; |
482 | | |
483 | | /** |
484 | | * Calculates bit size of the array element. |
485 | | * |
486 | | * \param element Element to use for calculation. |
487 | | * |
488 | | * \return Bit size of the array element. |
489 | | */ |
490 | | static size_t bitSizeOf(ElementType element) |
491 | 128 | { |
492 | 128 | return zserio::bitSizeOfVarInt16(element); |
493 | 128 | } |
494 | | |
495 | | /** |
496 | | * Calculates bit size of the array element. |
497 | | * |
498 | | * \param element Element to use for calculation. |
499 | | * |
500 | | * \return Bit size of the array element. |
501 | | */ |
502 | | static size_t bitSizeOf(size_t, ElementType element) |
503 | 128 | { |
504 | 128 | return bitSizeOf(element); |
505 | 128 | } |
506 | | |
507 | | /** |
508 | | * Initializes indexed offsets of the single array element. |
509 | | * |
510 | | * \param bitPosition Current bit position. |
511 | | * \param element Element to use for calculation. |
512 | | * |
513 | | * \return Updated bit position which points to the first bit after the array element. |
514 | | */ |
515 | | static size_t initializeOffsets(size_t bitPosition, ElementType element) |
516 | 64 | { |
517 | 64 | return bitPosition + bitSizeOf(bitPosition, element); |
518 | 64 | } |
519 | | |
520 | | /** |
521 | | * Reads the single array element. |
522 | | * |
523 | | * \param in Bit stream reader. |
524 | | * |
525 | | * \return Read element. |
526 | | */ |
527 | | static ElementType read(BitStreamReader& in, size_t = 0) |
528 | 848 | { |
529 | 848 | return in.readVarInt16(); |
530 | 848 | } |
531 | | |
532 | | /** |
533 | | * Writes the single array element. |
534 | | * |
535 | | * \param out Bit stream writer to use. |
536 | | * \param element Element to write. |
537 | | */ |
538 | | static void write(BitStreamWriter& out, ElementType element) |
539 | 64 | { |
540 | 64 | out.writeVarInt16(element); |
541 | 64 | } |
542 | | |
543 | | /** Determines whether the bit size of the single element is constant. */ |
544 | | static constexpr bool IS_BITSIZEOF_CONSTANT = false; |
545 | | }; |
546 | | |
547 | | /** |
548 | | * Array traits specialization for Zserio varint32 type. |
549 | | */ |
550 | | template <> |
551 | | struct VarIntNNArrayTraits<int32_t> |
552 | | { |
553 | | /** Element type. */ |
554 | | using ElementType = int32_t; |
555 | | |
556 | | /** |
557 | | * Calculates bit size of the array element. |
558 | | * |
559 | | * \param element Element to use for calculation. |
560 | | * |
561 | | * \return Bit size of the array element. |
562 | | */ |
563 | | static size_t bitSizeOf(ElementType element) |
564 | 256 | { |
565 | 256 | return zserio::bitSizeOfVarInt32(element); |
566 | 256 | } |
567 | | |
568 | | /** |
569 | | * Calculates bit size of the array element. |
570 | | * |
571 | | * \param element Element to use for calculation. |
572 | | * |
573 | | * \return Bit size of the array element. |
574 | | */ |
575 | | static size_t bitSizeOf(size_t, ElementType element) |
576 | 256 | { |
577 | 256 | return bitSizeOf(element); |
578 | 256 | } |
579 | | |
580 | | /** |
581 | | * Initializes indexed offsets of the single array element. |
582 | | * |
583 | | * \param bitPosition Current bit position. |
584 | | * \param element Element to use for calculation. |
585 | | * |
586 | | * \return Updated bit position which points to the first bit after the array element. |
587 | | */ |
588 | | static size_t initializeOffsets(size_t bitPosition, ElementType element) |
589 | 128 | { |
590 | 128 | return bitPosition + bitSizeOf(bitPosition, element); |
591 | 128 | } |
592 | | |
593 | | /** |
594 | | * Reads the single array element. |
595 | | * |
596 | | * \param in Bit stream reader. |
597 | | * |
598 | | * \return Read element. |
599 | | */ |
600 | | static ElementType read(BitStreamReader& in, size_t = 0) |
601 | 908 | { |
602 | 908 | return in.readVarInt32(); |
603 | 908 | } |
604 | | |
605 | | /** |
606 | | * Writes the single array element. |
607 | | * |
608 | | * \param out Bit stream writer to use. |
609 | | * \param element Element to write. |
610 | | */ |
611 | | static void write(BitStreamWriter& out, ElementType element) |
612 | 128 | { |
613 | 128 | out.writeVarInt32(element); |
614 | 128 | } |
615 | | |
616 | | /** Determines whether the bit size of the single element is constant. */ |
617 | | static constexpr bool IS_BITSIZEOF_CONSTANT = false; |
618 | | }; |
619 | | |
620 | | /** |
621 | | * Array traits specialization for Zserio varint64 type. |
622 | | */ |
623 | | template <> |
624 | | struct VarIntNNArrayTraits<int64_t> |
625 | | { |
626 | | /** Element type. */ |
627 | | using ElementType = int64_t; |
628 | | |
629 | | /** |
630 | | * Calculates bit size of the array element. |
631 | | * |
632 | | * \param element Element to use for calculation. |
633 | | * |
634 | | * \return Bit size of the array element. |
635 | | */ |
636 | | static size_t bitSizeOf(ElementType element) |
637 | 512 | { |
638 | 512 | return zserio::bitSizeOfVarInt64(element); |
639 | 512 | } |
640 | | |
641 | | /** |
642 | | * Calculates bit size of the array element. |
643 | | * |
644 | | * \param element Element to use for calculation. |
645 | | * |
646 | | * \return Bit size of the array element. |
647 | | */ |
648 | | static size_t bitSizeOf(size_t, ElementType element) |
649 | 512 | { |
650 | 512 | return bitSizeOf(element); |
651 | 512 | } |
652 | | |
653 | | /** |
654 | | * Initializes indexed offsets of the single array element. |
655 | | * |
656 | | * \param bitPosition Current bit position. |
657 | | * \param element Element to use for calculation. |
658 | | * |
659 | | * \return Updated bit position which points to the first bit after the array element. |
660 | | */ |
661 | | static size_t initializeOffsets(size_t bitPosition, ElementType element) |
662 | 256 | { |
663 | 256 | return bitPosition + bitSizeOf(bitPosition, element); |
664 | 256 | } |
665 | | |
666 | | /** |
667 | | * Reads the single array element. |
668 | | * |
669 | | * \param in Bit stream reader. |
670 | | * |
671 | | * \return Read element. |
672 | | */ |
673 | | static ElementType read(BitStreamReader& in, size_t = 0) |
674 | 1.03k | { |
675 | 1.03k | return in.readVarInt64(); |
676 | 1.03k | } |
677 | | |
678 | | /** |
679 | | * Writes the single array element. |
680 | | * |
681 | | * \param out Bit stream writer to use. |
682 | | * \param element Element to write. |
683 | | */ |
684 | | static void write(BitStreamWriter& out, ElementType element) |
685 | 256 | { |
686 | 256 | out.writeVarInt64(element); |
687 | 256 | } |
688 | | |
689 | | /** Determines whether the bit size of the single element is constant. */ |
690 | | static constexpr bool IS_BITSIZEOF_CONSTANT = false; |
691 | | }; |
692 | | |
693 | | /** |
694 | | * Array traits specialization for Zserio varuint16 type. |
695 | | */ |
696 | | template <> |
697 | | struct VarIntNNArrayTraits<uint16_t> |
698 | | { |
699 | | /** Element type. */ |
700 | | using ElementType = uint16_t; |
701 | | |
702 | | /** |
703 | | * Calculates bit size of the array element. |
704 | | * |
705 | | * \param element Element to use for calculation. |
706 | | * |
707 | | * \return Bit size of the array element. |
708 | | */ |
709 | | static size_t bitSizeOf(ElementType element) |
710 | 128 | { |
711 | 128 | return zserio::bitSizeOfVarUInt16(element); |
712 | 128 | } |
713 | | |
714 | | /** |
715 | | * Calculates bit size of the array element. |
716 | | * |
717 | | * \param element Element to use for calculation. |
718 | | * |
719 | | * \return Bit size of the array element. |
720 | | */ |
721 | | static size_t bitSizeOf(size_t, ElementType element) |
722 | 128 | { |
723 | 128 | return bitSizeOf(element); |
724 | 128 | } |
725 | | |
726 | | /** |
727 | | * Initializes indexed offsets of the single array element. |
728 | | * |
729 | | * \param bitPosition Current bit position. |
730 | | * \param element Element to use for calculation. |
731 | | * |
732 | | * \return Updated bit position which points to the first bit after the array element. |
733 | | */ |
734 | | static size_t initializeOffsets(size_t bitPosition, ElementType element) |
735 | 64 | { |
736 | 64 | return bitPosition + bitSizeOf(bitPosition, element); |
737 | 64 | } |
738 | | |
739 | | /** |
740 | | * Reads the single array element. |
741 | | * |
742 | | * \param in Bit stream reader. |
743 | | * |
744 | | * \return Read element. |
745 | | */ |
746 | | static ElementType read(BitStreamReader& in, size_t = 0) |
747 | 848 | { |
748 | 848 | return in.readVarUInt16(); |
749 | 848 | } |
750 | | |
751 | | /** |
752 | | * Writes the single array element. |
753 | | * |
754 | | * \param out Bit stream writer to use. |
755 | | * \param element Element to write. |
756 | | */ |
757 | | static void write(BitStreamWriter& out, ElementType element) |
758 | 64 | { |
759 | 64 | out.writeVarUInt16(element); |
760 | 64 | } |
761 | | |
762 | | /** Determines whether the bit size of the single element is constant. */ |
763 | | static constexpr bool IS_BITSIZEOF_CONSTANT = false; |
764 | | }; |
765 | | |
766 | | /** |
767 | | * Array traits specialization for Zserio varuint32 type. |
768 | | */ |
769 | | template <> |
770 | | struct VarIntNNArrayTraits<uint32_t> |
771 | | { |
772 | | /** Element type. */ |
773 | | using ElementType = uint32_t; |
774 | | |
775 | | /** |
776 | | * Calculates bit size of the array element. |
777 | | * |
778 | | * \param element Element to use for calculation. |
779 | | * |
780 | | * \return Bit size of the array element. |
781 | | */ |
782 | | static size_t bitSizeOf(ElementType element) |
783 | 256 | { |
784 | 256 | return zserio::bitSizeOfVarUInt32(element); |
785 | 256 | } |
786 | | |
787 | | /** |
788 | | * Calculates bit size of the array element. |
789 | | * |
790 | | * \param element Element to use for calculation. |
791 | | * |
792 | | * \return Bit size of the array element. |
793 | | */ |
794 | | static size_t bitSizeOf(size_t, ElementType element) |
795 | 256 | { |
796 | 256 | return bitSizeOf(element); |
797 | 256 | } |
798 | | |
799 | | /** |
800 | | * Initializes indexed offsets of the single array element. |
801 | | * |
802 | | * \param bitPosition Current bit position. |
803 | | * \param element Element to use for calculation. |
804 | | * |
805 | | * \return Updated bit position which points to the first bit after the array element. |
806 | | */ |
807 | | static size_t initializeOffsets(size_t bitPosition, ElementType element) |
808 | 128 | { |
809 | 128 | return bitPosition + bitSizeOf(bitPosition, element); |
810 | 128 | } |
811 | | |
812 | | /** |
813 | | * Reads the single array element. |
814 | | * |
815 | | * \param in Bit stream reader. |
816 | | * |
817 | | * \return Read element. |
818 | | */ |
819 | | static ElementType read(BitStreamReader& in, size_t = 0) |
820 | 908 | { |
821 | 908 | return in.readVarUInt32(); |
822 | 908 | } |
823 | | |
824 | | /** |
825 | | * Writes the single array element. |
826 | | * |
827 | | * \param out Bit stream writer to use. |
828 | | * \param element Element to write. |
829 | | */ |
830 | | static void write(BitStreamWriter& out, ElementType element) |
831 | 128 | { |
832 | 128 | out.writeVarUInt32(element); |
833 | 128 | } |
834 | | |
835 | | /** Determines whether the bit size of the single element is constant. */ |
836 | | static constexpr bool IS_BITSIZEOF_CONSTANT = false; |
837 | | }; |
838 | | |
839 | | /** |
840 | | * Array traits specialization for Zserio varuint64 type. |
841 | | */ |
842 | | template <> |
843 | | struct VarIntNNArrayTraits<uint64_t> |
844 | | { |
845 | | /** Element type. */ |
846 | | using ElementType = uint64_t; |
847 | | |
848 | | /** |
849 | | * Calculates bit size of the array element. |
850 | | * |
851 | | * \param element Element to use for calculation. |
852 | | * |
853 | | * \return Bit size of the array element. |
854 | | */ |
855 | | static size_t bitSizeOf(ElementType element) |
856 | 2.91k | { |
857 | 2.91k | return zserio::bitSizeOfVarUInt64(element); |
858 | 2.91k | } |
859 | | |
860 | | /** |
861 | | * Calculates bit size of the array element. |
862 | | * |
863 | | * \param element Element to use for calculation. |
864 | | * |
865 | | * \return Bit size of the array element. |
866 | | */ |
867 | | static size_t bitSizeOf(size_t, ElementType element) |
868 | 512 | { |
869 | 512 | return bitSizeOf(element); |
870 | 512 | } |
871 | | |
872 | | /** |
873 | | * Initializes indexed offsets of the single array element. |
874 | | * |
875 | | * \param bitPosition Current bit position. |
876 | | * \param element Element to use for calculation. |
877 | | * |
878 | | * \return Updated bit position which points to the first bit after the array element. |
879 | | */ |
880 | | static size_t initializeOffsets(size_t bitPosition, ElementType element) |
881 | 256 | { |
882 | 256 | return bitPosition + bitSizeOf(bitPosition, element); |
883 | 256 | } |
884 | | |
885 | | /** |
886 | | * Reads the single array element. |
887 | | * |
888 | | * \param in Bit stream reader. |
889 | | * |
890 | | * \return Read element. |
891 | | */ |
892 | | static ElementType read(BitStreamReader& in, size_t = 0) |
893 | 1.90k | { |
894 | 1.90k | return in.readVarUInt64(); |
895 | 1.90k | } |
896 | | |
897 | | /** |
898 | | * Writes the single array element. |
899 | | * |
900 | | * \param out Bit stream writer to use. |
901 | | * \param element Element to write. |
902 | | */ |
903 | | static void write(BitStreamWriter& out, ElementType element) |
904 | 736 | { |
905 | 736 | out.writeVarUInt64(element); |
906 | 736 | } |
907 | | |
908 | | /** Determines whether the bit size of the single element is constant. */ |
909 | | static constexpr bool IS_BITSIZEOF_CONSTANT = false; |
910 | | }; |
911 | | |
912 | | /** |
913 | | * Array traits for big variable integer Zserio types (varint, varuint). |
914 | | */ |
915 | | template <typename T> |
916 | | struct VarIntArrayTraits; |
917 | | |
918 | | /** |
919 | | * Array traits specialization for Zserio varint type. |
920 | | */ |
921 | | template <> |
922 | | struct VarIntArrayTraits<int64_t> |
923 | | { |
924 | | /** Element type. */ |
925 | | using ElementType = int64_t; |
926 | | |
927 | | /** |
928 | | * Calculates bit size of the array element. |
929 | | * |
930 | | * \param element Element to use for calculation. |
931 | | * |
932 | | * \return Bit size of the array element. |
933 | | */ |
934 | | static size_t bitSizeOf(ElementType element) |
935 | 1.40k | { |
936 | 1.40k | return zserio::bitSizeOfVarInt(element); |
937 | 1.40k | } |
938 | | |
939 | | /** |
940 | | * Calculates bit size of the array element. |
941 | | * |
942 | | * \param element Element to use for calculation. |
943 | | * |
944 | | * \return Bit size of the array element. |
945 | | */ |
946 | | static size_t bitSizeOf(size_t, ElementType element) |
947 | 1.40k | { |
948 | 1.40k | return bitSizeOf(element); |
949 | 1.40k | } |
950 | | |
951 | | /** |
952 | | * Initializes indexed offsets of the single array element. |
953 | | * |
954 | | * \param bitPosition Current bit position. |
955 | | * \param element Element to use for calculation. |
956 | | * |
957 | | * \return Updated bit position which points to the first bit after the array element. |
958 | | */ |
959 | | static size_t initializeOffsets(size_t bitPosition, ElementType element) |
960 | 704 | { |
961 | 704 | return bitPosition + bitSizeOf(bitPosition, element); |
962 | 704 | } |
963 | | |
964 | | /** |
965 | | * Reads the single array element. |
966 | | * |
967 | | * \param in Bit stream reader. |
968 | | * |
969 | | * \return Read element. |
970 | | */ |
971 | | static ElementType read(BitStreamReader& in, size_t = 0) |
972 | 1.48k | { |
973 | 1.48k | return in.readVarInt(); |
974 | 1.48k | } |
975 | | |
976 | | /** |
977 | | * Writes the single array element. |
978 | | * |
979 | | * \param out Bit stream writer to use. |
980 | | * \param element Element to write. |
981 | | */ |
982 | | static void write(BitStreamWriter& out, ElementType element) |
983 | 704 | { |
984 | 704 | out.writeVarInt(element); |
985 | 704 | } |
986 | | |
987 | | /** Determines whether the bit size of the single element is constant. */ |
988 | | static constexpr bool IS_BITSIZEOF_CONSTANT = false; |
989 | | }; |
990 | | |
991 | | /** |
992 | | * Array traits specialization for Zserio varuint type. |
993 | | */ |
994 | | template <> |
995 | | struct VarIntArrayTraits<uint64_t> |
996 | | { |
997 | | /** Element type. */ |
998 | | using ElementType = uint64_t; |
999 | | |
1000 | | /** |
1001 | | * Calculates bit size of the array element. |
1002 | | * |
1003 | | * \param element Element to use for calculation. |
1004 | | * |
1005 | | * \return Bit size of the array element. |
1006 | | */ |
1007 | | static size_t bitSizeOf(ElementType element) |
1008 | 704 | { |
1009 | 704 | return zserio::bitSizeOfVarUInt(element); |
1010 | 704 | } |
1011 | | |
1012 | | /** |
1013 | | * Calculates bit size of the array element. |
1014 | | * |
1015 | | * \param element Element to use for calculation. |
1016 | | * |
1017 | | * \return Bit size of the array element. |
1018 | | */ |
1019 | | static size_t bitSizeOf(size_t, ElementType element) |
1020 | 704 | { |
1021 | 704 | return bitSizeOf(element); |
1022 | 704 | } |
1023 | | |
1024 | | /** |
1025 | | * Initializes indexed offsets of the single array element. |
1026 | | * |
1027 | | * \param bitPosition Current bit position. |
1028 | | * \param element Element to use for calculation. |
1029 | | * |
1030 | | * \return Updated bit position which points to the first bit after the array element. |
1031 | | */ |
1032 | | static size_t initializeOffsets(size_t bitPosition, ElementType element) |
1033 | 352 | { |
1034 | 352 | return bitPosition + bitSizeOf(bitPosition, element); |
1035 | 352 | } |
1036 | | |
1037 | | /** |
1038 | | * Reads the single array element. |
1039 | | * |
1040 | | * \param in Bit stream reader. |
1041 | | * |
1042 | | * \return Read element. |
1043 | | */ |
1044 | | static ElementType read(BitStreamReader& in, size_t = 0) |
1045 | 1.13k | { |
1046 | 1.13k | return in.readVarUInt(); |
1047 | 1.13k | } |
1048 | | |
1049 | | /** |
1050 | | * Writes the single array element. |
1051 | | * |
1052 | | * \param out Bit stream writer to use. |
1053 | | * \param element Element to write. |
1054 | | */ |
1055 | | static void write(BitStreamWriter& out, ElementType element) |
1056 | 352 | { |
1057 | 352 | out.writeVarUInt(element); |
1058 | 352 | } |
1059 | | |
1060 | | /** Determines whether the bit size of the single element is constant. */ |
1061 | | static constexpr bool IS_BITSIZEOF_CONSTANT = false; |
1062 | | }; |
1063 | | |
1064 | | /** |
1065 | | * Array traits specialization for Zserio varsize type. |
1066 | | */ |
1067 | | struct VarSizeArrayTraits |
1068 | | { |
1069 | | /** Element type. */ |
1070 | | using ElementType = uint32_t; |
1071 | | |
1072 | | /** |
1073 | | * Calculates bit size of the array element. |
1074 | | * |
1075 | | * \param element Element to use for calculation. |
1076 | | * |
1077 | | * \return Bit size of the array element. |
1078 | | */ |
1079 | | static size_t bitSizeOf(ElementType element) |
1080 | 1.12k | { |
1081 | 1.12k | return zserio::bitSizeOfVarSize(element); |
1082 | 1.12k | } |
1083 | | |
1084 | | /** |
1085 | | * Calculates bit size of the array element. |
1086 | | * |
1087 | | * \param element Element to use for calculation. |
1088 | | * |
1089 | | * \return Bit size of the array element. |
1090 | | */ |
1091 | | static size_t bitSizeOf(size_t, ElementType element) |
1092 | 320 | { |
1093 | 320 | return bitSizeOf(element); |
1094 | 320 | } |
1095 | | |
1096 | | /** |
1097 | | * Initializes indexed offsets of the single array element. |
1098 | | * |
1099 | | * \param bitPosition Current bit position. |
1100 | | * \param element Element to use for calculation. |
1101 | | * |
1102 | | * \return Updated bit position which points to the first bit after the array element. |
1103 | | */ |
1104 | | static size_t initializeOffsets(size_t bitPosition, ElementType element) |
1105 | 160 | { |
1106 | 160 | return bitPosition + bitSizeOf(bitPosition, element); |
1107 | 160 | } |
1108 | | |
1109 | | /** |
1110 | | * Reads the single array element. |
1111 | | * |
1112 | | * \param in Bit stream reader. |
1113 | | * |
1114 | | * \return Read element. |
1115 | | */ |
1116 | | static ElementType read(BitStreamReader& in, size_t = 0) |
1117 | 1.29k | { |
1118 | 1.29k | return in.readVarSize(); |
1119 | 1.29k | } |
1120 | | |
1121 | | /** |
1122 | | * Writes the single array element. |
1123 | | * |
1124 | | * \param out Bit stream writer to use. |
1125 | | * \param element Element to write |
1126 | | */ |
1127 | | static void write(BitStreamWriter& out, ElementType element) |
1128 | 320 | { |
1129 | 320 | out.writeVarSize(element); |
1130 | 320 | } |
1131 | | |
1132 | | /** Determines whether the bit size of the single element is constant. */ |
1133 | | static constexpr bool IS_BITSIZEOF_CONSTANT = false; |
1134 | | }; |
1135 | | |
1136 | | /** |
1137 | | * Array traits for Zserio float16 type. |
1138 | | */ |
1139 | | struct Float16ArrayTraits |
1140 | | { |
1141 | | /** Element type. */ |
1142 | | using ElementType = float; |
1143 | | |
1144 | | /** |
1145 | | * Calculates bit size of the array element. |
1146 | | * |
1147 | | * \return Bit size of the array element. |
1148 | | */ |
1149 | | static size_t bitSizeOf() |
1150 | 266 | { |
1151 | 266 | return 16; |
1152 | 266 | } |
1153 | | |
1154 | | /** |
1155 | | * Calculates bit size of the array element. |
1156 | | * |
1157 | | * \return Bit size of the array element. |
1158 | | */ |
1159 | | static size_t bitSizeOf(size_t, ElementType) |
1160 | 120 | { |
1161 | 120 | return bitSizeOf(); |
1162 | 120 | } |
1163 | | |
1164 | | /** |
1165 | | * Initializes indexed offsets of the single array element. |
1166 | | * |
1167 | | * \param bitPosition Current bit position. |
1168 | | * \param element Element to use for calculation. |
1169 | | * |
1170 | | * \return Updated bit position which points to the first bit after the array element. |
1171 | | */ |
1172 | | static size_t initializeOffsets(size_t bitPosition, ElementType element) |
1173 | 120 | { |
1174 | 120 | return bitPosition + bitSizeOf(bitPosition, element); |
1175 | 120 | } |
1176 | | |
1177 | | /** |
1178 | | * Reads the single array element. |
1179 | | * |
1180 | | * \param in Bit stream reader. |
1181 | | * |
1182 | | * \return Read element. |
1183 | | */ |
1184 | | static ElementType read(BitStreamReader& in, size_t = 0) |
1185 | 120 | { |
1186 | 120 | return in.readFloat16(); |
1187 | 120 | } |
1188 | | |
1189 | | /** |
1190 | | * Writes the single array element. |
1191 | | * |
1192 | | * \param out Bit stream writer to use. |
1193 | | * \param element Element to write. |
1194 | | */ |
1195 | | static void write(BitStreamWriter& out, ElementType element) |
1196 | 120 | { |
1197 | 120 | out.writeFloat16(element); |
1198 | 120 | } |
1199 | | |
1200 | | /** Determines whether the bit size of the single element is constant. */ |
1201 | | static constexpr bool IS_BITSIZEOF_CONSTANT = true; |
1202 | | }; |
1203 | | |
1204 | | /** |
1205 | | * Array traits for Zserio float32 type. |
1206 | | */ |
1207 | | struct Float32ArrayTraits |
1208 | | { |
1209 | | /** Element type. */ |
1210 | | using ElementType = float; |
1211 | | |
1212 | | /** |
1213 | | * Calculates bit size of the array element. |
1214 | | * |
1215 | | * \return Bit size of the array element. |
1216 | | */ |
1217 | | static size_t bitSizeOf() |
1218 | 266 | { |
1219 | 266 | return 32; |
1220 | 266 | } |
1221 | | |
1222 | | /** |
1223 | | * Calculates bit size of the array element. |
1224 | | * |
1225 | | * \return Bit size of the array element. |
1226 | | */ |
1227 | | static size_t bitSizeOf(size_t, ElementType) |
1228 | 120 | { |
1229 | 120 | return bitSizeOf(); |
1230 | 120 | } |
1231 | | |
1232 | | /** |
1233 | | * Initializes indexed offsets of the single array element. |
1234 | | * |
1235 | | * \param bitPosition Current bit position. |
1236 | | * \param element Element to use for calculation. |
1237 | | * |
1238 | | * \return Updated bit position which points to the first bit after the array element. |
1239 | | */ |
1240 | | static size_t initializeOffsets(size_t bitPosition, ElementType element) |
1241 | 120 | { |
1242 | 120 | return bitPosition + bitSizeOf(bitPosition, element); |
1243 | 120 | } |
1244 | | |
1245 | | /** |
1246 | | * Reads the single array element. |
1247 | | * |
1248 | | * \param in Bit stream reader. |
1249 | | * |
1250 | | * \return Read element. |
1251 | | */ |
1252 | | static ElementType read(BitStreamReader& in, size_t = 0) |
1253 | 120 | { |
1254 | 120 | return in.readFloat32(); |
1255 | 120 | } |
1256 | | |
1257 | | /** |
1258 | | * Writes the single array element. |
1259 | | * |
1260 | | * \param out Bit stream writer to use. |
1261 | | * \param element Element to write. |
1262 | | */ |
1263 | | static void write(BitStreamWriter& out, ElementType element) |
1264 | 120 | { |
1265 | 120 | out.writeFloat32(element); |
1266 | 120 | } |
1267 | | |
1268 | | /** Determines whether the bit size of the single element is constant. */ |
1269 | | static constexpr bool IS_BITSIZEOF_CONSTANT = true; |
1270 | | }; |
1271 | | |
1272 | | /** |
1273 | | * Array traits for Zserio float64 type. |
1274 | | */ |
1275 | | struct Float64ArrayTraits |
1276 | | { |
1277 | | /** Element type. */ |
1278 | | using ElementType = double; |
1279 | | |
1280 | | /** |
1281 | | * Calculates bit size of the array element. |
1282 | | * |
1283 | | * \return Bit size of the array element. |
1284 | | */ |
1285 | | static size_t bitSizeOf() |
1286 | 266 | { |
1287 | 266 | return 64; |
1288 | 266 | } |
1289 | | |
1290 | | /** |
1291 | | * Calculates bit size of the array element. |
1292 | | * |
1293 | | * \return Bit size of the array element. |
1294 | | */ |
1295 | | static size_t bitSizeOf(size_t, ElementType) |
1296 | 120 | { |
1297 | 120 | return bitSizeOf(); |
1298 | 120 | } |
1299 | | |
1300 | | /** |
1301 | | * Initializes indexed offsets of the single array element. |
1302 | | * |
1303 | | * \param bitPosition Current bit position. |
1304 | | * \param element Element to use for calculation. |
1305 | | * |
1306 | | * \return Updated bit position which points to the first bit after the array element. |
1307 | | */ |
1308 | | static size_t initializeOffsets(size_t bitPosition, ElementType element) |
1309 | 120 | { |
1310 | 120 | return bitPosition + bitSizeOf(bitPosition, element); |
1311 | 120 | } |
1312 | | |
1313 | | /** |
1314 | | * Reads the single array element. |
1315 | | * |
1316 | | * \param in Bit stream reader. |
1317 | | * |
1318 | | * \return Read element. |
1319 | | */ |
1320 | | static ElementType read(BitStreamReader& in, size_t = 0) |
1321 | 120 | { |
1322 | 120 | return in.readFloat64(); |
1323 | 120 | } |
1324 | | |
1325 | | /** |
1326 | | * Writes the single array element. |
1327 | | * |
1328 | | * \param out Bit stream writer to use. |
1329 | | * \param element Element to write. |
1330 | | */ |
1331 | | static void write(BitStreamWriter& out, ElementType element) |
1332 | 120 | { |
1333 | 120 | out.writeFloat64(element); |
1334 | 120 | } |
1335 | | |
1336 | | /** Determines whether the bit size of the single element is constant. */ |
1337 | | static constexpr bool IS_BITSIZEOF_CONSTANT = true; |
1338 | | }; |
1339 | | |
1340 | | /** |
1341 | | * Array traits for Zserio bool type. |
1342 | | */ |
1343 | | struct BoolArrayTraits |
1344 | | { |
1345 | | /** Element type. */ |
1346 | | using ElementType = bool; |
1347 | | |
1348 | | /** |
1349 | | * Calculates bit size of the array element. |
1350 | | * |
1351 | | * \return Bit size of the array element. |
1352 | | */ |
1353 | | static size_t bitSizeOf() |
1354 | 170 | { |
1355 | 170 | return 1; |
1356 | 170 | } |
1357 | | |
1358 | | /** |
1359 | | * Calculates bit size of the array element. |
1360 | | * |
1361 | | * \return Bit size of the array element. |
1362 | | */ |
1363 | | static size_t bitSizeOf(size_t, ElementType) |
1364 | 64 | { |
1365 | 64 | return bitSizeOf(); |
1366 | 64 | } |
1367 | | |
1368 | | /** |
1369 | | * Initializes indexed offsets of the single array element. |
1370 | | * |
1371 | | * \param bitPosition Current bit position. |
1372 | | * \param element Element to use for calculation. |
1373 | | * |
1374 | | * \return Updated bit position which points to the first bit after the array element. |
1375 | | */ |
1376 | | static size_t initializeOffsets(size_t bitPosition, ElementType element) |
1377 | 64 | { |
1378 | 64 | return bitPosition + bitSizeOf(bitPosition, element); |
1379 | 64 | } |
1380 | | |
1381 | | /** |
1382 | | * Reads the single array element. |
1383 | | * |
1384 | | * \param in Bit stream reader. |
1385 | | * |
1386 | | * \return Read element. |
1387 | | */ |
1388 | | static ElementType read(BitStreamReader& in, size_t = 0) |
1389 | 64 | { |
1390 | 64 | return in.readBool(); |
1391 | 64 | } |
1392 | | |
1393 | | /** |
1394 | | * Writes the single array element. |
1395 | | * |
1396 | | * \param out Bit stream writer to use. |
1397 | | * \param element Element to write. |
1398 | | */ |
1399 | | static void write(BitStreamWriter& out, ElementType element) |
1400 | 64 | { |
1401 | 64 | out.writeBool(element); |
1402 | 64 | } |
1403 | | |
1404 | | /** Determines whether the bit size of the single element is constant. */ |
1405 | | static constexpr bool IS_BITSIZEOF_CONSTANT = true; |
1406 | | }; |
1407 | | |
1408 | | /** |
1409 | | * Array traits for Zserio bytes type. |
1410 | | */ |
1411 | | template <template <typename> class ALLOC = std::allocator> |
1412 | | struct BasicBytesArrayTraits |
1413 | | { |
1414 | | /** ElementType */ |
1415 | | using ElementType = zserio::vector<uint8_t, ALLOC<uint8_t>>; |
1416 | | |
1417 | | /** Allocator type. */ |
1418 | | using allocator_type = typename ElementType::allocator_type; |
1419 | | |
1420 | | /** |
1421 | | * Calculates bit size of the array element. |
1422 | | * |
1423 | | * \param element Element to use for calculation. |
1424 | | * |
1425 | | * \return Bit size of the array element. |
1426 | | */ |
1427 | | static size_t bitSizeOf(size_t, const ElementType& element) |
1428 | 128 | { |
1429 | 128 | return zserio::bitSizeOfBytes(element); |
1430 | 128 | } |
1431 | | |
1432 | | /** |
1433 | | * Initializes indexed offsets of the single array element. |
1434 | | * |
1435 | | * \param bitPosition Current bit position. |
1436 | | * \param element Element to use for calculation. |
1437 | | * |
1438 | | * \return Updated bit position which points to the first bit after the array element. |
1439 | | */ |
1440 | | static size_t initializeOffsets(size_t bitPosition, const ElementType& element) |
1441 | 64 | { |
1442 | 64 | return bitPosition + bitSizeOf(bitPosition, element); |
1443 | 64 | } |
1444 | | |
1445 | | /** |
1446 | | * Reads the single array element. |
1447 | | * |
1448 | | * \param rawArray Raw array to use. |
1449 | | * \param in Bit stream reader. |
1450 | | */ |
1451 | | template <typename RAW_ARRAY> |
1452 | | static void read(RAW_ARRAY& rawArray, BitStreamReader& in, size_t = 0) |
1453 | 456 | { |
1454 | 456 | rawArray.emplace_back(in.readBytes(rawArray.get_allocator())); |
1455 | 456 | } |
1456 | | |
1457 | | /** |
1458 | | * Writes the single array element. |
1459 | | * |
1460 | | * \param out Bit stream writer to use. |
1461 | | * \param element Element to write. |
1462 | | */ |
1463 | | static void write(BitStreamWriter& out, const ElementType& element) |
1464 | 64 | { |
1465 | 64 | out.writeBytes(element); |
1466 | 64 | } |
1467 | | |
1468 | | /** Determines whether the bit size of the single element is constant. */ |
1469 | | static constexpr bool IS_BITSIZEOF_CONSTANT = false; |
1470 | | }; |
1471 | | |
1472 | | using BytesArrayTraits = BasicBytesArrayTraits<>; |
1473 | | |
1474 | | /** |
1475 | | * Array traits for Zserio string type. |
1476 | | */ |
1477 | | template <template <typename> class ALLOC = std::allocator> |
1478 | | struct BasicStringArrayTraits |
1479 | | { |
1480 | | /** Element type. */ |
1481 | | using ElementType = zserio::string<ALLOC<char>>; |
1482 | | |
1483 | | /** Allocator type. */ |
1484 | | using allocator_type = typename ElementType::allocator_type; |
1485 | | |
1486 | | /** |
1487 | | * Calculates bit size of the array element. |
1488 | | * |
1489 | | * \param element Element to use for calculation. |
1490 | | * |
1491 | | * \return Bit size of the array element. |
1492 | | */ |
1493 | | static size_t bitSizeOf(size_t, const ElementType& element) |
1494 | 192 | { |
1495 | 192 | return zserio::bitSizeOfString(element); |
1496 | 192 | } |
1497 | | |
1498 | | /** |
1499 | | * Initializes indexed offsets of the single array element. |
1500 | | * |
1501 | | * \param bitPosition Current bit position. |
1502 | | * \param element Element to use for calculation. |
1503 | | * |
1504 | | * \return Updated bit position which points to the first bit after the array element. |
1505 | | */ |
1506 | | static size_t initializeOffsets(size_t bitPosition, const ElementType& element) |
1507 | 96 | { |
1508 | 96 | return bitPosition + bitSizeOf(bitPosition, element); |
1509 | 96 | } |
1510 | | |
1511 | | /** |
1512 | | * Reads the single array element. |
1513 | | * |
1514 | | * \param rawArray Raw array to use. |
1515 | | * \param in Bit stream reader. |
1516 | | */ |
1517 | | template <typename RAW_ARRAY> |
1518 | | static void read(RAW_ARRAY& rawArray, BitStreamReader& in, size_t = 0) |
1519 | 488 | { |
1520 | 488 | rawArray.emplace_back(in.readString(rawArray.get_allocator())); |
1521 | 488 | } |
1522 | | |
1523 | | /** |
1524 | | * Writes the single array element. |
1525 | | * |
1526 | | * \param out Bit stream writer to use. |
1527 | | * \param element Element to write. |
1528 | | */ |
1529 | | static void write(BitStreamWriter& out, const ElementType& element) |
1530 | 96 | { |
1531 | 96 | out.writeString(element); |
1532 | 96 | } |
1533 | | |
1534 | | /** Determines whether the bit size of the single element is constant. */ |
1535 | | static constexpr bool IS_BITSIZEOF_CONSTANT = false; |
1536 | | }; |
1537 | | |
1538 | | using StringArrayTraits = BasicStringArrayTraits<>; |
1539 | | |
1540 | | /** |
1541 | | * Array traits for Zserio extern bit buffer type. |
1542 | | */ |
1543 | | template <template <typename> class ALLOC = std::allocator> |
1544 | | struct BasicBitBufferArrayTraits |
1545 | | { |
1546 | | /** Element type. */ |
1547 | | using ElementType = BasicBitBuffer<ALLOC<uint8_t>>; |
1548 | | |
1549 | | /** Allocator type. */ |
1550 | | using allocator_type = typename ElementType::allocator_type; |
1551 | | |
1552 | | /** |
1553 | | * Calculates bit size of the array element. |
1554 | | * |
1555 | | * \param element Element to use for calculation. |
1556 | | * |
1557 | | * \return Bit size of the array element. |
1558 | | */ |
1559 | | static size_t bitSizeOf(size_t, const ElementType& element) |
1560 | 192 | { |
1561 | 192 | return zserio::bitSizeOfBitBuffer(element); |
1562 | 192 | } |
1563 | | |
1564 | | /** |
1565 | | * Initializes indexed offsets of the single array element. |
1566 | | * |
1567 | | * \param bitPosition Current bit position. |
1568 | | * \param element Element to use for calculation. |
1569 | | * |
1570 | | * \return Updated bit position which points to the first bit after the array element. |
1571 | | */ |
1572 | | static size_t initializeOffsets(size_t bitPosition, const ElementType& element) |
1573 | 96 | { |
1574 | 96 | return bitPosition + bitSizeOf(bitPosition, element); |
1575 | 96 | } |
1576 | | |
1577 | | /** |
1578 | | * Reads the single array element. |
1579 | | * |
1580 | | * \param rawArray Raw array to use. |
1581 | | * \param in Bit stream reader. |
1582 | | */ |
1583 | | template <typename RAW_ARRAY> |
1584 | | static void read(RAW_ARRAY& rawArray, BitStreamReader& in, size_t = 0) |
1585 | 488 | { |
1586 | 488 | rawArray.emplace_back(in.readBitBuffer(rawArray.get_allocator())); |
1587 | 488 | } |
1588 | | |
1589 | | /** |
1590 | | * Writes the single array element. |
1591 | | * |
1592 | | * \param out Bit stream writer to use. |
1593 | | * \param element Element to write. |
1594 | | */ |
1595 | | static void write(BitStreamWriter& out, const ElementType& element) |
1596 | 96 | { |
1597 | 96 | out.writeBitBuffer(element); |
1598 | 96 | } |
1599 | | |
1600 | | /** Determines whether the bit size of the single element is constant. */ |
1601 | | static constexpr bool IS_BITSIZEOF_CONSTANT = false; |
1602 | | }; |
1603 | | |
1604 | | using BitBufferArrayTraits = BasicBitBufferArrayTraits<>; |
1605 | | |
1606 | | /** |
1607 | | * Array traits for Zserio enumeration type. |
1608 | | */ |
1609 | | template <typename T> |
1610 | | struct EnumArrayTraits |
1611 | | { |
1612 | | /** Element type. */ |
1613 | | using ElementType = T; |
1614 | | |
1615 | | /** |
1616 | | * Calculates bit size of the array element. |
1617 | | * |
1618 | | * \param element Element to use for calculation. |
1619 | | * |
1620 | | * \return Bit size of the array element. |
1621 | | */ |
1622 | | static size_t bitSizeOf(size_t, ElementType element) |
1623 | 97 | { |
1624 | 97 | return zserio::bitSizeOf(element); |
1625 | 97 | } |
1626 | | |
1627 | | /** |
1628 | | * Initializes indexed offsets of the single array element. |
1629 | | * |
1630 | | * \param bitPosition Current bit position. |
1631 | | * \param element Element to use for calculation. |
1632 | | * |
1633 | | * \return Updated bit position which points to the first bit after the array element. |
1634 | | */ |
1635 | | static size_t initializeOffsets(size_t bitPosition, ElementType element) |
1636 | 97 | { |
1637 | 97 | return zserio::initializeOffsets(bitPosition, element); |
1638 | 97 | } |
1639 | | |
1640 | | /** |
1641 | | * Reads the single array element. |
1642 | | * |
1643 | | * \param in Bit stream reader. |
1644 | | * |
1645 | | * \return Read element. |
1646 | | */ |
1647 | | static ElementType read(BitStreamReader& in, size_t = 0) |
1648 | 490 | { |
1649 | 490 | return zserio::read<ElementType>(in); |
1650 | 490 | } |
1651 | | |
1652 | | /** |
1653 | | * Writes the single array element. |
1654 | | * |
1655 | | * \param out Bit stream writer to use. |
1656 | | * \param element Element to write. |
1657 | | */ |
1658 | | static void write(BitStreamWriter& out, ElementType element) |
1659 | 97 | { |
1660 | 97 | zserio::write(out, element); |
1661 | 97 | } |
1662 | | |
1663 | | // Be aware that T can be varuint, so bitSizeOf cannot return constant value. |
1664 | | /** Determines whether the bit size of the single element is constant. */ |
1665 | | static constexpr bool IS_BITSIZEOF_CONSTANT = false; |
1666 | | }; |
1667 | | |
1668 | | /** |
1669 | | * Array traits for Zserio bitmask type. |
1670 | | */ |
1671 | | template <typename T> |
1672 | | struct BitmaskArrayTraits |
1673 | | { |
1674 | | /** Element type. */ |
1675 | | using ElementType = T; |
1676 | | |
1677 | | /** |
1678 | | * Calculates bit size of the array element. |
1679 | | * |
1680 | | * \param element Element to use for calculation. |
1681 | | * |
1682 | | * \return Bit size of the array element. |
1683 | | */ |
1684 | | static size_t bitSizeOf(size_t, ElementType element) |
1685 | 96 | { |
1686 | 96 | return element.bitSizeOf(); |
1687 | 96 | } |
1688 | | |
1689 | | /** |
1690 | | * Initializes indexed offsets of the single array element. |
1691 | | * |
1692 | | * \param bitPosition Current bit position. |
1693 | | * \param element Element to use for calculation. |
1694 | | * |
1695 | | * \return Updated bit position which points to the first bit after the array element. |
1696 | | */ |
1697 | | static size_t initializeOffsets(size_t bitPosition, ElementType element) |
1698 | 96 | { |
1699 | 96 | return element.initializeOffsets(bitPosition); |
1700 | 96 | } |
1701 | | |
1702 | | /** |
1703 | | * Reads the single array element. |
1704 | | * |
1705 | | * \param in Bit stream reader. |
1706 | | * |
1707 | | * \return Read element. |
1708 | | */ |
1709 | | static ElementType read(BitStreamReader& in, size_t = 0) |
1710 | 888 | { |
1711 | 888 | return ElementType(in); |
1712 | 888 | } |
1713 | | |
1714 | | /** |
1715 | | * Writes the single array element. |
1716 | | * |
1717 | | * \param out Bit stream writer to use. |
1718 | | * \param element Element to write. |
1719 | | */ |
1720 | | static void write(BitStreamWriter& out, ElementType element) |
1721 | 96 | { |
1722 | 96 | element.write(out); |
1723 | 96 | } |
1724 | | |
1725 | | // Be aware that T can be varuint, so bitSizeOf cannot return constant value. |
1726 | | /** Determines whether the bit size of the single element is constant. */ |
1727 | | static constexpr bool IS_BITSIZEOF_CONSTANT = false; |
1728 | | }; |
1729 | | |
1730 | | /** |
1731 | | * Array traits for Zserio structure, choice and union types. |
1732 | | */ |
1733 | | template <typename T, typename ELEMENT_FACTORY> |
1734 | | class ObjectArrayTraits |
1735 | | { |
1736 | | public: |
1737 | | /** Element type. */ |
1738 | | using ElementType = T; |
1739 | | |
1740 | | /** Allocator type. */ |
1741 | | using allocator_type = typename ElementType::allocator_type; |
1742 | | |
1743 | | /** Typedef for the array's owner type. */ |
1744 | | using OwnerType = typename ELEMENT_FACTORY::OwnerType; |
1745 | | |
1746 | | /** |
1747 | | * Calculates bit size of the array element. |
1748 | | * |
1749 | | * \param bitPosition Current bit position. |
1750 | | * \param element Element to use for calculation. |
1751 | | * |
1752 | | * \return Bit size of the array element. |
1753 | | */ |
1754 | | static size_t bitSizeOf(const OwnerType&, size_t bitPosition, const ElementType& element) |
1755 | 192 | { |
1756 | 192 | return element.bitSizeOf(bitPosition); |
1757 | 192 | } |
1758 | | |
1759 | | /** |
1760 | | * Initializes indexed offsets of the single array element. |
1761 | | * |
1762 | | * \param bitPosition Current bit position. |
1763 | | * \param element Element to use. |
1764 | | * |
1765 | | * \return Updated bit position which points to the first bit after the array element. |
1766 | | */ |
1767 | | static size_t initializeOffsets(OwnerType&, size_t bitPosition, ElementType& element) |
1768 | 192 | { |
1769 | 192 | return element.initializeOffsets(bitPosition); |
1770 | 192 | } |
1771 | | |
1772 | | /** |
1773 | | * Reads the single array element. |
1774 | | * |
1775 | | * \param owner Owner of the array. |
1776 | | * \param rawArray Raw array to use. |
1777 | | * \param in Bit stream reader. |
1778 | | * \param index Index need in case of parameterized type which depends on the current index. |
1779 | | */ |
1780 | | template <typename RAW_ARRAY> |
1781 | | static void read(OwnerType& owner, RAW_ARRAY& rawArray, BitStreamReader& in, size_t index) |
1782 | 448 | { |
1783 | 448 | ELEMENT_FACTORY::create(owner, rawArray, in, index); |
1784 | 448 | } |
1785 | | |
1786 | | /** |
1787 | | * Writes the single array element. |
1788 | | * |
1789 | | * \param out Bit stream writer to use. |
1790 | | * \param element Element to write. |
1791 | | */ |
1792 | | static void write(const OwnerType&, BitStreamWriter& out, const ElementType& element) |
1793 | 192 | { |
1794 | 192 | element.write(out); |
1795 | 192 | } |
1796 | | |
1797 | | /** Determines whether the bit size of the single element is constant. */ |
1798 | | static constexpr bool IS_BITSIZEOF_CONSTANT = false; |
1799 | | }; |
1800 | | |
1801 | | namespace detail |
1802 | | { |
1803 | | |
1804 | | template <typename ARRAY_TRAITS> |
1805 | | struct packed_array_traits_overloaded : std::false_type |
1806 | | {}; |
1807 | | |
1808 | | template <typename T, typename ELEMENT_FACTORY> |
1809 | | struct packed_array_traits_overloaded<ObjectArrayTraits<T, ELEMENT_FACTORY>> : std::true_type |
1810 | | {}; |
1811 | | |
1812 | | } // namespace detail |
1813 | | |
1814 | | /** |
1815 | | * Packed array traits. |
1816 | | * |
1817 | | * Packed array traits are used for all packable built-in types. Works with a single DeltaContext. |
1818 | | */ |
1819 | | template <typename ARRAY_TRAITS, typename = void> |
1820 | | class PackedArrayTraits |
1821 | | { |
1822 | | public: |
1823 | | /** Typedef for array traits. */ |
1824 | | using ArrayTraits = ARRAY_TRAITS; |
1825 | | |
1826 | | /** Element type. */ |
1827 | | using ElementType = typename ARRAY_TRAITS::ElementType; |
1828 | | |
1829 | | /** |
1830 | | * Calls context initialization step for the current element. |
1831 | | * |
1832 | | * Available for traits which do not need the owner. |
1833 | | * |
1834 | | * \param deltaContext Delta context. |
1835 | | * \param element Current element. |
1836 | | */ |
1837 | | static void initContext(DeltaContext& deltaContext, ElementType element) |
1838 | 9.69k | { |
1839 | 9.69k | deltaContext.template init<ArrayTraits>(element); |
1840 | 9.69k | } |
1841 | | |
1842 | | /** |
1843 | | * Returns length of the array element stored in the bit stream in bits. |
1844 | | * |
1845 | | * Available for traits which do not need the owner. |
1846 | | * |
1847 | | * \param deltaContext Delta context. |
1848 | | * \param element Current element. |
1849 | | * |
1850 | | * \return Length of the array element stored in the bit stream in bits. |
1851 | | */ |
1852 | | static size_t bitSizeOf(DeltaContext& deltaContext, size_t, ElementType element) |
1853 | 6.46k | { |
1854 | 6.46k | return deltaContext.template bitSizeOf<ArrayTraits>(element); |
1855 | 6.46k | } |
1856 | | |
1857 | | /** |
1858 | | * Calls indexed offsets initialization for the current element. |
1859 | | * |
1860 | | * Available for traits which do not need the owner. |
1861 | | * |
1862 | | * \param deltaContext Delta context. |
1863 | | * \param bitPosition Current bit stream position. |
1864 | | * \param element Current element. |
1865 | | * |
1866 | | * \return Updated bit stream position which points to the first bit after this element. |
1867 | | */ |
1868 | | static size_t initializeOffsets(DeltaContext& deltaContext, size_t bitPosition, ElementType element) |
1869 | 3.23k | { |
1870 | 3.23k | return bitPosition + bitSizeOf(deltaContext, bitPosition, element); |
1871 | 3.23k | } |
1872 | | |
1873 | | /** |
1874 | | * Reads an element from the bit stream. |
1875 | | * |
1876 | | * Available for traits which do not need the owner. |
1877 | | * |
1878 | | * \param deltaContext Delta context. |
1879 | | * \param in Bit stream reader. |
1880 | | * |
1881 | | * \return Read element value. |
1882 | | */ |
1883 | | static ElementType read(DeltaContext& deltaContext, BitStreamReader& in, size_t = 0) |
1884 | 5.61k | { |
1885 | 5.61k | return deltaContext.template read<ArrayTraits>(in); |
1886 | 5.61k | } |
1887 | | |
1888 | | /** |
1889 | | * Writes the element to the bit stream. |
1890 | | * |
1891 | | * Available for traits which do not need the owner. |
1892 | | * |
1893 | | * \param deltaContext Delta context. |
1894 | | * \param out Bit stream writer. |
1895 | | * \param element Element to write. |
1896 | | */ |
1897 | | static void write(DeltaContext& deltaContext, BitStreamWriter& out, ElementType element) |
1898 | 3.23k | { |
1899 | 3.23k | deltaContext.template write<ArrayTraits>(out, element); |
1900 | 3.23k | } |
1901 | | }; |
1902 | | |
1903 | | /** |
1904 | | * Specialization of packed array traits for traits which needs the array's owner. |
1905 | | * |
1906 | | * This packed array traits are used for all packable built-in types which needs the array's owner. |
1907 | | */ |
1908 | | template <typename ARRAY_TRAITS> |
1909 | | class PackedArrayTraits<ARRAY_TRAITS, typename std::enable_if<has_owner_type<ARRAY_TRAITS>::value>::type> |
1910 | | { |
1911 | | public: |
1912 | | /** Typedef for array traits. */ |
1913 | | using ArrayTraits = ARRAY_TRAITS; |
1914 | | |
1915 | | /** Element type. */ |
1916 | | using ElementType = typename ARRAY_TRAITS::ElementType; |
1917 | | |
1918 | | /** Typedef for the array's owner type. */ |
1919 | | using OwnerType = typename ARRAY_TRAITS::OwnerType; |
1920 | | |
1921 | | /** |
1922 | | * Calls context initialization step for the current element. |
1923 | | * |
1924 | | * Available for traits which need the owner. |
1925 | | * |
1926 | | * \param owner Owner of the current array. |
1927 | | * \param deltaContext Delta context node which keeps the context. |
1928 | | * \param element Current element. |
1929 | | */ |
1930 | | static void initContext(const OwnerType& owner, DeltaContext& deltaContext, ElementType element) |
1931 | 576 | { |
1932 | 576 | deltaContext.template init<ArrayTraits>(owner, element); |
1933 | 576 | } |
1934 | | |
1935 | | /** |
1936 | | * Returns length of the array element stored in the bit stream in bits. |
1937 | | * |
1938 | | * Available for traits which need the owner. |
1939 | | * |
1940 | | * \param owner Owner of the current array. |
1941 | | * \param deltaContext Delta context. |
1942 | | * \param element Current element. |
1943 | | * |
1944 | | * \return Length of the array element stored in the bit stream in bits. |
1945 | | */ |
1946 | | static size_t bitSizeOf(const OwnerType& owner, DeltaContext& deltaContext, size_t, ElementType element) |
1947 | 384 | { |
1948 | 384 | return deltaContext.template bitSizeOf<ArrayTraits>(owner, element); |
1949 | 384 | } |
1950 | | |
1951 | | /** |
1952 | | * Calls indexed offsets initialization for the current element. |
1953 | | * |
1954 | | * Available for traits which need the owner. |
1955 | | * |
1956 | | * \param owner Owner of the current array. |
1957 | | * \param deltaContext Delta context. |
1958 | | * \param bitPosition Current bit stream position. |
1959 | | * \param element Current element. |
1960 | | * |
1961 | | * \return Updated bit stream position which points to the first bit after this element. |
1962 | | */ |
1963 | | static size_t initializeOffsets( |
1964 | | const OwnerType& owner, DeltaContext& deltaContext, size_t bitPosition, ElementType element) |
1965 | 192 | { |
1966 | 192 | return bitPosition + bitSizeOf(owner, deltaContext, bitPosition, element); |
1967 | 192 | } |
1968 | | |
1969 | | /** |
1970 | | * Reads an element from the bit stream. |
1971 | | * |
1972 | | * Available for traits which need the owner. |
1973 | | * |
1974 | | * \param owner Owner of the current array. |
1975 | | * \param deltaContext Delta context. |
1976 | | * \param in Bit stream reader. |
1977 | | * |
1978 | | * \return Read element value. |
1979 | | */ |
1980 | | static ElementType read(const OwnerType& owner, DeltaContext& deltaContext, BitStreamReader& in, size_t = 0) |
1981 | 412 | { |
1982 | 412 | return deltaContext.template read<ArrayTraits>(owner, in); |
1983 | 412 | } |
1984 | | |
1985 | | /** |
1986 | | * Writes the element to the bit stream. |
1987 | | * |
1988 | | * Available for traits which need the owner. |
1989 | | * |
1990 | | * \param owner Owner of the current array. |
1991 | | * \param deltaContext Delta context. |
1992 | | * \param out Bit stream writer. |
1993 | | * \param element Element to write. |
1994 | | */ |
1995 | | static void write( |
1996 | | const OwnerType& owner, DeltaContext& deltaContext, BitStreamWriter& out, ElementType element) |
1997 | 192 | { |
1998 | 192 | deltaContext.template write<ArrayTraits>(owner, out, element); |
1999 | 192 | } |
2000 | | }; |
2001 | | |
2002 | | /** |
2003 | | * Specialization of packed array traits for Zserio enums. |
2004 | | */ |
2005 | | template <typename T> |
2006 | | class PackedArrayTraits<EnumArrayTraits<T>> |
2007 | | { |
2008 | | public: |
2009 | | /** Typedef for array traits. */ |
2010 | | using ArrayTraits = EnumArrayTraits<T>; |
2011 | | |
2012 | | /** Element type. */ |
2013 | | using ElementType = T; |
2014 | | |
2015 | | /** |
2016 | | * Calls context initialization step for the current element. |
2017 | | * |
2018 | | * \param deltaContext Delta context. |
2019 | | * \param element Current element. |
2020 | | */ |
2021 | | static void initContext(DeltaContext& deltaContext, ElementType element) |
2022 | 288 | { |
2023 | 288 | zserio::initPackingContext(deltaContext, element); |
2024 | 288 | } |
2025 | | |
2026 | | /** |
2027 | | * Returns length of the array element stored in the bit stream in bits. |
2028 | | * |
2029 | | * \param deltaContext Delta context. |
2030 | | * \param element Current element. |
2031 | | * |
2032 | | * \return Length of the array element stored in the bit stream in bits. |
2033 | | */ |
2034 | | static size_t bitSizeOf(DeltaContext& deltaContext, size_t, ElementType element) |
2035 | 96 | { |
2036 | 96 | return zserio::bitSizeOf(deltaContext, element); |
2037 | 96 | } |
2038 | | |
2039 | | /** |
2040 | | * Calls indexed offsets initialization for the current element. |
2041 | | * |
2042 | | * \param deltaContext Delta context. |
2043 | | * \param bitPosition Current bit stream position. |
2044 | | * \param element Current element. |
2045 | | * |
2046 | | * \return Updated bit stream position which points to the first bit after this element. |
2047 | | */ |
2048 | | static size_t initializeOffsets(DeltaContext& deltaContext, size_t bitPosition, ElementType element) |
2049 | 96 | { |
2050 | 96 | return zserio::initializeOffsets(deltaContext, bitPosition, element); |
2051 | 96 | } |
2052 | | |
2053 | | /** |
2054 | | * Reads an element from the bit stream. |
2055 | | * |
2056 | | * \param deltaContext Delta context. |
2057 | | * \param in Bit stream reader. |
2058 | | * |
2059 | | * \return Read element value. |
2060 | | */ |
2061 | | static ElementType read(DeltaContext& deltaContext, BitStreamReader& in, size_t = 0) |
2062 | 290 | { |
2063 | 290 | return zserio::read<ElementType>(deltaContext, in); |
2064 | 290 | } |
2065 | | |
2066 | | /** |
2067 | | * Writes the element to the bit stream. |
2068 | | * |
2069 | | * \param deltaContext Delta context. |
2070 | | * \param out Bit stream writer. |
2071 | | * \param element Element to write. |
2072 | | */ |
2073 | | static void write(DeltaContext& deltaContext, BitStreamWriter& out, ElementType element) |
2074 | 96 | { |
2075 | 96 | zserio::write(deltaContext, out, element); |
2076 | 96 | } |
2077 | | }; |
2078 | | |
2079 | | /** |
2080 | | * Specialization of packed array traits for Zserio bitmasks. |
2081 | | */ |
2082 | | template <typename T> |
2083 | | class PackedArrayTraits<BitmaskArrayTraits<T>> |
2084 | | { |
2085 | | public: |
2086 | | /** Typedef for array traits. */ |
2087 | | using ArrayTraits = BitmaskArrayTraits<T>; |
2088 | | |
2089 | | /** Element type. */ |
2090 | | using ElementType = T; |
2091 | | |
2092 | | /** |
2093 | | * Calls context initialization step for the current element. |
2094 | | * |
2095 | | * \param deltaContext Delta context. |
2096 | | */ |
2097 | | static void initContext(DeltaContext& deltaContext, const ElementType& element) |
2098 | 288 | { |
2099 | 288 | element.initPackingContext(deltaContext); |
2100 | 288 | } |
2101 | | |
2102 | | /** |
2103 | | * Returns length of the array element stored in the bit stream in bits. |
2104 | | * |
2105 | | * \param deltaContext Delta context. |
2106 | | * \param bitPosition Current bit stream position. |
2107 | | * \param element Current element. |
2108 | | * |
2109 | | * \return Length of the array element stored in the bit stream in bits. |
2110 | | */ |
2111 | | static size_t bitSizeOf(DeltaContext& deltaContext, size_t bitPosition, const ElementType& element) |
2112 | 96 | { |
2113 | 96 | return element.bitSizeOf(deltaContext, bitPosition); |
2114 | 96 | } |
2115 | | |
2116 | | /** |
2117 | | * Calls indexed offsets initialization for the current element. |
2118 | | * |
2119 | | * \param deltaContext Delta context. |
2120 | | * \param bitPosition Current bit stream position. |
2121 | | * \param element Current element. |
2122 | | * |
2123 | | * \return Updated bit stream position which points to the first bit after this element. |
2124 | | */ |
2125 | | static size_t initializeOffsets(DeltaContext& deltaContext, size_t bitPosition, const ElementType& element) |
2126 | 96 | { |
2127 | 96 | return element.initializeOffsets(deltaContext, bitPosition); |
2128 | 96 | } |
2129 | | |
2130 | | /** |
2131 | | * Reads an element from the bit stream. |
2132 | | * |
2133 | | * \param deltaContext Delta context. |
2134 | | * \param in Bit stream reader. |
2135 | | * |
2136 | | * \return Read element. |
2137 | | */ |
2138 | | static ElementType read(DeltaContext& deltaContext, BitStreamReader& in, size_t = 0) |
2139 | 316 | { |
2140 | 316 | return ElementType(deltaContext, in); |
2141 | 316 | } |
2142 | | |
2143 | | /** |
2144 | | * Writes the element to the bit stream. |
2145 | | * |
2146 | | * \param deltaContext Delta context. |
2147 | | * \param out Bit stream writer. |
2148 | | * \param element Element to write. |
2149 | | */ |
2150 | | static void write(DeltaContext& deltaContext, BitStreamWriter& out, const ElementType& element) |
2151 | 96 | { |
2152 | 96 | element.write(deltaContext, out); |
2153 | 96 | } |
2154 | | }; |
2155 | | |
2156 | | // note: enable_if is needed to be more specific then specialization for types which needs owner type |
2157 | | /** |
2158 | | * Specialization of packed array traits for Zserio objects. |
2159 | | */ |
2160 | | template <typename T, typename ELEMENT_FACTORY> |
2161 | | class PackedArrayTraits<ObjectArrayTraits<T, ELEMENT_FACTORY>, |
2162 | | typename std::enable_if<has_owner_type<ObjectArrayTraits<T, ELEMENT_FACTORY>>::value>::type> |
2163 | | { |
2164 | | public: |
2165 | | /** Typedef for array traits. */ |
2166 | | using ArrayTraits = ObjectArrayTraits<T, ELEMENT_FACTORY>; |
2167 | | |
2168 | | /** Element type. */ |
2169 | | using ElementType = T; |
2170 | | |
2171 | | /** Allocator type. */ |
2172 | | using allocator_type = typename T::allocator_type; |
2173 | | |
2174 | | /** Typedef for the array's owner type. */ |
2175 | | using OwnerType = typename ArrayTraits::OwnerType; |
2176 | | |
2177 | | /** |
2178 | | * Calls context initialization step for the current element. |
2179 | | * |
2180 | | * \param packingContext Packing context node which keeps the appropriate subtree of contexts. |
2181 | | */ |
2182 | | template <typename PACKING_CONTEXT> |
2183 | | static void initContext( |
2184 | | const typename ArrayTraits::OwnerType&, PACKING_CONTEXT& packingContext, const ElementType& element) |
2185 | 576 | { |
2186 | 576 | element.initPackingContext(packingContext); |
2187 | 576 | } |
2188 | | |
2189 | | /** |
2190 | | * Returns length of the array element stored in the bit stream in bits. |
2191 | | * |
2192 | | * \param packingContext Packing context node which keeps the appropriate subtree of contexts. |
2193 | | * \param bitPosition Current bit stream position. |
2194 | | * \param element Current element. |
2195 | | * |
2196 | | * \return Length of the array element stored in the bit stream in bits. |
2197 | | */ |
2198 | | template <typename PACKING_CONTEXT> |
2199 | | static size_t bitSizeOf(const typename ArrayTraits::OwnerType&, PACKING_CONTEXT& packingContext, |
2200 | | size_t bitPosition, const ElementType& element) |
2201 | 192 | { |
2202 | 192 | return element.bitSizeOf(packingContext, bitPosition); |
2203 | 192 | } |
2204 | | |
2205 | | /** |
2206 | | * Calls indexed offsets initialization for the current element. |
2207 | | * |
2208 | | * \param packingContext Packing context node which keeps the appropriate subtree of contexts. |
2209 | | * \param bitPosition Current bit stream position. |
2210 | | * \param element Current element. |
2211 | | * |
2212 | | * \return Updated bit stream position which points to the first bit after this element. |
2213 | | */ |
2214 | | template <typename PACKING_CONTEXT> |
2215 | | static size_t initializeOffsets(const typename ArrayTraits::OwnerType&, PACKING_CONTEXT& packingContext, |
2216 | | size_t bitPosition, ElementType& element) |
2217 | 192 | { |
2218 | 192 | return element.initializeOffsets(packingContext, bitPosition); |
2219 | 192 | } |
2220 | | |
2221 | | /** |
2222 | | * Reads an element from the bit stream. |
2223 | | * |
2224 | | * \param owner Owner of the array. |
2225 | | * \param rawArray Raw array to use. |
2226 | | * \param packingContext Packing context node which keeps the appropriate subtree of contexts. |
2227 | | * \param in Bit stream reader. |
2228 | | * \param allocator Allocator to use. |
2229 | | * \param index Index of the current element. |
2230 | | */ |
2231 | | template <typename RAW_ARRAY, typename PACKING_CONTEXT> |
2232 | | static void read(typename ArrayTraits::OwnerType& owner, RAW_ARRAY& rawArray, |
2233 | | PACKING_CONTEXT& packingContext, BitStreamReader& in, size_t index) |
2234 | 318 | { |
2235 | 318 | ELEMENT_FACTORY::create(owner, rawArray, packingContext, in, index); |
2236 | 318 | } |
2237 | | |
2238 | | /** |
2239 | | * Writes the element to the bit stream. |
2240 | | * |
2241 | | * \param packingContext Packing context node which keeps the appropriate subtree of contexts. |
2242 | | * \param out Bit stream writer. |
2243 | | * \param element Element to write. |
2244 | | */ |
2245 | | template <typename PACKING_CONTEXT> |
2246 | | static void write(const typename ArrayTraits::OwnerType&, PACKING_CONTEXT& packingContext, |
2247 | | BitStreamWriter& out, const ElementType& element) |
2248 | 192 | { |
2249 | 192 | element.write(packingContext, out); |
2250 | 192 | } |
2251 | | }; |
2252 | | |
2253 | | } // namespace zserio |
2254 | | |
2255 | | #endif // ZSERIO_ARRAY_TRAITS_H_INC |