Coverage Report

Created: 2025-07-31 16:33

src/zserio/IntrospectableDataBase.h
Line
Count
Source (jump to first uncovered line)
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
11
/**
12
 * Base class for all introspectable data implementations.
13
 */
14
template <typename I, typename ALLOC>
15
class IntrospectableDataBase : public I
16
{
17
public:
18
    /** Shared pointer to the constant introspectable interface. */
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
27
    /**
28
     * Constructor.
29
     *
30
     * \param typeInfo Type info of the object.
31
     */
32
    explicit IntrospectableDataBase(const IBasicTypeInfo<ALLOC>& typeInfo);
33
34
    /** Destructor. */
35
1.68k
    ~IntrospectableDataBase() override = default;
36
37
    /**
38
     * Copying and moving is disallowed!
39
     * \{
40
     */
41
    IntrospectableDataBase(const IntrospectableDataBase&) = delete;
42
    IntrospectableDataBase& operator=(const IntrospectableDataBase&) = delete;
43
44
    IntrospectableDataBase(const IntrospectableDataBase&&) = delete;
45
    IntrospectableDataBase& operator=(const IntrospectableDataBase&&) = delete;
46
    /**
47
     * \}
48
     */
49
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
1.68k
        m_typeInfo(typeInfo)
98
1.68k
{}
99
100
template <typename I, typename ALLOC>
101
const IBasicTypeInfo<ALLOC>& IntrospectableDataBase<I, ALLOC>::getTypeInfo() const
102
6.22k
{
103
6.22k
    return m_typeInfo;
104
6.22k
}
105
106
template <typename I, typename ALLOC>
107
bool IntrospectableDataBase<I, ALLOC>::isArray() const
108
705
{
109
705
    return false;
110
705
}
111
112
template <typename I, typename ALLOC>
113
typename IntrospectableDataBase<I, ALLOC>::ConstPtr IntrospectableDataBase<I, ALLOC>::getField(
114
        std::string_view) const
115
332
{
116
332
    throw CppRuntimeException("Type '") << m_typeInfo.getSchemaName() << "' has no fields to get!";
117
332
}
118
119
template <typename I, typename ALLOC>
120
std::string_view IntrospectableDataBase<I, ALLOC>::getChoice() const
121
158
{
122
158
    throw CppRuntimeException("Type '") << m_typeInfo.getSchemaName() << "' is neither choice nor union!";
123
158
}
124
125
template <typename I, typename ALLOC>
126
size_t IntrospectableDataBase<I, ALLOC>::size() const
127
468
{
128
468
    throw CppRuntimeException("Type '") << m_typeInfo.getSchemaName() << "' is not an array!";
129
468
}
130
131
template <typename I, typename ALLOC>
132
typename IntrospectableDataBase<I, ALLOC>::ConstPtr IntrospectableDataBase<I, ALLOC>::at(size_t) const
133
467
{
134
467
    throw CppRuntimeException("Type '") << m_typeInfo.getSchemaName() << "' is not an array!";
135
467
}
136
137
template <typename I, typename ALLOC>
138
typename IntrospectableDataBase<I, ALLOC>::ConstPtr IntrospectableDataBase<I, ALLOC>::operator[](size_t) const
139
467
{
140
467
    throw CppRuntimeException("Type '") << m_typeInfo.getSchemaName() << "' is not an array!";
141
467
}
142
143
template <typename I, typename ALLOC>
144
BasicAny<ALLOC> IntrospectableDataBase<I, ALLOC>::getAnyValue(const ALLOC&) const
145
1
{
146
1
    throw CppRuntimeException("Type '") << m_typeInfo.getSchemaName() << "' is not implemented!";
147
1
}
148
149
template <typename I, typename ALLOC>
150
BasicAny<ALLOC> IntrospectableDataBase<I, ALLOC>::getAnyValue() const
151
408
{
152
408
    return getAnyValue(ALLOC());
153
408
}
154
155
template <typename I, typename ALLOC>
156
bool IntrospectableDataBase<I, ALLOC>::getBool() const
157
454
{
158
454
    throw CppRuntimeException("'") << m_typeInfo.getSchemaName() << "' is not boolean type!";
159
454
}
160
161
template <typename I, typename ALLOC>
162
int8_t IntrospectableDataBase<I, ALLOC>::getInt8() const
163
392
{
164
392
    throw CppRuntimeException("'") << m_typeInfo.getSchemaName() << "' is not int8 type!";
165
392
}
166
167
template <typename I, typename ALLOC>
168
int16_t IntrospectableDataBase<I, ALLOC>::getInt16() const
169
436
{
170
436
    throw CppRuntimeException("'") << m_typeInfo.getSchemaName() << "' is not int16 type!";
171
436
}
172
173
template <typename I, typename ALLOC>
174
int32_t IntrospectableDataBase<I, ALLOC>::getInt32() const
175
436
{
176
436
    throw CppRuntimeException("'") << m_typeInfo.getSchemaName() << "' is not int32 type!";
177
436
}
178
179
template <typename I, typename ALLOC>
180
int64_t IntrospectableDataBase<I, ALLOC>::getInt64() const
181
422
{
182
422
    throw CppRuntimeException("'") << m_typeInfo.getSchemaName() << "' is not int64 type!";
183
422
}
184
185
template <typename I, typename ALLOC>
186
uint8_t IntrospectableDataBase<I, ALLOC>::getUInt8() const
187
404
{
188
404
    throw CppRuntimeException("'") << m_typeInfo.getSchemaName() << "' is not uint8 type!";
189
404
}
190
191
template <typename I, typename ALLOC>
192
uint16_t IntrospectableDataBase<I, ALLOC>::getUInt16() const
193
436
{
194
436
    throw CppRuntimeException("'") << m_typeInfo.getSchemaName() << "' is not uint16 type!";
195
436
}
196
197
template <typename I, typename ALLOC>
198
uint32_t IntrospectableDataBase<I, ALLOC>::getUInt32() const
199
422
{
200
422
    throw CppRuntimeException("'") << m_typeInfo.getSchemaName() << "' is not uint32 type!";
201
422
}
202
203
template <typename I, typename ALLOC>
204
uint64_t IntrospectableDataBase<I, ALLOC>::getUInt64() const
205
422
{
206
422
    throw CppRuntimeException("'") << m_typeInfo.getSchemaName() << "' is not uint64 type!";
207
422
}
208
209
template <typename I, typename ALLOC>
210
float IntrospectableDataBase<I, ALLOC>::getFloat() const
211
449
{
212
449
    throw CppRuntimeException("'") << m_typeInfo.getSchemaName() << "' is not float type!";
213
449
}
214
215
template <typename I, typename ALLOC>
216
double IntrospectableDataBase<I, ALLOC>::getDouble() const
217
457
{
218
457
    throw CppRuntimeException("'") << m_typeInfo.getSchemaName() << "' is not double type!";
219
457
}
220
221
template <typename I, typename ALLOC>
222
BytesView IntrospectableDataBase<I, ALLOC>::getBytes() const
223
458
{
224
458
    throw CppRuntimeException("'") << m_typeInfo.getSchemaName() << "' is not bytes type!";
225
458
}
226
227
template <typename I, typename ALLOC>
228
std::string_view IntrospectableDataBase<I, ALLOC>::getStringView() const
229
453
{
230
453
    throw CppRuntimeException("'") << m_typeInfo.getSchemaName() << "' is not string type!";
231
453
}
232
233
template <typename I, typename ALLOC>
234
const BasicBitBuffer<ALLOC>& IntrospectableDataBase<I, ALLOC>::getBitBuffer() const
235
458
{
236
458
    throw CppRuntimeException("'") << m_typeInfo.getSchemaName() << "' is not an extern type!";
237
458
}
238
239
template <typename I, typename ALLOC>
240
int64_t IntrospectableDataBase<I, ALLOC>::toInt() const
241
282
{
242
282
    throw CppRuntimeException("Conversion from '")
243
282
            << m_typeInfo.getSchemaName() << "' to signed integer is not available!";
244
282
}
245
246
template <typename I, typename ALLOC>
247
uint64_t IntrospectableDataBase<I, ALLOC>::toUInt() const
248
266
{
249
266
    throw CppRuntimeException("Conversion from '")
250
266
            << m_typeInfo.getSchemaName() << "' to unsigned integer is not available!";
251
266
}
252
253
template <typename I, typename ALLOC>
254
double IntrospectableDataBase<I, ALLOC>::toDouble() const
255
50
{
256
50
    throw CppRuntimeException("Conversion from '")
257
50
            << m_typeInfo.getSchemaName() << "' to double is not available!";
258
50
}
259
260
template <typename I, typename ALLOC>
261
BasicString<RebindAlloc<ALLOC, char>> IntrospectableDataBase<I, ALLOC>::toString(const ALLOC&) const
262
65
{
263
65
    throw CppRuntimeException("Conversion from '")
264
65
            << m_typeInfo.getSchemaName() << "' to string is not available!";
265
65
}
266
267
template <typename I, typename ALLOC>
268
BasicString<RebindAlloc<ALLOC, char>> IntrospectableDataBase<I, ALLOC>::toString() const
269
581
{
270
581
    return toString(ALLOC());
271
581
}
272
273
template <typename I, typename ALLOC>
274
typename IntrospectableDataBase<I, ALLOC>::ConstPtr IntrospectableDataBase<I, ALLOC>::find(
275
        std::string_view) const
276
0
{
277
0
    throw CppRuntimeException("Find is not available for '") << m_typeInfo.getSchemaName() << "'!";
278
0
}
279
280
template <typename I, typename ALLOC>
281
typename IntrospectableDataBase<I, ALLOC>::ConstPtr IntrospectableDataBase<I, ALLOC>::operator[](
282
        std::string_view path) const
283
414
{
284
414
    return find(path);
285
414
}
286
287
} // namespace detail
288
} // namespace zserio
289
290
#endif // ZSERIO_INTROSPECTABLE_DATA_BASE_H_INC