Boost logo

Boost :

From: Oliver.Kowalke_at_[hidden]
Date: 2006-02-09 01:21:16


>Oliver.Kowalke_at_[hidden] wrote:
>> Hello,
>> is it possible to prevent that the archive of class X can be restored
>> in class Y (both have the same layout)?

>I don't understand what this means.
>Robert Ramey

I don't want Y created from a archive containing a X.
Please look at the code below:

#include <iostream>
#include <sstream>
#include <stdexcept>

#include <boost/archive/text_oarchive.hpp>
#include <boost/archive/text_iarchive.hpp>
#include <boost/archive/xml_iarchive.hpp>
#include <boost/archive/xml_oarchive.hpp>
#include <boost/serialization/nvp.hpp>
#include <boost/serialization/level.hpp>
#include <boost/serialization/export.hpp>

class X
{
public:
    friend class boost::serialization::access;

    template< typename Archive >
    void serialize( Archive & ar, unsigned int version)
    {
        ar & BOOST_SERIALIZATION_NVP( degrees);
                ar & BOOST_SERIALIZATION_NVP( city);
    }

    int degrees;
        std::string city;

    X(){};

    X( int d, std::string const& c)
        : degrees( d), city( c)
    {}
};

// BOOST_CLASS_EXPORT(X)
// BOOST_CLASS_IMPLEMENTATION(X,
boost::serialization::object_class_info)

class Y
{
public:
    friend class boost::serialization::access;

    template< typename Archive >
    void serialize( Archive & ar, unsigned int version)
    {
        ar & BOOST_SERIALIZATION_NVP( degrees);
                ar & BOOST_SERIALIZATION_NVP( city);
    }

    int degrees;
        std::string city;

    Y(){};

    Y( int d, std::string const& c)
        : degrees( d), city( c)
    {}
};

// BOOST_CLASS_EXPORT(Y)
// BOOST_CLASS_IMPLEMENTATION(Y,
boost::serialization::object_class_info)

int main()
{
        try
        {
                std::stringstream ss;
        
                int degrees = -1342;
                std::string city;
        
                city += "abc";
        
                {
                        boost::archive::text_oarchive oa( ss);
        
                        const X g( degrees, city);
        
                        oa << g;
                }
        
                std::cout << ss.str() << std::endl;
        
                {
                        boost::archive::text_iarchive ia( ss);
        
                        Y g;
        
                        ia >> g; // I would expect an exception; at
least not Y rtestored
        
                        if ( g.degrees == degrees && g.city == city)
                                std::cout << "equal" << std::endl;
                        else
                                std::cout << "not equal" << std::endl;
                }
        
                return 0;
        }
        catch ( std::exception const& e)
        { std::cerr << e.what() << std::endl; }
        catch (...)
        { std::cerr << "unhandled" << std::endl; }

        return -1;
}


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