1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
   | #include <boost/archive/xml_iarchive.hpp>
#include <boost/archive/xml_oarchive.hpp>
#include <boost/serialization/vector.hpp>
#include <fstream>
#include <vector>
 
// classe à sérialiser
class Test{
public:
	Test(){}
    Test(int i, std::string c, float f1, float f2){
        a=i;
        s=c;
        v.push_back(f1);
        v.push_back(f2);
    }
 
template<class Archive>
void serialize(Archive& ar, const unsigned int version)
{
     ar & BOOST_SERIALIZATION_NVP(a) &
	 BOOST_SERIALIZATION_NVP(s) &
	 BOOST_SERIALIZATION_NVP(v);
}
    int a;
    std::string s;
    std::vector<float> v;
};
 
 
int main()
{
   { // ecriture
      std::ofstream ofile("archive.xml");
      boost::archive::xml_oarchive oTextArchive(ofile);
      Test t(2, std::string("essai"), 2.0, 3.5);
      oTextArchive << BOOST_SERIALIZATION_NVP(t);    // sérialisation de t
   }
 
   {// lecture
      std::ifstream ifile("archive.xml");
      boost::archive::xml_iarchive iTextArchive(ifile);
      Test t2;
      iTextArchive >> BOOST_SERIALIZATION_NVP(t2);    // sérialisation de t
   }
 
   return 0;
} | 
Partager