Boost logo

Boost :

From: Daniel Kunz (Slash_Fire_at_[hidden])
Date: 2008-06-23 01:02:57


Hi,

I'm very new at Boost and want to develop a project for school with the
Boost:Serialization package.

I've already read the documantation and serveral other sources.

I'm not very good in programming C++, so please excuse that my code
looks not perfect.

I hope it's ok that I've posted some sourcecode. I've tried to keep it
short...

        *My problem:*

I have to create a program to serialise and deserialse a xml-file to a
binary-stream.
I think to binary stream is already done but the xml-serialisation is
not ready and I don't know whats wrong.

I have 3 classes:

Goal
------------
class Goal
{
    private:
        enum Scored{home=0, visitors=1};
        friend class boost::serialization::access;
        template<class Archive>
        void serialize(Archive & ar, const unsigned int version)
        {
            ar & name;
            ar & minute;
            ar & shootout;
            ar & scored;
            ar & penality;
        }
        string name;
        int minute;
        bool shootout;
        Scored scored;
        bool penality;
    public:
        Goal(){};
        Goal(const string _name, int &_minute, bool &_shootout, Scored
&_scored, bool &_penality);
        ~Goal(){};
        friend ostream & operator<<(ostream &os, const Goal &_goal);
};
---------------

Stadium
---------------
class Stadium
{
    private:
        friend class boost::serialization::access;
        template<class Archive>
        void serialize(Archive & ar, const unsigned int version)
        {
            ar & name;
            ar & city;
            ar & spectators;
        }
        string name;
        string city;
        int spectators;
    public:
        Stadium(){};
        Stadium(const string _name, const string _city, int &_spectators);
        friend ostream & operator<<(ostream &os, Stadium &_stadium);
};
----------------

and Match
----------------
class Match
{
    private:
        friend class boost::serialization::access;
        template<class Archive>
        void serialize(Archive & ar, const unsigned int version)
        {
            ar & home;
            ar & visitors;
            ar & kickoff;
            /*lGoals::const_iterator pos;
            for (pos = goals.begin(); pos != goals.end(); pos++)
            {
                ar & pos;
            }*/
            ar & BOOST_SERIALIZATION_NVP(goals);
            ar & stadium;
        }
        string home;
        string visitors;
        time_t kickoff;
        typedef list<Goal> lGoals;
        lGoals goals;
        Stadium stadium;
    public:
        Match(){};
        Match(const string _home, const string _visitors, time_t
&_kickoff, Goal _goals[100], Stadium &_stadium);
        Match& operator = (Match &_match);
        friend ostream & operator<<(ostream &out, Match &_match);
};
----------------

Now I have to serialise this classes in an xml structure. Therefor I've
created the file xml.h/.cpp. I only post the xml.cpp here:
----------------
#include <iomanip>
#include <iostream>
#include <fstream>
#include <string>

#include "goal.h"
#include "stadium.h"
#include "match.h"

#include <boost/serialization/base_object.hpp>
#include <boost/serialization/utility.hpp>
#include <boost/serialization/list.hpp>
#include <boost/serialization/is_abstract.hpp>
#include <boost/archive/xml_iarchive.hpp>
#include <boost/archive/xml_oarchive.hpp>

//Tracing and Logging module
//#include "Logger.h"
//#include "Tracer.h"

using namespace std;

/**
   * \brief Fuction to Deserialise a xml-file to an object structure
   * \param const Match &m, const string &filename
   * \return void
*/
void save_xml(const Match &m, const string &filename){
    // make an archive
    ofstream ofs(filename.c_str());
    boost::archive::xml_oarchive oa(ofs);
    //fill data to archive
    oa <<BOOST_SERIALIZATION_NVP(m);
    ofs.flush();
}

/**
   * \brief Fuction to Serialise an object structure to a xml-file
   * \param Match &m, const string &filename
   * \return void
*/
void restore_xml(Match &m, const string &filename)
{
    // open the archive
    ifstream ifs(filename.c_str());
    boost::archive::xml_iarchive ia(ifs);
    //restore the data from the archive
    ia >>BOOST_SERIALIZATION_NVP(m);
}
----------------

So thats it and I hope its not all wrong I tried.
I hope you can help me finish that project.
Thanks!


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