Boost logo

Boost Users :

Subject: Re: [Boost-users] [serialization] export.hpp changes from 1.37 to 1.38
From: Chard (boost_at_[hidden])
Date: 2009-02-18 15:39:41


It turns out that we can't get away with the register_type<> workaround, at
least if we want to support anything we've serialized with earlier versions.

The register_type<> approach produces incompatible archives to those
produced with BOOST_CLASS_EXPORT.

This all comes about because the class type is registered in the archive the
first time it is seen. For the register_type<> case, it is marked as 'seen'
immediately, so it is never written to the archive..

IMO I don't believe the choice of how the class is registered should affect
the archive contents, but addressing this issue could be tricky at this
stage - to ensure it is backwards compatible, for sure. The archive would
have to be examined to see if the class were there or not.

If it's too much to address this issue, then, at least, the documentation
should be updated to make it clear that once the choice is made...that's it.

Anyway, FWIW, here's the test code to demonstrate:

#include <boost/archive/xml_oarchive.hpp>
#include <boost/archive/xml_iarchive.hpp>

#include <boost/serialization/shared_ptr.hpp>
#include <boost/serialization/export.hpp>

#include <sstream>

struct B
{
    virtual ~B() {}

    template <typename A>
    void serialize(A &a, const unsigned int i)
    {}
};
typedef boost::shared_ptr<B> BP;

struct D : B
{
    template <typename A>
    void serialize(A &a, const unsigned int i)
    {
        a & boost::serialization::make_nvp("B",
boost::serialization::base_object<B>(*this));
    }
};

BOOST_CLASS_EXPORT(D)

void
serialize_export_out_register_in()
{
    std::string data;
    {
        BP bp(new D);
        std::ostringstream str;
        boost::archive::xml_oarchive arch(str);
        arch & boost::serialization::make_nvp("b", bp);
        data = str.str();
    }
    {
        BP bp;
        std::istringstream str(data);
        boost::archive::xml_iarchive arch(str);
        arch.register_type<D>(); // Pre-register
        arch & boost::serialization::make_nvp("b", bp); // Stream error
    }
}

void
serialize_register_out_export_in()
{
    std::string data;
    {
        BP bp(new D);
        std::ostringstream str;
        boost::archive::xml_oarchive arch(str);
        arch.register_type<D>(); // Pre-register
        arch & boost::serialization::make_nvp("b", bp);
        data = str.str();
    }
    {
        BP bp;
        std::istringstream str(data);
        boost::archive::xml_iarchive arch(str);
        arch & boost::serialization::make_nvp("b", bp); // Segment fault
    }
}

Chard.

"Steven Watanabe" <watanabesj_at_[hidden]> wrote in message
news:499B3A58.4090006_at_providere-consulting.com...
> AMDG
>
> Robert Ramey wrote:
>> I would like to change it back. but before I do, I would like
>> to hear other opinions.
>>
>
> Adding an anonymous namespace should not cause
> any code that works now to break.
>
> In Christ,
> Steven Watanabe


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