1 #ifndef ZSERIO_REFLECTABLE_DATA_H_INC
2 #define ZSERIO_REFLECTABLE_DATA_H_INC
19 template <
typename ALLOC>
20 class ReflectableDataBase :
public IntrospectableDataBase<IBasicReflectableData<ALLOC>, ALLOC>
26 using IntrospectableDataBase<IBasicReflectableData<ALLOC>, ALLOC>::getField;
27 using IntrospectableDataBase<IBasicReflectableData<ALLOC>, ALLOC>::at;
28 using IntrospectableDataBase<IBasicReflectableData<ALLOC>, ALLOC>::operator[];
29 using IntrospectableDataBase<IBasicReflectableData<ALLOC>, ALLOC>::getAnyValue;
36 explicit ReflectableDataBase(
const IBasicTypeInfo<ALLOC>&
typeInfo);
39 ~ReflectableDataBase()
override = 0;
45 ReflectableDataBase(
const ReflectableDataBase&) =
delete;
46 ReflectableDataBase& operator=(
const ReflectableDataBase&) =
delete;
48 ReflectableDataBase(
const ReflectableDataBase&&) =
delete;
49 ReflectableDataBase& operator=(
const ReflectableDataBase&&) =
delete;
56 void setField(std::string_view name,
const BasicAny<ALLOC>& value)
override;
58 void resize(
size_t size)
override;
59 Ptr at(
size_t index)
override;
61 void setAt(
const BasicAny<ALLOC>& value,
size_t index)
override;
62 void append(
const BasicAny<ALLOC>& value)
override;
64 BasicAny<ALLOC>
getAnyValue(
const ALLOC& allocator)
override;
68 Ptr find(std::string_view path)
override;
72 ConstPtr getFieldFromObject(
const IBasicReflectableData<ALLOC>&
object, std::string_view name)
const;
73 ConstPtr getFromObject(
const IBasicReflectableData<ALLOC>&
object, std::string_view path,
size_t pos)
const;
74 Ptr getFieldFromObject(IBasicReflectableData<ALLOC>&
object, std::string_view name);
75 Ptr getFromObject(IBasicReflectableData<ALLOC>&
object, std::string_view path,
size_t pos);
83 template <
typename T,
typename ALLOC>
84 class BuiltinReflectableDataBase :
public ReflectableDataBase<ALLOC>
87 using Base = ReflectableDataBase<ALLOC>;
88 using ReflectableDataBase<ALLOC>::getAnyValue;
93 BuiltinReflectableDataBase(
const IBasicTypeInfo<ALLOC>&
typeInfo, T value) :
104 BasicAny<ALLOC>
getAnyValue(
const ALLOC& allocator)
const override
106 return BasicAny<ALLOC>(m_value, allocator);
109 BasicAny<ALLOC>
getAnyValue(
const ALLOC& allocator)
override
111 return BasicAny<ALLOC>(m_value, allocator);
123 template <
typename T,
typename ALLOC>
124 class IntegralReflectableDataBase :
public BuiltinReflectableDataBase<T, ALLOC>
127 static_assert(std::is_integral_v<typename T::ValueType>,
"T must be a signed integral type!");
129 using Base = BuiltinReflectableDataBase<T, ALLOC>;
132 IntegralReflectableDataBase(
const IBasicTypeInfo<ALLOC>&
typeInfo, T value) :
138 return static_cast<double>(Base::getValue());
141 BasicString<RebindAlloc<ALLOC, char>>
toString(
const ALLOC& allocator)
const override
143 return zserio::toString<ALLOC>(Base::getValue(), allocator);
152 template <
typename T,
typename ALLOC>
153 class SignedReflectableDataBase :
public IntegralReflectableDataBase<T, ALLOC>
156 static_assert(std::is_signed_v<typename T::ValueType>,
"T must be a signed integral type!");
158 using Base = IntegralReflectableDataBase<T, ALLOC>;
163 int64_t
toInt()
const override
165 return Base::getValue();
174 template <
typename T,
typename ALLOC>
175 class UnsignedReflectableDataBase :
public IntegralReflectableDataBase<T, ALLOC>
178 static_assert(std::is_unsigned<typename T::ValueType>::value,
"T must be an unsigned integral type!");
180 using Base = IntegralReflectableDataBase<T, ALLOC>;
185 uint64_t
toUInt()
const override
187 return Base::getValue();
194 template <
typename ALLOC>
195 class BoolReflectableData :
public UnsignedReflectableDataBase<Bool, ALLOC>
198 using Base = UnsignedReflectableDataBase<Bool, ALLOC>;
201 explicit BoolReflectableData(
Bool value) :
207 return Base::getValue();
214 template <
typename T,
typename ALLOC>
215 class Int8ReflectableData :
public SignedReflectableDataBase<T, ALLOC>
218 using Base = SignedReflectableDataBase<T, ALLOC>;
220 static_assert(std::is_same_v<int8_t, typename T::ValueType>,
"T must be based on int8_t!");
223 explicit Int8ReflectableData(T value) :
227 int8_t
getInt8()
const override
229 return Base::getValue();
236 template <
typename T,
typename ALLOC>
237 class Int16ReflectableData :
public SignedReflectableDataBase<T, ALLOC>
240 using Base = SignedReflectableDataBase<T, ALLOC>;
242 static_assert(std::is_same_v<int16_t, typename T::ValueType>,
"T must be based on int16_t!");
245 explicit Int16ReflectableData(T value) :
251 return Base::getValue();
258 template <
typename T,
typename ALLOC>
259 class Int32ReflectableData :
public SignedReflectableDataBase<T, ALLOC>
262 using Base = SignedReflectableDataBase<T, ALLOC>;
264 static_assert(std::is_same_v<int32_t, typename T::ValueType>,
"T must be based on int32_t!");
267 explicit Int32ReflectableData(T value) :
273 return Base::getValue();
280 template <
typename T,
typename ALLOC>
281 class Int64ReflectableData :
public SignedReflectableDataBase<T, ALLOC>
284 using Base = SignedReflectableDataBase<T, ALLOC>;
286 static_assert(std::is_same_v<int64_t, typename T::ValueType>,
"T must be based on int64_t!");
289 explicit Int64ReflectableData(T value) :
295 return Base::getValue();
302 template <
typename T,
typename ALLOC>
303 class UInt8ReflectableData :
public UnsignedReflectableDataBase<T, ALLOC>
306 using Base = UnsignedReflectableDataBase<T, ALLOC>;
308 static_assert(std::is_same_v<uint8_t, typename T::ValueType>,
"T must be based on uint8_t!");
311 explicit UInt8ReflectableData(T value) :
317 return Base::getValue();
324 template <
typename T,
typename ALLOC>
325 class UInt16ReflectableData :
public UnsignedReflectableDataBase<T, ALLOC>
328 using Base = UnsignedReflectableDataBase<T, ALLOC>;
330 static_assert(std::is_same_v<uint16_t, typename T::ValueType>,
"T must be based on uint16_t!");
333 explicit UInt16ReflectableData(T value) :
339 return Base::getValue();
346 template <
typename T,
typename ALLOC>
347 class UInt32ReflectableData :
public UnsignedReflectableDataBase<T, ALLOC>
350 using Base = UnsignedReflectableDataBase<T, ALLOC>;
352 static_assert(std::is_same_v<uint32_t, typename T::ValueType>,
"T must be based on uint32_t!");
355 explicit UInt32ReflectableData(T value) :
361 return Base::getValue();
368 template <
typename T,
typename ALLOC>
369 class UInt64ReflectableData :
public UnsignedReflectableDataBase<T, ALLOC>
372 using Base = UnsignedReflectableDataBase<T, ALLOC>;
374 static_assert(std::is_same_v<uint64_t, typename T::ValueType>,
"T must be based on uint64_t!");
377 explicit UInt64ReflectableData(T value) :
383 return Base::getValue();
390 template <
typename T,
typename ALLOC>
391 class FloatingPointReflectableDataBase :
public BuiltinReflectableDataBase<T, ALLOC>
394 static_assert(std::is_floating_point_v<typename T::ValueType>,
"T must be a floating point type!");
396 using Base = BuiltinReflectableDataBase<T, ALLOC>;
402 return static_cast<double>(Base::getValue());
409 template <
typename T,
typename ALLOC>
410 class FloatReflectableData :
public FloatingPointReflectableDataBase<T, ALLOC>
413 using Base = FloatingPointReflectableDataBase<T, ALLOC>;
416 explicit FloatReflectableData(T value) :
422 return Base::getValue();
429 template <
typename ALLOC>
430 class DoubleReflectableData :
public FloatingPointReflectableDataBase<Float64, ALLOC>
433 using Base = FloatingPointReflectableDataBase<Float64, ALLOC>;
436 explicit DoubleReflectableData(
Float64 value) :
442 return Base::getValue();
449 template <
typename ALLOC>
450 class BytesReflectableData :
public BuiltinReflectableDataBase<BytesView, ALLOC>
453 using Base = BuiltinReflectableDataBase<BytesView, ALLOC>;
456 explicit BytesReflectableData(
BytesView value) :
462 return Base::getValue();
469 template <
typename ALLOC>
470 class StringReflectableData :
public BuiltinReflectableDataBase<std::string_view, ALLOC>
473 using Base = BuiltinReflectableDataBase<std::string_view, ALLOC>;
476 explicit StringReflectableData(std::string_view value) :
482 return Base::getValue();
485 BasicString<RebindAlloc<ALLOC, char>>
toString(
const ALLOC& allocator)
const override
494 template <
typename ALLOC>
495 class BitBufferReflectableData
496 :
public BuiltinReflectableDataBase<std::reference_wrapper<const BasicBitBuffer<ALLOC>>, ALLOC>
499 using Base = BuiltinReflectableDataBase<std::reference_wrapper<const BasicBitBuffer<ALLOC>>, ALLOC>;
502 explicit BitBufferReflectableData(std::reference_wrapper<
const BasicBitBuffer<ALLOC>> value) :
503 Base(
typeInfo<BasicBitBuffer<ALLOC>>(), value)
506 const BasicBitBuffer<ALLOC>&
getBitBuffer()
const override
508 return Base::getValue();
515 template <
typename ALLOC>
516 class ReflectableDataAllocatorHolderBase :
public ReflectableDataBase<ALLOC>,
public AllocatorHolder<ALLOC>
519 ReflectableDataAllocatorHolderBase(
const IBasicTypeInfo<ALLOC>&
typeInfo,
const ALLOC& allocator) :
520 ReflectableDataBase<ALLOC>(
typeInfo),
530 template <
typename ALLOC>
531 class ReflectableDataConstAllocatorHolderBase :
public ReflectableDataAllocatorHolderBase<ALLOC>
534 using Base = ReflectableDataAllocatorHolderBase<ALLOC>;
538 using Base::getTypeInfo;
540 IBasicReflectableDataPtr<ALLOC>
getField(std::string_view name)
override;
541 void setField(std::string_view name,
const BasicAny<ALLOC>& value)
override;
543 BasicAny<ALLOC>
getAnyValue(
const ALLOC& allocator)
override;
551 template <
typename ALLOC>
552 class ReflectableDataArrayBase :
public ReflectableDataAllocatorHolderBase<ALLOC>
555 using Base = ReflectableDataAllocatorHolderBase<ALLOC>;
559 using Base::getTypeInfo;
566 IBasicReflectableDataConstPtr<ALLOC>
getField(std::string_view name)
const override;
567 IBasicReflectableDataPtr<ALLOC>
getField(std::string_view name)
override;
568 IBasicReflectableDataPtr<ALLOC>
createField(std::string_view name)
override;
569 void setField(std::string_view name,
const BasicAny<ALLOC>& value)
override;
571 IBasicReflectableDataConstPtr<ALLOC>
operator[](
size_t index)
const override;
572 IBasicReflectableDataPtr<ALLOC>
operator[](
size_t index)
override;
575 int8_t
getInt8()
const override;
587 const BasicBitBuffer<ALLOC>&
getBitBuffer()
const override;
589 int64_t
toInt()
const override;
590 uint64_t
toUInt()
const override;
592 BasicString<RebindAlloc<ALLOC, char>>
toString(
const ALLOC& allocator)
const override;
600 template <
typename ALLOC>
601 class ReflectableDataConstArrayBase :
public ReflectableDataArrayBase<ALLOC>
604 using Base = ReflectableDataArrayBase<ALLOC>;
608 using Base::getTypeInfo;
610 void resize(
size_t index)
override;
611 IBasicReflectableDataPtr<ALLOC>
at(
size_t index)
override;
612 IBasicReflectableDataPtr<ALLOC>
operator[](
size_t index)
override;
613 void setAt(
const BasicAny<ALLOC>& value,
size_t index)
override;
614 void append(
const BasicAny<ALLOC>& value)
override;
616 BasicAny<ALLOC>
getAnyValue(
const ALLOC& allocator)
override;
621 template <
typename ALLOC = std::allocator<u
int8_t>>
624 return std::allocate_shared<detail::BoolReflectableData<ALLOC>>(allocator, value);
627 template <BitSize BIT_SIZE,
bool IS_SIGNED,
typename ALLOC = std::allocator<u
int8_t>>
629 detail::FixedIntWrapper<BIT_SIZE, IS_SIGNED> value,
const ALLOC& allocator = ALLOC())
631 using Type = detail::FixedIntWrapper<BIT_SIZE, IS_SIGNED>;
632 using ValueType =
typename Type::ValueType;
634 if constexpr (std::is_signed_v<ValueType>)
636 if constexpr (
sizeof(ValueType) > 4)
638 return std::allocate_shared<detail::Int64ReflectableData<Type, ALLOC>>(allocator, value);
640 else if constexpr (
sizeof(ValueType) > 2)
642 return std::allocate_shared<detail::Int32ReflectableData<Type, ALLOC>>(allocator, value);
644 else if constexpr (
sizeof(ValueType) > 1)
646 return std::allocate_shared<detail::Int16ReflectableData<Type, ALLOC>>(allocator, value);
650 return std::allocate_shared<detail::Int8ReflectableData<Type, ALLOC>>(allocator, value);
655 if constexpr (
sizeof(ValueType) > 4)
657 return std::allocate_shared<detail::UInt64ReflectableData<Type, ALLOC>>(allocator, value);
659 else if constexpr (
sizeof(ValueType) > 2)
661 return std::allocate_shared<detail::UInt32ReflectableData<Type, ALLOC>>(allocator, value);
663 else if constexpr (
sizeof(ValueType) > 1)
665 return std::allocate_shared<detail::UInt16ReflectableData<Type, ALLOC>>(allocator, value);
669 return std::allocate_shared<detail::UInt8ReflectableData<Type, ALLOC>>(allocator, value);
674 template <
typename T,
typename ALLOC = std::allocator<u
int8_t>>
677 using Type = detail::DynIntWrapper<T>;
679 if constexpr (std::is_signed_v<T>)
681 if constexpr (
sizeof(T) > 4)
683 return std::allocate_shared<detail::Int64ReflectableData<Type, ALLOC>>(allocator, value);
685 else if constexpr (
sizeof(T) > 2)
687 return std::allocate_shared<detail::Int32ReflectableData<Type, ALLOC>>(allocator, value);
689 else if constexpr (
sizeof(T) > 1)
691 return std::allocate_shared<detail::Int16ReflectableData<Type, ALLOC>>(allocator, value);
695 return std::allocate_shared<detail::Int8ReflectableData<Type, ALLOC>>(allocator, value);
700 if constexpr (
sizeof(T) > 4)
702 return std::allocate_shared<detail::UInt64ReflectableData<Type, ALLOC>>(allocator, value);
704 else if constexpr (
sizeof(T) > 2)
706 return std::allocate_shared<detail::UInt32ReflectableData<Type, ALLOC>>(allocator, value);
708 else if constexpr (
sizeof(T) > 1)
710 return std::allocate_shared<detail::UInt16ReflectableData<Type, ALLOC>>(allocator, value);
714 return std::allocate_shared<detail::UInt8ReflectableData<Type, ALLOC>>(allocator, value);
719 template <
typename ALLOC = std::allocator<u
int8_t>>
722 return std::allocate_shared<detail::Int16ReflectableData<VarInt16, ALLOC>>(allocator, value);
725 template <
typename ALLOC = std::allocator<u
int8_t>>
726 IBasicReflectableDataPtr<ALLOC>
reflectable(
VarInt32 value,
const ALLOC& allocator = ALLOC())
728 return std::allocate_shared<detail::Int32ReflectableData<VarInt32, ALLOC>>(allocator, value);
731 template <
typename ALLOC = std::allocator<u
int8_t>>
732 IBasicReflectableDataPtr<ALLOC>
reflectable(
VarInt64 value,
const ALLOC& allocator = ALLOC())
734 return std::allocate_shared<detail::Int64ReflectableData<VarInt64, ALLOC>>(allocator, value);
737 template <
typename ALLOC = std::allocator<u
int8_t>>
738 IBasicReflectableDataPtr<ALLOC>
reflectable(
VarInt value,
const ALLOC& allocator = ALLOC())
740 return std::allocate_shared<detail::Int64ReflectableData<VarInt, ALLOC>>(allocator, value);
743 template <
typename ALLOC = std::allocator<u
int8_t>>
746 return std::allocate_shared<detail::UInt16ReflectableData<VarUInt16, ALLOC>>(allocator, value);
749 template <
typename ALLOC = std::allocator<u
int8_t>>
752 return std::allocate_shared<detail::UInt32ReflectableData<VarUInt32, ALLOC>>(allocator, value);
755 template <
typename ALLOC = std::allocator<u
int8_t>>
758 return std::allocate_shared<detail::UInt64ReflectableData<VarUInt64, ALLOC>>(allocator, value);
761 template <
typename ALLOC = std::allocator<u
int8_t>>
762 IBasicReflectableDataPtr<ALLOC>
reflectable(
VarUInt value,
const ALLOC& allocator = ALLOC())
764 return std::allocate_shared<detail::UInt64ReflectableData<VarUInt, ALLOC>>(allocator, value);
767 template <
typename ALLOC = std::allocator<u
int8_t>>
768 IBasicReflectableDataPtr<ALLOC>
reflectable(
VarSize value,
const ALLOC& allocator = ALLOC())
770 return std::allocate_shared<detail::UInt32ReflectableData<VarSize, ALLOC>>(allocator, value);
773 template <
typename ALLOC = std::allocator<u
int8_t>>
776 return std::allocate_shared<detail::FloatReflectableData<Float16, ALLOC>>(allocator, value);
779 template <
typename ALLOC = std::allocator<u
int8_t>>
780 IBasicReflectableDataPtr<ALLOC>
reflectable(
Float32 value,
const ALLOC& allocator = ALLOC())
782 return std::allocate_shared<detail::FloatReflectableData<Float32, ALLOC>>(allocator, value);
785 template <
typename ALLOC = std::allocator<u
int8_t>>
786 IBasicReflectableDataPtr<ALLOC>
reflectable(
Float64 value,
const ALLOC& allocator = ALLOC())
788 return std::allocate_shared<detail::DoubleReflectableData<ALLOC>>(allocator, value);
791 template <
typename ALLOC = std::allocator<u
int8_t>>
794 return std::allocate_shared<detail::BytesReflectableData<ALLOC>>(allocator, value);
797 template <
typename ALLOC = std::allocator<u
int8_t>>
800 return std::allocate_shared<detail::BytesReflectableData<ALLOC>>(allocator, value);
803 template <
typename STRING_ALLOC,
typename ALLOC = std::allocator<u
int8_t>>
807 return std::allocate_shared<detail::StringReflectableData<ALLOC>>(allocator, value);
810 template <
typename ALLOC = std::allocator<u
int8_t>>
813 return std::allocate_shared<detail::StringReflectableData<ALLOC>>(allocator, value);
816 template <
typename ALLOC = std::allocator<u
int8_t>>
819 return std::allocate_shared<detail::BitBufferReflectableData<ALLOC>>(allocator, value);
822 template <
typename ALLOC = std::allocator<u
int8_t>>
826 return std::allocate_shared<detail::BitBufferReflectableData<ALLOC>>(allocator, value);
836 template <
typename RAW_ARRAY,
typename ALLOC>
837 class ReflectableDataConstArray :
public ReflectableDataConstArrayBase<ALLOC>
840 using Base = ReflectableDataConstArrayBase<ALLOC>;
844 using Base::getTypeInfo;
845 using Base::operator[];
846 using Base::getAnyValue;
848 explicit ReflectableDataConstArray(
const RAW_ARRAY& rawArray,
const ALLOC& allocator = {}) :
849 Base(typeInfo<typename RAW_ARRAY::value_type, ALLOC>(), allocator),
853 size_t size()
const override
855 return m_rawArray.size();
858 IBasicReflectableDataConstPtr<ALLOC>
at(
size_t index)
const override
862 throw CppRuntimeException(
"Index ")
864 <<
"' of size " <<
size() <<
"!";
867 return reflectable(m_rawArray[index], Base::get_allocator());
870 BasicAny<ALLOC>
getAnyValue(
const ALLOC& allocator)
const override
872 return BasicAny<ALLOC>(std::cref(m_rawArray), allocator);
876 const RAW_ARRAY& m_rawArray;
879 template <
typename RAW_ARRAY,
typename ALLOC>
880 class ReflectableDataArray :
public ReflectableDataArrayBase<ALLOC>
883 using Base = ReflectableDataArrayBase<ALLOC>;
886 using Base::getAnyValue;
887 using Base::getTypeInfo;
889 explicit ReflectableDataArray(RAW_ARRAY& rawArray,
const ALLOC& allocator = {}) :
890 Base(typeInfo<typename RAW_ARRAY::value_type, ALLOC>(), allocator),
894 size_t size()
const override
896 return m_rawArray.size();
899 void resize(
size_t size)
override
901 m_rawArray.resize(size);
904 IBasicReflectableDataConstPtr<ALLOC>
at(
size_t index)
const override
908 throw CppRuntimeException(
"Index ")
910 <<
"' of size " << size() <<
"!";
913 return reflectable(m_rawArray[index], Base::get_allocator());
916 IBasicReflectableDataPtr<ALLOC>
at(
size_t index)
override
920 throw CppRuntimeException(
"Index ")
922 <<
"' of size " << size() <<
"!";
925 return reflectable(m_rawArray[index], Base::get_allocator());
928 void setAt(
const BasicAny<ALLOC>& value,
size_t index)
override
932 throw CppRuntimeException(
"Index ")
934 <<
"' of size " << size() <<
"!";
937 m_rawArray[index] = ReflectableUtil::fromAny<typename RAW_ARRAY::value_type>(value);
940 void append(
const BasicAny<ALLOC>& value)
override
942 m_rawArray.push_back(ReflectableUtil::fromAny<typename RAW_ARRAY::value_type>(value));
945 BasicAny<ALLOC>
getAnyValue(
const ALLOC& allocator)
const override
947 return BasicAny<ALLOC>(std::cref(m_rawArray), allocator);
950 BasicAny<ALLOC>
getAnyValue(
const ALLOC& allocator)
override
952 return BasicAny<ALLOC>(std::ref(m_rawArray), allocator);
956 RAW_ARRAY& m_rawArray;
964 template <
typename T,
typename ALLOC =
typename T::allocator_type>
965 class ReflectableDataOwner :
public IBasicReflectableData<ALLOC>
968 ReflectableDataOwner() :
969 ReflectableDataOwner(ALLOC())
972 explicit ReflectableDataOwner(
const ALLOC& allocator) :
977 const IBasicTypeInfo<ALLOC>&
getTypeInfo()
const override
979 return m_reflectable->getTypeInfo();
984 return m_reflectable->isArray();
987 IBasicReflectableDataConstPtr<ALLOC>
getField(std::string_view name)
const override
989 return m_reflectable->getField(name);
992 IBasicReflectableDataPtr<ALLOC>
getField(std::string_view name)
override
994 return m_reflectable->getField(name);
997 IBasicReflectableDataPtr<ALLOC>
createField(std::string_view name)
override
999 return m_reflectable->createField(name);
1002 void setField(std::string_view name,
const BasicAny<ALLOC>& value)
override
1004 m_reflectable->setField(name, value);
1007 IBasicReflectableDataConstPtr<ALLOC>
find(std::string_view path)
const override
1009 return m_reflectable->find(path);
1012 IBasicReflectableDataPtr<ALLOC>
find(std::string_view path)
override
1014 return m_reflectable->find(path);
1017 IBasicReflectableDataConstPtr<ALLOC>
operator[](std::string_view path)
const override
1019 return m_reflectable->operator[](path);
1022 IBasicReflectableDataPtr<ALLOC>
operator[](std::string_view path)
override
1024 return m_reflectable->operator[](path);
1027 std::string_view
getChoice()
const override
1029 return m_reflectable->getChoice();
1032 size_t size()
const override
1034 return m_reflectable->size();
1037 void resize(
size_t size)
override
1039 m_reflectable->resize(size);
1042 IBasicReflectableDataConstPtr<ALLOC>
at(
size_t index)
const override
1044 return m_reflectable->at(index);
1047 IBasicReflectableDataPtr<ALLOC>
at(
size_t index)
override
1049 return m_reflectable->at(index);
1052 IBasicReflectableDataConstPtr<ALLOC>
operator[](
size_t index)
const override
1054 return m_reflectable->operator[](index);
1057 IBasicReflectableDataPtr<ALLOC>
operator[](
size_t index)
override
1059 return m_reflectable->operator[](index);
1062 void setAt(
const BasicAny<ALLOC>& value,
size_t index)
override
1064 m_reflectable->setAt(value, index);
1067 void append(
const BasicAny<ALLOC>& value)
override
1069 m_reflectable->append(value);
1072 BasicAny<ALLOC>
getAnyValue(
const ALLOC& allocator)
const override
1074 return m_reflectable->getAnyValue(allocator);
1077 BasicAny<ALLOC>
getAnyValue(
const ALLOC& allocator)
override
1079 return m_reflectable->getAnyValue(allocator);
1095 return m_reflectable->getBool();
1097 int8_t
getInt8()
const override
1099 return m_reflectable->getInt8();
1103 return m_reflectable->getInt16();
1107 return m_reflectable->getInt32();
1111 return m_reflectable->getInt64();
1115 return m_reflectable->getUInt8();
1119 return m_reflectable->getUInt16();
1123 return m_reflectable->getUInt32();
1127 return m_reflectable->getUInt64();
1131 return m_reflectable->getFloat();
1135 return m_reflectable->getDouble();
1137 Span<const uint8_t>
getBytes()
const override
1139 return m_reflectable->getBytes();
1143 return m_reflectable->getStringView();
1145 const BasicBitBuffer<ALLOC>&
getBitBuffer()
const override
1147 return m_reflectable->getBitBuffer();
1151 int64_t
toInt()
const override
1153 return m_reflectable->toInt();
1155 uint64_t
toUInt()
const override
1157 return m_reflectable->toUInt();
1161 return m_reflectable->toDouble();
1163 BasicString<RebindAlloc<ALLOC, char>>
toString(
const ALLOC& allocator)
const override
1165 return m_reflectable->toString(allocator);
1167 BasicString<RebindAlloc<ALLOC, char>>
toString()
const override
1174 IBasicReflectableDataPtr<ALLOC> m_reflectable;
1179 template <
typename ALLOC>
1180 ReflectableDataBase<ALLOC>::ReflectableDataBase(
const IBasicTypeInfo<ALLOC>&
typeInfo) :
1181 IntrospectableDataBase<IBasicReflectableData<ALLOC>, ALLOC>(
typeInfo)
1184 template <
typename ALLOC>
1185 ReflectableDataBase<ALLOC>::~ReflectableDataBase() =
default;
1187 template <
typename ALLOC>
1188 typename ReflectableDataBase<ALLOC>::Ptr ReflectableDataBase<ALLOC>::getField(std::string_view)
1190 throw CppRuntimeException(
"Type '")
1191 << ReflectableDataBase<ALLOC>::getTypeInfo().getSchemaName() <<
"' has no fields to get!";
1194 template <
typename ALLOC>
1195 typename ReflectableDataBase<ALLOC>::Ptr ReflectableDataBase<ALLOC>::createField(std::string_view)
1197 throw CppRuntimeException(
"Type '")
1198 << ReflectableDataBase<ALLOC>::getTypeInfo().getSchemaName() <<
"' has no fields to create!";
1201 template <
typename ALLOC>
1202 void ReflectableDataBase<ALLOC>::setField(std::string_view,
const BasicAny<ALLOC>&)
1204 throw CppRuntimeException(
"Type '")
1205 << ReflectableDataBase<ALLOC>::getTypeInfo().getSchemaName() <<
"' has no fields to set!";
1208 template <
typename ALLOC>
1209 void ReflectableDataBase<ALLOC>::resize(
size_t)
1211 throw CppRuntimeException(
"Type '")
1212 << ReflectableDataBase<ALLOC>::getTypeInfo().getSchemaName() <<
"' is not an array!";
1215 template <
typename ALLOC>
1216 typename ReflectableDataBase<ALLOC>::Ptr ReflectableDataBase<ALLOC>::at(
size_t)
1218 throw CppRuntimeException(
"Type '")
1219 << ReflectableDataBase<ALLOC>::getTypeInfo().getSchemaName() <<
"' is not an array!";
1222 template <
typename ALLOC>
1223 typename ReflectableDataBase<ALLOC>::Ptr ReflectableDataBase<ALLOC>::operator[](
size_t)
1225 throw CppRuntimeException(
"Type '")
1226 << ReflectableDataBase<ALLOC>::getTypeInfo().getSchemaName() <<
"' is not an array!";
1229 template <
typename ALLOC>
1230 void ReflectableDataBase<ALLOC>::setAt(
const BasicAny<ALLOC>&,
size_t)
1232 throw CppRuntimeException(
"Type '")
1233 << ReflectableDataBase<ALLOC>::getTypeInfo().getSchemaName() <<
"' is not an array!";
1236 template <
typename ALLOC>
1237 void ReflectableDataBase<ALLOC>::append(
const BasicAny<ALLOC>&)
1239 throw CppRuntimeException(
"Type '")
1240 << ReflectableDataBase<ALLOC>::getTypeInfo().getSchemaName() <<
"' is not an array!";
1243 template <
typename ALLOC>
1244 BasicAny<ALLOC> ReflectableDataBase<ALLOC>::getAnyValue(
const ALLOC&)
1246 throw CppRuntimeException(
"Type '")
1247 << ReflectableDataBase<ALLOC>::getTypeInfo().getSchemaName() <<
"' is not implemented!";
1250 template <
typename ALLOC>
1251 BasicAny<ALLOC> ReflectableDataBase<ALLOC>::getAnyValue()
1253 return getAnyValue(ALLOC());
1256 template <
typename ALLOC>
1257 typename ReflectableDataBase<ALLOC>::ConstPtr ReflectableDataBase<ALLOC>::find(std::string_view path)
const
1259 return getFromObject(*
this, path, 0);
1262 template <
typename ALLOC>
1263 typename ReflectableDataBase<ALLOC>::Ptr ReflectableDataBase<ALLOC>::find(std::string_view path)
1265 return getFromObject(*
this, path, 0);
1268 template <
typename ALLOC>
1269 typename ReflectableDataBase<ALLOC>::Ptr ReflectableDataBase<ALLOC>::operator[](std::string_view path)
1274 template <
typename ALLOC>
1275 typename ReflectableDataBase<ALLOC>::ConstPtr ReflectableDataBase<ALLOC>::getFieldFromObject(
1276 const IBasicReflectableData<ALLOC>&
object, std::string_view name)
const
1278 const auto&
typeInfo =
object.getTypeInfo();
1283 std::find_if(fields.begin(), fields.end(), [name](
const BasicFieldInfo<ALLOC>& fieldInfo) {
1284 return fieldInfo.schemaName == name;
1286 if (fieldsIt != fields.end())
1288 return object.getField(name);
1295 template <
typename ALLOC>
1296 typename ReflectableDataBase<ALLOC>::ConstPtr ReflectableDataBase<ALLOC>::getFromObject(
1297 const IBasicReflectableData<ALLOC>&
object, std::string_view path,
size_t pos)
const
1301 const size_t dotPos = path.find(
'.', pos);
1302 const bool isLast = dotPos == std::string_view::npos;
1303 const std::string_view name =
1304 path.substr(pos, dotPos == std::string_view::npos ? std::string_view::npos : dotPos - pos);
1306 auto field = getFieldFromObject(
object, name);
1309 return isLast ? std::move(field) : getFromObject(*field, path, dotPos + 1);
1312 catch (
const CppRuntimeException&)
1318 template <
typename ALLOC>
1319 typename ReflectableDataBase<ALLOC>::Ptr ReflectableDataBase<ALLOC>::getFieldFromObject(
1320 IBasicReflectableData<ALLOC>&
object, std::string_view name)
1322 const auto&
typeInfo =
object.getTypeInfo();
1327 std::find_if(fields.begin(), fields.end(), [name](
const BasicFieldInfo<ALLOC>& fieldInfo) {
1328 return fieldInfo.schemaName == name;
1330 if (fieldsIt != fields.end())
1332 return object.getField(name);
1339 template <
typename ALLOC>
1340 typename ReflectableDataBase<ALLOC>::Ptr ReflectableDataBase<ALLOC>::getFromObject(
1341 IBasicReflectableData<ALLOC>&
object, std::string_view path,
size_t pos)
1345 const size_t dotPos = path.find(
'.', pos);
1346 const bool isLast = dotPos == std::string_view::npos;
1347 const std::string_view name =
1348 path.substr(pos, dotPos == std::string_view::npos ? std::string_view::npos : dotPos - pos);
1350 auto field = getFieldFromObject(
object, name);
1353 return isLast ? std::move(field) : getFromObject(*field, path, dotPos + 1);
1356 catch (
const CppRuntimeException&)
1362 template <
typename ALLOC>
1363 IBasicReflectableDataPtr<ALLOC> ReflectableDataConstAllocatorHolderBase<ALLOC>::getField(std::string_view)
1365 throw CppRuntimeException(
"Reflectable '") << getTypeInfo().getSchemaName() <<
"' is constant!";
1368 template <
typename ALLOC>
1369 void ReflectableDataConstAllocatorHolderBase<ALLOC>::setField(std::string_view,
const BasicAny<ALLOC>&)
1371 throw CppRuntimeException(
"Reflectable '") << getTypeInfo().getSchemaName() <<
"' is constant!";
1374 template <
typename ALLOC>
1375 BasicAny<ALLOC> ReflectableDataConstAllocatorHolderBase<ALLOC>::getAnyValue(
const ALLOC&)
1377 throw CppRuntimeException(
"Reflectable '") << getTypeInfo().getSchemaName() <<
"' is constant!";
1380 template <
typename ALLOC>
1381 IBasicReflectableDataConstPtr<ALLOC> ReflectableDataArrayBase<ALLOC>::getField(std::string_view)
const
1383 throw CppRuntimeException(
"Reflectable is an array '") << getTypeInfo().getSchemaName() <<
"[]'!";
1386 template <
typename ALLOC>
1387 IBasicReflectableDataPtr<ALLOC> ReflectableDataArrayBase<ALLOC>::getField(std::string_view)
1389 throw CppRuntimeException(
"Reflectable is an array '") << getTypeInfo().getSchemaName() <<
"[]'!";
1392 template <
typename ALLOC>
1393 IBasicReflectableDataPtr<ALLOC> ReflectableDataArrayBase<ALLOC>::createField(std::string_view)
1395 throw CppRuntimeException(
"Reflectable is an array '") << getTypeInfo().getSchemaName() <<
"[]'!";
1398 template <
typename ALLOC>
1399 void ReflectableDataArrayBase<ALLOC>::setField(std::string_view,
const BasicAny<ALLOC>&)
1401 throw CppRuntimeException(
"Reflectable is an array '") << getTypeInfo().getSchemaName() <<
"[]'!";
1404 template <
typename ALLOC>
1405 IBasicReflectableDataConstPtr<ALLOC> ReflectableDataArrayBase<ALLOC>::operator[](
size_t index)
const
1407 return this->at(index);
1410 template <
typename ALLOC>
1411 IBasicReflectableDataPtr<ALLOC> ReflectableDataArrayBase<ALLOC>::operator[](
size_t index)
1413 return this->at(index);
1416 template <
typename ALLOC>
1417 bool ReflectableDataArrayBase<ALLOC>::getBool()
const
1419 throw CppRuntimeException(
"Reflectable is an array '") << getTypeInfo().getSchemaName() <<
"[]'!";
1422 template <
typename ALLOC>
1423 int8_t ReflectableDataArrayBase<ALLOC>::getInt8()
const
1425 throw CppRuntimeException(
"Reflectable is an array '") << getTypeInfo().getSchemaName() <<
"[]'!";
1428 template <
typename ALLOC>
1429 int16_t ReflectableDataArrayBase<ALLOC>::getInt16()
const
1431 throw CppRuntimeException(
"Reflectable is an array '") << getTypeInfo().getSchemaName() <<
"[]'!";
1434 template <
typename ALLOC>
1435 int32_t ReflectableDataArrayBase<ALLOC>::getInt32()
const
1437 throw CppRuntimeException(
"Reflectable is an array '") << getTypeInfo().getSchemaName() <<
"[]'!";
1440 template <
typename ALLOC>
1441 int64_t ReflectableDataArrayBase<ALLOC>::getInt64()
const
1443 throw CppRuntimeException(
"Reflectable is an array '") << getTypeInfo().getSchemaName() <<
"[]'!";
1446 template <
typename ALLOC>
1447 uint8_t ReflectableDataArrayBase<ALLOC>::getUInt8()
const
1449 throw CppRuntimeException(
"Reflectable is an array '") << getTypeInfo().getSchemaName() <<
"[]'!";
1452 template <
typename ALLOC>
1453 uint16_t ReflectableDataArrayBase<ALLOC>::getUInt16()
const
1455 throw CppRuntimeException(
"Reflectable is an array '") << getTypeInfo().getSchemaName() <<
"[]'!";
1458 template <
typename ALLOC>
1459 uint32_t ReflectableDataArrayBase<ALLOC>::getUInt32()
const
1461 throw CppRuntimeException(
"Reflectable is an array '") << getTypeInfo().getSchemaName() <<
"[]'!";
1464 template <
typename ALLOC>
1465 uint64_t ReflectableDataArrayBase<ALLOC>::getUInt64()
const
1467 throw CppRuntimeException(
"Reflectable is an array '") << getTypeInfo().getSchemaName() <<
"[]'!";
1470 template <
typename ALLOC>
1471 float ReflectableDataArrayBase<ALLOC>::getFloat()
const
1473 throw CppRuntimeException(
"Reflectable is an array '") << getTypeInfo().getSchemaName() <<
"[]'!";
1476 template <
typename ALLOC>
1477 double ReflectableDataArrayBase<ALLOC>::getDouble()
const
1479 throw CppRuntimeException(
"Reflectable is an array '") << getTypeInfo().getSchemaName() <<
"[]'!";
1482 template <
typename ALLOC>
1483 BytesView ReflectableDataArrayBase<ALLOC>::getBytes()
const
1485 throw CppRuntimeException(
"Reflectable is an array '") << getTypeInfo().getSchemaName() <<
"[]'!";
1488 template <
typename ALLOC>
1489 std::string_view ReflectableDataArrayBase<ALLOC>::getStringView()
const
1491 throw CppRuntimeException(
"Reflectable is an array '") << getTypeInfo().getSchemaName() <<
"[]'!";
1494 template <
typename ALLOC>
1495 const BasicBitBuffer<ALLOC>& ReflectableDataArrayBase<ALLOC>::getBitBuffer()
const
1497 throw CppRuntimeException(
"Reflectable is an array '") << getTypeInfo().getSchemaName() <<
"[]'!";
1500 template <
typename ALLOC>
1501 int64_t ReflectableDataArrayBase<ALLOC>::toInt()
const
1503 throw CppRuntimeException(
"Reflectable is an array '") << getTypeInfo().getSchemaName() <<
"[]'!";
1506 template <
typename ALLOC>
1507 uint64_t ReflectableDataArrayBase<ALLOC>::toUInt()
const
1509 throw CppRuntimeException(
"Reflectable is an array '") << getTypeInfo().getSchemaName() <<
"[]'!";
1512 template <
typename ALLOC>
1513 double ReflectableDataArrayBase<ALLOC>::toDouble()
const
1515 throw CppRuntimeException(
"Reflectable is an array '") << getTypeInfo().getSchemaName() <<
"[]'!";
1518 template <
typename ALLOC>
1521 throw CppRuntimeException(
"Reflectable is an array '") << getTypeInfo().getSchemaName() <<
"[]'!";
1524 template <
typename ALLOC>
1525 void ReflectableDataConstArrayBase<ALLOC>::resize(
size_t)
1527 throw CppRuntimeException(
"Reflectable '") << getTypeInfo().getSchemaName() <<
"' is a constant array!";
1530 template <
typename ALLOC>
1531 IBasicReflectableDataPtr<ALLOC> ReflectableDataConstArrayBase<ALLOC>::at(
size_t)
1533 throw CppRuntimeException(
"Reflectable '") << getTypeInfo().getSchemaName() <<
"' is a constant array!";
1536 template <
typename ALLOC>
1537 IBasicReflectableDataPtr<ALLOC> ReflectableDataConstArrayBase<ALLOC>::operator[](
size_t)
1539 throw CppRuntimeException(
"Reflectable '") << getTypeInfo().getSchemaName() <<
"' is a constant array!";
1542 template <
typename ALLOC>
1543 void ReflectableDataConstArrayBase<ALLOC>::setAt(
const BasicAny<ALLOC>&,
size_t)
1545 throw CppRuntimeException(
"Reflectable '") << getTypeInfo().getSchemaName() <<
"' is a constant array!";
1548 template <
typename ALLOC>
1549 void ReflectableDataConstArrayBase<ALLOC>::append(
const BasicAny<ALLOC>&)
1551 throw CppRuntimeException(
"Reflectable '") << getTypeInfo().getSchemaName() <<
"' is a constant array!";
1554 template <
typename ALLOC>
1555 BasicAny<ALLOC> ReflectableDataConstArrayBase<ALLOC>::getAnyValue(
const ALLOC&)
1557 throw CppRuntimeException(
"Reflectable '") << getTypeInfo().getSchemaName() <<
"' is a constant array!";
1562 template <
typename T,
typename VECTOR_ALLOC,
typename ALLOC = std::allocator<u
int8_t>>
1564 const std::vector<T, VECTOR_ALLOC>& array,
const ALLOC& allocator = ALLOC())
1566 return std::allocate_shared<detail::ReflectableDataConstArray<std::vector<T, VECTOR_ALLOC>, ALLOC>>(
1570 template <
typename T,
typename VECTOR_ALLOC,
typename ALLOC = std::allocator<u
int8_t>>
1572 std::vector<T, VECTOR_ALLOC>& array,
const ALLOC& allocator = ALLOC())
1574 return std::allocate_shared<detail::ReflectableDataArray<std::vector<T, VECTOR_ALLOC>, ALLOC>>(
typename IIntrospectableData< IBasicReflectableData< ALLOC >, ALLOC >::ConstPtr ConstPtr
virtual BasicAny< ALLOC > getAnyValue(const ALLOC &allocator)=0
virtual Ptr createField(std::string_view name)=0
virtual Ptr find(std::string_view path)=0
virtual void append(const BasicAny< ALLOC > &value)=0
virtual Ptr at(size_t index)=0
virtual void setAt(const BasicAny< ALLOC > &value, size_t index)=0
virtual void resize(size_t size)=0
virtual Ptr operator[](size_t index)=0
std::shared_ptr< IBasicReflectableData > Ptr
virtual void setField(std::string_view name, const BasicAny< ALLOC > &value)=0
virtual Ptr getField(std::string_view name)=0
virtual Span< const BasicFieldInfo< ALLOC > > getFields() const =0
virtual SchemaType getSchemaType() const =0
virtual std::string_view getSchemaName() const =0
virtual std::string_view getStringView() const=0
virtual int32_t getInt32() const=0
virtual uint8_t getUInt8() const=0
virtual float getFloat() const=0
virtual int8_t getInt8() const=0
virtual bool isArray() const=0
virtual double getDouble() const=0
virtual size_t size() const=0
virtual const IBasicTypeInfo< std::allocator< uint8_t > > & getTypeInfo() const=0
virtual BytesView getBytes() const=0
virtual int64_t toInt() const=0
virtual uint64_t toUInt() const=0
virtual uint32_t getUInt32() const=0
virtual int64_t getInt64() const=0
virtual uint64_t getUInt64() const=0
virtual int16_t getInt16() const=0
virtual uint16_t getUInt16() const=0
virtual bool getBool() const=0
virtual const BasicBitBuffer< std::allocator< uint8_t > > & getBitBuffer() const=0
virtual std::string_view getChoice() const =0
virtual double toDouble() const=0
typename IBasicReflectableData< ALLOC >::ConstPtr IBasicReflectableDataConstPtr
Span< const uint8_t > BytesView
detail::VarIntWrapper< uint64_t, detail::VarIntType::VAR > VarUInt
typename IBasicReflectableData< ALLOC >::Ptr IBasicReflectableDataPtr
std::basic_string< char, std::char_traits< char >, ALLOC > BasicString
detail::FloatWrapper< float, detail::FloatType::FLOAT16 > Float16
IBasicReflectableDataConstPtr< ALLOC > reflectable(const T &value, const ALLOC &allocator=ALLOC())
std::vector< uint8_t, ALLOC > BasicBytes
detail::FloatWrapper< float, detail::FloatType::FLOAT32 > Float32
IBasicReflectableDataPtr< ALLOC > reflectableArray(std::vector< T, VECTOR_ALLOC > &array, const ALLOC &allocator=ALLOC())
detail::VarIntWrapper< int32_t, detail::VarIntType::VAR32 > VarInt32
typename std::allocator_traits< ALLOC >::template rebind_alloc< T > RebindAlloc
detail::VarIntWrapper< uint16_t, detail::VarIntType::VAR16 > VarUInt16
detail::VarIntWrapper< uint64_t, detail::VarIntType::VAR64 > VarUInt64
const IBasicTypeInfo< ALLOC > & typeInfo()
BasicString< RebindAlloc< ALLOC, char > > toString(T value, const ALLOC &allocator=ALLOC())
detail::VarIntWrapper< int16_t, detail::VarIntType::VAR16 > VarInt16
detail::FloatWrapper< double, detail::FloatType::FLOAT64 > Float64
detail::VarIntWrapper< uint32_t, detail::VarIntType::VAR32 > VarUInt32
detail::VarIntWrapper< int64_t, detail::VarIntType::VAR > VarInt
detail::VarIntWrapper< int64_t, detail::VarIntType::VAR64 > VarInt64
detail::VarIntWrapper< uint32_t, detail::VarIntType::VARSIZE > VarSize