Boost logo

Boost :

Subject: Re: [boost] [serialization] Must the objects being serialized outlive the archive?
From: Chris Yuen (kizzx2+boost_at_[hidden])
Date: 2010-09-17 05:11:48


Robert,

> It is a fundamental concept that saving data to an archive should
> not alter the data. In many cases this is detected at compile time
> and a warning is issued. As noted in another post, you might
> snooker the system by suppressing tracking,

I don't really understand. Why does the tracking has something to do with
the object's lifetime? Doesn't boost::serialization has all the information
it needs (tracking info, data, etc.) when I call "oa << base_ptr"? Isn't it
possible to just write out everything to the stream?

> But I'm intrigued why you want to do this? Why don't you just use

> {
> std::ofstream stream(kFilename, std::ios::binary);
> boost::archive::text_oarchive oa(stream);
>
> {
> const Strings s("Hello", "World"));
> oa << s
> }
> ...
> }

As you can see from the code example, I want to be able to deserialize a
list of these "commands" from the base pointer, because I actually have
hundreds of these polymorphic commands and they need to each have a virtual
"Execute()" method so that they know how to apply themselve to an
"Actioner." (Instead of using a giant switch-case in the Actioner to
identify each command) I figured it's better than using a magic number,
opcode approach since boost::serialization already provides tracking.

I was under the impression that if I want to deserialize using a base
pointer, I must also serialize via the base pointer, is that correct? So I
guess the below won't work with the original deserialization code?

> {
> boost::scoped_ptr<Strings> s;
> const Strings * s = new Strings("Hello", "World"));
> oa << s;
> .... // same for all the other stuff
> // s deleted as it goes out of scope
> }

Best regards,
Chris Yuen


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk