Coverage Report

Created: 2026-10-19 14:15

src/zserio/IReflectable.h
Line
Count
Source
1
#ifndef ZSERIO_I_REFLECTABLE_H_INC
2
#define ZSERIO_I_REFLECTABLE_H_INC
3
4
#include <memory>
5
6
#include "zserio/AnyHolder.h"
7
#include "zserio/BitBuffer.h"
8
#include "zserio/BitStreamWriter.h"
9
#include "zserio/ParsingInfo.h"
10
#include "zserio/RebindAlloc.h"
11
#include "zserio/Span.h"
12
#include "zserio/String.h"
13
#include "zserio/StringView.h"
14
#include "zserio/Types.h"
15
#include "zserio/Vector.h"
16
17
namespace zserio
18
{
19
20
// forward declaration
21
template <typename ALLOC>
22
class IBasicTypeInfo;
23
24
/**
25
 * Interface for reflectable view to instances of zserio objects.
26
 *
27
 * \note Users are responsible to maintain life-time of reflected objects which must exist
28
 * as long as the views are used.
29
 *
30
 * \note The object in this context may be also an instance of a built-in type.
31
 */
32
template <typename ALLOC = std::allocator<uint8_t>>
33
class IBasicReflectable
34
{
35
public:
36
    /** Shared pointer to the reflectable interface. */
37
    using Ptr = std::shared_ptr<IBasicReflectable>;
38
39
    /** Shared pointer to the constant reflectable interface. */
40
    using ConstPtr = std::shared_ptr<const IBasicReflectable>;
41
42
    /**
43
     * Destructor.
44
     */
45
1.76k
    virtual ~IBasicReflectable() = default;
46
47
    /**
48
     * Gets type info for the current zserio object.
49
     *
50
     * \return Reference to the static instance of type info.
51
     */
52
    virtual const IBasicTypeInfo<ALLOC>& getTypeInfo() const = 0;
53
54
    /**
55
     * Gets whether the reflected object is an array.
56
     *
57
     * \return True when the object is an array, false otherwise.
58
     */
59
    virtual bool isArray() const = 0;
60
61
    /**
62
     * Initializes children of the reflected compound. Calls initializeChildren method on the generated
63
     * C++ object, which recursively initializes the whole object tree. When nothing within the object tree is
64
     * parameterized, does nothing.
65
     *
66
     * \note This method is designed to be called on the top level object (i.e. root).
67
     *
68
     * \throw CppRuntimeException When the reflected object is not a compound type.
69
     */
70
    virtual void initializeChildren() = 0;
71
72
    /**
73
     * Initializes the reflected parameterized compound object. Calls initialize method on the generated
74
     * C++ object. Note that the arguments must exactly match. In case that the argument is a compound type,
75
     * which is normally passed as a reference, it must be wrapped in a reference wrapper.
76
     *
77
     * \throw CppRuntimeException When the reflected object is not parameterized or when the arguments
78
     *                            do not match.
79
     */
80
    virtual void initialize(const vector<AnyHolder<ALLOC>, ALLOC>& typeArguments) = 0;
81
82
    /**
83
     * Initializes indexed offsets of the reflected compound object.
84
     *
85
     * \param bitPosition The bit stream position to be used for calculation.
86
     *
87
     * \throw CppRuntimeException When the reflected object is not a compound.
88
     *
89
     * \return Updated bit position which points to the first bit after the compound.
90
     */
91
    virtual size_t initializeOffsets(size_t bitPosition) = 0;
92
93
    /**
94
     * Initializes indexed offsets of the reflected compound object.
95
     *
96
     * The bit stream position to be used for calculation is defaulted to zero.
97
     *
98
     * \throw CppRuntimeException When the reflected object is not a compound.
99
     *
100
     * \return Updated bit position which points to the first bit after the compound.
101
     */
102
    virtual size_t initializeOffsets() = 0;
103
104
    /**
105
     * Gets the number of bits needed for serialization of the reflected object.
106
     *
107
     * \note Works for all reflectable types except arrays!
108
     *
109
     * \param bitPosition The bit stream position to be used for calculation.
110
     *
111
     * \throw CppRuntimeException When the reflected object is an array.
112
     *
113
     * \return The size of the serialized reflected object in bits.
114
     */
115
    virtual size_t bitSizeOf(size_t bitPosition) const = 0;
116
117
    /**
118
     * Gets the number of bits needed for serialization of the reflected object.
119
     *
120
     * The bit stream position to be used for calculation is defaulted to zero.
121
     *
122
     * \note Works for all reflectable types except arrays!
123
     *
124
     * \throw CppRuntimeException When the reflected object is an array.
125
     *
126
     * \return The size of the serialized reflected object in bits.
127
     */
128
    virtual size_t bitSizeOf() const = 0;
129
130
    /**
131
     * Writes the reflected object to a bit stream using the given bit stream writer.
132
     *
133
     * \note Works for all reflectable types except arrays!
134
     *
135
     * \param writer Bit stream writer to use.
136
     *
137
     * \throw CppRuntimeException When the reflected object is an array.
138
     */
139
    virtual void write(BitStreamWriter& writer) const = 0;
140
141
    /**
142
     * Gets reflectable view to the field (i.e. member) with the given schema name.
143
     *
144
     * \note Can be called only when the reflected object is a zserio compound type.
145
     *
146
     * \param name Field schema name.
147
     *
148
     * \return Reflectable view to the requested field.
149
     *
150
     * \throw CppRuntimeException When the reflected object is not a compound type or when the field with
151
     *                            the given name doesn't exist or when the field getter itself throws.
152
     */
153
    /** \{ */
154
    virtual ConstPtr getField(StringView name) const = 0;
155
    virtual Ptr getField(StringView name) = 0;
156
    /** \} */
157
158
    /**
159
     * Sets the field (i.e. member) with the given schema name.
160
     *
161
     * \note For optional fields, the value can be also nullptr of type std::nullptr_t which allows
162
     *       to reset an optional field.
163
     *
164
     * \param name Field schema name.
165
     * \param value Value to set. The type must exactly match the type of the zserio field mapped to C++!
166
     *
167
     * \throw CppRuntimeException When the reflected object is not a compound type or when the field with
168
     *                            the given name doesn't exist or when the provided value is of a wrong type
169
     *                            or when the field setter itself throws.
170
     */
171
    virtual void setField(StringView name, const AnyHolder<ALLOC>& value) = 0;
172
173
    /**
174
     * Creates a default constructed field within current object and returns reflectable pointer to it.
175
     *
176
     * \note When the field already exists, it's reset with the new default constructed value.
177
     *
178
     * \param name Name of the optional field to create.
179
     *
180
     * \return Reflectable to just created object.
181
     *
182
     * \throw CppRuntimeException When the reflected object is not a compound type or when the field with
183
     *                            the given name doesn't exists.
184
     */
185
    virtual Ptr createField(StringView name) = 0;
186
187
    /**
188
     * Gets reflectable view to the parameter (i.e. member) with the given schema name.
189
     *
190
     * \note Can be called only when the reflected object is a zserio compound type.
191
     *
192
     * \param name Parameter schema name.
193
     *
194
     * \return Reflectable view to the requested parameter.
195
     *
196
     * \throw CppRuntimeException When the reflected object is not a compound type or when the parameter with
197
     *                            the given name doesn't exist or when the parameter getter itself throws.
198
     */
199
    /** \{ */
200
    virtual ConstPtr getParameter(StringView name) const = 0;
201
    virtual Ptr getParameter(StringView name) = 0;
202
    /** \} */
203
204
    /**
205
     * Calls function with the given name on the reflected zserio object and gets reflectable view to its
206
     * result.
207
     *
208
     * \note Can be called only when the reflected object is a zserio compound type.
209
     *
210
     * \param name Function schema name.
211
     *
212
     * \return Reflectable view to the value returns from the called function.
213
     *
214
     * \throw CppRuntimeException When the reflected object is not a compound type or when the function with
215
     *                            the given name doesn't exist or the the function call itself throws.
216
     */
217
    /** \{ */
218
    virtual ConstPtr callFunction(StringView name) const = 0;
219
    virtual Ptr callFunction(StringView name) = 0;
220
    /** \} */
221
222
    /**
223
     * Gets name of the field which is active in the reflected choice type.
224
     *
225
     * \note Applicable only on zserio unions and choices.
226
     *
227
     * \return Name of the active field (i.e. currently selected choice).
228
     *
229
     * \throw CppRuntimeException When the reflected object is not a choice type (or union).
230
     */
231
    virtual StringView getChoice() const = 0;
232
233
    /**
234
     * Universal accessor to zserio entities within the zserio sub-tree represented by the reflected object.
235
     *
236
     * Supports dot notation corresponding to the tree defined in zserio language. Can access fields or
237
     * parameters or call functions within the zserio sub-tree.
238
     *
239
     * Examples:
240
     * * 'fieldA.param' - Gets reflectable view to parameter 'param' within the parameterized field 'fieldA'.
241
     * * 'child.getValue' - Gets reflectable view to result of the function called on field ̈́'child'.
242
     * * 'child.nonexisting.field' - Gets nullptr since the path doesn't represent a valid entity.
243
     *
244
     * \param path Dot notation corresponding to the zserio tree.
245
     *
246
     * \return Reflectable view to the result of the given path. Returns nullptr when the path doesn't exist
247
     *         or when the requested operation throws CppRuntimeException.
248
     */
249
    /** \{ */
250
    virtual ConstPtr find(StringView path) const = 0;
251
    virtual Ptr find(StringView path) = 0;
252
    /** \} */
253
254
    /**
255
     * \copydoc IBasicReflectable::find
256
     *
257
     * Overloaded method provided for convenience.
258
     */
259
    /** \{ */
260
    virtual ConstPtr operator[](StringView path) const = 0;
261
    virtual Ptr operator[](StringView path) = 0;
262
    /** \} */
263
264
    /**
265
     * Gets size of the reflected array.
266
     *
267
     * \note Can be called only when the reflected object is an array.
268
     *
269
     * \return Size of the reflected array.
270
     *
271
     * \throw CppRuntimeException When the reflected object is not an array.
272
     */
273
    virtual size_t size() const = 0;
274
275
    /**
276
     * Resizes the reflected array.
277
     *
278
     * \note Can be called only when the reflected object is an array.
279
     *
280
     * \param size New array size.
281
     *
282
     * \throws CppRuntimeException When the reflected object is not an array.
283
     */
284
    virtual void resize(size_t size) = 0;
285
286
    /**
287
     * Gets reflectable view to an array element.
288
     *
289
     * \note Can be called only when the reflected object is an array.
290
     *
291
     * \return Reflectable view to an array element on the given index.
292
     *
293
     * \throw CppRuntimeException When the reflected object is not an array or when the given index is
294
     *                            out of bounds of the underlying array.
295
     */
296
    /** \{ */
297
    virtual ConstPtr at(size_t index) const = 0;
298
    virtual Ptr at(size_t index) = 0;
299
    /** \} */
300
301
    /**
302
     * \copydoc IBasicReflectable::at
303
     *
304
     * Overloaded method provided for convenience.
305
     */
306
    /** \{ */
307
    virtual ConstPtr operator[](size_t index) const = 0;
308
    virtual Ptr operator[](size_t index) = 0;
309
    /** \} */
310
311
    /**
312
     * Sets an element value at the given index within the reflected array.
313
     *
314
     * \param value Value to set.
315
     * \param index Index of the element to set.
316
     *
317
     * \throws CppRuntimeException When the reflected object is not an array.
318
     */
319
    virtual void setAt(const AnyHolder<ALLOC>& value, size_t index) = 0;
320
321
    /**
322
     * Appends an element at the given index within the reflected array.
323
     *
324
     * \param value Value to append.
325
     *
326
     * \throws CppRuntimeException When the reflected object is not an array.
327
     */
328
    virtual void append(const AnyHolder<ALLOC>& value) = 0;
329
330
    /**
331
     * Gets any value within the reflected object.
332
     *
333
     * For builtin types, enums and bitmasks the value is "returned by value" - i.e. it's copied
334
     * into the any holder, but note that for bytes the any holder contains Span,
335
     * for string the any holder contains an appropriate StringView and for compounds, bit buffers and arrays
336
     * the value is "returned by reference" - i.e. the any holder contains std::reference_wrapper<T> with the
337
     * reference to the compound type or the raw array type.
338
     *
339
     * \note For bit buffers only const reference is available.
340
     * \note Overloads without parameter use default constructed allocator.
341
     *
342
     * \param allocator Allocator to use for the value allocation.
343
     *
344
     * \return Any value.
345
     */
346
    /** \{ */
347
    virtual AnyHolder<ALLOC> getAnyValue(const ALLOC& allocator) const = 0;
348
    virtual AnyHolder<ALLOC> getAnyValue(const ALLOC& allocator) = 0;
349
    virtual AnyHolder<ALLOC> getAnyValue() const = 0;
350
    virtual AnyHolder<ALLOC> getAnyValue() = 0;
351
    /** \} */
352
353
    /**
354
     * Gets bool value of the bool reflectable.
355
     *
356
     * \return Bool value.
357
     * \throw CppRuntimeException When the reflected object is not a bool type.
358
     */
359
    virtual bool getBool() const = 0;
360
361
    /**
362
     * Gets 8-bit signed integral value of the int8_t reflectable.
363
     *
364
     * \return 8-bit signed integral value.
365
     * \throw CppRuntimeException When the reflected object is not a int8_t type.
366
     */
367
    virtual int8_t getInt8() const = 0;
368
369
    /**
370
     * Gets 16-bit signed integral value of the int16_t reflectable.
371
     *
372
     * \return 16-bit signed integral value.
373
     * \throw CppRuntimeException When the reflected object is not a int16_t type.
374
     */
375
    virtual int16_t getInt16() const = 0;
376
377
    /**
378
     * Gets 32-bit signed integral value of the int32_t reflectable.
379
     *
380
     * \return 32-bit signed integral value.
381
     * \throw CppRuntimeException When the reflected object is not a int32_t type.
382
     */
383
    virtual int32_t getInt32() const = 0;
384
385
    /**
386
     * Gets 64-bit signed integral value of the int64_t reflectable.
387
     *
388
     * \return 64-bit signed integral value.
389
     * \throw CppRuntimeException When the reflected object is not a int64_t type.
390
     */
391
    virtual int64_t getInt64() const = 0;
392
393
    /**
394
     * Gets 8-bit unsigned integral value of the uint8_t reflectable.
395
     *
396
     * \return 8-bit unsigned integral value.
397
     * \throw CppRuntimeException When the reflected object is not a uint8_t type.
398
     */
399
    virtual uint8_t getUInt8() const = 0;
400
401
    /**
402
     * Gets 16-bit unsigned integral value of the uint16_t reflectable.
403
     *
404
     * \return 16-bit unsigned integral value.
405
     * \throw CppRuntimeException When the reflected object is not a uint16_t type.
406
     */
407
    virtual uint16_t getUInt16() const = 0;
408
409
    /**
410
     * Gets 32-bit unsigned integral value of the uint32_t reflectable.
411
     *
412
     * \return 32-bit unsigned integral value.
413
     * \throw CppRuntimeException When the reflected object is not a uint32_t type.
414
     */
415
    virtual uint32_t getUInt32() const = 0;
416
417
    /**
418
     * Gets 64-bit unsigned integral value of the uint64_t reflectable.
419
     *
420
     * \return 64-bit unsigned integral value.
421
     * \throw CppRuntimeException When the reflected object is not a uint64_t type.
422
     */
423
    virtual uint64_t getUInt64() const = 0;
424
425
    /**
426
     * Gets float value of the float reflectable.
427
     *
428
     * \return Float value.
429
     * \throw CppRuntimeException When the reflected object is not a float type.
430
     */
431
    virtual float getFloat() const = 0;
432
433
    /**
434
     * Gets double value of the double reflectable.
435
     *
436
     * \return Double value.
437
     * \throw CppRuntimeException When the reflected object is not a double type.
438
     */
439
    virtual double getDouble() const = 0;
440
441
    /**
442
     * Gets byte value of the bytes reflectable.
443
     *
444
     * \return Bytes value as a span.
445
     * \throw CppRuntimeException When the reflected object is not a bytes type.
446
     */
447
    virtual Span<const uint8_t> getBytes() const = 0;
448
449
    /**
450
     * Gets reference to the string value of the string reflectable.
451
     *
452
     * \return Reference to the string value.
453
     * \throw CppRuntimeException When the reflected object is not a string type.
454
     */
455
    virtual StringView getStringView() const = 0;
456
457
    /**
458
     * Gets reference to the reflected bit buffer.
459
     *
460
     * \return Reference to the bit buffer.
461
     * \throw CppRuntimeException When the reflected object is not a bit buffer (i.e. extern type).
462
     */
463
    virtual const BasicBitBuffer<ALLOC>& getBitBuffer() const = 0;
464
465
    /**
466
     * Converts any signed integral value to 64-bit signed integer.
467
     *
468
     * Works also for enum types defined with signed underlying type.
469
     *
470
     * \return 64-bit signed integral value.
471
     * \throw CppRuntimeException When the reflected object cannot be converted to a signed integral value.
472
     */
473
    virtual int64_t toInt() const = 0;
474
475
    /**
476
     * Converts any unsigned integral value to 64-bit unsigned integer.
477
     *
478
     * Works also for bitmask and enum typed defined with unsigned underlying type.
479
     *
480
     * \return 64-bit unsigned integral value.
481
     * \throw CppRuntimeException When the reflected object cannot be converted to
482
     *                            an unsigned integral value.
483
     */
484
    virtual uint64_t toUInt() const = 0;
485
486
    /**
487
     * Converts any numeric value to double.
488
     *
489
     * Works also for bitmask and enum types.
490
     *
491
     * \return Double value.
492
     * \throw CppRuntimeException When the reflected object cannot be converted to double.
493
     */
494
    virtual double toDouble() const = 0;
495
496
    /**
497
     * Converts an reflected object to string.
498
     *
499
     * Works for all integral types including bool, bitmask and enum types and for string types.
500
     *
501
     * \note Floating point types are not currently supported!
502
     * \note The conversion to string can be implemented for more types in future versions.
503
     * \note Overload without parameter use default constructed allocator.
504
     *
505
     * \param allocator Allocator to use for the string allocation.
506
     *
507
     * \return String value representing the reflected object.
508
     */
509
    /** \{ */
510
    virtual string<ALLOC> toString(const ALLOC& allocator) const = 0;
511
    virtual string<ALLOC> toString() const = 0;
512
    /** \} */
513
514
    /**
515
     * Returns the parsing information after reading of the reflectable object.
516
     *
517
     * This feature is experimental and can be removed without any warning!
518
     *
519
     * \note Note that the returned parsing information is valid only directly after read! If the Zserio object
520
     *       has been changed after reading, the result is undefined!
521
     * \note The parsing information is available only for code generated using `-withParsingInfo` option.
522
     *
523
     * \return The parsing information of the reflectable object.
524
     * \throw CppRuntimeException If the object was compiled without the parsing info feature enabled.
525
     */
526
    virtual const ParsingInfo& parsingInfo() const = 0;
527
};
528
529
/** Typedef to reflectable smart pointer needed for convenience in generated code. */
530
/** \{ */
531
template <typename ALLOC = std::allocator<uint8_t>>
532
using IBasicReflectablePtr = typename IBasicReflectable<ALLOC>::Ptr;
533
534
template <typename ALLOC = std::allocator<uint8_t>>
535
using IBasicReflectableConstPtr = typename IBasicReflectable<ALLOC>::ConstPtr;
536
/** \} */
537
538
/** Typedef to reflectable interface provided for convenience - using default std::allocator<uint8_t>. */
539
/** \{ */
540
using IReflectable = IBasicReflectable<>;
541
using IReflectablePtr = IBasicReflectablePtr<>;
542
using IReflectableConstPtr = IBasicReflectableConstPtr<>;
543
/** \} */
544
545
/**
546
 * Gets reflectable view for the given enum item.
547
 *
548
 * \param value Enum value to reflect.
549
 * \param allocator Allocator to use for reflectable allocation.
550
 *
551
 * \return Enum reflectable view.
552
 */
553
template <typename T, typename ALLOC = std::allocator<uint8_t>>
554
IBasicReflectablePtr<ALLOC> enumReflectable(T value, const ALLOC& allocator = ALLOC());
555
556
} // namespace zserio
557
558
#endif // ZSERIO_I_REFLECTABLE_H_INC