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"/>
</element>
<element name="nInt8" type="tInt8" arraysize="1">
<
serialized byteorder=
"LE" bytepos=
"1" bitpos=
"0" numbits=
"8"/>
</element>
<element name="nUInt32" type="tUInt32" arraysize="1">
<
serialized byteorder=
"LE" bytepos=
"2" bitpos=
"0" numbits=
"32"/>
</element>
<element name="fFloat32" type="tFloat32" arraysize="1">
<
serialized byteorder=
"LE" bytepos=
"6" bitpos=
"0" numbits=
"32"/>
</element>
</struct>
@ serialized
serialized data, i.e network, on disks, can msgs, ...
@ deserialized
deserialized data, c-structs, arrays, ...
Decoding
Read access is handled with the cDecoder class:
double ReadData(const void* pConstData, size_t nDataSize)
{
cCodecFactory oFactory("tTest", "<adtf:ddl>....");
auto oDecoder = oFactory.MakeDecoderFor(pConstData, nDataSize);
adtf_util::cVariant oValue;
{
}
{
oDecoder.GetElementValue(3, oValue);
}
return oValue;
}
tResult get_value(const T &oDecoder, const A_UTILS_NS::cString &strElementName, void *pValue)
Get the value of an element by name.
Encoding
Write access is handled with the cCodec class:
void WriteData(void* pData, size_t nDataSize, double fData)
{
cCodecFactory oFactory("tTest", "<adtf:ddl>....");
auto oCodec = oFactory.MakeCodecFor(pData, nDataSize);
oCodec.SetElementValue(3, fData);
}
tResult set_value(T &oCodec, const A_UTILS_NS::cString &strElementName, const void *pValue)
Set the value of an element by name.
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,
@ Serialized
alias names for legacy reasons
Inspection
You can inspect the struct handled by a decoder/codec with the help of cStaticDecoder::GetElement:
void DumpElements(const cStaticDecoder& oDecoder)
{
for (size_t nElement = 0; nElement < oDecoder.GetElementCount(); ++nElement)
{
const tStructElement* pElement;
oDecoder.GetElement(nElement, pElement);
std::cout << pElement->name << std::endl;
}
}
Transformation
Converting between the representations can be done with serialization::transform:
uint8_t tUInt8
type definition for unsigned integer values (8bit) (platform and compiler independent type).
size_t tSize
type definition for a array size values, map size values etc.
tResult transform(const Decoder &oDecoder, Encoder &oEncoder)
Copies all elements from a decoder to a codec.
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.