Boost logo

Boost Users :

From: p.speck_at_[hidden]
Date: 2005-08-22 06:46:13


I have a problem with serialization in XML-format.

The ser.routine saves the data as xml, but when trying to reload, the
execution breaks. When using the text-output saving and loading works
fine...

I hope someone can help me:
Here the code of the functions:
------------------------------------------------
main.cpp:
------------------------------------------------

#include "fixture.h"
#include <boost/archive/xml_oarchive.hpp>
#include <boost/archive/xml_iarchive.hpp>
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>

void save(CFixture* fixt, const char* filename)
{
ofstream ofs(filename);
assert(ofs.good());
boost::archive::xml_oarchive oa(ofs);
oa << BOOST_SERIALIZATION_NVP(*fixt);
ofs.close();
}

void load(CFixture* fixt, const char* filename)
{
ifstream ifs(filename);
assert(ifs.good());
cout << filename << endl;
boost::archive::xml_iarchive ia(ifs);
cout << "Archive" << endl;
ia >> BOOST_SERIALIZATION_NVP(*fixt);
cout << "loaded" << endl;
ifs.close();
}

--------------------------------------------------
fixture.h:
-------------------------------------------------
/* Header für Fixtures */

#include <vector>
#include <string>
#include <boost/archive/text_oarchive.hpp>
#include <boost/archive/text_iarchive.hpp>
#include <boost/serialization/string.hpp>
#include <boost/serialization/vector.hpp>
#include <boost/serialization/nvp.hpp>

class CFvalue
{
public:
friend class boost::serialization::access;
template<class Archive>
void serialize(Archive & ar, const unsigned int version)
{
cout << "CFvalue" << endl;
ar & BOOST_SERIALIZATION_NVP(min);
ar & BOOST_SERIALIZATION_NVP(max);
ar & BOOST_SERIALIZATION_NVP(name);
ar & BOOST_SERIALIZATION_NVP(picture);
}

//...CUT...//

class CFunctionvalues
{
protected:
string svalue;
int dvalue;
int dvalue2;
double pvalue;

public:
virtual bool setdvalue(int) = 0;
virtual bool setdvalue2(int) = 0;
virtual bool setpvalue(double) = 0;
virtual bool setsvalue(const char*) = 0;

virtual int getdvalue() = 0;
virtual int getdvalue2() = 0;
virtual double getpvalue() = 0;
virtual string getsvalue() = 0;

friend class boost::serialization::access;
template<class Archive>
void serialize(Archive & ar, const unsigned int version)

{
cout << "CFunctionvalue" << endl;
ar & BOOST_SERIALIZATION_NVP(svalue);
ar & BOOST_SERIALIZATION_NVP(dvalue);
ar & BOOST_SERIALIZATION_NVP(dvalue2);
ar & BOOST_SERIALIZATION_NVP(pvalue);
}

};

class CFunction : public CFunctionvalues
{

public:
friend class boost::serialization::access;
template<class Archive>
void serialize(Archive & ar, const unsigned int version)

{
cout << "CFunction" << endl;
ar & BOOST_SERIALIZATION_NVP(min);
ar & BOOST_SERIALIZATION_NVP(max);
ar & BOOST_SERIALIZATION_NVP(channel);
ar & BOOST_SERIALIZATION_NVP(channel2);
ar & BOOST_SERIALIZATION_NVP(has16bit);
ar & BOOST_SERIALIZATION_NVP(type);
ar & BOOST_SERIALIZATION_NVP(inverted);
ar & BOOST_SERIALIZATION_NVP(name);
ar & BOOST_SERIALIZATION_NVP(valuelist);
ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(CFunctionvalues);

}

//...CUT...//

class CFunctionset : public CFunctionvalues
{
protected:
CFunction* base;
public:

friend class boost::serialization::access;
template<class Archive>
void serialize(Archive & ar, const unsigned int version)

{
cout << "CFunctionset" << endl;
ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(CFunctionvalue);
}

//...CUT...//

-------------------------------------------------
XML-Output
-------------------------------------------------

<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<!DOCTYPE boost_serialization>
<boost_serialization signature="serialization::archive" version="3">
<*fixt class_id="0" tracking_level="0" version="0">
        <channels>2</channels>
        <type>1</type>
        <name>MAX250</name>
        <manufacturer>GLP</manufacturer>
        <picture>max.jpg</picture>
        <functions class_id="1" tracking_level="0" version="0">
                <count>2</count>
                <item class_id="2" tracking_level="0" version="0">
                        <min>0</min>
                        <max>255</max>
                        <channel>1</channel>
                        <channel2>0</channel2>
                        <has16bit>0</has16bit>
                        <type>1</type>
                        <inverted>0</inverted>
                        <name>PAN</name>
                        <valuelist class_id="3" tracking_level="0"
version="0">
                                <count>2</count>
                                <item class_id="4" tracking_level="0"
version="0">
                                        <min>0</min>
                                        <max>100</max>
                                        <name>links</name>
                                        <picture></picture>
                                </item>
                                <item>
                                        <min>101</min>
                                        <max>255</max>
                                        <name>rechts</name>
                                        <picture></picture>
                                </item>
                        </valuelist>
                        <svalue></svalue>
                        <dvalue>0</dvalue>
                        <dvalue2>0</dvalue2>
                        <pvalue>0</pvalue>
                </item>
                <item>
                        <min>0</min>
                        <max>255</max>
                        <channel>4</channel>
                        <channel2>0</channel2>
                        <has16bit>0</has16bit>
                        <type>3</type>
                        <inverted>0</inverted>
                        <name>COLOR</name>
                        <valuelist>
                                <count>3</count>
                                <item>
                                        <min>0</min>
                                        <max>50</max>
                                        <name>White</name>
                                        <picture></picture>
                                </item>
                                <item>
                                        <min>51</min>
                                        <max>100</max>
                                        <name>Rot</name>
                                        <picture></picture>
                                </item>
                                <item>
                                        <min>101</min>
                                        <max>255</max>
                                        <name>Gelb</name>
                                        <picture></picture>
                                </item>
                        </valuelist>
                        <svalue></svalue>
                        <dvalue>0</dvalue>
                        <dvalue2>0</dvalue2>
                        <pvalue>0</pvalue>
                </item>
        </functions>
</*fixt>


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