| 1 | #include <fstream>
|
|---|
| 2 | #include <vector>
|
|---|
| 3 | #include <map>
|
|---|
| 4 | #include <boost/serialization/access.hpp>
|
|---|
| 5 | #include <boost/serialization/map.hpp>
|
|---|
| 6 | #include <boost/serialization/vector.hpp>
|
|---|
| 7 | #include <boost/archive/xml_iarchive.hpp>
|
|---|
| 8 | #include <boost/archive/xml_oarchive.hpp>
|
|---|
| 9 | #include <boost/serialization/nvp.hpp>
|
|---|
| 10 |
|
|---|
| 11 | class A
|
|---|
| 12 | {
|
|---|
| 13 | public:
|
|---|
| 14 | A(int _a):a(_a){}
|
|---|
| 15 | private:
|
|---|
| 16 | int a;
|
|---|
| 17 | A():a(0){}
|
|---|
| 18 | friend class boost::serialization::access;
|
|---|
| 19 | //uncomment next line to make work as a hack
|
|---|
| 20 | //friend class std::pair<const int,A>;
|
|---|
| 21 | template<class Archive>
|
|---|
| 22 | void serialize(Archive& ar, const unsigned int version)
|
|---|
| 23 | {
|
|---|
| 24 | ar & BOOST_SERIALIZATION_NVP(a);
|
|---|
| 25 | }
|
|---|
| 26 | };
|
|---|
| 27 |
|
|---|
| 28 | int main(int argc, char** argv)
|
|---|
| 29 | {
|
|---|
| 30 | const A a1(5);
|
|---|
| 31 | {
|
|---|
| 32 | std::ofstream ofile;
|
|---|
| 33 | ofile.open("test.xml");
|
|---|
| 34 | boost::archive::xml_oarchive oa(ofile);
|
|---|
| 35 | oa<<BOOST_SERIALIZATION_NVP(a1);
|
|---|
| 36 | }
|
|---|
| 37 |
|
|---|
| 38 | std::vector<A> avector;
|
|---|
| 39 | std::vector<A>::value_type v1(A(10));
|
|---|
| 40 | avector.push_back(v1);
|
|---|
| 41 | {
|
|---|
| 42 | std::ofstream ofile;
|
|---|
| 43 | ofile.open("testvector.xml");
|
|---|
| 44 | boost::archive::xml_oarchive oa(ofile);
|
|---|
| 45 | oa<<BOOST_SERIALIZATION_NVP(avector);
|
|---|
| 46 | }
|
|---|
| 47 | {
|
|---|
| 48 | std::ifstream ifile("testvector.xml");
|
|---|
| 49 | boost::archive::xml_iarchive ia(ifile);
|
|---|
| 50 | ia>>BOOST_SERIALIZATION_NVP(avector);
|
|---|
| 51 | }
|
|---|
| 52 |
|
|---|
| 53 | std::map<int,A> amap;
|
|---|
| 54 | std::map<int,A>::value_type m1(1,A(10));
|
|---|
| 55 | amap.insert(m1);
|
|---|
| 56 | {
|
|---|
| 57 | std::ofstream ofile;
|
|---|
| 58 | ofile.open("testmap.xml");
|
|---|
| 59 | boost::archive::xml_oarchive oa(ofile);
|
|---|
| 60 | oa<<BOOST_SERIALIZATION_NVP(amap);
|
|---|
| 61 | }
|
|---|
| 62 | {
|
|---|
| 63 | std::ifstream ifile("testmap.xml");
|
|---|
| 64 | boost::archive::xml_iarchive ia(ifile);
|
|---|
| 65 | ia>>BOOST_SERIALIZATION_NVP(amap);
|
|---|
| 66 | }
|
|---|
| 67 |
|
|---|
| 68 | return 0;
|
|---|
| 69 | }
|
|---|