Closed SleepyBag closed 2 years ago
Does passing the input Reader wrapped in a std::reference_wrapper
achieve what you want? E.g.,
#include <functional>
bond::CompactBinaryReader<bond::InputBuffer> reader(buffer);
bond::Deserialize(std::ref(reader), firstObject);
(It's been a while since I've worked with C++ and my memory of how template type resolution works is hazy on the details...)
If you have control over the stream that you "pack" multiple Bond objects into, you could add a length prefix before each Bond payload. This would give you other benefits, like being able to skip a payload or--possibly--seek over a corrupt payload. With this information, you could seek in input buffer or construct one for each payload as needed.
<little-endian-uint32-size-of-next-Bond-payload><Bond-marshalled-data><little-endian-uint32-size-of-next-Bond-payload><Bond-marshalled-data>
I believe this has been answered, so I'm going to close this. If not, feel free to add another comment with your follow up questions.
Say I have two bond structs lying continuously in a bulk of memory and I want to read them both. I make an
CompactBinaryReader
from theblob
of the memory.I can successfully deserialize the first struct from the memory using the snippet above.
But, how can I deserialize the second? It turns out that after the call of
Deserialize
, the pointer of theInputBuffer
inreader
is still at the beginning of the memory. This is becauseDeserialize
takes a copy of thereader
parameter:And the constructor of
CompactBinaryReader
also takes a copy of theInputBuffer
parameter:So, seems there's no way that we can deserialize an object and continue from where it ends - the buffer point is copied and will be destroyed once
Deserialze
returns. And it seems there is no way to pass a reference of eitherInputBuffer
orCompactBinaryReader
to the function call. Is this the expected behavior, or is it a mistake? If it is expected, what is the right pattern to deserialize two continuous objects from the memory?