|
Boost : |
From: Noah Stein (noah_at_[hidden])
Date: 2002-02-27 19:14:13
> -----Original Message-----
> From: rameysb [mailto:ramey_at_[hidden]]
> Sent: Wednesday, February 27, 2002 1:29 PM
> To: boost_at_[hidden]
> Subject: [boost] Re: Serialization (Persistence) library draft
> submission
>
> The devil is in the details - properly serializing pointers is harder
> than it looks - no argument from me on that point.
>
> 1) You are guarenteed to write out the object only once - regardless
> of whether it is serialized through a pointer to a base class or
> derived class. That is when a pointer is serialized - it is the most
> derived class that is actually serialized. Tracking of duplicates is
> done on the most derived class. This turns out to be essential to
> serializing collections of polymorphic pointers - one of the most
> useful features of this system
I haven't looked too closely at your code yet, but I have a feeling that
there is no guarantee of properly writing out a class whose parents are
multiply-inherited. For instance, examine the following code:
class B1
{
float b1;
public:
virtual void OpA() {};
};
class B2
{
char b2;
public:
virtual void OpB() {};
};
class C : public B1, public B2
{
short c;
};
main()
{
C* c=new C;
B1* b1p=c;
B2* b2p=c;
}
When I run the above code under VC6, the address of B2 is 8 greater than B1.
Currently, there's no inherent facility in the serialization code to handle
this properly as far as I can tell. If B1 and B2 are serialized through
non-virtual functions, you will end up with two separate sliced objects. If
only B1 or only B2 is pointed to at the time of seralization, you will only
save a single slice of information, also unusable. If you virtualize the
serialization member functions in B1 and B2 to allow them to defer to the
derived class, you will save out the same object twice when there are
pointers to B1 and B2 present. This could be overcome if the save routine
were allowed to specify the true object pointer. Using save() from
trip_info in demo.cpp, something possibly like:
void save(boost::oarchive &ar) const
{
ar.begin_object(static_cast<void*>(this));
ar << driver;
ar << hour;
ar << minute;
ar.end_object();
}
I haven't looked too closely at the code, so I might be wrong about how this
situation is handled.
-- Noah
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk