Coverage Report

Created: 2024-07-18 11:41

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