Boost logo

Boost Users :

Subject: [Boost-users] Problem using serialization::xml_archive with polymrphic classes
From: przemyslaw.sliwa_at_[hidden]
Date: 2010-09-01 12:34:23


HelloHello,

I have written code below and it works fine when using text archives.
However, when I try to run it with the xml archives it is throwing a
strange error saying "Invalid XML tag name". I have no idea what that
means and could not find it in the documentation of the serialization
library. Could someone tell me what I am doing wrong in this code?

Thanks a lot for help!

#include <boost/smart_ptr/shared_ptr.hpp>

#include <boost/archive/xml_oarchive.hpp>
#include <boost/archive/xml_iarchive.hpp>
#include <boost/archive/text_oarchive.hpp>
#include <boost/archive/text_iarchive.hpp>
#include <boost/serialization/vector.hpp>
#include <boost/serialization/string.hpp>
#include <boost/serialization/shared_ptr.hpp>

#include <boost/date_time/local_time/local_time.hpp>
#include <boost/serialization/export.hpp>

#include <iostream>

using namespace boost;
using namespace std;

class Actor
{
public:
        Actor( void ) {}
        Actor( char gender )
                : gender( gender )
        {}
        virtual ~Actor() {}
        char Gender( void ) const { return gender; }
private:
        friend class boost::serialization::access;
        template<class Archive>
        void serialize(Archive & ar, const unsigned int version)
        {
                ar & BOOST_SERIALIZATION_NVP(gender);
        }
        char gender;
};

class Crit: public Actor
{
public:
        Crit( void ) {}
        Crit( int age )
                : age( age )
        {
        }
        int Age( void ) const { return age; }
private:
        friend class boost::serialization::access;
        template<class Archive>
        void serialize(Archive & ar, const unsigned int version)
        {
                ar &
BOOST_SERIALIZATION_NVP(boost::serialization::base_object< Actor >( *this
));
                ar & BOOST_SERIALIZATION_NVP(age);
        }
        int age;
};

class Food: public Actor
{
public:
        Food( void ) {}
        Food( int energy )
                : energy( energy )
        {
        }
        int Energy( void ) const { return energy; }
private:
        friend class boost::serialization::access;
        template<class Archive>
        void serialize(Archive & ar, const unsigned int version)
        {
                ar &
BOOST_SERIALIZATION_NVP(boost::serialization::base_object< Actor >( *this
));
                ar & BOOST_SERIALIZATION_NVP(energy);
        }
        int energy;
};

BOOST_CLASS_EXPORT_GUID( Actor, "Actor")
BOOST_CLASS_EXPORT_GUID( Crit, "Crit")
BOOST_CLASS_EXPORT_GUID( Food, "Food")

template< typename T >
void save( T& data, string filename )
{
        ofstream ofs( filename.c_str() );
        boost::archive::xml_oarchive oa( ofs );
        oa & BOOST_SERIALIZATION_NVP(data);
}

template< typename T >
void load( T& data, string filename )
{
        ifstream ofs( filename.c_str() );
        boost::archive::xml_iarchive ia( ofs );
        ia & BOOST_SERIALIZATION_NVP(data);
}

void TestAlpha( void )
{
        shared_ptr< Crit > critAlpha( new Crit( 18 ) );
        shared_ptr< Crit > critBravo( new Crit( 19 ) );
        shared_ptr< Food > foodAlpha( new Food( 20 ) );
        shared_ptr< Food > foodBravo( new Food( 21 ) );
        vector< shared_ptr< Actor > > conAlpha;
        vector< shared_ptr< Actor > > conBravo;

        conAlpha.push_back( critAlpha );
        conAlpha.push_back( foodAlpha );

        // save
        string filename = "sav.sav";
        save( conAlpha, filename );

        // test
        assert( critBravo->Age() == 19 );
        assert( foodBravo->Energy() == 21 );

        // load
        load( conBravo, filename );

        // test
        critBravo = dynamic_pointer_cast< Crit >( conBravo[ 0 ] );
        assert( critBravo->Age() == 18 );
        foodBravo = dynamic_pointer_cast< Food >( conBravo[ 1 ] );
        assert( foodBravo->Energy() == 20 );
}

int main()
{
        try
        {
                TestAlpha();
        }
        catch(boost::archive::archive_exception& e)
        {
                return -1;
        }

        return 0;
}

___________________________________________________________
This e-mail may contain confidential and/or privileged information. If you are not the intended recipient (or have received this e-mail in error) please notify the sender immediately and delete this e-mail. Any unauthorised copying, disclosure or distribution of the material in this e-mail is prohibited.

Please refer to http://www.bnpparibas.co.uk/en/information/legal_information.asp?Code=ECAS-845C5H for additional disclosures.



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