Guest User

boost serialization experiment

a guest
May 2nd, 2014
207
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.94 KB | None | 0 0
  1. #include <boost/archive/text_oarchive.hpp>
  2. #include <boost/archive/text_iarchive.hpp>
  3. #include <boost/serialization/map.hpp>
  4. #include <boost/serialization/shared_ptr.hpp>
  5. #include <string>
  6. #include <stdio.h>
  7. #include <sstream>
  8.  
  9. struct A
  10. {
  11. public:
  12.     std::string oldname;
  13.     std::string newname;
  14.  
  15.     friend class boost::serialization::access;
  16.     template<class Archive>
  17.     void serialize(Archive &ar, const unsigned int version)
  18.     {
  19.         ar & oldname;
  20.         ar & newname;
  21.     }
  22.  
  23.     bool operator==(const A& other) {
  24.         return oldname == other.oldname && newname == other.newname;
  25.     }
  26. };
  27.  
  28. struct Example
  29. {
  30. public:
  31.     bool check;
  32.     std::map<std::string,boost::shared_ptr<A>> Atype;
  33.  
  34. private:
  35.     friend class boost::serialization::access;
  36.     template<class Archive>
  37.     void serialize(Archive &ar, const unsigned int version)
  38.     {
  39.         ar & check;
  40.         ar & Atype;
  41.     }
  42. };
  43.  
  44. void set_data(boost::shared_ptr<A> e)
  45. {
  46.     e->oldname="a";
  47.     e->newname="b";
  48. }
  49.  
  50. void set_data(Example *e)
  51. {
  52.     e->check=false;
  53.  
  54.     // to initialize e->Atype
  55.     boost::shared_ptr<A> a1 (new A());
  56.     set_data(a1);
  57.     e->Atype.insert(std::make_pair("a",a1));
  58. }
  59.  
  60. void BOOST_CHECK(bool x) {
  61.     if (!x) {
  62.         printf("FAILED!\n");
  63.     } else {
  64.         printf("pass...\n");
  65.     }
  66. }
  67.  
  68. void compare_data(const std::map<std::string,boost::shared_ptr<A>>& e1,
  69.                   const std::map<std::string,boost::shared_ptr<A>>& e2)
  70. {
  71.    // because it is of type map, order IS guaranteed
  72.    typedef std::map<std::string,boost::shared_ptr<A>>::const_iterator i1;
  73.  
  74.    i1 e1_i1= e1.begin();
  75.    i1 e2_i1 =e2.begin();
  76.    while ( e1_i1 != e1.end() && e2_i1 != e2.end())
  77.    {
  78.       BOOST_CHECK( e1_i1->first == e2_i1->first );
  79.       if (e1_i1->second) {
  80.          // Data value is present on e1, must be present on e2 too
  81.          BOOST_CHECK( e2_i1->second );
  82.          // The two data values must be equal
  83.          BOOST_CHECK( *e1_i1->second == *e2_i1->second ); // Note the *
  84.       } else {
  85.          // Data value is not present on e1, must not be present in e2 either
  86.          BOOST_CHECK( !e2_i1->second );
  87.       }
  88.       e1_i1++;
  89.       e2_i1++;
  90.    }
  91.    // The iteration must terminate at the same time for e1 and e2
  92.    // (i.e. neither of them must have extra elements in respect to the other)
  93.    BOOST_CHECK( e1_i1 == e1.end() && e2_i1 == e2.end() );
  94. }
  95.  
  96. void compare_data(Example *e1,Example *e2)
  97. {
  98.     BOOST_CHECK(e1->check == e2->check);
  99.  
  100.     // need to compare e1->Atype with e2->Atype
  101.     compare_data(e1->Atype,e2->Atype);
  102. }
  103.  
  104. int main(int argc, const char *argv[]) {
  105.     boost::archive::text_oarchive ao(std::cout);
  106.  
  107.     Example c;
  108.     set_data(&c);
  109.  
  110.     const Example & oc=c;
  111.     ao << oc;
  112.  
  113.     std::stringstream ss;
  114.     boost::archive::text_oarchive oa(ss);
  115.     oa << oc;
  116.  
  117.     boost::archive::text_iarchive ia(ss);
  118.  
  119.     Example d;
  120.     ia >> d;
  121.  
  122.     compare_data(&c,&d);
  123. }
Advertisement
Add Comment
Please, Sign In to add comment