Accessing Data with a Decoder/Codec
Let's take a look at a basic structure:
<struct alignment="4" name="tTest" version="1" ddlversion="2.0">
<element name="bBool" type="tBool" arraysize="1">
<serialized byteorder="LE" bytepos="0" bitpos="0" numbits="8"/>
<deserialized alignment="1"/>
</element>
<element name="nInt8" type="tInt8" arraysize="1">
<serialized byteorder="LE" bytepos="1" bitpos="0" numbits="8"/>
<deserialized alignment="1"/>
</element>
<element name="nUInt32" type="tUInt32" arraysize="1">
<serialized byteorder="LE" bytepos="2" bitpos="0" numbits="32"/>
<deserialized alignment="4"/>
</element>
<element name="fFloat32" type="tFloat32" arraysize="1">
<serialized byteorder="LE" bytepos="6" bitpos="0" numbits="32"/>
<deserialized alignment="4"/>
</element>
</struct>
Decoding
Read access is handled with the ddl::codec::StaticDecoder or ddl::codec::Decoder class:
tFloat64 readData(const void* const_data, size_t data_size)
{
auto codec_index_for_fFloat32 = factory.getElement("fFloat32").getIndex();
auto decoder = factory.makeDecoderFor(const_data, data_size);
float value;
{
value = decoder.getElement("fFloat32").getValue<float>();
}
{
value = decoder.getElementValue<float>(codec_index_for_fFloat32);
}
return value;
}
Definition codec_factory_legacy.h:36
The example shows the usage of the ddl::codec::Decoder class with which handling of dynamic elements is possible (see also dynamic arrays within structs). A more efficient decoding initialization is provided via ddl::codec::StaticDecoder, but it allows only access to the static elements.
Encoding
Write access is handled with the ddl::codec::Codec or ddl::codec::StaticCodec class:
void writeData(void* data, size_t data_size, tFloat64 value)
{
auto codec_index_for_fFloat32 = factory.getElement("fFloat32").getIndex();
auto codec = factory.makeCodecFor(data, data_size);
codec.getElement("fFloat32").setValue(value);
codec.setElementValue(codec_index_for_fFloat32, value);
}
Definition codec_factory.h:31
The example shows the usage of the ddl::codec::Coder class with which handling of dynamic elements is possible (see also dynamic arrays within structs). A more efficient coding initialization is provided via ddl::codec::StaticCodec, but it allows only access to the static elements.
Selecting the Data Representation
By default decoders/codecs are created for the deserialized representation. To create them for the serialized representation pass the correct parameters to the "make..." methods.
auto decoder = factory.makeDecoderFor(const_data, data_size,
DataRepresentation::serialized);
Inspection
You can inspect the struct handled by a decoder/codec with the help of forEachLeafElement and ddl::codec::StaticDecoder::getElements:
{
[](auto& element)
{
std::cout << element.getFullName();
});
}
Definition static_codec.h:38
const Elements & getElements() const
Transformation
Converting between the representations can be done with ddl::codec::transform:
{
return buffer;
}
Memory buffer class to encapsulate and manage raw contiguously memory.
Definition memorybuffer.h:23
Codec makeCodecFor(void *data, size_t data_size, DataRepresentation representation) const
size_t getStaticBufferSize() const
Gets the static buffer size in bytes for the main structure.
a_util::result::Result transform(const DECODER &decoder, ENCODER &encoder, const TransformOption transform_option)
Definition serialization.h:57
@ serialized
serialized data, i.e network, on disks, can msgs, ...
Definition data_representation.h:26