Zserio C++17 runtime library 1.3.0
Built for Zserio 2.20.0
Loading...
Searching...
No Matches
IntrospectableDataBase.h
Go to the documentation of this file.
1#ifndef ZSERIO_INTROSPECTABLE_DATA_BASE_H_INC
2#define ZSERIO_INTROSPECTABLE_DATA_BASE_H_INC
3
4#include "zserio/TypeInfo.h"
5
6namespace zserio
7{
8namespace detail
9{
10
14template <typename I, typename ALLOC>
15class IntrospectableDataBase : public I
16{
17public:
19 using ConstPtr = typename I::ConstPtr;
20
21 using I::at;
22 using I::getField;
23 using I::operator[];
24 using I::find;
25 using I::getAnyValue;
26
32 explicit IntrospectableDataBase(const IBasicTypeInfo<ALLOC>& typeInfo);
33
35 ~IntrospectableDataBase() override = default;
36
41 IntrospectableDataBase(const IntrospectableDataBase&) = delete;
42 IntrospectableDataBase& operator=(const IntrospectableDataBase&) = delete;
43
44 IntrospectableDataBase(const IntrospectableDataBase&&) = delete;
45 IntrospectableDataBase& operator=(const IntrospectableDataBase&&) = delete;
50 const IBasicTypeInfo<ALLOC>& getTypeInfo() const override;
51 bool isArray() const override;
52
53 ConstPtr getField(std::string_view name) const override;
54 std::string_view getChoice() const override;
55
56 size_t size() const override;
57 ConstPtr at(size_t index) const override;
58 ConstPtr operator[](size_t index) const override;
59
60 BasicAny<ALLOC> getAnyValue(const ALLOC& allocator) const override;
61 BasicAny<ALLOC> getAnyValue() const override;
62
63 // exact checked getters
64 bool getBool() const override;
65 int8_t getInt8() const override;
66 int16_t getInt16() const override;
67 int32_t getInt32() const override;
68 int64_t getInt64() const override;
69 uint8_t getUInt8() const override;
70 uint16_t getUInt16() const override;
71 uint32_t getUInt32() const override;
72 uint64_t getUInt64() const override;
73 float getFloat() const override;
74 double getDouble() const override;
75 BytesView getBytes() const override;
76 std::string_view getStringView() const override;
77 const BasicBitBuffer<ALLOC>& getBitBuffer() const override;
78
79 // convenience conversions
80 int64_t toInt() const override;
81 uint64_t toUInt() const override;
82 double toDouble() const override;
83 BasicString<RebindAlloc<ALLOC, char>> toString(const ALLOC& allocator) const override;
84 BasicString<RebindAlloc<ALLOC, char>> toString() const override;
85
86 ConstPtr find(std::string_view path) const override;
87 ConstPtr operator[](std::string_view path) const override;
88
89private:
90 const IBasicTypeInfo<ALLOC>& m_typeInfo;
91};
92
93// implementation of base classes methods
94
95template <typename I, typename ALLOC>
96IntrospectableDataBase<I, ALLOC>::IntrospectableDataBase(const IBasicTypeInfo<ALLOC>& typeInfo) :
97 m_typeInfo(typeInfo)
98{}
99
100template <typename I, typename ALLOC>
101const IBasicTypeInfo<ALLOC>& IntrospectableDataBase<I, ALLOC>::getTypeInfo() const
102{
103 return m_typeInfo;
104}
105
106template <typename I, typename ALLOC>
107bool IntrospectableDataBase<I, ALLOC>::isArray() const
108{
109 return false;
110}
111
112template <typename I, typename ALLOC>
113typename IntrospectableDataBase<I, ALLOC>::ConstPtr IntrospectableDataBase<I, ALLOC>::getField(
114 std::string_view) const
115{
116 throw CppRuntimeException("Type '") << m_typeInfo.getSchemaName() << "' has no fields to get!";
117}
118
119template <typename I, typename ALLOC>
120std::string_view IntrospectableDataBase<I, ALLOC>::getChoice() const
121{
122 throw CppRuntimeException("Type '") << m_typeInfo.getSchemaName() << "' is neither choice nor union!";
123}
124
125template <typename I, typename ALLOC>
126size_t IntrospectableDataBase<I, ALLOC>::size() const
127{
128 throw CppRuntimeException("Type '") << m_typeInfo.getSchemaName() << "' is not an array!";
129}
130
131template <typename I, typename ALLOC>
132typename IntrospectableDataBase<I, ALLOC>::ConstPtr IntrospectableDataBase<I, ALLOC>::at(size_t) const
133{
134 throw CppRuntimeException("Type '") << m_typeInfo.getSchemaName() << "' is not an array!";
135}
136
137template <typename I, typename ALLOC>
138typename IntrospectableDataBase<I, ALLOC>::ConstPtr IntrospectableDataBase<I, ALLOC>::operator[](size_t) const
139{
140 throw CppRuntimeException("Type '") << m_typeInfo.getSchemaName() << "' is not an array!";
141}
142
143template <typename I, typename ALLOC>
144BasicAny<ALLOC> IntrospectableDataBase<I, ALLOC>::getAnyValue(const ALLOC&) const
145{
146 throw CppRuntimeException("Type '") << m_typeInfo.getSchemaName() << "' is not implemented!";
147}
148
149template <typename I, typename ALLOC>
150BasicAny<ALLOC> IntrospectableDataBase<I, ALLOC>::getAnyValue() const
151{
152 return getAnyValue(ALLOC());
153}
154
155template <typename I, typename ALLOC>
156bool IntrospectableDataBase<I, ALLOC>::getBool() const
157{
158 throw CppRuntimeException("'") << m_typeInfo.getSchemaName() << "' is not boolean type!";
159}
160
161template <typename I, typename ALLOC>
162int8_t IntrospectableDataBase<I, ALLOC>::getInt8() const
163{
164 throw CppRuntimeException("'") << m_typeInfo.getSchemaName() << "' is not int8 type!";
165}
166
167template <typename I, typename ALLOC>
168int16_t IntrospectableDataBase<I, ALLOC>::getInt16() const
169{
170 throw CppRuntimeException("'") << m_typeInfo.getSchemaName() << "' is not int16 type!";
171}
172
173template <typename I, typename ALLOC>
174int32_t IntrospectableDataBase<I, ALLOC>::getInt32() const
175{
176 throw CppRuntimeException("'") << m_typeInfo.getSchemaName() << "' is not int32 type!";
177}
178
179template <typename I, typename ALLOC>
180int64_t IntrospectableDataBase<I, ALLOC>::getInt64() const
181{
182 throw CppRuntimeException("'") << m_typeInfo.getSchemaName() << "' is not int64 type!";
183}
184
185template <typename I, typename ALLOC>
186uint8_t IntrospectableDataBase<I, ALLOC>::getUInt8() const
187{
188 throw CppRuntimeException("'") << m_typeInfo.getSchemaName() << "' is not uint8 type!";
189}
190
191template <typename I, typename ALLOC>
192uint16_t IntrospectableDataBase<I, ALLOC>::getUInt16() const
193{
194 throw CppRuntimeException("'") << m_typeInfo.getSchemaName() << "' is not uint16 type!";
195}
196
197template <typename I, typename ALLOC>
198uint32_t IntrospectableDataBase<I, ALLOC>::getUInt32() const
199{
200 throw CppRuntimeException("'") << m_typeInfo.getSchemaName() << "' is not uint32 type!";
201}
202
203template <typename I, typename ALLOC>
204uint64_t IntrospectableDataBase<I, ALLOC>::getUInt64() const
205{
206 throw CppRuntimeException("'") << m_typeInfo.getSchemaName() << "' is not uint64 type!";
207}
208
209template <typename I, typename ALLOC>
210float IntrospectableDataBase<I, ALLOC>::getFloat() const
211{
212 throw CppRuntimeException("'") << m_typeInfo.getSchemaName() << "' is not float type!";
213}
214
215template <typename I, typename ALLOC>
216double IntrospectableDataBase<I, ALLOC>::getDouble() const
217{
218 throw CppRuntimeException("'") << m_typeInfo.getSchemaName() << "' is not double type!";
219}
220
221template <typename I, typename ALLOC>
222BytesView IntrospectableDataBase<I, ALLOC>::getBytes() const
223{
224 throw CppRuntimeException("'") << m_typeInfo.getSchemaName() << "' is not bytes type!";
225}
226
227template <typename I, typename ALLOC>
228std::string_view IntrospectableDataBase<I, ALLOC>::getStringView() const
229{
230 throw CppRuntimeException("'") << m_typeInfo.getSchemaName() << "' is not string type!";
231}
232
233template <typename I, typename ALLOC>
234const BasicBitBuffer<ALLOC>& IntrospectableDataBase<I, ALLOC>::getBitBuffer() const
235{
236 throw CppRuntimeException("'") << m_typeInfo.getSchemaName() << "' is not an extern type!";
237}
238
239template <typename I, typename ALLOC>
240int64_t IntrospectableDataBase<I, ALLOC>::toInt() const
241{
242 throw CppRuntimeException("Conversion from '")
243 << m_typeInfo.getSchemaName() << "' to signed integer is not available!";
244}
245
246template <typename I, typename ALLOC>
247uint64_t IntrospectableDataBase<I, ALLOC>::toUInt() const
248{
249 throw CppRuntimeException("Conversion from '")
250 << m_typeInfo.getSchemaName() << "' to unsigned integer is not available!";
251}
252
253template <typename I, typename ALLOC>
254double IntrospectableDataBase<I, ALLOC>::toDouble() const
255{
256 throw CppRuntimeException("Conversion from '")
257 << m_typeInfo.getSchemaName() << "' to double is not available!";
258}
259
260template <typename I, typename ALLOC>
261BasicString<RebindAlloc<ALLOC, char>> IntrospectableDataBase<I, ALLOC>::toString(const ALLOC&) const
262{
263 throw CppRuntimeException("Conversion from '")
264 << m_typeInfo.getSchemaName() << "' to string is not available!";
265}
266
267template <typename I, typename ALLOC>
268BasicString<RebindAlloc<ALLOC, char>> IntrospectableDataBase<I, ALLOC>::toString() const
269{
270 return toString(ALLOC());
271}
272
273template <typename I, typename ALLOC>
274typename IntrospectableDataBase<I, ALLOC>::ConstPtr IntrospectableDataBase<I, ALLOC>::find(
275 std::string_view) const
276{
277 throw CppRuntimeException("Find is not available for '") << m_typeInfo.getSchemaName() << "'!";
278}
279
280template <typename I, typename ALLOC>
281typename IntrospectableDataBase<I, ALLOC>::ConstPtr IntrospectableDataBase<I, ALLOC>::operator[](
282 std::string_view path) const
283{
284 return find(path);
285}
286
287} // namespace detail
288} // namespace zserio
289
290#endif // ZSERIO_INTROSPECTABLE_DATA_BASE_H_INC
Span< const uint8_t > BytesView
Definition Bytes.h:30
const IBasicTypeInfo< ALLOC > & typeInfo()
Definition ITypeInfo.h:668
BasicString< RebindAlloc< ALLOC, char > > toString(T value, const ALLOC &allocator=ALLOC())