Coverage Report

Created: 2026-10-19 14:15

src/zserio/ITypeInfo.h
Line
Count
Source
1
#ifndef ZSERIO_I_TYPE_INFO_INC_H
2
#define ZSERIO_I_TYPE_INFO_INC_H
3
4
#include "zserio/IReflectable.h"
5
#include "zserio/Span.h"
6
#include "zserio/StringView.h"
7
#include "zserio/Types.h"
8
9
namespace zserio
10
{
11
12
/** Enumeration which specifies C++ type used in type information. */
13
enum class CppType
14
{
15
    BOOL, /**< C++ bool type */
16
    INT8, /**< C++ int8_t type */
17
    INT16, /**< C++ int16_t type */
18
    INT32, /**< C++ int32_t type */
19
    INT64, /**< C++ int64_t type */
20
    UINT8, /**< C++ uint8_t type */
21
    UINT16, /**< C++ uint16_t type */
22
    UINT32, /**< C++ int32_t type */
23
    UINT64, /**< C++ int64_t type */
24
    FLOAT, /**< C++ float type */
25
    DOUBLE, /**< C++ double type */
26
    BYTES, /**< C++ bytes type (mapped as std::vector<uint8_t>) */
27
    STRING, /**< C++ string type */
28
    BIT_BUFFER, /**< C++ zserio::BitBuffer type */
29
    ENUM, /**< C++ enumeration generated from zserio enumeration type */
30
    BITMASK, /**< C++ object generated from zserio bitmask type */
31
    STRUCT, /**< C++ object generated from zserio structure type */
32
    CHOICE, /**< C++ object generated from zserio choice type */
33
    UNION, /**< C++ object generated from zserio union type */
34
    SQL_TABLE, /**< C++ object generated from zserio SQL table type */
35
    SQL_DATABASE, /**< C++ object generated from zserio SQL database type */
36
    SERVICE, /**< C++ object generated from zserio service type */
37
    PUBSUB /**< C++ object generated from zserio pubsub type */
38
};
39
40
/** Enumeration which specifies zserio type used in type information. */
41
enum class SchemaType
42
{
43
    BOOL, /**< zserio bool type */
44
    INT8, /**< zserio int8 type */
45
    INT16, /**< zserio int16 type */
46
    INT32, /**< zserio int32 type */
47
    INT64, /**< zserio int64 type */
48
    UINT8, /**< zserio uint8 type */
49
    UINT16, /**< zserio uint16 type */
50
    UINT32, /**< zserio uint32 type */
51
    UINT64, /**< zserio uint64 type */
52
    VARINT16, /**< zserio varint16 type */
53
    VARINT32, /**< zserio varint32 type */
54
    VARINT64, /**< zserio varint64 type */
55
    VARINT, /**< zserio varint type */
56
    VARUINT16, /**< zserio varuint16 type */
57
    VARUINT32, /**< zserio varuint32 type */
58
    VARUINT64, /**< zserio varuint64 type */
59
    VARUINT, /**< zserio varuint type */
60
    VARSIZE, /**< zserio varsize type */
61
    FIXED_SIGNED_BITFIELD, /**< zserio fixed signed bitfield type */
62
    FIXED_UNSIGNED_BITFIELD, /**< zserio fixed unsigned bitfield type */
63
    DYNAMIC_SIGNED_BITFIELD, /**< zserio dynamic signed bitfield type */
64
    DYNAMIC_UNSIGNED_BITFIELD, /**< zserio dynamic unsigned bitfield type */
65
    FLOAT16, /**< zserio float16 type */
66
    FLOAT32, /**< zserio float32 type */
67
    FLOAT64, /**< zserio float64 type */
68
    BYTES, /**< zserio bytes type */
69
    STRING, /**< zserio string type */
70
    EXTERN, /**< zserio extern type */
71
    ENUM, /**< zserio enumeration type */
72
    BITMASK, /**< zserio bitmask type */
73
    STRUCT, /**< zserio structure type */
74
    CHOICE, /**< zserio choice type */
75
    UNION, /**< zserio union type */
76
    SQL_TABLE, /**< zserio SQL table type */
77
    SQL_DATABASE, /**< zserio SQL database type */
78
    SERVICE, /**< zserio service type */
79
    PUBSUB /**< zserio pubsub type */
80
};
81
82
// forward declarations
83
template <typename ALLOC>
84
struct BasicFieldInfo;
85
template <typename ALLOC>
86
struct BasicParameterInfo;
87
template <typename ALLOC>
88
struct BasicFunctionInfo;
89
template <typename ALLOC>
90
struct BasicCaseInfo;
91
template <typename ALLOC>
92
struct BasicColumnInfo;
93
template <typename ALLOC>
94
struct BasicTableInfo;
95
struct ItemInfo;
96
template <typename ALLOC>
97
struct BasicTemplateArgumentInfo;
98
template <typename ALLOC>
99
struct BasicMessageInfo;
100
template <typename ALLOC>
101
struct BasicMethodInfo;
102
103
/**
104
 * Type information interface which is returned from the generated zserio objects.
105
 *
106
 * This interface provides additional schema information of the corresponded zserio object, like schema
107
 * name, schema type, etc...
108
 *
109
 * Not all methods are implemented for all zserio objects. For example, the method getFields() is implemented
110
 * for compound types only. To check the zserio object type consider to use TypeInfoUtil helper methods.
111
 */
112
template <typename ALLOC = std::allocator<uint8_t>>
113
class IBasicTypeInfo
114
{
115
public:
116
    /**
117
     * Virtual destructor.
118
     */
119
316
    virtual ~IBasicTypeInfo() = default;
120
121
    /**
122
     * Gets the schema name.
123
     *
124
     * \return The zserio full name stored in schema.
125
     */
126
    virtual StringView getSchemaName() const = 0;
127
128
    /**
129
     * Gets the schema type.
130
     *
131
     * \return The zserio type stored in schema.
132
     */
133
    virtual SchemaType getSchemaType() const = 0;
134
135
    /**
136
     * Gets the C++ type.
137
     *
138
     * \return The C++ type to which zserio type is mapped.
139
     */
140
    virtual CppType getCppType() const = 0;
141
142
    // method for fixed size integral types
143
144
    /**
145
     * Gets the bit size of the fixed size integral schema type.
146
     *
147
     * \return The bit size of zserio type.
148
     *
149
     * \throw CppRuntimeException If the zserio type is not fixed size integral (e.g. varint).
150
     */
151
    virtual uint8_t getBitSize() const = 0;
152
153
    // methods for compound types
154
155
    /**
156
     * Gets the type information for compound type fields.
157
     *
158
     * \return Sequence of type information for fields.
159
     *
160
     * \throw CppRuntimeException If the zserio type is not compound type.
161
     */
162
    virtual Span<const BasicFieldInfo<ALLOC>> getFields() const = 0;
163
164
    /**
165
     * Gets the type information for compound type parameters.
166
     *
167
     * \return Sequence of type information for parameters.
168
     *
169
     * \throw CppRuntimeException If the zserio type is not compound type.
170
     */
171
    virtual Span<const BasicParameterInfo<ALLOC>> getParameters() const = 0;
172
173
    /**
174
     * Gets the type information for compound type functions.
175
     *
176
     * \return Sequence of type information for functions.
177
     *
178
     * \throw CppRuntimeException If the zserio type is not compound type.
179
     */
180
    virtual Span<const BasicFunctionInfo<ALLOC>> getFunctions() const = 0;
181
182
    // methods for choice type
183
184
    /**
185
     * Gets the selector for choice type.
186
     *
187
     * \return Selector expression of choice type.
188
     *
189
     * \throw CppRuntimeException If the zserio type is not choice type.
190
     */
191
    virtual StringView getSelector() const = 0;
192
193
    /**
194
     * Gets the type information for choice type cases.
195
     *
196
     * \return Sequence of type information for choice type cases.
197
     *
198
     * \throw CppRuntimeException If the zserio type is not choice type.
199
     */
200
    virtual Span<const BasicCaseInfo<ALLOC>> getCases() const = 0;
201
202
    // methods for enumeration and bitmask types
203
204
    /**
205
     * Gets the reference to type information of underlying zserio type.
206
     *
207
     * \return Reference to type information of underlying zserio type.
208
     *
209
     * \throw CppRuntimeException If the zserio type is not enumeration or bitmask type.
210
     */
211
    virtual const IBasicTypeInfo<ALLOC>& getUnderlyingType() const = 0;
212
213
    /**
214
     * Gets the reference to type information of underlying zserio type arguments.
215
     *
216
     * \return Underlying zserio type arguments.
217
     *
218
     * \throw CppRuntimeException If the zserio type is not enumeration or bitmask type.
219
     */
220
    virtual Span<const StringView> getUnderlyingTypeArguments() const = 0;
221
222
    /**
223
     * Gets the type information for enumeration type items.
224
     *
225
     * \return Sequence of type information for enumeration type items.
226
     *
227
     * \throw CppRuntimeException If the zserio type is not enumeration type.
228
     */
229
    virtual Span<const ItemInfo> getEnumItems() const = 0;
230
231
    /**
232
     * Gets the type information for bitmask type values.
233
     *
234
     * \return Sequence of type information for bitmask type values.
235
     *
236
     * \throw CppRuntimeException If the zserio type is not bitmask type.
237
     */
238
    virtual Span<const ItemInfo> getBitmaskValues() const = 0;
239
240
    // methods for SQL table types
241
242
    /**
243
     * Gets the type information for SQL table columns.
244
     *
245
     * \return Sequence of type information for SQL table columns.
246
     *
247
     * \throw CppRuntimeException If the zserio type is not SQL table type.
248
     */
249
    virtual Span<const BasicColumnInfo<ALLOC>> getColumns() const = 0;
250
251
    /**
252
     * Gets the SQL table constraint.
253
     *
254
     * \return The SQL table constraint.
255
     *
256
     * \throw CppRuntimeException If the zserio type is not SQL table type.
257
     */
258
    virtual StringView getSqlConstraint() const = 0;
259
260
    /**
261
     * Gets the SQL virtual table using specification.
262
     *
263
     * \return The SQL virtual table using specification.
264
     *
265
     * \throw CppRuntimeException If the zserio type is not SQL table type.
266
     */
267
    virtual StringView getVirtualTableUsing() const = 0;
268
269
    /**
270
     * Checks if SQL table is without row id table.
271
     *
272
     * \return true if SQL table is without row id table, otherwise false.
273
     *
274
     * \throw CppRuntimeException If the zserio type is not SQL table type.
275
     */
276
    virtual bool isWithoutRowId() const = 0;
277
278
    // method for SQL database type
279
280
    /**
281
     * Gets the type information for SQL database tables.
282
     *
283
     * \return Sequence of type information for SQL database tables.
284
     *
285
     * \throw CppRuntimeException If the zserio type is not SQL database type.
286
     */
287
    virtual Span<const BasicTableInfo<ALLOC>> getTables() const = 0;
288
289
    // methods for templatable types
290
291
    /**
292
     * Gets the full schema template name.
293
     *
294
     * \return The full schema template name.
295
     *
296
     * \throw CppRuntimeException If the zserio type is not templatable.
297
     */
298
    virtual StringView getTemplateName() const = 0;
299
300
    /**
301
     * Gets the type information for template arguments.
302
     *
303
     * \return Sequence of type information for template arguments.
304
     *
305
     * \throw CppRuntimeException If the zserio type is not templatable.
306
     */
307
    virtual Span<const BasicTemplateArgumentInfo<ALLOC>> getTemplateArguments() const = 0;
308
309
    // method for pubsub type
310
311
    /**
312
     * Gets the type information for pubsub messages.
313
     *
314
     * \return Sequence of type information for pubsub messages.
315
     *
316
     * \throw CppRuntimeException If the zserio type is not pubsub type.
317
     */
318
    virtual Span<const BasicMessageInfo<ALLOC>> getMessages() const = 0;
319
320
    // method for service type
321
322
    /**
323
     * Gets the type information for service methods.
324
     *
325
     * \return Sequence of type information for service methods.
326
     *
327
     * \throw CppRuntimeException If the zserio type is not service type.
328
     */
329
    virtual Span<const BasicMethodInfo<ALLOC>> getMethods() const = 0;
330
331
    /**
332
     * Creates new instance of the zserio compound type.
333
     *
334
     * \param allocator Allocator to use for allocation of new instance.
335
     *
336
     * \return New instance of zserio compound type.
337
     *
338
     * \throw CppRuntimeException If the zserio type is not compound type.
339
     */
340
    virtual IBasicReflectablePtr<ALLOC> createInstance(const ALLOC& allocator) const = 0;
341
342
    /**
343
     * Creates new instance of the zserio compound type.
344
     *
345
     * \note Default constructed allocator is used for allocation of new instance.
346
     *
347
     * \return New instance of zserio compound type.
348
     *
349
     * \throw CppRuntimeException If the zserio type is not compound type.
350
     */
351
    virtual IBasicReflectablePtr<ALLOC> createInstance() const = 0;
352
};
353
354
/**
355
 * Type information for compound type field.
356
 */
357
template <typename ALLOC = std::allocator<uint8_t>>
358
struct BasicFieldInfo
359
{
360
    BasicFieldInfo(StringView schemaName_, const IBasicTypeInfo<ALLOC>& typeInfo_,
361
            Span<const StringView> typeArguments_, bool isExtended_, StringView alignment_, StringView offset_,
362
            StringView initializer_, bool isOptional_, StringView optionalCondition_, StringView constraint_,
363
            bool isArray_, StringView arrayLength_, bool isPacked_, bool isImplicit_) :
364
            schemaName(schemaName_),
365
            typeInfo(typeInfo_),
366
            typeArguments(typeArguments_),
367
            isExtended(isExtended_),
368
            alignment(alignment_),
369
            offset(offset_),
370
            initializer(initializer_),
371
            isOptional(isOptional_),
372
            optionalCondition(optionalCondition_),
373
            constraint(constraint_),
374
            isArray(isArray_),
375
            arrayLength(arrayLength_),
376
            isPacked(isPacked_),
377
            isImplicit(isImplicit_)
378
624
    {}
379
380
    StringView schemaName; /**< field schema name */
381
    const IBasicTypeInfo<ALLOC>& typeInfo; /**< reference to type information for a field type */
382
    Span<const StringView> typeArguments; /**< sequence of field type arguments */
383
    bool isExtended; /**< true if field is extended */
384
    StringView alignment; /**< field alignment or empty in case of no alignment */
385
    StringView offset; /**< field offset or empty in case of no alignment */
386
    StringView initializer; /**< field initializer or empty in case of no alignment */
387
    bool isOptional; /**< true if field is optional */
388
    StringView optionalCondition; /**< optional condition or empty if field is not optional */
389
    StringView constraint; /**< constraint or empty if field does not have constraint */
390
    bool isArray; /**< true if field is array */
391
    StringView arrayLength; /**< array length or empty if field is not array or is auto/implicit array */
392
    bool isPacked; /**< true if field is array and packed */
393
    bool isImplicit; /**< true if field is array and implicit */
394
};
395
396
/**
397
 * Type information for compound type parameter.
398
 */
399
template <typename ALLOC = std::allocator<uint8_t>>
400
struct BasicParameterInfo
401
{
402
    StringView schemaName; /**< parameter schema name */
403
    const IBasicTypeInfo<ALLOC>& typeInfo; /**< reference to type information for a parameter type */
404
};
405
406
/**
407
 * Type information for compound type function.
408
 */
409
template <typename ALLOC = std::allocator<uint8_t>>
410
struct BasicFunctionInfo
411
{
412
    StringView schemaName; /**< function schema name */
413
    const IBasicTypeInfo<ALLOC>& typeInfo; /**< reference to type information for a resulting function type */
414
    StringView functionResult; /**< specifies the function result */
415
};
416
417
/**
418
 * Type information for choice type case.
419
 */
420
template <typename ALLOC = std::allocator<uint8_t>>
421
struct BasicCaseInfo
422
{
423
    Span<const StringView> caseExpressions; /**< sequence of case expressions */
424
    const BasicFieldInfo<ALLOC>* field; /**< pointer to type information for a case field */
425
};
426
427
/**
428
 * Type information for enumeration type item or for bitmask type value.
429
 */
430
struct ItemInfo
431
{
432
    ItemInfo(StringView schemaName_, uint64_t value_, bool isDeprecated_, bool isRemoved_) :
433
            schemaName(schemaName_),
434
            value(value_),
435
            isDeprecated(isDeprecated_),
436
            isRemoved(isRemoved_)
437
255
    {}
438
439
    StringView schemaName; /**< enumeration item or bitmask value schema name */
440
    uint64_t value; /**< enumeration item or bitmask value cast to uint64_t */
441
    bool isDeprecated; /**< flag whether the item is deprecated */
442
    bool isRemoved; /**< flag whether the item is removed */
443
};
444
445
/**
446
 * Type information for SQL table column.
447
 */
448
template <typename ALLOC = std::allocator<uint8_t>>
449
struct BasicColumnInfo
450
{
451
    BasicColumnInfo(StringView schemaName_, const IBasicTypeInfo<ALLOC>& typeInfo_,
452
            Span<const StringView> typeArguments_, StringView sqlTypeName_, StringView sqlConstraint_,
453
            bool isVirtual_) :
454
            schemaName(schemaName_),
455
            typeInfo(typeInfo_),
456
            typeArguments(typeArguments_),
457
            sqlTypeName(sqlTypeName_),
458
            sqlConstraint(sqlConstraint_),
459
            isVirtual(isVirtual_)
460
    {}
461
462
    StringView schemaName; /**< column schema name */
463
    const IBasicTypeInfo<ALLOC>& typeInfo; /**< reference to type information for a column type */
464
    Span<const StringView> typeArguments; /**< sequence of column type arguments */
465
    StringView sqlTypeName; /**< column SQL type name */
466
    StringView sqlConstraint; /**< column constraint or empty if column does not have any constraint */
467
    bool isVirtual; /**< true if SQL table is virtual */
468
};
469
470
/**
471
 * Type information for SQL database table.
472
 */
473
template <typename ALLOC = std::allocator<uint8_t>>
474
struct BasicTableInfo
475
{
476
    StringView schemaName; /**< table schema name */
477
    const IBasicTypeInfo<ALLOC>& typeInfo; /**< reference to type information for a table */
478
};
479
480
/**
481
 * Type information for template argument.
482
 */
483
template <typename ALLOC = std::allocator<uint8_t>>
484
struct BasicTemplateArgumentInfo
485
{
486
    const IBasicTypeInfo<ALLOC>& typeInfo; /**< reference to type information for a template argument */
487
};
488
489
/**
490
 * Type information for pubsub message.
491
 */
492
template <typename ALLOC = std::allocator<uint8_t>>
493
struct BasicMessageInfo
494
{
495
    BasicMessageInfo(StringView schemaName_, const IBasicTypeInfo<ALLOC>& typeInfo_, bool isPublished_,
496
            bool isSubscribed_, StringView topic_) :
497
            schemaName(schemaName_),
498
            typeInfo(typeInfo_),
499
            isPublished(isPublished_),
500
            isSubscribed(isSubscribed_),
501
            topic(topic_)
502
    {}
503
504
    StringView schemaName; /**< message schema name */
505
    const IBasicTypeInfo<ALLOC>& typeInfo; /**< reference to type information for a message type */
506
    bool isPublished; /**< true if the message is published */
507
    bool isSubscribed; /**< true if the message is subscribed */
508
    StringView topic; /**< pubsub topic for a message */
509
};
510
511
/**
512
 * Type information for service method.
513
 */
514
template <typename ALLOC = std::allocator<uint8_t>>
515
struct BasicMethodInfo
516
{
517
    /** service schema name */
518
    StringView schemaName;
519
    /** reference to type information for a method response type */
520
    const IBasicTypeInfo<ALLOC>& responseTypeInfo;
521
    /** reference to type information for a method request type */
522
    const IBasicTypeInfo<ALLOC>& requestTypeInfo;
523
};
524
525
/**
526
 * Gets type info for the given enum type.
527
 *
528
 * \return Enum type info.
529
 */
530
template <typename T, typename ALLOC = std::allocator<uint8_t>>
531
const IBasicTypeInfo<ALLOC>& enumTypeInfo();
532
533
/** Typedef provided for convenience - using default std::allocator<uint8_t>. */
534
/** \{ */
535
using ITypeInfo = IBasicTypeInfo<>;
536
using FieldInfo = BasicFieldInfo<>;
537
using ParameterInfo = BasicParameterInfo<>;
538
using FunctionInfo = BasicFunctionInfo<>;
539
using CaseInfo = BasicCaseInfo<>;
540
using ColumnInfo = BasicColumnInfo<>;
541
using TableInfo = BasicTableInfo<>;
542
using TemplateArgumentInfo = BasicTemplateArgumentInfo<>;
543
using MessageInfo = BasicMessageInfo<>;
544
using MethodInfo = BasicMethodInfo<>;
545
/** \} */
546
547
} // namespace zserio
548
549
#endif // ZSERIO_I_TYPE_INFO_INC_H