|
In order to serialize a given parameters object, it is necessary to prepare the data buffer of the appropriate size. The size of the target buffer can be obtained from the parameters object with the serialize_buffer_size()
function:
std::size_t buffer_size = params.serialize_buffer_size();
When the serialization size is known, the buffer has to be allocated and the buffer descriptor needs to be filled so that the knowledge about all target buffers is concentrated in a single place. In this example there is only one continuous buffer that has a total size as required by the parameters object:
std::vector<char> buffer(buffer_size); char * buffers[] = { &buffer[0] }; std::size_t sizes[] = { buffer_size };
Above, the buffer
object is created with the appropriate size.
The buffer descriptor is a pair of arrays - one containing pointers to all involved buffers (there is only one buffer here, so the array of pointers has only one element) and another containing sizes of those buffers, matching by position.
Having the buffers properly described the serialization can be completed with a single call:
params.serialize(buffers, sizes, 1);
The last parameter to the serialize()
function is the number of entries in descriptor arrays - since there is only one target buffer in this example, that value is 1
.
If there is a need to use many disconnected buffers, the scheme is similar and can be generalized with these steps:
Obviously, the parameters object should not be modified between the first and last of the above steps.
Deserialization is symmetric - if the buffers are already filled with data and the buffer descriptor is prepared as above, the parameters object can be reconstructed with a single call:
params.deserialize(buffers, sizes, number_of_buffers);