|
Boost Users : |
Subject: [Boost-users] [Q] help serializing a multiset of shared ptrs
From: V S P (toreason_at_[hidden])
Date: 2009-06-15 00:57:39
Hello,
I am sure I am not doing things quite right in serializing my
multiset, however I could not 'connect the dots' in the serialize
documentation between serializing shared pointers and containers.
Therefore, asking for help.
I created a complete compilable example, that I am attaching below
and the created output file. The program crashes on the last line
when I am trying to load the data back in.
Any help is greatly appreciated,
Vlad
-------------- prog start ----------------
#include <iostream>
#include <fstream>
#include <ostream>
#include <ctime>
#include <cstdlib>
#include <set>
#include <algorithm>
#include <boost/archive/tmpdir.hpp>
#include <boost/archive/text_iarchive.hpp>
#include <boost/archive/text_oarchive.hpp>
#include <boost/serialization/base_object.hpp>
#include <boost/serialization/utility.hpp>
#include <boost/serialization/list.hpp>
#include <boost/serialization/assume_abstract.hpp>
#include <boost/serialization/base_object.hpp>
#include <boost/serialization/nvp.hpp>
#include <boost/archive/tmpdir.hpp>
#include <boost/archive/xml_oarchive.hpp>
#include <boost/archive/xml_iarchive.hpp>
#include <boost/lexical_cast.hpp>
using namespace std;
using namespace boost;
template <typename TRow, typename TContainer=std::multiset<
shared_ptr<TRow> > >
struct cactivetable_t: public TContainer,
private boost::noncopyable
{
template <typename Archive>
void
serialize (Archive& ar, const unsigned int version)
{
for (TContainer::const_iterator it=begin();
it!=end();it++)
{
(*it)->myserialize(ar,version);
}
}
};
struct tRow
{
int a;
string b;
template <typename Archive>
void
myserialize (Archive& ar, const unsigned int version)
{
ar&boost::serialization::make_nvp("a", a);
ar&boost::serialization::make_nvp("b", b);
}
};
struct randrow
{
shared_ptr<tRow> operator()(void)
{
shared_ptr<tRow> p(new tRow);
p->a=rand()%100;
p->b=lexical_cast<string>(p->a);
return p;
}
};
void main (void)
{
cactivetable_t<tRow> mytb;
shared_ptr<tRow> tmpP(new tRow);
for (size_t i=0;i<10;i++)
{
/*note for multiset we can
insert the same thing many times */
mytb.insert(tmpP);
}
randrow rw;
generate(mytb.begin(),mytb.end(),rw);
// make an archive
{
ofstream ofs("xyz.sxml");
assert(ofs.good());
boost::archive::xml_oarchive oa(ofs);
oa<<BOOST_SERIALIZATION_NVP(mytb);
}//scope to close file
cout<<"finished serializing, now lets try to read back"<<endl;
mytb.clear();
{
// open the archive
std::ifstream ifs("xyz.sxml");
assert(ifs.good());
boost::archive::xml_iarchive ia(ifs);
// restore the schedule from the archive
ia >> BOOST_SERIALIZATION_NVP(mytb);
}
cout<<"after restore tb size is: "<<mytb.size()<<endl;
}
-------------- prog end -------------------
-- archive xyz.sxml XML start ----
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<!DOCTYPE boost_serialization>
<boost_serialization signature="serialization::archive" version="5">
<mytb class_id="0" tracking_level="0" version="0">
<a>41</a>
<b>41</b>
<a>67</a>
<b>67</b>
<a>34</a>
<b>34</b>
<a>0</a>
<b>0</b>
<a>69</a>
<b>69</b>
<a>24</a>
<b>24</b>
<a>78</a>
<b>78</b>
<a>58</a>
<b>58</b>
<a>62</a>
<b>62</b>
<a>64</a>
<b>64</b>
</mytb>
</boost_serialization>
--- archive XML end ----
-- V S P toreason_at_[hidden] -- http://www.fastmail.fm - Access all of your messages and folders wherever you are
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