Slightly related question to this one and this one.
Basically, I would like to serialize objects as they come, much like a log file, except that I want to unserialize them later. This means that I don’t have all the objects initially.
From previous answers, it looks like if one keeps the same archive open, one can keep on adding more and more objects to the archive.
But how would I extract them? Do I need to look ahead and see whether eof is reached before each extraction? Should I place a linebreak into the saving routine so that I can later read the input line by line (this would probably only work with binary archives (and maybe text), as xml uses linebreaks, and maybe not even there if binary might use a linebreak occasionally)? Maybe the >> operation throws an exception if the file end is reached and I can wrap it in an infinite loop with a try catch around it?
And how would I go about it if I wanted to do so for different kinds of objects? Maybe have an enum for all objects and serialize the enum just before, and on unserializing have a switch based on the enum?
Thanks
Here is what I did in the end. Since Nicol is right and this really is a bit of not-intended use, first one has to make sure to disable pointer tracking. Otherwise one gets spurious shared objects. Hence to begin with, loads of
I also settled for just logging objects that derive from the same base object (you could just create an empty virtual base for this purpose). Once that is done, it is important to make sure it is seen as abstract (I made sure to have virtual destructors, and still added
Register all derived classes with the archive after creating it (both write and read)
Only register final non abstract class (if A derives from B derives from C, don’t register B). At least any registered class needs to have tracking disabled.
Finally add them to the archive as they come.
To reload them, rather than using a special token for end of file, which would require checking, I went for
That might catch a bit too much, so might need some extra checks but this is working for me. The advantage is that a proper shut down isn’t as important, e.g. if your app crashes midway through, you can still process up to the last readable message.