Boost logo

Boost Users :

Subject: Re: [Boost-users] [serialization] Different behaviors between archive and its polymorphic variant
From: Maxime van Noppen (maxime_at_[hidden])
Date: 2010-08-18 12:10:46


On 08/18/2010 06:42 PM, Robert Ramey wrote:
> I went back and looked at your example. You're on the wrong track here.

Thank you very much!

> First, classes to be serialized should be not dependent in anyway upon
> any archive class. So use:

Ouch. The reason why I wanted polymorphic archives was to be able to
have a non-template serialize() method so that it may be implemented and
compiled once for all in the .cpp file.

> Test the above to verify that it works according to your taste.

I still have a different behavior when serializing through
polymorphic_test_oarchive.

> Now use a "templated typedef" to generate the polymorphic version:
>
> #include <boost/config.hpp>
> #include <boost/archive/xml_oarchive.hpp>
> #include <boost/archive/detail/polymorphic_oarchive_route.hpp>
>
> typedef boost::archive::detail::polymorphic_oarchive_route<
> test_oarchive
>> polymorphic_test_oarchive;
>
> // required by export
> BOOST_SERIALIZATION_REGISTER_ARCHIVE(
> test_oarchive
> )
>
> or something like this. Now you should have test_oarchive and
> polymorphic_test_oarchive

That's what I'm doing.

So now 'unit' only has a template serialize method, but I still

> int main()
> {
> unit u(42);
>
> {
> std::cout << "template:" << std::endl;
> test_oarchive ar(std::cout);
> ar << BOOST_SERIALIZATION_NVP(u);
> std::cout << std::endl;
> }
>
> {
> std::cout << "polymorphic:" << std::endl;
> polymorphic_test_oarchive ar(std::cout);
> ar << BOOST_SERIALIZATION_NVP(u);
> std::cout << std::endl;
> }
> }

Which outputs:

> template:
> <name=u><name=id_>42</name=id_>
> </name=u>
>
> polymorphic:
> 42

Code is attached.

-- 
Maxime



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