
On Sun, 2005-09-04 at 11:33 -0700, Robert Ramey wrote:
I wondered how the Boost.Serialization library determines how many bytes to read from an input stream to "fill" a class with data.
That is, does it read an entire input stream until the end of the stream and use that as the data to fill a class with, or does it perform a sizeof(class) and read that from the stream ?
Neither. Each class has its serialize function. Within this function it calls the operator << to copy the member data to archive. There are two cases:
a) the data type is a primitive - int, etc - in which case the archive has method for storing it and knows the length of an int on the particular platform.
b) the datatype is a class - in which case that class's serialize function is called.
So eventually the serialization resolves to a string of calls to primitive types - all of which are supported by the archive class.
Ok, sounds logical. What about objects from the stdlib, such as std::string ? I assume this function does not have a serialize() function, but in some prototypes I created I noticed that this worked perfectly.
If it is the latter, then out of curiosity, how does it determine the size of an object with different class versions ( so if an object version 1 has been saved and then tries to load version
The first piece of data stored with the class is a version number. On loading, this is available to be used in conditional ar >> operations. When one adds a new member to the class - the version number should be updated so that the loading of new member can be skipped if the archive is an older version.
Yes, I saw that in an example on the documentation. Looks like a great feature!
2 ) ? Does it write some sort of object size at the beginning of the file ?
no.
If it reads the entire stream until the end,
None of the currently implmented archive classes do this.
Ok, but then, out of curiosity, how does it know when to stop reading ?
what would be a good approach to save multiple objects in a single file, and be able to read just a few objects out of the file ?
Current archive classes don't support a good way to do this. Actually this is an iteresting application - but its not supported by current archives. One might be able make a new archive class or a new derivation from one of the existing ones to support this, but I have no idea how difficult it would be. At a minium it would require a pretty thorough understanding of the library and archive classes.
Yes, and I assume this will be a very hard task making it portable. Ok, guess I'll look further away from serializations for a solution... Thanks a lot for your lengthy reply, at least I'm all out of hopes for the boost serialization library now. :) Regards, Leon Mergen