ADTF  3.13.0
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 cDecoder class:

tFloat64 ReadData(const tVoid* pConstData, tSize nDataSize)
{
// this should of course be cached (as a member etc.)
cCodecFactory oFactory("tTest", "<adtf:ddl>....");
auto oDecoder = oFactory.MakeDecoderFor(pConstData, nDataSize);
adtf_util::cVariant oValue;
// for name based lookup use the access_element namespace
{
oValue = access_element::get_value(oDecoder, "fFloat32");
}
// alternativley you can of course use indexed based lookup
{
oDecoder.GetElementValue(3, oValue);
}
return oValue;
}

Encoding

Write access is handled with the cCodec class:

void WriteData(tVoid* pData, tSize nDataSize, tFloat64 fData)
{
// this should of course be cached (as a member etc.)
cCodecFactory oFactory("tTest", "<adtf:ddl>....");
auto oCodec = oFactory.MakeCodecFor(pData, nDataSize);
// name based lookup
access_element::set_value(oCodec, "fFloat32", fData);
// or index based
oCodec.SetElementValue(3, fData);
}

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 oDecoder = oFactory.MakeDecoderFor(pConstData, nDataSize,
tDataRepresentation::Serialized);

Inspection

You can inspect the struct handled by a decoder/codec with the help of cStaticDecoder::GetElement:

tVoid DumpElements(const cStaticDecoder& oDecoder)
{
for (tSize nElement = 0; nElement < oDecoder.GetElementCount(); ++nElement)
{
const tStructElement* pElement;
oDecoder.GetElement(nElement, pElement);
std::cout << pElement->strName << std::endl;
}
}

Transformation

Converting between the representations can be done with serialization::transform:

tSize nSerializedSize = oDecoder.GetBufferSize(tDataRepresentation::Serialized);
tUInt8* pBuffer = new tUInt8[nSerializedSize];
auto Codec = oDecoder.MakeCodecFor(pBuffer, nSerializedSize, tDataRepresentation::Serialized);
serialization::transform(oDecoder, oCodec);

There is also a convienence method serialization::transform_to_buffer that handles the allocation of memory for you with the help of an adtf_util::cMemoryBlock.


Copyright © Audi Electronics Venture GmbH. All rights reserved. (Generated on Tue Sep 28 2021 by doxygen 1.8.14)