Zserio C++17 runtime library 1.3.0
Built for Zserio 2.20.0
Loading...
Searching...
No Matches
IntrospectableView.h
Go to the documentation of this file.
1#ifndef ZSERIO_INTROSPECTABLE_VIEW_H_INC
2#define ZSERIO_INTROSPECTABLE_VIEW_H_INC
3
4#include "zserio/ArrayView.h"
9
10namespace zserio
11{
12namespace detail
13{
14
20template <typename T, typename ALLOC>
21class IntrospectableViewBase : public IntrospectableDataBase<IBasicIntrospectableView<ALLOC>, ALLOC>
22{
23private:
24 using Base = IntrospectableDataBase<IBasicIntrospectableView<ALLOC>, ALLOC>;
25
26public:
28 using ConstPtr = typename IBasicIntrospectableView<ALLOC>::ConstPtr;
29
36 IntrospectableViewBase(const IBasicTypeInfo<ALLOC>& typeInfo, T value) :
37 Base(typeInfo),
38 m_value(value)
39 {}
40
46 T getValue() const
47 {
48 return m_value;
49 }
50
52 ~IntrospectableViewBase() override = default;
53
58 IntrospectableViewBase(const IntrospectableViewBase&) = delete;
59 IntrospectableViewBase& operator=(const IntrospectableViewBase&) = delete;
60
61 IntrospectableViewBase(const IntrospectableViewBase&&) = delete;
62 IntrospectableViewBase& operator=(const IntrospectableViewBase&&) = delete;
67 BasicAny<ALLOC> getAnyValue(const ALLOC& allocator) const override
68 {
69 return BasicAny<ALLOC>(m_value, allocator);
70 }
71
72 ConstPtr find(std::string_view path) const override
73 {
74 return getFromObject(*this, path, 0);
75 }
76
77 ConstPtr getParameter(std::string_view) const override
78 {
79 throw CppRuntimeException("Type '")
81 << "' has no parameters to get!";
82 }
83
84 ConstPtr callFunction(std::string_view) const override
85 {
86 throw CppRuntimeException("Type '")
88 << "' has no functions to call!";
89 }
90
91 BasicBitBuffer<ALLOC> serialize(const ALLOC&) const override
92 {
93 throw CppRuntimeException("Type '")
95 << "' is not a compound type!";
96 }
97
98 BasicBitBuffer<ALLOC> serialize() const override
99 {
100 return serialize(ALLOC());
101 }
102
103private:
104 ConstPtr getFieldFromObject(const IBasicIntrospectableView<ALLOC>& object, std::string_view name) const;
105 ConstPtr getParameterFromObject(const IBasicIntrospectableView<ALLOC>& object, std::string_view name) const;
106 ConstPtr callFunctionInObject(const IBasicIntrospectableView<ALLOC>& object, std::string_view name) const;
107 ConstPtr getFromObject(
108 const IBasicIntrospectableView<ALLOC>& object, std::string_view path, size_t pos) const;
109
110 T m_value;
111};
112
118template <typename T, typename ALLOC>
119class IntegralIntrospectableViewBase : public IntrospectableViewBase<T, ALLOC>
120{
121protected:
122 static_assert(std::is_integral_v<typename T::ValueType>, "T must be a signed integral type!");
123
124 using Base = IntrospectableViewBase<T, ALLOC>;
125
126public:
127 IntegralIntrospectableViewBase(const IBasicTypeInfo<ALLOC>& typeInfo, T value) :
128 Base(typeInfo, value)
129 {}
130
131 double toDouble() const override
132 {
133 return static_cast<double>(Base::getValue());
134 }
135
136 BasicString<RebindAlloc<ALLOC, char>> toString(const ALLOC& allocator) const override
137 {
138 return zserio::toString<ALLOC>(Base::getValue(), allocator);
139 }
140};
141
147template <typename T, typename ALLOC>
148class SignedIntrospectableViewBase : public IntegralIntrospectableViewBase<T, ALLOC>
149{
150protected:
151 static_assert(std::is_signed_v<typename T::ValueType>, "T must be a signed integral type!");
152
153 using Base = IntegralIntrospectableViewBase<T, ALLOC>;
154
155 using Base::Base;
156
157public:
158 int64_t toInt() const override
159 {
160 return Base::getValue();
161 }
162};
163
169template <typename T, typename ALLOC>
170class UnsignedIntrospectableViewBase : public IntegralIntrospectableViewBase<T, ALLOC>
171{
172protected:
173 static_assert(std::is_unsigned<typename T::ValueType>::value, "T must be an unsigned integral type!");
174
175 using Base = IntegralIntrospectableViewBase<T, ALLOC>;
176
177 using Base::Base;
178
179public:
180 uint64_t toUInt() const override
181 {
182 return Base::getValue();
183 }
184};
185
189template <typename ALLOC>
190class BoolIntrospectableView : public UnsignedIntrospectableViewBase<Bool, ALLOC>
191{
192private:
193 using Base = UnsignedIntrospectableViewBase<Bool, ALLOC>;
194
195public:
196 explicit BoolIntrospectableView(Bool value) :
197 Base(typeInfo<zserio::Bool, ALLOC>(), value)
198 {}
199
200 bool getBool() const override
201 {
202 return Base::getValue();
203 }
204};
205
209template <typename T, typename ALLOC>
210class Int8IntrospectableView : public SignedIntrospectableViewBase<T, ALLOC>
211{
212private:
213 using Base = SignedIntrospectableViewBase<T, ALLOC>;
214
215 static_assert(std::is_same_v<int8_t, typename T::ValueType>, "T must be based on int8_t!");
216
217public:
218 explicit Int8IntrospectableView(T value) :
219 Base(typeInfo<T, ALLOC>(), value)
220 {}
221
222 int8_t getInt8() const override
223 {
224 return Base::getValue();
225 }
226};
227
231template <typename T, typename ALLOC>
232class Int16IntrospectableView : public SignedIntrospectableViewBase<T, ALLOC>
233{
234private:
235 using Base = SignedIntrospectableViewBase<T, ALLOC>;
236
237 static_assert(std::is_same_v<int16_t, typename T::ValueType>, "T must be based on int16_t!");
238
239public:
240 explicit Int16IntrospectableView(T value) :
241 Base(typeInfo<T, ALLOC>(), value)
242 {}
243
244 int16_t getInt16() const override
245 {
246 return Base::getValue();
247 }
248};
249
253template <typename T, typename ALLOC>
254class Int32IntrospectableView : public SignedIntrospectableViewBase<T, ALLOC>
255{
256private:
257 using Base = SignedIntrospectableViewBase<T, ALLOC>;
258
259 static_assert(std::is_same_v<int32_t, typename T::ValueType>, "T must be based on int32_t!");
260
261public:
262 explicit Int32IntrospectableView(T value) :
263 Base(typeInfo<T, ALLOC>(), value)
264 {}
265
266 int32_t getInt32() const override
267 {
268 return Base::getValue();
269 }
270};
271
275template <typename T, typename ALLOC>
276class Int64IntrospectableView : public SignedIntrospectableViewBase<T, ALLOC>
277{
278private:
279 using Base = SignedIntrospectableViewBase<T, ALLOC>;
280
281 static_assert(std::is_same_v<int64_t, typename T::ValueType>, "T must be based on int64_t!");
282
283public:
284 explicit Int64IntrospectableView(T value) :
285 Base(typeInfo<T, ALLOC>(), value)
286 {}
287
288 int64_t getInt64() const override
289 {
290 return Base::getValue();
291 }
292};
293
297template <typename T, typename ALLOC>
298class UInt8IntrospectableView : public UnsignedIntrospectableViewBase<T, ALLOC>
299{
300private:
301 using Base = UnsignedIntrospectableViewBase<T, ALLOC>;
302
303 static_assert(std::is_same_v<uint8_t, typename T::ValueType>, "T must be based on uint8_t!");
304
305public:
306 explicit UInt8IntrospectableView(T value) :
307 Base(typeInfo<T, ALLOC>(), value)
308 {}
309
310 uint8_t getUInt8() const override
311 {
312 return Base::getValue();
313 }
314};
315
319template <typename T, typename ALLOC>
320class UInt16IntrospectableView : public UnsignedIntrospectableViewBase<T, ALLOC>
321{
322private:
323 using Base = UnsignedIntrospectableViewBase<T, ALLOC>;
324
325 static_assert(std::is_same_v<uint16_t, typename T::ValueType>, "T must be based on uint16_t!");
326
327public:
328 explicit UInt16IntrospectableView(T value) :
329 Base(typeInfo<T, ALLOC>(), value)
330 {}
331
332 uint16_t getUInt16() const override
333 {
334 return Base::getValue();
335 }
336};
337
341template <typename T, typename ALLOC>
342class UInt32IntrospectableView : public UnsignedIntrospectableViewBase<T, ALLOC>
343{
344private:
345 using Base = UnsignedIntrospectableViewBase<T, ALLOC>;
346
347 static_assert(std::is_same_v<uint32_t, typename T::ValueType>, "T must be based on uint32_t!");
348
349public:
350 explicit UInt32IntrospectableView(T value) :
351 Base(typeInfo<T, ALLOC>(), value)
352 {}
353
354 uint32_t getUInt32() const override
355 {
356 return Base::getValue();
357 }
358};
359
363template <typename T, typename ALLOC>
364class UInt64IntrospectableView : public UnsignedIntrospectableViewBase<T, ALLOC>
365{
366private:
367 using Base = UnsignedIntrospectableViewBase<T, ALLOC>;
368
369 static_assert(std::is_same_v<uint64_t, typename T::ValueType>, "T must be based on uint64_t!");
370
371public:
372 explicit UInt64IntrospectableView(T value) :
373 Base(typeInfo<T, ALLOC>(), value)
374 {}
375
376 uint64_t getUInt64() const override
377 {
378 return Base::getValue();
379 }
380};
381
385template <typename T, typename ALLOC>
386class DynInt8IntrospectableView : public SignedIntrospectableViewBase<View<T>, ALLOC>
387{
388private:
389 using Base = SignedIntrospectableViewBase<View<T>, ALLOC>;
390
391 static_assert(std::is_same_v<int8_t, typename T::ValueType>, "T must be based on int8_t!");
392
393public:
394 explicit DynInt8IntrospectableView(View<T> value) :
395 Base(typeInfo<T, ALLOC>(), value)
396 {}
397
398 int8_t getInt8() const override
399 {
400 return Base::getValue();
401 }
402};
403
407template <typename T, typename ALLOC>
408class DynInt16IntrospectableView : public SignedIntrospectableViewBase<View<T>, ALLOC>
409{
410private:
411 using Base = SignedIntrospectableViewBase<View<T>, ALLOC>;
412
413 static_assert(std::is_same_v<int16_t, typename T::ValueType>, "T must be based on int16_t!");
414
415public:
416 explicit DynInt16IntrospectableView(View<T> value) :
417 Base(typeInfo<T, ALLOC>(), value)
418 {}
419
420 int16_t getInt16() const override
421 {
422 return Base::getValue();
423 }
424};
425
429template <typename T, typename ALLOC>
430class DynInt32IntrospectableView : public SignedIntrospectableViewBase<View<T>, ALLOC>
431{
432private:
433 using Base = SignedIntrospectableViewBase<View<T>, ALLOC>;
434
435 static_assert(std::is_same_v<int32_t, typename T::ValueType>, "T must be based on int32_t!");
436
437public:
438 explicit DynInt32IntrospectableView(View<T> value) :
439 Base(typeInfo<T, ALLOC>(), value)
440 {}
441
442 int32_t getInt32() const override
443 {
444 return Base::getValue();
445 }
446};
447
451template <typename T, typename ALLOC>
452class DynInt64IntrospectableView : public SignedIntrospectableViewBase<View<T>, ALLOC>
453{
454private:
455 using Base = SignedIntrospectableViewBase<View<T>, ALLOC>;
456
457 static_assert(std::is_same_v<int64_t, typename T::ValueType>, "T must be based on int64_t!");
458
459public:
460 explicit DynInt64IntrospectableView(View<T> value) :
461 Base(typeInfo<T, ALLOC>(), value)
462 {}
463
464 int64_t getInt64() const override
465 {
466 return Base::getValue();
467 }
468};
469
473template <typename T, typename ALLOC>
474class DynUInt8IntrospectableView : public UnsignedIntrospectableViewBase<View<T>, ALLOC>
475{
476private:
477 using Base = UnsignedIntrospectableViewBase<View<T>, ALLOC>;
478
479 static_assert(std::is_same_v<uint8_t, typename T::ValueType>, "T must be based on uint8_t!");
480
481public:
482 explicit DynUInt8IntrospectableView(View<T> value) :
483 Base(typeInfo<T, ALLOC>(), value)
484 {}
485
486 uint8_t getUInt8() const override
487 {
488 return Base::getValue();
489 }
490};
491
495template <typename T, typename ALLOC>
496class DynUInt16IntrospectableView : public UnsignedIntrospectableViewBase<View<T>, ALLOC>
497{
498private:
499 using Base = UnsignedIntrospectableViewBase<View<T>, ALLOC>;
500
501 static_assert(std::is_same_v<uint16_t, typename T::ValueType>, "T must be based on uint16_t!");
502
503public:
504 explicit DynUInt16IntrospectableView(View<T> value) :
505 Base(typeInfo<T, ALLOC>(), value)
506 {}
507
508 uint16_t getUInt16() const override
509 {
510 return Base::getValue();
511 }
512};
513
517template <typename T, typename ALLOC>
518class DynUInt32IntrospectableView : public UnsignedIntrospectableViewBase<View<T>, ALLOC>
519{
520private:
521 using Base = UnsignedIntrospectableViewBase<View<T>, ALLOC>;
522
523 static_assert(std::is_same_v<uint32_t, typename T::ValueType>, "T must be based on uint32_t!");
524
525public:
526 explicit DynUInt32IntrospectableView(View<T> value) :
527 Base(typeInfo<T, ALLOC>(), value)
528 {}
529
530 uint32_t getUInt32() const override
531 {
532 return Base::getValue();
533 }
534};
535
539template <typename T, typename ALLOC>
540class DynUInt64IntrospectableView : public UnsignedIntrospectableViewBase<View<T>, ALLOC>
541{
542private:
543 using Base = UnsignedIntrospectableViewBase<View<T>, ALLOC>;
544
545 static_assert(std::is_same_v<uint64_t, typename T::ValueType>, "T must be based on uint64_t!");
546
547public:
548 explicit DynUInt64IntrospectableView(View<T> value) :
549 Base(typeInfo<T, ALLOC>(), value)
550 {}
551
552 uint64_t getUInt64() const override
553 {
554 return Base::getValue();
555 }
556};
557
561template <typename T, typename ALLOC>
562class FloatingPointIntrospectableViewBase : public IntrospectableViewBase<T, ALLOC>
563{
564protected:
565 static_assert(std::is_floating_point_v<typename T::ValueType>, "T must be a floating point type!");
566
567 using Base = IntrospectableViewBase<T, ALLOC>;
568 using Base::Base;
569
570public:
571 double toDouble() const override
572 {
573 return static_cast<double>(Base::getValue());
574 }
575};
576
580template <typename T, typename ALLOC>
581class FloatIntrospectableView : public FloatingPointIntrospectableViewBase<T, ALLOC>
582{
583private:
584 using Base = FloatingPointIntrospectableViewBase<T, ALLOC>;
585
586public:
587 explicit FloatIntrospectableView(T value) :
588 Base(typeInfo<T, ALLOC>(), value)
589 {}
590
591 float getFloat() const override
592 {
593 return Base::getValue();
594 }
595};
596
600template <typename ALLOC>
601class DoubleIntrospectableView : public FloatingPointIntrospectableViewBase<Float64, ALLOC>
602{
603private:
604 using Base = FloatingPointIntrospectableViewBase<Float64, ALLOC>;
605
606public:
607 explicit DoubleIntrospectableView(Float64 value) :
608 Base(typeInfo<Float64, ALLOC>(), value)
609 {}
610
611 double getDouble() const override
612 {
613 return Base::getValue();
614 }
615};
616
620template <typename ALLOC>
621class BytesIntrospectableView : public IntrospectableViewBase<BytesView, ALLOC>
622{
623private:
624 using Base = IntrospectableViewBase<BytesView, ALLOC>;
625
626public:
627 explicit BytesIntrospectableView(BytesView value) :
628 Base(typeInfo<BasicBytes<ALLOC>>(), value)
629 {}
630
631 BytesView getBytes() const override
632 {
633 return Base::getValue();
634 }
635};
636
640template <typename ALLOC>
641class StringIntrospectableView : public IntrospectableViewBase<std::string_view, ALLOC>
642{
643private:
644 using Base = IntrospectableViewBase<std::string_view, ALLOC>;
645
646public:
647 explicit StringIntrospectableView(std::string_view value) :
648 Base(typeInfo<BasicString<RebindAlloc<ALLOC, char>>>(), value)
649 {}
650
651 std::string_view getStringView() const override
652 {
653 return Base::getValue();
654 }
655
656 BasicString<RebindAlloc<ALLOC, char>> toString(const ALLOC& allocator) const override
657 {
658 return zserio::toString(Base::getValue(), allocator);
659 }
660};
661
665template <typename ALLOC>
666class BitBufferIntrospectableView : public IntrospectableViewBase<BasicBitBufferView<ALLOC>, ALLOC>
667{
668private:
669 using Base = IntrospectableViewBase<BasicBitBufferView<ALLOC>, ALLOC>;
670
671public:
672 explicit BitBufferIntrospectableView(BasicBitBufferView<ALLOC> value) :
673 Base(typeInfo<BasicBitBuffer<ALLOC>>(), value)
674 {}
675
676 const BasicBitBuffer<ALLOC>& getBitBuffer() const override
677 {
678 return Base::getValue();
679 }
680};
681
682} // namespace detail
683
684template <typename ALLOC = std::allocator<uint8_t>>
686{
687 return std::allocate_shared<detail::BoolIntrospectableView<ALLOC>>(allocator, value);
688}
689
690template <BitSize BIT_SIZE, bool IS_SIGNED, typename ALLOC = std::allocator<uint8_t>>
692 detail::FixedIntWrapper<BIT_SIZE, IS_SIGNED> value, const ALLOC& allocator = ALLOC())
693{
694 using Type = detail::FixedIntWrapper<BIT_SIZE, IS_SIGNED>;
695 using ValueType = typename Type::ValueType;
696
697 if constexpr (IS_SIGNED)
698 {
699 if constexpr (sizeof(ValueType) > 4)
700 {
701 return std::allocate_shared<detail::Int64IntrospectableView<Type, ALLOC>>(allocator, value);
702 }
703 else if constexpr (sizeof(ValueType) > 2)
704 {
705 return std::allocate_shared<detail::Int32IntrospectableView<Type, ALLOC>>(allocator, value);
706 }
707 else if constexpr (sizeof(ValueType) > 1)
708 {
709 return std::allocate_shared<detail::Int16IntrospectableView<Type, ALLOC>>(allocator, value);
710 }
711 else
712 {
713 return std::allocate_shared<detail::Int8IntrospectableView<Type, ALLOC>>(allocator, value);
714 }
715 }
716 else
717 {
718 if constexpr (sizeof(ValueType) > 4)
719 {
720 return std::allocate_shared<detail::UInt64IntrospectableView<Type, ALLOC>>(allocator, value);
721 }
722 else if constexpr (sizeof(ValueType) > 2)
723 {
724 return std::allocate_shared<detail::UInt32IntrospectableView<Type, ALLOC>>(allocator, value);
725 }
726 else if constexpr (sizeof(ValueType) > 1)
727 {
728 return std::allocate_shared<detail::UInt16IntrospectableView<Type, ALLOC>>(allocator, value);
729 }
730 else
731 {
732 return std::allocate_shared<detail::UInt8IntrospectableView<Type, ALLOC>>(allocator, value);
733 }
734 }
735}
736
737template <typename T, typename ALLOC = std::allocator<uint8_t>>
739 const View<detail::DynIntWrapper<T>>& value, const ALLOC& allocator = ALLOC())
740{
741 using Type = detail::DynIntWrapper<T>;
742
743 if constexpr (std::is_signed_v<T>)
744 {
745 if constexpr (sizeof(T) > 4)
746 {
747 return std::allocate_shared<detail::DynInt64IntrospectableView<Type, ALLOC>>(allocator, value);
748 }
749 else if constexpr (sizeof(T) > 2)
750 {
751 return std::allocate_shared<detail::DynInt32IntrospectableView<Type, ALLOC>>(allocator, value);
752 }
753 else if constexpr (sizeof(T) > 1)
754 {
755 return std::allocate_shared<detail::DynInt16IntrospectableView<Type, ALLOC>>(allocator, value);
756 }
757 else
758 {
759 return std::allocate_shared<detail::DynInt8IntrospectableView<Type, ALLOC>>(allocator, value);
760 }
761 }
762 else
763 {
764 if constexpr (sizeof(T) > 4)
765 {
766 return std::allocate_shared<detail::DynUInt64IntrospectableView<Type, ALLOC>>(allocator, value);
767 }
768 else if constexpr (sizeof(T) > 2)
769 {
770 return std::allocate_shared<detail::DynUInt32IntrospectableView<Type, ALLOC>>(allocator, value);
771 }
772 else if constexpr (sizeof(T) > 1)
773 {
774 return std::allocate_shared<detail::DynUInt16IntrospectableView<Type, ALLOC>>(allocator, value);
775 }
776 else
777 {
778 return std::allocate_shared<detail::DynUInt8IntrospectableView<Type, ALLOC>>(allocator, value);
779 }
780 }
781}
782
783template <typename ALLOC = std::allocator<uint8_t>>
785{
786 return std::allocate_shared<detail::Int16IntrospectableView<VarInt16, ALLOC>>(allocator, value);
787}
788
789template <typename ALLOC = std::allocator<uint8_t>>
790IBasicIntrospectableViewConstPtr<ALLOC> introspectable(VarInt32 value, const ALLOC& allocator = ALLOC())
791{
792 return std::allocate_shared<detail::Int32IntrospectableView<VarInt32, ALLOC>>(allocator, value);
793}
794
795template <typename ALLOC = std::allocator<uint8_t>>
796IBasicIntrospectableViewConstPtr<ALLOC> introspectable(VarInt64 value, const ALLOC& allocator = ALLOC())
797{
798 return std::allocate_shared<detail::Int64IntrospectableView<VarInt64, ALLOC>>(allocator, value);
799}
800
801template <typename ALLOC = std::allocator<uint8_t>>
802IBasicIntrospectableViewConstPtr<ALLOC> introspectable(VarInt value, const ALLOC& allocator = ALLOC())
803{
804 return std::allocate_shared<detail::Int64IntrospectableView<VarInt, ALLOC>>(allocator, value);
805}
806
807template <typename ALLOC = std::allocator<uint8_t>>
808IBasicIntrospectableViewConstPtr<ALLOC> introspectable(VarUInt16 value, const ALLOC& allocator = ALLOC())
809{
810 return std::allocate_shared<detail::UInt16IntrospectableView<VarUInt16, ALLOC>>(allocator, value);
811}
812
813template <typename ALLOC = std::allocator<uint8_t>>
814IBasicIntrospectableViewConstPtr<ALLOC> introspectable(VarUInt32 value, const ALLOC& allocator = ALLOC())
815{
816 return std::allocate_shared<detail::UInt32IntrospectableView<VarUInt32, ALLOC>>(allocator, value);
817}
818
819template <typename ALLOC = std::allocator<uint8_t>>
820IBasicIntrospectableViewConstPtr<ALLOC> introspectable(VarUInt64 value, const ALLOC& allocator = ALLOC())
821{
822 return std::allocate_shared<detail::UInt64IntrospectableView<VarUInt64, ALLOC>>(allocator, value);
823}
824
825template <typename ALLOC = std::allocator<uint8_t>>
826IBasicIntrospectableViewConstPtr<ALLOC> introspectable(VarUInt value, const ALLOC& allocator = ALLOC())
827{
828 return std::allocate_shared<detail::UInt64IntrospectableView<VarUInt, ALLOC>>(allocator, value);
829}
830
831template <typename ALLOC = std::allocator<uint8_t>>
832IBasicIntrospectableViewConstPtr<ALLOC> introspectable(VarSize value, const ALLOC& allocator = ALLOC())
833{
834 return std::allocate_shared<detail::UInt32IntrospectableView<VarSize, ALLOC>>(allocator, value);
835}
836
837template <typename ALLOC = std::allocator<uint8_t>>
839{
840 return std::allocate_shared<detail::FloatIntrospectableView<Float16, ALLOC>>(allocator, value);
841}
842
843template <typename ALLOC = std::allocator<uint8_t>>
844IBasicIntrospectableViewConstPtr<ALLOC> introspectable(Float32 value, const ALLOC& allocator = ALLOC())
845{
846 return std::allocate_shared<detail::FloatIntrospectableView<Float32, ALLOC>>(allocator, value);
847}
848
849template <typename ALLOC = std::allocator<uint8_t>>
850IBasicIntrospectableViewConstPtr<ALLOC> introspectable(Float64 value, const ALLOC& allocator = ALLOC())
851{
852 return std::allocate_shared<detail::DoubleIntrospectableView<ALLOC>>(allocator, value);
853}
854
855template <typename ALLOC = std::allocator<uint8_t>>
857{
858 return std::allocate_shared<detail::BytesIntrospectableView<ALLOC>>(allocator, value);
859}
860
861template <typename ALLOC = std::allocator<uint8_t>>
863{
864 return std::allocate_shared<detail::StringIntrospectableView<ALLOC>>(allocator, value);
865}
866
867template <typename ALLOC = std::allocator<uint8_t>>
870{
871 return std::allocate_shared<detail::BitBufferIntrospectableView<ALLOC>>(allocator, value);
872}
873
874namespace detail
875{
876
880template <typename ARRAY_VIEW, typename ALLOC>
881class IntrospectableViewArray : public IntrospectableViewBase<ARRAY_VIEW, ALLOC>, public AllocatorHolder<ALLOC>
882{
883private:
884 using Base = IntrospectableViewBase<ARRAY_VIEW, ALLOC>;
885
886public:
887 using Base::getTypeInfo;
889
890 explicit IntrospectableViewArray(const ARRAY_VIEW& value, const ALLOC& allocator = {}) :
891 Base(typeInfo<typename ARRAY_VIEW::ValueType, ALLOC>(), value),
892 AllocatorHolder<ALLOC>(allocator)
893 {}
894
895 bool isArray() const override
896 {
897 return true;
898 }
899
900 IBasicIntrospectableViewConstPtr<ALLOC> getField(std::string_view) const override
901 {
902 throw CppRuntimeException("Introspectable is an array '") << getTypeInfo().getSchemaName() << "[]'!";
903 }
904
905 std::string_view getChoice() const override
906 {
907 throw CppRuntimeException("Introspectable is an array '") << getTypeInfo().getSchemaName() << "[]'!";
908 }
909
910 size_t size() const override
911 {
912 return Base::getValue().size();
913 }
914
915 IBasicIntrospectableViewConstPtr<ALLOC> at(size_t index) const override
916 {
917 return introspectable(Base::getValue().at(index), get_allocator());
918 }
919
920 IBasicIntrospectableViewConstPtr<ALLOC> operator[](size_t index) const override
921 {
922 return this->at(index);
923 }
924
925 BasicAny<ALLOC> getAnyValue(const ALLOC& allocator) const override
926 {
927 return BasicAny<ALLOC>(Base::getValue(), allocator);
928 }
929
930 bool getBool() const override
931 {
932 throw CppRuntimeException("Introspectable is an array '") << getTypeInfo().getSchemaName() << "[]'!";
933 }
934
935 int8_t getInt8() const override
936 {
937 throw CppRuntimeException("Introspectable is an array '") << getTypeInfo().getSchemaName() << "[]'!";
938 }
939
940 int16_t getInt16() const override
941 {
942 throw CppRuntimeException("Introspectable is an array '") << getTypeInfo().getSchemaName() << "[]'!";
943 }
944
945 int32_t getInt32() const override
946 {
947 throw CppRuntimeException("Introspectable is an array '") << getTypeInfo().getSchemaName() << "[]'!";
948 }
949
950 int64_t getInt64() const override
951 {
952 throw CppRuntimeException("Introspectable is an array '") << getTypeInfo().getSchemaName() << "[]'!";
953 }
954
955 uint8_t getUInt8() const override
956 {
957 throw CppRuntimeException("Introspectable is an array '") << getTypeInfo().getSchemaName() << "[]'!";
958 }
959
960 uint16_t getUInt16() const override
961 {
962 throw CppRuntimeException("Introspectable is an array '") << getTypeInfo().getSchemaName() << "[]'!";
963 }
964
965 uint32_t getUInt32() const override
966 {
967 throw CppRuntimeException("Introspectable is an array '") << getTypeInfo().getSchemaName() << "[]'!";
968 }
969
970 uint64_t getUInt64() const override
971 {
972 throw CppRuntimeException("Introspectable is an array '") << getTypeInfo().getSchemaName() << "[]'!";
973 }
974
975 float getFloat() const override
976 {
977 throw CppRuntimeException("Introspectable is an array '") << getTypeInfo().getSchemaName() << "[]'!";
978 }
979
980 double getDouble() const override
981 {
982 throw CppRuntimeException("Introspectable is an array '") << getTypeInfo().getSchemaName() << "[]'!";
983 }
984
985 BytesView getBytes() const override
986 {
987 throw CppRuntimeException("Introspectable is an array '") << getTypeInfo().getSchemaName() << "[]'!";
988 }
989
990 std::string_view getStringView() const override
991 {
992 throw CppRuntimeException("Introspectable is an array '") << getTypeInfo().getSchemaName() << "[]'!";
993 }
994
995 const BasicBitBuffer<ALLOC>& getBitBuffer() const override
996 {
997 throw CppRuntimeException("Introspectable is an array '") << getTypeInfo().getSchemaName() << "[]'!";
998 }
999
1000 int64_t toInt() const override
1001 {
1002 throw CppRuntimeException("Introspectable is an array '") << getTypeInfo().getSchemaName() << "[]'!";
1003 }
1004
1005 uint64_t toUInt() const override
1006 {
1007 throw CppRuntimeException("Introspectable is an array '") << getTypeInfo().getSchemaName() << "[]'!";
1008 }
1009
1010 double toDouble() const override
1011 {
1012 throw CppRuntimeException("Introspectable is an array '") << getTypeInfo().getSchemaName() << "[]'!";
1013 }
1014
1015 BasicString<RebindAlloc<ALLOC, char>> toString(const ALLOC&) const override
1016 {
1017 throw CppRuntimeException("Introspectable is an array '") << getTypeInfo().getSchemaName() << "[]'!";
1018 }
1019
1020 IBasicIntrospectableViewConstPtr<ALLOC> getParameter(std::string_view) const override
1021 {
1022 throw CppRuntimeException("Introspectable is an array '") << getTypeInfo().getSchemaName() << "[]'!";
1023 }
1024
1025 IBasicIntrospectableViewConstPtr<ALLOC> callFunction(std::string_view) const override
1026 {
1027 throw CppRuntimeException("Introspectable is an array '") << getTypeInfo().getSchemaName() << "[]'!";
1028 }
1029
1030 BasicBitBuffer<ALLOC> serialize(const ALLOC&) const override
1031 {
1032 throw CppRuntimeException("Introspectable is an array '") << getTypeInfo().getSchemaName() << "[]'!";
1033 }
1034};
1035
1036template <typename T, typename ALLOC>
1037class CompoundIntrospectableViewBase
1038 : public IntrospectableViewBase<View<T>, ALLOC>,
1039 public AllocatorHolder<ALLOC>
1040{
1041private:
1042 using Base = IntrospectableViewBase<View<T>, ALLOC>;
1043
1044public:
1045 CompoundIntrospectableViewBase(const View<T>& view, const ALLOC& allocator) :
1046 Base(typeInfo<T, ALLOC>(), view),
1047 AllocatorHolder<ALLOC>(allocator)
1048 {}
1049
1050 BasicBitBuffer<ALLOC> serialize(const ALLOC& allocator) const override
1051 {
1052 return zserio::serialize(Base::getValue(), allocator);
1053 }
1054};
1055
1056template <typename T, typename ALLOC>
1057IBasicIntrospectableViewConstPtr<ALLOC> IntrospectableViewBase<T, ALLOC>::getFieldFromObject(
1058 const IBasicIntrospectableView<ALLOC>& object, std::string_view name) const
1059{
1060 const auto& typeInfo = object.getTypeInfo();
1061 if (TypeInfoUtil::isCompound(typeInfo.getSchemaType()))
1062 {
1063 const auto& fields = typeInfo.getFields();
1064 auto fieldsIt =
1065 std::find_if(fields.begin(), fields.end(), [name](const BasicFieldInfo<ALLOC>& fieldInfo) {
1066 return fieldInfo.schemaName == name;
1067 });
1068 if (fieldsIt != fields.end())
1069 {
1070 return object.getField(name);
1071 }
1072 }
1073
1074 return nullptr;
1075}
1076
1077template <typename T, typename ALLOC>
1078IBasicIntrospectableViewConstPtr<ALLOC> IntrospectableViewBase<T, ALLOC>::getParameterFromObject(
1079 const IBasicIntrospectableView<ALLOC>& object, std::string_view name) const
1080{
1081 const auto& typeInfo = object.getTypeInfo();
1082 if (TypeInfoUtil::isCompound(typeInfo.getSchemaType()))
1083 {
1084 const auto& parameters = typeInfo.getParameters();
1085 auto parametersIt = std::find_if(
1086 parameters.begin(), parameters.end(), [name](const BasicParameterInfo<ALLOC>& parameterInfo) {
1087 return parameterInfo.schemaName == name;
1088 });
1089 if (parametersIt != parameters.end())
1090 {
1091 return object.getParameter(name);
1092 }
1093 }
1094
1095 return nullptr;
1096}
1097
1098template <typename T, typename ALLOC>
1099IBasicIntrospectableViewConstPtr<ALLOC> IntrospectableViewBase<T, ALLOC>::callFunctionInObject(
1100 const IBasicIntrospectableView<ALLOC>& object, std::string_view name) const
1101{
1102 const auto& typeInfo = object.getTypeInfo();
1103 if (TypeInfoUtil::isCompound(typeInfo.getSchemaType()))
1104 {
1105 const auto& functions = typeInfo.getFunctions();
1106 auto functionsIt = std::find_if(
1107 functions.begin(), functions.end(), [name](const BasicFunctionInfo<ALLOC>& functionInfo) {
1108 return functionInfo.schemaName == name;
1109 });
1110 if (functionsIt != functions.end())
1111 {
1112 return object.callFunction(name);
1113 }
1114 }
1115
1116 return nullptr;
1117}
1118
1119template <typename T, typename ALLOC>
1120IBasicIntrospectableViewConstPtr<ALLOC> IntrospectableViewBase<T, ALLOC>::getFromObject(
1121 const IBasicIntrospectableView<ALLOC>& object, std::string_view path, size_t pos) const
1122{
1123 try
1124 {
1125 const size_t dotPos = path.find('.', pos);
1126 const bool isLast = dotPos == std::string_view::npos;
1127 const std::string_view name =
1128 path.substr(pos, dotPos == std::string_view::npos ? std::string_view::npos : dotPos - pos);
1129
1130 auto field = getFieldFromObject(object, name);
1131 if (field)
1132 {
1133 return isLast ? std::move(field) : getFromObject(*field, path, dotPos + 1);
1134 }
1135
1136 auto parameter = getParameterFromObject(object, name);
1137 if (parameter)
1138 {
1139 return isLast ? std::move(parameter) : getFromObject(*parameter, path, dotPos + 1);
1140 }
1141
1142 auto functionResult = callFunctionInObject(object, name);
1143 if (functionResult)
1144 {
1145 return isLast ? std::move(functionResult) : getFromObject(*functionResult, path, dotPos + 1);
1146 }
1147 }
1148 catch (const CppRuntimeException&)
1149 {}
1150
1151 return nullptr;
1152}
1153
1154} // namespace detail
1155
1156template <typename T, typename TRAITS, typename ALLOC = std::allocator<uint8_t>>
1158 ArrayView<T, TRAITS> value, const ALLOC& allocator = ALLOC())
1159{
1160 return std::allocate_shared<detail::IntrospectableViewArray<ArrayView<T, TRAITS>, ALLOC>>(allocator, value);
1161}
1162
1163} // namespace zserio
1164
1165#endif // ZSERIO_INTROSPECTABLE_VIEW_H_INC
allocator_type get_allocator() const
virtual BasicBitBuffer< ALLOC > serialize() const=0
virtual ConstPtr getParameter(std::string_view name) const=0
virtual BasicBitBuffer< ALLOC > serialize(const ALLOC &allocator) const=0
virtual ConstPtr callFunction(std::string_view name) const=0
typename IIntrospectableData< IBasicIntrospectableView< ALLOC >, ALLOC >::ConstPtr ConstPtr
virtual BasicString< RebindAlloc< ALLOC, char > > toString() const=0
virtual const BasicBitBuffer< ALLOC > & getBitBuffer() const=0
virtual ConstPtr find(std::string_view path) const=0
virtual const IBasicTypeInfo< ALLOC > & getTypeInfo() const=0
virtual ConstPtr getField(std::string_view name) const=0
Span< const uint8_t > BytesView
Definition Bytes.h:30
const IBasicTypeInfo< ALLOC > & typeInfo()
Definition ITypeInfo.h:668
detail::BoolWrapper Bool
Definition Types.h:732
detail::VarIntWrapper< uint64_t, detail::VarIntType::VAR > VarUInt
Definition Types.h:888
std::basic_string< char, std::char_traits< char >, ALLOC > BasicString
Definition String.h:17
detail::FloatWrapper< float, detail::FloatType::FLOAT16 > Float16
Definition Types.h:892
BasicBitBuffer< ALLOC > serialize(const T &data, const ALLOC &allocator, ARGS &&... arguments)
std::vector< uint8_t, ALLOC > BasicBytes
Definition Bytes.h:20
detail::FloatWrapper< float, detail::FloatType::FLOAT32 > Float32
Definition Types.h:893
detail::VarIntWrapper< int32_t, detail::VarIntType::VAR32 > VarInt32
Definition Types.h:881
typename std::allocator_traits< ALLOC >::template rebind_alloc< T > RebindAlloc
Definition RebindAlloc.h:10
detail::VarIntWrapper< uint16_t, detail::VarIntType::VAR16 > VarUInt16
Definition Types.h:885
detail::VarIntWrapper< uint64_t, detail::VarIntType::VAR64 > VarUInt64
Definition Types.h:887
BasicString< RebindAlloc< ALLOC, char > > toString(T value, const ALLOC &allocator=ALLOC())
IBasicIntrospectableViewConstPtr< ALLOC > introspectableArray(ArrayView< T, TRAITS > value, const ALLOC &allocator=ALLOC())
detail::VarIntWrapper< int16_t, detail::VarIntType::VAR16 > VarInt16
Definition Types.h:880
detail::FloatWrapper< double, detail::FloatType::FLOAT64 > Float64
Definition Types.h:894
IBasicIntrospectableViewConstPtr< ALLOC > introspectable(const View< T > &view, const ALLOC &allocator=ALLOC())
detail::VarIntWrapper< uint32_t, detail::VarIntType::VAR32 > VarUInt32
Definition Types.h:886
detail::VarIntWrapper< int64_t, detail::VarIntType::VAR > VarInt
Definition Types.h:883
detail::VarIntWrapper< int64_t, detail::VarIntType::VAR64 > VarInt64
Definition Types.h:882
detail::VarIntWrapper< uint32_t, detail::VarIntType::VARSIZE > VarSize
Definition Types.h:890
static bool isCompound(SchemaType schemaType)