Currently offsets are implemented by mutable keyword in the Data abstraction. Such solution is simple, however it is not enough for offsets in unions or choices.
Consider the following example:
union TestUnion
{
uint32 offset;
string name[];
};
struct TestStruct
{
TestUnion unionField;
unionField.offset:
uint32 u32Field;
};
In this case, there is no field offset in generated Data, therefore keyword mutable cannot be used. Because of that, the abstraction View must accept non-const reference to the Data:
template <>
class View<::offsets::offset_in_union::TestUnion>
{
public:
explicit View(::offsets::offset_in_union::TestUnion& data) noexcept;
::zserio::UInt32& offset() const;
ArrayView<const ::zserio::String> name() const;
::offsets::offset_in_union::TestUnion::Tag zserioChoiceTag() const;
const ::offsets::offset_in_union::TestUnion& zserioData() const;
protected:
View(::offsets::offset_in_union::TestUnion& data, const View& other) noexcept;
private:
::offsets::offset_in_union::TestUnion* m_data;
};
Therefore, it would be better to generate new dedicated abstraction OffsetInitializer similarly to View. This new abstraction could accept non-const reference to the Data and View could accept const reference as expected.
Currently offsets are implemented by
mutablekeyword in theDataabstraction. Such solution is simple, however it is not enough for offsets in unions or choices.Consider the following example:
In this case, there is no field
offsetin generatedData, therefore keywordmutablecannot be used. Because of that, the abstractionViewmust accept non-const reference to theData:Therefore, it would be better to generate new dedicated abstraction
OffsetInitializersimilarly to View. This new abstraction could accept non-const reference to theDataandViewcould accept const reference as expected.