Zserio C++17 runtime library 1.3.0
Built for Zserio 2.20.0
Loading...
Searching...
No Matches
Types.h
Go to the documentation of this file.
1#ifndef ZSERIO_TYPES_H_INC
2#define ZSERIO_TYPES_H_INC
3
4#include <cstdint>
5#include <limits>
6#include <string_view>
7#include <type_traits>
8
9#include "zserio/BitSize.h"
12#include "zserio/Traits.h"
13
14namespace zserio
15{
16
24template <typename T>
26{};
27
28namespace detail
29{
30
41template <typename VALUE_TYPE>
42class NumericTypeWrapper
43{
44public:
46 using ValueType = VALUE_TYPE;
47
51 constexpr explicit NumericTypeWrapper() noexcept :
52 m_value()
53 {}
54
60 constexpr NumericTypeWrapper(ValueType value) noexcept :
61 m_value(value)
62 {}
63
67 constexpr operator ValueType() const noexcept
68 {
69 return m_value;
70 }
71
80 constexpr NumericTypeWrapper& operator++() noexcept
81 {
82 ++m_value;
83 return *this;
84 }
85
86 constexpr NumericTypeWrapper operator++(int) noexcept
87 {
88 NumericTypeWrapper old = *this;
89 operator++();
90 return old;
91 }
92
93 constexpr NumericTypeWrapper& operator--() noexcept
94 {
95 --m_value;
96 return *this;
97 }
98
99 constexpr NumericTypeWrapper operator--(int) noexcept
100 {
101 NumericTypeWrapper old = *this;
102 operator--();
103 return old;
104 }
105
106 constexpr NumericTypeWrapper& operator+=(ValueType value) noexcept
107 {
108 m_value += value;
109 return *this;
110 }
111
112 constexpr NumericTypeWrapper& operator-=(ValueType value) noexcept
113 {
114 m_value -= value;
115 return *this;
116 }
117
118 constexpr NumericTypeWrapper& operator*=(ValueType value) noexcept
119 {
120 m_value *= value;
121 return *this;
122 }
123
124 constexpr NumericTypeWrapper& operator/=(ValueType value) noexcept
125 {
126 m_value /= value;
127 return *this;
128 }
129
130 constexpr NumericTypeWrapper& operator%=(ValueType value) noexcept
131 {
132 m_value %= value;
133 return *this;
134 }
135
136 constexpr NumericTypeWrapper& operator&=(ValueType value) noexcept
137 {
138 m_value &= value;
139 return *this;
140 }
141
142 constexpr NumericTypeWrapper& operator|=(ValueType value) noexcept
143 {
144 m_value |= value;
145 return *this;
146 }
147
148 constexpr NumericTypeWrapper& operator^=(ValueType value) noexcept
149 {
150 m_value ^= value;
151 return *this;
152 }
153
154 constexpr NumericTypeWrapper& operator<<=(ValueType value) noexcept
155 {
156 m_value <<= value;
157 return *this;
158 }
159
160 constexpr NumericTypeWrapper& operator>>=(ValueType value) noexcept
161 {
162 m_value >>= value;
163 return *this;
164 }
165
168private:
169 ValueType m_value;
170};
171
172class BoolWrapper : public NumericTypeWrapper<bool>
173{
174public:
175 using NumericTypeWrapper<bool>::NumericTypeWrapper;
176};
177
178constexpr bool operator<(const BoolWrapper& lhs, const BoolWrapper& rhs)
179{
180 return static_cast<int>(lhs) < static_cast<int>(rhs);
181}
182
183constexpr bool operator>(const BoolWrapper& lhs, const BoolWrapper& rhs)
184{
185 return rhs < lhs;
186}
187
188constexpr bool operator<=(const BoolWrapper& lhs, const BoolWrapper& rhs)
189{
190 return !(rhs < lhs);
191}
192
193constexpr bool operator>=(const BoolWrapper& lhs, const BoolWrapper& rhs)
194{
195 return !(lhs < rhs);
196}
197
198template <BitSize BIT_SIZE, bool IS_SIGNED, typename = void>
199struct fixed_int_value_type;
200
201template <BitSize BIT_SIZE>
202struct fixed_int_value_type<BIT_SIZE, true, std::enable_if_t<(BIT_SIZE > 0 && BIT_SIZE <= 8)>>
203{
204 using type = int8_t;
205};
206
207template <BitSize BIT_SIZE>
208struct fixed_int_value_type<BIT_SIZE, true, std::enable_if_t<(BIT_SIZE > 8 && BIT_SIZE <= 16)>>
209{
210 using type = int16_t;
211};
212
213template <BitSize BIT_SIZE>
214struct fixed_int_value_type<BIT_SIZE, true, std::enable_if_t<(BIT_SIZE > 16 && BIT_SIZE <= 32)>>
215{
216 using type = int32_t;
217};
218
219template <BitSize BIT_SIZE>
220struct fixed_int_value_type<BIT_SIZE, true, std::enable_if_t<(BIT_SIZE > 32 && BIT_SIZE <= 64)>>
221{
222 using type = int64_t;
223};
224
225template <BitSize BIT_SIZE>
226struct fixed_int_value_type<BIT_SIZE, false, std::enable_if_t<(BIT_SIZE > 0 && BIT_SIZE <= 8)>>
227{
228 using type = uint8_t;
229};
230
231template <BitSize BIT_SIZE>
232struct fixed_int_value_type<BIT_SIZE, false, std::enable_if_t<(BIT_SIZE > 8 && BIT_SIZE <= 16)>>
233{
234 using type = uint16_t;
235};
236
237template <BitSize BIT_SIZE>
238struct fixed_int_value_type<BIT_SIZE, false, std::enable_if_t<(BIT_SIZE > 16 && BIT_SIZE <= 32)>>
239{
240 using type = uint32_t;
241};
242
243template <BitSize BIT_SIZE>
244struct fixed_int_value_type<BIT_SIZE, false, std::enable_if_t<(BIT_SIZE > 32 && BIT_SIZE <= 64)>>
245{
246 using type = uint64_t;
247};
248
249template <BitSize BIT_SIZE, bool IS_SIGNED>
250using fixed_int_value_type_t = typename fixed_int_value_type<BIT_SIZE, IS_SIGNED>::type;
251
252template <BitSize BIT_SIZE, bool IS_SIGNED>
253class FixedIntWrapper : public NumericTypeWrapper<fixed_int_value_type_t<BIT_SIZE, IS_SIGNED>>
254{
255public:
256 using ValueType = fixed_int_value_type_t<BIT_SIZE, IS_SIGNED>;
257 using NumericTypeWrapper<ValueType>::NumericTypeWrapper;
258
259 static_assert(BIT_SIZE > 0, "BitSize cannot be 0!");
260 static_assert((BIT_SIZE + 7) / 8 <= sizeof(ValueType), "BitSize doesn't fit to the VALUE_TYPE!");
261};
262
263template <typename VALUE_TYPE>
264class DynIntWrapper : public NumericTypeWrapper<VALUE_TYPE>
265{
266public:
267 using NumericTypeWrapper<VALUE_TYPE>::NumericTypeWrapper;
268};
269
270enum class VarIntType : uint8_t
271{
272 VAR16,
273 VAR32,
274 VAR64,
275 VAR,
276 VARSIZE
277};
278
279template <typename VALUE_TYPE, VarIntType VAR_TYPE>
280class VarIntWrapper : public NumericTypeWrapper<VALUE_TYPE>
281{
282 static_assert(
283 VAR_TYPE != VarIntType::VARSIZE || std::is_unsigned_v<VALUE_TYPE>, "VARSIZE must be unsigned!");
284
285public:
286 using NumericTypeWrapper<VALUE_TYPE>::NumericTypeWrapper;
287};
288
289enum class FloatType : uint8_t
290{
291 FLOAT16,
292 FLOAT32,
293 FLOAT64
294};
295
296template <typename VALUE_TYPE, FloatType FLOAT_TYPE>
297class FloatWrapper : public NumericTypeWrapper<VALUE_TYPE>
298{
299public:
300 using NumericTypeWrapper<VALUE_TYPE>::NumericTypeWrapper;
301};
302
303// helper traits to check if range checking is needed (used in checkedCast)
304template <typename T>
305struct needs_range_check
306{};
307
308template <typename T>
309inline constexpr bool needs_range_check_v = needs_range_check<T>::value;
310
311template <>
312struct needs_range_check<BoolWrapper> : std::false_type
313{};
314
315template <BitSize BIT_SIZE, bool IS_SIGNED>
316struct needs_range_check<FixedIntWrapper<BIT_SIZE, IS_SIGNED>>
317{
318 using ValueType = typename FixedIntWrapper<BIT_SIZE, IS_SIGNED>::ValueType;
319
320 static constexpr bool value = sizeof(ValueType) * 8 != BIT_SIZE ||
321 (BIT_SIZE != 8 && BIT_SIZE != 16 && BIT_SIZE != 32 && BIT_SIZE != 64);
322};
323
324template <typename VALUE_TYPE>
325struct needs_range_check<DynIntWrapper<VALUE_TYPE>> : std::true_type
326{};
327
328template <typename VALUE_TYPE, VarIntType VAR_TYPE>
329struct needs_range_check<VarIntWrapper<VALUE_TYPE, VAR_TYPE>>
330{
331 static constexpr bool value = VAR_TYPE != VarIntType::VAR;
332};
333
334template <typename VALUE_TYPE, FloatType FLOAT_TYPE>
335struct needs_range_check<FloatWrapper<VALUE_TYPE, FLOAT_TYPE>> : std::false_type
336{
337 // TODO[mikir]: Does it have sense to have range checking for Float16?
338};
339
340// range checker used by checkedCast, specialized for particular types when needed
341template <typename T>
342struct RangeChecker
343{
344 static constexpr void check(typename T::ValueType value, std::string_view fieldName = "")
345 {
346 std::string_view fieldNamePrefix = (fieldName.empty()) ? "" : " for field ";
347 if constexpr (std::is_signed_v<typename T::ValueType>)
348 {
349 if (value < NumericLimits<T>::min() || value > NumericLimits<T>::max())
350 {
351 throw OutOfRangeException("Value '")
352 << value << "' out of range '<" << NumericLimits<T>::min() << ", "
353 << NumericLimits<T>::max() << ">'" << fieldNamePrefix << fieldName << "!";
354 }
355 }
356 else
357 {
358 if (value > NumericLimits<T>::max())
359 {
360 throw OutOfRangeException("Value '")
361 << value << "' out of bounds '" << NumericLimits<T>::max() << "'" << fieldNamePrefix
362 << fieldName << "!";
363 }
364 }
365 }
366};
367
368template <typename VALUE_TYPE>
369struct RangeChecker<DynIntWrapper<VALUE_TYPE>>
370{
371 static constexpr void check(VALUE_TYPE value, BitSize numBits, std::string_view fieldName = "")
372 {
373 std::string_view fieldNamePrefix = (fieldName.empty()) ? "" : " for field ";
374 if constexpr (std::is_signed_v<VALUE_TYPE>)
375 {
376 if (value < NumericLimits<DynIntWrapper<VALUE_TYPE>>::min(numBits) ||
377 value > NumericLimits<DynIntWrapper<VALUE_TYPE>>::max(numBits))
378 {
379 throw OutOfRangeException("Value '")
380 << value << "' out of range '<"
381 << NumericLimits<DynIntWrapper<VALUE_TYPE>>::min(numBits) << ", "
382 << NumericLimits<DynIntWrapper<VALUE_TYPE>>::max(numBits) << ">'" << fieldNamePrefix
383 << fieldName << "!";
384 }
385 }
386 else
387 {
388 if (value > NumericLimits<DynIntWrapper<VALUE_TYPE>>::max(numBits))
389 {
390 throw OutOfRangeException("Value '")
391 << value << "' out of bounds '"
392 << NumericLimits<DynIntWrapper<VALUE_TYPE>>::max(numBits) << "'" << fieldNamePrefix
393 << fieldName << "!";
394 }
395 }
396 }
397};
398
399} // namespace detail
400
404template <>
405struct NumericLimits<detail::BoolWrapper>
406{
407 static constexpr detail::BoolWrapper min() noexcept
408 {
409 return false;
410 }
411
412 static constexpr detail::BoolWrapper max() noexcept
413 {
414 return true;
415 }
416};
417
421template <BitSize BIT_SIZE, bool IS_SIGNED>
422struct NumericLimits<detail::FixedIntWrapper<BIT_SIZE, IS_SIGNED>>
423{
424 using ValueType = typename detail::FixedIntWrapper<BIT_SIZE, IS_SIGNED>::ValueType;
425
426 static constexpr detail::FixedIntWrapper<BIT_SIZE, IS_SIGNED> min() noexcept
427 {
428 if constexpr (!detail::needs_range_check_v<detail::FixedIntWrapper<BIT_SIZE, IS_SIGNED>>)
429 {
430 return std::numeric_limits<ValueType>::min();
431 }
432 else if constexpr (std::is_signed_v<ValueType>)
433 {
434 return static_cast<ValueType>(-static_cast<ValueType>(1ULL << (BIT_SIZE - 1U)));
435 }
436 else
437 {
438 return 0;
439 }
440 }
441
442 static constexpr detail::FixedIntWrapper<BIT_SIZE, IS_SIGNED> max() noexcept
443 {
444 if constexpr (!detail::needs_range_check_v<detail::FixedIntWrapper<BIT_SIZE, IS_SIGNED>>)
445 {
446 return std::numeric_limits<ValueType>::max();
447 }
448 else if constexpr (std::is_signed_v<ValueType>)
449 {
450 return static_cast<ValueType>((1ULL << (BIT_SIZE - 1U)) - 1U);
451 }
452 else
453 {
454 return static_cast<ValueType>((1ULL << BIT_SIZE) - 1U);
455 }
456 }
457};
458
462template <typename VALUE_TYPE, detail::VarIntType VAR_TYPE>
463struct NumericLimits<detail::VarIntWrapper<VALUE_TYPE, VAR_TYPE>>
464{
465 static constexpr detail::VarIntWrapper<VALUE_TYPE, VAR_TYPE> min() noexcept
466 {
467 if constexpr (std::is_signed_v<VALUE_TYPE>)
468 {
469 if constexpr (VAR_TYPE == detail::VarIntType::VAR)
470 {
471 return std::numeric_limits<VALUE_TYPE>::min();
472 }
473 else
474 {
475 return -max();
476 }
477 }
478 else
479 {
480 return 0;
481 }
482 }
483
484 static constexpr detail::VarIntWrapper<VALUE_TYPE, VAR_TYPE> max() noexcept
485 {
486 if constexpr (VAR_TYPE == detail::VarIntType::VAR16)
487 {
488 return static_cast<VALUE_TYPE>((UINT64_C(1) << (FIRST_BYTE_BITS + 8)) - 1);
489 }
490 else if constexpr (VAR_TYPE == detail::VarIntType::VAR32)
491 {
492 return static_cast<VALUE_TYPE>((UINT64_C(1) << (FIRST_BYTE_BITS + 7 + 7 + 8)) - 1);
493 }
494 else if constexpr (VAR_TYPE == detail::VarIntType::VAR64)
495 {
496 return static_cast<VALUE_TYPE>((UINT64_C(1) << (FIRST_BYTE_BITS + 7 + 7 + 7 + 7 + 7 + 7 + 8)) - 1);
497 }
498 else if constexpr (VAR_TYPE == detail::VarIntType::VAR)
499 {
500 return std::numeric_limits<VALUE_TYPE>::max();
501 }
502 else // VARSIZE
503 {
504 return (UINT64_C(1) << (2U + 7U + 7U + 7U + 8U)) - 1;
505 }
506 }
507
508private:
509 static constexpr uint64_t FIRST_BYTE_BITS = std::is_signed_v<VALUE_TYPE> ? 6 : 7;
510};
511
515template <typename VALUE_TYPE>
516struct NumericLimits<detail::DynIntWrapper<VALUE_TYPE>>
517{
518 static constexpr detail::DynIntWrapper<VALUE_TYPE> min(BitSize numBits)
519 {
520 if constexpr (std::is_signed_v<VALUE_TYPE>)
521 {
522 checkNumBits(numBits);
523
524 return minSignedValue(numBits);
525 }
526 else
527 {
528 return 0;
529 }
530 }
531
532 static constexpr detail::DynIntWrapper<VALUE_TYPE> max(BitSize numBits)
533 {
534 checkNumBits(numBits);
535
536 if constexpr (std::is_signed_v<VALUE_TYPE>)
537 {
538 return maxSignedValue(numBits);
539 }
540 else
541 {
542 return maxUnsignedValue(numBits);
543 }
544 }
545
546private:
547 static constexpr void checkNumBits(BitSize numBits)
548 {
549 if (numBits < 1 || numBits > sizeof(VALUE_TYPE) * 8)
550 {
551 throw OutOfRangeException("Dynamic bit field numBits '")
552 << numBits << "' out of range '<1, " << sizeof(VALUE_TYPE) * 8 << ">'!";
553 }
554 }
555
556 static constexpr VALUE_TYPE minSignedValue(BitSize numBits)
557 {
558 if constexpr (std::is_same_v<VALUE_TYPE, int64_t>)
559 {
560 if (numBits == 64)
561 {
562 return INT64_MIN;
563 }
564 }
565
566 return static_cast<VALUE_TYPE>(-static_cast<int64_t>(1ULL << (numBits - 1U)));
567 }
568
569 static constexpr VALUE_TYPE maxSignedValue(BitSize numBits)
570 {
571 if constexpr (std::is_same_v<VALUE_TYPE, int64_t>)
572 {
573 if (numBits == 64)
574 {
575 return INT64_MAX;
576 }
577 }
578
579 return static_cast<VALUE_TYPE>((1ULL << (numBits - 1U)) - 1U);
580 }
581
582 static constexpr VALUE_TYPE maxUnsignedValue(BitSize numBits)
583 {
584 if constexpr (std::is_same_v<VALUE_TYPE, uint64_t>)
585 {
586 if (numBits == 64)
587 {
588 return UINT64_MAX;
589 }
590 }
591
592 return static_cast<VALUE_TYPE>((1ULL << numBits) - 1U);
593 }
594};
595
601template <>
602struct NumericLimits<detail::FloatWrapper<float, detail::FloatType::FLOAT16>>
603{
604 static constexpr detail::FloatWrapper<float, detail::FloatType::FLOAT16> min() noexcept
605 {
606 return 6.103515625e-05F;
607 }
608
609 static constexpr detail::FloatWrapper<float, detail::FloatType::FLOAT16> max() noexcept
610 {
611 return 65504.F;
612 }
613};
614
618template <>
619struct NumericLimits<detail::FloatWrapper<float, detail::FloatType::FLOAT32>>
620{
621 static constexpr detail::FloatWrapper<float, detail::FloatType::FLOAT32> min() noexcept
622 {
623 return std::numeric_limits<float>::min();
624 }
625
626 static constexpr detail::FloatWrapper<float, detail::FloatType::FLOAT32> max() noexcept
627 {
628 return std::numeric_limits<float>::max();
629 }
630};
631
635template <>
636struct NumericLimits<detail::FloatWrapper<double, detail::FloatType::FLOAT64>>
637{
638 static constexpr detail::FloatWrapper<double, detail::FloatType::FLOAT64> min() noexcept
639 {
640 return std::numeric_limits<double>::min();
641 }
642
643 static constexpr detail::FloatWrapper<double, detail::FloatType::FLOAT64> max() noexcept
644 {
645 return std::numeric_limits<double>::max();
646 }
647};
648
658template <typename T, std::enable_if_t<is_numeric_wrapper_v<T>, int> = 0>
659constexpr T fromCheckedValue(typename T::ValueType value) noexcept(!detail::needs_range_check_v<T>)
660{
661 if constexpr (detail::needs_range_check_v<T>)
662 {
663 detail::RangeChecker<T>::check(value);
664 }
665
666 return T(value);
667}
668
681template <typename T, std::enable_if_t<is_numeric_wrapper_v<T>, int> = 0>
682constexpr T fromCheckedValue(typename T::ValueType value, BitSize numBits)
683{
684 detail::RangeChecker<T>::check(value, numBits);
685
686 return T(value);
687}
688
698template <typename T, std::enable_if_t<is_numeric_wrapper_v<T>, int> = 0>
699constexpr typename T::ValueType toCheckedValue(T wrapper) noexcept(!detail::needs_range_check_v<T>)
700{
701 if constexpr (detail::needs_range_check_v<T>)
702 {
703 detail::RangeChecker<T>::check(wrapper);
704 }
705
706 return wrapper;
707}
708
721template <typename T, std::enable_if_t<is_numeric_wrapper_v<T>, int> = 0>
722constexpr typename T::ValueType toCheckedValue(T wrapper, BitSize numBits)
723{
724 detail::RangeChecker<T>::check(wrapper, numBits);
725
726 return wrapper;
727}
728
732using Bool = detail::BoolWrapper;
733
734using Int1 = detail::FixedIntWrapper<1, true>;
735using Int2 = detail::FixedIntWrapper<2, true>;
736using Int3 = detail::FixedIntWrapper<3, true>;
737using Int4 = detail::FixedIntWrapper<4, true>;
738using Int5 = detail::FixedIntWrapper<5, true>;
739using Int6 = detail::FixedIntWrapper<6, true>;
740using Int7 = detail::FixedIntWrapper<7, true>;
741using Int8 = detail::FixedIntWrapper<8, true>;
742using Int9 = detail::FixedIntWrapper<9, true>;
743using Int10 = detail::FixedIntWrapper<10, true>;
744using Int11 = detail::FixedIntWrapper<11, true>;
745using Int12 = detail::FixedIntWrapper<12, true>;
746using Int13 = detail::FixedIntWrapper<13, true>;
747using Int14 = detail::FixedIntWrapper<14, true>;
748using Int15 = detail::FixedIntWrapper<15, true>;
749using Int16 = detail::FixedIntWrapper<16, true>;
750using Int17 = detail::FixedIntWrapper<17, true>;
751using Int18 = detail::FixedIntWrapper<18, true>;
752using Int19 = detail::FixedIntWrapper<19, true>;
753using Int20 = detail::FixedIntWrapper<20, true>;
754using Int21 = detail::FixedIntWrapper<21, true>;
755using Int22 = detail::FixedIntWrapper<22, true>;
756using Int23 = detail::FixedIntWrapper<23, true>;
757using Int24 = detail::FixedIntWrapper<24, true>;
758using Int25 = detail::FixedIntWrapper<25, true>;
759using Int26 = detail::FixedIntWrapper<26, true>;
760using Int27 = detail::FixedIntWrapper<27, true>;
761using Int28 = detail::FixedIntWrapper<28, true>;
762using Int29 = detail::FixedIntWrapper<29, true>;
763using Int30 = detail::FixedIntWrapper<30, true>;
764using Int31 = detail::FixedIntWrapper<31, true>;
765using Int32 = detail::FixedIntWrapper<32, true>;
766using Int33 = detail::FixedIntWrapper<33, true>;
767using Int34 = detail::FixedIntWrapper<34, true>;
768using Int35 = detail::FixedIntWrapper<35, true>;
769using Int36 = detail::FixedIntWrapper<36, true>;
770using Int37 = detail::FixedIntWrapper<37, true>;
771using Int38 = detail::FixedIntWrapper<38, true>;
772using Int39 = detail::FixedIntWrapper<39, true>;
773using Int40 = detail::FixedIntWrapper<40, true>;
774using Int41 = detail::FixedIntWrapper<41, true>;
775using Int42 = detail::FixedIntWrapper<42, true>;
776using Int43 = detail::FixedIntWrapper<43, true>;
777using Int44 = detail::FixedIntWrapper<44, true>;
778using Int45 = detail::FixedIntWrapper<45, true>;
779using Int46 = detail::FixedIntWrapper<46, true>;
780using Int47 = detail::FixedIntWrapper<47, true>;
781using Int48 = detail::FixedIntWrapper<48, true>;
782using Int49 = detail::FixedIntWrapper<49, true>;
783using Int50 = detail::FixedIntWrapper<50, true>;
784using Int51 = detail::FixedIntWrapper<51, true>;
785using Int52 = detail::FixedIntWrapper<52, true>;
786using Int53 = detail::FixedIntWrapper<53, true>;
787using Int54 = detail::FixedIntWrapper<54, true>;
788using Int55 = detail::FixedIntWrapper<55, true>;
789using Int56 = detail::FixedIntWrapper<56, true>;
790using Int57 = detail::FixedIntWrapper<57, true>;
791using Int58 = detail::FixedIntWrapper<58, true>;
792using Int59 = detail::FixedIntWrapper<59, true>;
793using Int60 = detail::FixedIntWrapper<60, true>;
794using Int61 = detail::FixedIntWrapper<61, true>;
795using Int62 = detail::FixedIntWrapper<62, true>;
796using Int63 = detail::FixedIntWrapper<63, true>;
797using Int64 = detail::FixedIntWrapper<64, true>;
798
799using UInt1 = detail::FixedIntWrapper<1, false>;
800using UInt2 = detail::FixedIntWrapper<2, false>;
801using UInt3 = detail::FixedIntWrapper<3, false>;
802using UInt4 = detail::FixedIntWrapper<4, false>;
803using UInt5 = detail::FixedIntWrapper<5, false>;
804using UInt6 = detail::FixedIntWrapper<6, false>;
805using UInt7 = detail::FixedIntWrapper<7, false>;
806using UInt8 = detail::FixedIntWrapper<8, false>;
807using UInt9 = detail::FixedIntWrapper<9, false>;
808using UInt10 = detail::FixedIntWrapper<10, false>;
809using UInt11 = detail::FixedIntWrapper<11, false>;
810using UInt12 = detail::FixedIntWrapper<12, false>;
811using UInt13 = detail::FixedIntWrapper<13, false>;
812using UInt14 = detail::FixedIntWrapper<14, false>;
813using UInt15 = detail::FixedIntWrapper<15, false>;
814using UInt16 = detail::FixedIntWrapper<16, false>;
815using UInt17 = detail::FixedIntWrapper<17, false>;
816using UInt18 = detail::FixedIntWrapper<18, false>;
817using UInt19 = detail::FixedIntWrapper<19, false>;
818using UInt20 = detail::FixedIntWrapper<20, false>;
819using UInt21 = detail::FixedIntWrapper<21, false>;
820using UInt22 = detail::FixedIntWrapper<22, false>;
821using UInt23 = detail::FixedIntWrapper<23, false>;
822using UInt24 = detail::FixedIntWrapper<24, false>;
823using UInt25 = detail::FixedIntWrapper<25, false>;
824using UInt26 = detail::FixedIntWrapper<26, false>;
825using UInt27 = detail::FixedIntWrapper<27, false>;
826using UInt28 = detail::FixedIntWrapper<28, false>;
827using UInt29 = detail::FixedIntWrapper<29, false>;
828using UInt30 = detail::FixedIntWrapper<30, false>;
829using UInt31 = detail::FixedIntWrapper<31, false>;
830using UInt32 = detail::FixedIntWrapper<32, false>;
831using UInt33 = detail::FixedIntWrapper<33, false>;
832using UInt34 = detail::FixedIntWrapper<34, false>;
833using UInt35 = detail::FixedIntWrapper<35, false>;
834using UInt36 = detail::FixedIntWrapper<36, false>;
835using UInt37 = detail::FixedIntWrapper<37, false>;
836using UInt38 = detail::FixedIntWrapper<38, false>;
837using UInt39 = detail::FixedIntWrapper<39, false>;
838using UInt40 = detail::FixedIntWrapper<40, false>;
839using UInt41 = detail::FixedIntWrapper<41, false>;
840using UInt42 = detail::FixedIntWrapper<42, false>;
841using UInt43 = detail::FixedIntWrapper<43, false>;
842using UInt44 = detail::FixedIntWrapper<44, false>;
843using UInt45 = detail::FixedIntWrapper<45, false>;
844using UInt46 = detail::FixedIntWrapper<46, false>;
845using UInt47 = detail::FixedIntWrapper<47, false>;
846using UInt48 = detail::FixedIntWrapper<48, false>;
847using UInt49 = detail::FixedIntWrapper<49, false>;
848using UInt50 = detail::FixedIntWrapper<50, false>;
849using UInt51 = detail::FixedIntWrapper<51, false>;
850using UInt52 = detail::FixedIntWrapper<52, false>;
851using UInt53 = detail::FixedIntWrapper<53, false>;
852using UInt54 = detail::FixedIntWrapper<54, false>;
853using UInt55 = detail::FixedIntWrapper<55, false>;
854using UInt56 = detail::FixedIntWrapper<56, false>;
855using UInt57 = detail::FixedIntWrapper<57, false>;
856using UInt58 = detail::FixedIntWrapper<58, false>;
857using UInt59 = detail::FixedIntWrapper<59, false>;
858using UInt60 = detail::FixedIntWrapper<60, false>;
859using UInt61 = detail::FixedIntWrapper<61, false>;
860using UInt62 = detail::FixedIntWrapper<62, false>;
861using UInt63 = detail::FixedIntWrapper<63, false>;
862using UInt64 = detail::FixedIntWrapper<64, false>;
863
864template <size_t BIT_SIZE>
865using Int = detail::FixedIntWrapper<BIT_SIZE, true>;
866
867template <size_t BIT_SIZE>
868using UInt = detail::FixedIntWrapper<BIT_SIZE, false>;
869
870using DynInt8 = detail::DynIntWrapper<int8_t>;
871using DynInt16 = detail::DynIntWrapper<int16_t>;
872using DynInt32 = detail::DynIntWrapper<int32_t>;
873using DynInt64 = detail::DynIntWrapper<int64_t>;
874
875using DynUInt8 = detail::DynIntWrapper<uint8_t>;
876using DynUInt16 = detail::DynIntWrapper<uint16_t>;
877using DynUInt32 = detail::DynIntWrapper<uint32_t>;
878using DynUInt64 = detail::DynIntWrapper<uint64_t>;
879
880using VarInt16 = detail::VarIntWrapper<int16_t, detail::VarIntType::VAR16>;
881using VarInt32 = detail::VarIntWrapper<int32_t, detail::VarIntType::VAR32>;
882using VarInt64 = detail::VarIntWrapper<int64_t, detail::VarIntType::VAR64>;
883using VarInt = detail::VarIntWrapper<int64_t, detail::VarIntType::VAR>;
884
885using VarUInt16 = detail::VarIntWrapper<uint16_t, detail::VarIntType::VAR16>;
886using VarUInt32 = detail::VarIntWrapper<uint32_t, detail::VarIntType::VAR32>;
887using VarUInt64 = detail::VarIntWrapper<uint64_t, detail::VarIntType::VAR64>;
888using VarUInt = detail::VarIntWrapper<uint64_t, detail::VarIntType::VAR>;
889
890using VarSize = detail::VarIntWrapper<uint32_t, detail::VarIntType::VARSIZE>;
891
892using Float16 = detail::FloatWrapper<float, detail::FloatType::FLOAT16>;
893using Float32 = detail::FloatWrapper<float, detail::FloatType::FLOAT32>;
894using Float64 = detail::FloatWrapper<double, detail::FloatType::FLOAT64>;
895
906template <typename VALUE_TYPE>
907CppRuntimeException& operator<<(CppRuntimeException& exception, detail::NumericTypeWrapper<VALUE_TYPE> value)
908{
910}
911
912namespace detail
913{
914
915template <typename T, std::enable_if_t<is_numeric_wrapper_v<T>, int> = 0>
916void validate(T wrapper, std::string_view fieldName) noexcept(!detail::needs_range_check_v<T>)
917{
918 if constexpr (detail::needs_range_check_v<T>)
919 {
920 detail::RangeChecker<T>::check(wrapper, fieldName);
921 }
922}
923
924template <typename T, std::enable_if_t<is_numeric_wrapper_v<T>, int> = 0>
925void validate(T wrapper, BitSize numBits, std::string_view fieldName)
926{
927 detail::RangeChecker<T>::check(wrapper, numBits, fieldName);
928}
929
930inline BitSize bitSizeOf(BoolWrapper, BitSize = 0)
931{
932 return 1;
933}
934
935template <BitSize BIT_SIZE, bool IS_SIGNED>
936BitSize bitSizeOf(FixedIntWrapper<BIT_SIZE, IS_SIGNED>, BitSize = 0)
937{
938 static_assert(BIT_SIZE != 0, "Variable dynamic bit fields not allowed here!");
939 return BIT_SIZE;
940}
941
942BitSize bitSizeOf(VarInt16 value, BitSize = 0);
943
944BitSize bitSizeOf(VarInt32 value, BitSize = 0);
945
946BitSize bitSizeOf(VarInt64 value, BitSize = 0);
947
948BitSize bitSizeOf(VarUInt16 value, BitSize = 0);
949
950BitSize bitSizeOf(VarUInt32 value, BitSize = 0);
951
952BitSize bitSizeOf(VarUInt64 value, BitSize = 0);
953
954BitSize bitSizeOf(VarInt value, BitSize = 0);
955
956BitSize bitSizeOf(VarUInt value, BitSize = 0);
957
958BitSize bitSizeOf(VarSize value, BitSize = 0);
959
960inline BitSize bitSizeOf(Float16, BitSize = 0)
961{
962 return 16;
963}
964
965inline BitSize bitSizeOf(Float32, BitSize = 0)
966{
967 return 32;
968}
969
970inline BitSize bitSizeOf(Float64, BitSize = 0)
971{
972 return 64;
973}
974
975inline BitSize initializeOffsets(BoolWrapper value, BitSize bitPosition)
976{
977 return bitSizeOf(value, bitPosition);
978}
979
980template <BitSize BIT_SIZE, bool IS_SIGNED>
981BitSize initializeOffsets(FixedIntWrapper<BIT_SIZE, IS_SIGNED> value, BitSize bitPosition)
982{
983 return bitSizeOf(value, bitPosition);
984}
985
986BitSize initializeOffsets(VarInt16 value, BitSize = 0);
987
988BitSize initializeOffsets(VarInt32 value, BitSize = 0);
989
990BitSize initializeOffsets(VarInt64 value, BitSize = 0);
991
992BitSize initializeOffsets(VarUInt16 value, BitSize = 0);
993
994BitSize initializeOffsets(VarUInt32 value, BitSize = 0);
995
996BitSize initializeOffsets(VarUInt64 value, BitSize = 0);
997
998BitSize initializeOffsets(VarInt value, BitSize = 0);
999
1000BitSize initializeOffsets(VarUInt value, BitSize = 0);
1001
1002BitSize initializeOffsets(VarSize value, BitSize = 0);
1003
1004inline BitSize initializeOffsets(Float16 value, BitSize bitPosition)
1005{
1006 return bitSizeOf(value, bitPosition);
1007}
1008
1009inline BitSize initializeOffsets(Float32 value, BitSize bitPosition)
1010{
1011 return bitSizeOf(value, bitPosition);
1012}
1013
1014inline BitSize initializeOffsets(Float64 value, BitSize bitPosition)
1015{
1016 return bitSizeOf(value, bitPosition);
1017}
1018
1019} // namespace detail
1020
1021} // namespace zserio
1022
1023#endif // ifndef ZSERIO_TYPES_H_INC
uint8_t numBits(uint64_t numValues)
detail::FixedIntWrapper< 39, false > UInt39
Definition Types.h:837
detail::FixedIntWrapper< 14, true > Int14
Definition Types.h:747
detail::FixedIntWrapper< 6, false > UInt6
Definition Types.h:804
detail::DynIntWrapper< uint16_t > DynUInt16
Definition Types.h:876
detail::FixedIntWrapper< 19, true > Int19
Definition Types.h:752
detail::FixedIntWrapper< 44, false > UInt44
Definition Types.h:842
detail::FixedIntWrapper< 31, false > UInt31
Definition Types.h:829
detail::FixedIntWrapper< 23, false > UInt23
Definition Types.h:821
detail::FixedIntWrapper< 28, false > UInt28
Definition Types.h:826
bool operator>(const BasicBitBufferView< ALLOC > &lhs, const BasicBitBufferView< ALLOC > &rhs)
Definition BitBuffer.h:526
detail::FixedIntWrapper< 45, false > UInt45
Definition Types.h:843
detail::FixedIntWrapper< 33, false > UInt33
Definition Types.h:831
detail::FixedIntWrapper< 60, true > Int60
Definition Types.h:793
detail::FixedIntWrapper< 2, false > UInt2
Definition Types.h:800
detail::FixedIntWrapper< 35, true > Int35
Definition Types.h:768
constexpr T fromCheckedValue(typename T::ValueType value) noexcept(!detail::needs_range_check_v< T >)
Definition Types.h:659
detail::FixedIntWrapper< 64, false > UInt64
Definition Types.h:862
detail::FixedIntWrapper< 48, false > UInt48
Definition Types.h:846
detail::DynIntWrapper< uint32_t > DynUInt32
Definition Types.h:877
detail::FixedIntWrapper< 16, true > Int16
Definition Types.h:749
detail::FixedIntWrapper< 61, false > UInt61
Definition Types.h:859
detail::FixedIntWrapper< 4, true > Int4
Definition Types.h:737
detail::FixedIntWrapper< 59, false > UInt59
Definition Types.h:857
detail::FixedIntWrapper< 49, true > Int49
Definition Types.h:782
detail::FixedIntWrapper< 9, true > Int9
Definition Types.h:742
detail::FixedIntWrapper< 2, true > Int2
Definition Types.h:735
detail::FixedIntWrapper< 37, true > Int37
Definition Types.h:770
detail::FixedIntWrapper< 10, true > Int10
Definition Types.h:743
detail::FixedIntWrapper< BIT_SIZE, true > Int
Definition Types.h:865
detail::BoolWrapper Bool
Definition Types.h:732
detail::FixedIntWrapper< 18, true > Int18
Definition Types.h:751
detail::FixedIntWrapper< 15, false > UInt15
Definition Types.h:813
detail::FixedIntWrapper< 56, false > UInt56
Definition Types.h:854
detail::FixedIntWrapper< 20, true > Int20
Definition Types.h:753
detail::VarIntWrapper< uint64_t, detail::VarIntType::VAR > VarUInt
Definition Types.h:888
unsigned int BitSize
Definition BitSize.h:8
detail::FixedIntWrapper< 7, true > Int7
Definition Types.h:740
detail::FixedIntWrapper< 16, false > UInt16
Definition Types.h:814
detail::FixedIntWrapper< 56, true > Int56
Definition Types.h:789
detail::FixedIntWrapper< 11, false > UInt11
Definition Types.h:809
detail::FixedIntWrapper< 8, false > UInt8
Definition Types.h:806
detail::FixedIntWrapper< 53, true > Int53
Definition Types.h:786
detail::FixedIntWrapper< 63, true > Int63
Definition Types.h:796
detail::FixedIntWrapper< 26, true > Int26
Definition Types.h:759
detail::FixedIntWrapper< 14, false > UInt14
Definition Types.h:812
CppRuntimeException & operator<<(CppRuntimeException &exception, const BasicBitBuffer< ALLOC > &bitBuffer)
Definition BitBuffer.h:554
detail::FixedIntWrapper< 25, true > Int25
Definition Types.h:758
detail::FixedIntWrapper< 42, false > UInt42
Definition Types.h:840
detail::FixedIntWrapper< 32, true > Int32
Definition Types.h:765
detail::FixedIntWrapper< 5, false > UInt5
Definition Types.h:803
detail::FixedIntWrapper< 17, false > UInt17
Definition Types.h:815
detail::FixedIntWrapper< 6, true > Int6
Definition Types.h:739
detail::FloatWrapper< float, detail::FloatType::FLOAT16 > Float16
Definition Types.h:892
detail::FixedIntWrapper< 20, false > UInt20
Definition Types.h:818
detail::FloatWrapper< float, detail::FloatType::FLOAT32 > Float32
Definition Types.h:893
detail::FixedIntWrapper< 49, false > UInt49
Definition Types.h:847
detail::FixedIntWrapper< 51, true > Int51
Definition Types.h:784
detail::FixedIntWrapper< 28, true > Int28
Definition Types.h:761
detail::VarIntWrapper< int32_t, detail::VarIntType::VAR32 > VarInt32
Definition Types.h:881
detail::FixedIntWrapper< 40, false > UInt40
Definition Types.h:838
detail::FixedIntWrapper< 40, true > Int40
Definition Types.h:773
detail::FixedIntWrapper< 30, true > Int30
Definition Types.h:763
detail::FixedIntWrapper< 60, false > UInt60
Definition Types.h:858
detail::FixedIntWrapper< 42, true > Int42
Definition Types.h:775
detail::FixedIntWrapper< 7, false > UInt7
Definition Types.h:805
detail::DynIntWrapper< int8_t > DynInt8
Definition Types.h:870
detail::FixedIntWrapper< 3, true > Int3
Definition Types.h:736
detail::FixedIntWrapper< 38, false > UInt38
Definition Types.h:836
detail::DynIntWrapper< uint64_t > DynUInt64
Definition Types.h:878
detail::FixedIntWrapper< 43, false > UInt43
Definition Types.h:841
detail::VarIntWrapper< uint16_t, detail::VarIntType::VAR16 > VarUInt16
Definition Types.h:885
bool operator<(const BasicBitBufferView< ALLOC > &lhs, const BasicBitBufferView< ALLOC > &rhs)
Definition BitBuffer.h:520
detail::FixedIntWrapper< 36, false > UInt36
Definition Types.h:834
detail::FixedIntWrapper< 58, true > Int58
Definition Types.h:791
detail::FixedIntWrapper< 22, true > Int22
Definition Types.h:755
detail::FixedIntWrapper< 25, false > UInt25
Definition Types.h:823
detail::FixedIntWrapper< 29, false > UInt29
Definition Types.h:827
detail::FixedIntWrapper< 1, false > UInt1
Definition Types.h:799
detail::DynIntWrapper< int64_t > DynInt64
Definition Types.h:873
detail::FixedIntWrapper< 55, false > UInt55
Definition Types.h:853
detail::FixedIntWrapper< 45, true > Int45
Definition Types.h:778
detail::FixedIntWrapper< 11, true > Int11
Definition Types.h:744
detail::FixedIntWrapper< 39, true > Int39
Definition Types.h:772
detail::FixedIntWrapper< 58, false > UInt58
Definition Types.h:856
detail::FixedIntWrapper< 43, true > Int43
Definition Types.h:776
detail::FixedIntWrapper< 24, true > Int24
Definition Types.h:757
detail::FixedIntWrapper< 3, false > UInt3
Definition Types.h:801
detail::VarIntWrapper< uint64_t, detail::VarIntType::VAR64 > VarUInt64
Definition Types.h:887
detail::FixedIntWrapper< 18, false > UInt18
Definition Types.h:816
detail::FixedIntWrapper< 22, false > UInt22
Definition Types.h:820
detail::FixedIntWrapper< 4, false > UInt4
Definition Types.h:802
detail::FixedIntWrapper< 34, true > Int34
Definition Types.h:767
detail::FixedIntWrapper< 54, false > UInt54
Definition Types.h:852
detail::FixedIntWrapper< 32, false > UInt32
Definition Types.h:830
detail::FixedIntWrapper< 41, true > Int41
Definition Types.h:774
detail::FixedIntWrapper< 37, false > UInt37
Definition Types.h:835
detail::FixedIntWrapper< 27, true > Int27
Definition Types.h:760
detail::FixedIntWrapper< 13, true > Int13
Definition Types.h:746
detail::FixedIntWrapper< 57, true > Int57
Definition Types.h:790
detail::FixedIntWrapper< 53, false > UInt53
Definition Types.h:851
constexpr T::ValueType toCheckedValue(T wrapper) noexcept(!detail::needs_range_check_v< T >)
Definition Types.h:699
detail::FixedIntWrapper< 62, false > UInt62
Definition Types.h:860
detail::FixedIntWrapper< 15, true > Int15
Definition Types.h:748
detail::FixedIntWrapper< 46, false > UInt46
Definition Types.h:844
detail::FixedIntWrapper< 12, true > Int12
Definition Types.h:745
detail::FixedIntWrapper< 10, false > UInt10
Definition Types.h:808
detail::FixedIntWrapper< 52, false > UInt52
Definition Types.h:850
detail::FixedIntWrapper< 8, true > Int8
Definition Types.h:741
detail::FixedIntWrapper< 64, true > Int64
Definition Types.h:797
detail::FixedIntWrapper< 48, true > Int48
Definition Types.h:781
detail::FixedIntWrapper< 41, false > UInt41
Definition Types.h:839
detail::FixedIntWrapper< 30, false > UInt30
Definition Types.h:828
detail::FixedIntWrapper< 26, false > UInt26
Definition Types.h:824
detail::FixedIntWrapper< 36, true > Int36
Definition Types.h:769
detail::FixedIntWrapper< 19, false > UInt19
Definition Types.h:817
detail::FixedIntWrapper< 35, false > UInt35
Definition Types.h:833
detail::FixedIntWrapper< BIT_SIZE, false > UInt
Definition Types.h:868
detail::FixedIntWrapper< 50, true > Int50
Definition Types.h:783
detail::VarIntWrapper< int16_t, detail::VarIntType::VAR16 > VarInt16
Definition Types.h:880
detail::FixedIntWrapper< 13, false > UInt13
Definition Types.h:811
bool operator<=(const BasicBitBufferView< ALLOC > &lhs, const BasicBitBufferView< ALLOC > &rhs)
Definition BitBuffer.h:532
detail::FixedIntWrapper< 46, true > Int46
Definition Types.h:779
detail::FixedIntWrapper< 29, true > Int29
Definition Types.h:762
detail::FixedIntWrapper< 55, true > Int55
Definition Types.h:788
detail::FixedIntWrapper< 33, true > Int33
Definition Types.h:766
detail::FixedIntWrapper< 61, true > Int61
Definition Types.h:794
detail::FixedIntWrapper< 57, false > UInt57
Definition Types.h:855
detail::DynIntWrapper< int16_t > DynInt16
Definition Types.h:871
detail::FloatWrapper< double, detail::FloatType::FLOAT64 > Float64
Definition Types.h:894
detail::FixedIntWrapper< 38, true > Int38
Definition Types.h:771
detail::FixedIntWrapper< 17, true > Int17
Definition Types.h:750
detail::FixedIntWrapper< 24, false > UInt24
Definition Types.h:822
detail::FixedIntWrapper< 47, true > Int47
Definition Types.h:780
detail::VarIntWrapper< uint32_t, detail::VarIntType::VAR32 > VarUInt32
Definition Types.h:886
detail::FixedIntWrapper< 21, true > Int21
Definition Types.h:754
detail::FixedIntWrapper< 50, false > UInt50
Definition Types.h:848
detail::FixedIntWrapper< 62, true > Int62
Definition Types.h:795
detail::FixedIntWrapper< 34, false > UInt34
Definition Types.h:832
detail::FixedIntWrapper< 54, true > Int54
Definition Types.h:787
detail::DynIntWrapper< int32_t > DynInt32
Definition Types.h:872
detail::FixedIntWrapper< 23, true > Int23
Definition Types.h:756
bool operator>=(const BasicBitBufferView< ALLOC > &lhs, const BasicBitBufferView< ALLOC > &rhs)
Definition BitBuffer.h:538
detail::FixedIntWrapper< 21, false > UInt21
Definition Types.h:819
detail::FixedIntWrapper< 44, true > Int44
Definition Types.h:777
detail::FixedIntWrapper< 1, true > Int1
Definition Types.h:734
detail::FixedIntWrapper< 51, false > UInt51
Definition Types.h:849
detail::DynIntWrapper< uint8_t > DynUInt8
Definition Types.h:875
detail::FixedIntWrapper< 12, false > UInt12
Definition Types.h:810
detail::FixedIntWrapper< 9, false > UInt9
Definition Types.h:807
detail::FixedIntWrapper< 47, false > UInt47
Definition Types.h:845
detail::FixedIntWrapper< 59, true > Int59
Definition Types.h:792
detail::VarIntWrapper< int64_t, detail::VarIntType::VAR > VarInt
Definition Types.h:883
detail::FixedIntWrapper< 63, false > UInt63
Definition Types.h:861
detail::VarIntWrapper< int64_t, detail::VarIntType::VAR64 > VarInt64
Definition Types.h:882
detail::VarIntWrapper< uint32_t, detail::VarIntType::VARSIZE > VarSize
Definition Types.h:890
detail::FixedIntWrapper< 31, true > Int31
Definition Types.h:764
detail::FixedIntWrapper< 27, false > UInt27
Definition Types.h:825
detail::FixedIntWrapper< 52, true > Int52
Definition Types.h:785
detail::FixedIntWrapper< 5, true > Int5
Definition Types.h:738