Boost logo

Boost Users :

Subject: Re: [Boost-users] [Serialization] Is this example legal usage of the serialization library
From: Robert Ramey (ramey_at_[hidden])
Date: 2010-08-26 14:52:12


Elizabeta wrote:
> Hi,
>
> Is it ok to serialize/deserialize in the object I get when
> dereference a pointer ?
>
> void f()
> {
>
> B* bObj=new B();
>
> /// save
> {
> std::ofstream ofs("C:\\hh.txt");
> boost::archive::xml_oarchive oa(ofs);
> oa & make_nvp("bObj",*bObj);
> }
> //// Load
> {
> std::ifstream ifs("C:\\hh.txt");
>
> boost::archive::xml_iarchive ia(ifs);
> ia & make_nvp("bObj",*bObj);
> }
> }
>
> My second question :
> Is it ok to serialize/deserialize in *this ?
>
> Thanks

I'm going to rephrase the question for my own convenience:

"How is the serialization library intended to be used to do the equivalent
of: stream << *x ?"

Here is my answer to my own question.

Serializing a data object in a way that most people intend requires that the
object recreated be "equivalent or substitutable" for the original object.
When an object includes a pointer, it becomes clear that this is not so
easy. There are a number of issues the crop up - including

a) the fact that pointer to class X might really be refering to some derived
class (virtual inheritance)
b) there might be multiple pointers refering to the same object.
c) same problems for references
d) an the fact that all this might be going on simultaneously.

The usage of ar << *x or ar << *this is an indication that the user is
likely to "re-implement" some of the serialization library. This might
occur because the simple interface of the library suggests that it doesn't
do much and this is not at all true. Sooooo if you find yourself wanting to
do this, step back and think about why you want to do this. Look into the
library documentation and examples to see how you might get what you want
without doing this. If it's still not clear, it's likely that your
overlooking something that the library does that is not obvious. If you
still feel you have to do this, I've failed in my task.

So the short answer to my phrasing of your question is:

"How is the serialization library intended to be used to do the equivalent
of: stream << *x ?"

ar << x

This DOES do ar << *x plus everything else that has to be done to make such
an operation make any sense at all.

Robert Ramey


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net