Zserio C++17 runtime library  0.5.0
Built for Zserio 2.17.0
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 
6 namespace zserio
7 {
8 namespace detail
9 {
10 
14 template <typename I, typename ALLOC>
15 class IntrospectableDataBase : public I
16 {
17 public:
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 
89 private:
90  const IBasicTypeInfo<ALLOC>& m_typeInfo;
91 };
92 
93 // implementation of base classes methods
94 
95 template <typename I, typename ALLOC>
96 IntrospectableDataBase<I, ALLOC>::IntrospectableDataBase(const IBasicTypeInfo<ALLOC>& typeInfo) :
97  m_typeInfo(typeInfo)
98 {}
99 
100 template <typename I, typename ALLOC>
101 const IBasicTypeInfo<ALLOC>& IntrospectableDataBase<I, ALLOC>::getTypeInfo() const
102 {
103  return m_typeInfo;
104 }
105 
106 template <typename I, typename ALLOC>
107 bool IntrospectableDataBase<I, ALLOC>::isArray() const
108 {
109  return false;
110 }
111 
112 template <typename I, typename ALLOC>
113 typename 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 
119 template <typename I, typename ALLOC>
120 std::string_view IntrospectableDataBase<I, ALLOC>::getChoice() const
121 {
122  throw CppRuntimeException("Type '") << m_typeInfo.getSchemaName() << "' is neither choice nor union!";
123 }
124 
125 template <typename I, typename ALLOC>
126 size_t IntrospectableDataBase<I, ALLOC>::size() const
127 {
128  throw CppRuntimeException("Type '") << m_typeInfo.getSchemaName() << "' is not an array!";
129 }
130 
131 template <typename I, typename ALLOC>
132 typename 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 
137 template <typename I, typename ALLOC>
138 typename 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 
143 template <typename I, typename ALLOC>
144 BasicAny<ALLOC> IntrospectableDataBase<I, ALLOC>::getAnyValue(const ALLOC&) const
145 {
146  throw CppRuntimeException("Type '") << m_typeInfo.getSchemaName() << "' is not implemented!";
147 }
148 
149 template <typename I, typename ALLOC>
150 BasicAny<ALLOC> IntrospectableDataBase<I, ALLOC>::getAnyValue() const
151 {
152  return getAnyValue(ALLOC());
153 }
154 
155 template <typename I, typename ALLOC>
156 bool IntrospectableDataBase<I, ALLOC>::getBool() const
157 {
158  throw CppRuntimeException("'") << m_typeInfo.getSchemaName() << "' is not boolean type!";
159 }
160 
161 template <typename I, typename ALLOC>
162 int8_t IntrospectableDataBase<I, ALLOC>::getInt8() const
163 {
164  throw CppRuntimeException("'") << m_typeInfo.getSchemaName() << "' is not int8 type!";
165 }
166 
167 template <typename I, typename ALLOC>
168 int16_t IntrospectableDataBase<I, ALLOC>::getInt16() const
169 {
170  throw CppRuntimeException("'") << m_typeInfo.getSchemaName() << "' is not int16 type!";
171 }
172 
173 template <typename I, typename ALLOC>
174 int32_t IntrospectableDataBase<I, ALLOC>::getInt32() const
175 {
176  throw CppRuntimeException("'") << m_typeInfo.getSchemaName() << "' is not int32 type!";
177 }
178 
179 template <typename I, typename ALLOC>
180 int64_t IntrospectableDataBase<I, ALLOC>::getInt64() const
181 {
182  throw CppRuntimeException("'") << m_typeInfo.getSchemaName() << "' is not int64 type!";
183 }
184 
185 template <typename I, typename ALLOC>
186 uint8_t IntrospectableDataBase<I, ALLOC>::getUInt8() const
187 {
188  throw CppRuntimeException("'") << m_typeInfo.getSchemaName() << "' is not uint8 type!";
189 }
190 
191 template <typename I, typename ALLOC>
192 uint16_t IntrospectableDataBase<I, ALLOC>::getUInt16() const
193 {
194  throw CppRuntimeException("'") << m_typeInfo.getSchemaName() << "' is not uint16 type!";
195 }
196 
197 template <typename I, typename ALLOC>
198 uint32_t IntrospectableDataBase<I, ALLOC>::getUInt32() const
199 {
200  throw CppRuntimeException("'") << m_typeInfo.getSchemaName() << "' is not uint32 type!";
201 }
202 
203 template <typename I, typename ALLOC>
204 uint64_t IntrospectableDataBase<I, ALLOC>::getUInt64() const
205 {
206  throw CppRuntimeException("'") << m_typeInfo.getSchemaName() << "' is not uint64 type!";
207 }
208 
209 template <typename I, typename ALLOC>
210 float IntrospectableDataBase<I, ALLOC>::getFloat() const
211 {
212  throw CppRuntimeException("'") << m_typeInfo.getSchemaName() << "' is not float type!";
213 }
214 
215 template <typename I, typename ALLOC>
216 double IntrospectableDataBase<I, ALLOC>::getDouble() const
217 {
218  throw CppRuntimeException("'") << m_typeInfo.getSchemaName() << "' is not double type!";
219 }
220 
221 template <typename I, typename ALLOC>
222 BytesView IntrospectableDataBase<I, ALLOC>::getBytes() const
223 {
224  throw CppRuntimeException("'") << m_typeInfo.getSchemaName() << "' is not bytes type!";
225 }
226 
227 template <typename I, typename ALLOC>
228 std::string_view IntrospectableDataBase<I, ALLOC>::getStringView() const
229 {
230  throw CppRuntimeException("'") << m_typeInfo.getSchemaName() << "' is not string type!";
231 }
232 
233 template <typename I, typename ALLOC>
234 const BasicBitBuffer<ALLOC>& IntrospectableDataBase<I, ALLOC>::getBitBuffer() const
235 {
236  throw CppRuntimeException("'") << m_typeInfo.getSchemaName() << "' is not an extern type!";
237 }
238 
239 template <typename I, typename ALLOC>
240 int64_t IntrospectableDataBase<I, ALLOC>::toInt() const
241 {
242  throw CppRuntimeException("Conversion from '")
243  << m_typeInfo.getSchemaName() << "' to signed integer is not available!";
244 }
245 
246 template <typename I, typename ALLOC>
247 uint64_t IntrospectableDataBase<I, ALLOC>::toUInt() const
248 {
249  throw CppRuntimeException("Conversion from '")
250  << m_typeInfo.getSchemaName() << "' to unsigned integer is not available!";
251 }
252 
253 template <typename I, typename ALLOC>
254 double IntrospectableDataBase<I, ALLOC>::toDouble() const
255 {
256  throw CppRuntimeException("Conversion from '")
257  << m_typeInfo.getSchemaName() << "' to double is not available!";
258 }
259 
260 template <typename I, typename ALLOC>
261 BasicString<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 
267 template <typename I, typename ALLOC>
268 BasicString<RebindAlloc<ALLOC, char>> IntrospectableDataBase<I, ALLOC>::toString() const
269 {
270  return toString(ALLOC());
271 }
272 
273 template <typename I, typename ALLOC>
274 typename 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 
280 template <typename I, typename ALLOC>
281 typename 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())