Serialization using boost Library

Source: Internet
Author: User

 

Part 1: vs2010 + boost:

    • (1) download and install the boost Library

      • : Http://www.boost.org/
      • Install to a specified directory, for example:
        • E: \ boost_000051
    • (2) Use the boost library in vs2010
      • Configure the boost Library

        • Configure the include path

          • Visual Studio-> right-click the project name-> properties-> Configuration properties-> VC ++ directory-> include directory-> click Edit

            • Enter: e: \ boost_000051
        • Configure the Lib path
          • Visual Studio-> right-click the project name-> properties-> Configuration properties-> VC ++ directory-> library directory-> click Edit

            • Enter: e: \ boost_000051 \ Lib

 

Part 2: serialization

    • For detailed instructions, see tutorial on the boost official website:

        • Http://www.boost.org/doc/libs/1_52_0/libs/serialization/doc/index.html

 

  • Why serialization?

We hope to save the values in the member variables of the class object so that they can be directly restored next time. This can be achieved through serialization, to keep these variable values, and then directly restore the value to the member variables of the class object when necessary. In short, the object state can be maintained through serialization.

  • Serialization tool:

Through the boost library, we can easily serialize class objects.

  • The following three examples illustrate how to use the boost Library (Note: More than these three methods ).

    • Example 1-intrusion version

      • There is an existing person class, which is declared and defined as follows:

        • Person. h
        • class Person {public:Person() ;Person(int _age, std::string _name) ;~Person() ;void addScore(const float _score) ;private:int m_age ;std::string m_name ;std::vector<float> m_score ;};
        • Person. cpp
      • Person::Person(){m_age = 0 ;m_name = "name" ;}Person::~Person(){if (m_score.size() > 0){m_score.clear() ;}}Person::Person(int _age, std::string _name):m_age(_age), m_name(_name){}void Person::addScore(const float _score){m_score.push_back(_score) ;}
      • Serialized person class:
      • Person. h
      • class Person {public:Person() ;Person(int _age, std::string _name) ;~Person() ;void addScore(const float _score) ;private:friend class boost::serialization::access ;template<class Archive>void serialize(Archive & ar, const unsigned int version){ar & m_age ;ar & m_name ;ar & m_score ;}private:int m_age ;std::string m_name ;std::vector<float> m_score ;};

      • Test procedure:
      • Person onePerson(25, "fang") ;onePerson.addScore(95.2f) ;std::ofstream ofs("out.bin", std::ios_base::binary) ;if (ofs.is_open()){boost::archive::binary_oarchive oa(ofs) ;oa << onePerson ;}Person otherPerson ;std::ifstream ifs("out.bin", std::ios_base::binary) ;if (ifs.is_open()){boost::archive::binary_iarchive oa(ifs) ;oa >> otherPerson ;}

    • Add the above red code to the person class. Serialize class member variables in the serialize () function. According to your own needs, you can choose to serialize several variables, not all of them.
    • The example shows that the intrusion version needs to modify the original class.
    • Example 2 -- non intrusion version

      • An existing animal class is declared and defined as follows:
        • Animal. h
        • class Animal {public:Animal() ;Animal(int _age, std::string _name) ;~Animal() ;void addScore(const float _score) ;public:int m_age ;std::string m_name ;std::vector<float> m_score ;};

        • Animal. cpp 
        • Animal::Animal(){m_age = 0 ;m_name = "name" ;}Animal::~Animal(){if (m_score.size() > 0){m_score.clear() ;}}Animal::Animal(int _age, std::string _name):m_age(_age), m_name(_name){}void Animal::addScore(const float _score){m_score.push_back(_score) ;}

        • Serialized animal class:
        • class Animal {public:Animal() ;Animal(int _age, std::string _name) ;~Animal() ;void addScore(const float _score) ;public:int m_age ;std::string m_name ;std::vector<float> m_score ;};namespace boost{namespace serialization{template<class Archive>void serialize(Archive & ar, Animal & animal, const unsigned int version){ar & animal.m_age ;ar & animal.m_name ;ar & animal.m_score ;}}}

           

        • Test procedure:
        •  

          Animal oneAnimal(25, "dog") ;oneAnimal.addScore(95.2f) ;std::ofstream ofs("outAnimal.bin", std::ios_base::binary) ;if (ofs.is_open()){boost::archive::binary_oarchive oa(ofs) ;oa << oneAnimal ;}Animal otherAnimal ;std::ifstream ifs("outAnimal.bin", std::ios_base::binary) ;if (ifs.is_open()){boost::archive::binary_iarchive oa(ifs) ;oa >> otherAnimal;}

               

      • The difference between this example and Example 1 is that the original class does not need to be modified. However, this method requires the public type when the class member variable is used.
 
    • Example 3 --- non-intrusion version

      • A plant class is defined as follows:
      • Plant. h
      • class Plant {public:Plant() ;Plant(int _age, std::string _name) ;~Plant() ;void addScore(const float _score) ;public:void setScore(const std::vector<float> _scoreVct) ;std::vector<float> getScore() const ; int m_age ;std::string m_name ;private:std::vector<float> m_score ;};
      • Plant. cpp
        Plant::Plant(){m_age = 0 ;m_name = "name" ;}Plant::~Plant(){if (m_score.size() > 0){m_score.clear() ;}}Plant::Plant(int _age, std::string _name):m_age(_age), m_name(_name){}void Plant::addScore(const float _score){m_score.push_back(_score) ;}void Plant::setScore(const std::vector<float> _scoreVct) {m_score = _scoreVct ;}std::vector<float> Plant::getScore() const {return m_score ;}

      • Serialized plant class:
      • Plant. h
      • Class plant {public: Plant (); plant (INT _ age, STD: String _ name );~ Plant (); void addscore (const float _ score); Public: void setscore (const STD: vector <float> _ scorevct); STD: vector <float> getscore () const; int m_age; STD: String m_name; private: STD: vector <float> m_score;}; // This macro divides serialization: the advantages of load and save // are that the load and save functions can write different codes. The serialize function in the previous two examples acts as load and saveboost_serialization_split_free (plant) namespace boost {namespace serialization {template <class archive> void save (Archive & AR, const plant & plant, const unsigned int version) {AR & plant. m_age; AR & plant. m_name; // obtain the private member variable STD: vector <float> scores (plant. getscore (); AR & scores;} template <class archive> void load (Archive & AR, plant & plant, const unsigned int version) {AR & plant. m_age; AR & plant. m_name; // set the private member variable STD: vector <float> scores; AR & scores; plant. setscore (scores );}}}

      • Test procedure:

 Plant onePlant(25, "tree") ;onePlant.addScore(95.2f) ;std::ofstream ofs("outPlant.bin", std::ios_base::binary) ;if (ofs.is_open()){boost::archive::binary_oarchive oa(ofs) ;oa << onePlant ;}Plant otherPlant ;std::ifstream ifs("outPlant.bin", std::ios_base::binary) ;if (ifs.is_open()){boost::archive::binary_iarchive oa(ifs) ;oa >> otherPlant;}

 

      • The difference between this example and Example 2 is:

        • 1) the serialization operation of this class is divided into two actions: load and save. The operations inside the load and save functions can be different. We can write different codes for them respectively. The serialize function in the previous two examples acts as load and save, in serialization, it acts as the Save role, and in deserialization, it acts as the load role.
        • 2) For member variables not of the Public type, we can perform read and write operations on them through the public type member functions, so as to serialize them.




          -------------------------------------------------------
          <Reprint Please note: http://blog.csdn.net/icvpr>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.