Boost: serialization split serialize Function

Source: Internet
Author: User

In the previous article "boost: serialization", the base class pointer is used to store the derived class (a lot of errors, a wave of twists and turns). In this article, we use the serialize function to implement serialization. The code format is as follows:

private:friend class boost::serialization::access;template
 
  void serialize(Archive& ar, const unsigned int version){ar & BOOST_SERIALIZATION_NVP(name_);ar & BOOST_SERIALIZATION_VP(number_);ar & BOOST_SERIALIZATION_NVP(grade_);}
 
In fact, this function is equivalent to two functions: but when we use xxx_oarchive, it is equivalent to saving the object data to the document,
When xxx_iachive is used, it is equivalent to load data from the document to the object data. Therefore, it can be split into two functions.
Save and load functions. The preceding example is also used.
Save function:
template
 
  void save(Archive& ar, const unsigned int version) const{ar << BOOST_SERIALIZATION_NVP(name_);ar << BOOST_SERIALIZATION_NVP(number_);ar << BOOST_SERIALIZATION_NVP(grade_);}
 
Load function:
template
 
  void load(Archive& ar, const unsigned int version){ar >> BOOST_SERIALIZATION_NVP(name_);ar >> BOOST_SERIALIZATION_NVP(number_);ar >> BOOST_SERIALIZATION_NVP(grade_);}
 
Add a macro:
BOOST_SERIALIZATION_SPLIT_MEMBER() //must be part of class
This macro must be inside the class. In fact, this macro implements a template function:
#define BOOST_SERIALIZATION_SPLIT_MEMBER()                       \template
 
                                            \void serialize(                                                  \Archive &ar,                                                 \const unsigned int file_version                              \){                                                               \boost::serialization::split_member(ar, *this, file_version); \} 
 
It is to call a split_member function, which is in the header file split_member.hpp:
template
 
  inline void split_member( Archive & ar, T & t, const unsigned int file_version){typedef BOOST_DEDUCED_TYPENAME mpl::eval_if
  
   , mpl::identity
   
    >::type typex;//#1typex::invoke(ar, t, file_version);}
   
  
 
#1 use traits technology to export the type. In fact, this type has only two types.
Template Struct member_saver;
Template Struct member_loader;
Both struct have an invoke function (in the header file split_member.hpp),
Respectively, call the access-like member_save and member_load. And the two functions are respectively
Call our own save and load functions. This is why the boost: serialization: access class should be declared.
Reasons for the membership category:
Template
   
    
Static void member_save (Archive & ar, // const T & t, T & t, const unsigned int file_version) {t. save (ar, file_version); // access class member function member_save. It can be seen that the boost design mode is complex, but it is worth learning} friend class boost: serialization: access; // Our save and load Code
   
In this way, access can call the save and load Code we have written.
Now let's take a look at the complete code:
Class student_info {public: student_info () {} virtual ~ Student_info () {} student_info (const std: string & sn, const std: string & snm, const std: string & sg, const std: string & cs ): name _ (sn), number _ (snm), grade _ (sg), class _ (cs) {}virtual void print_info () const {std :: cout <name _ <"" <number _ <"" <grade _ <"" <class _ <"";} private: // friend class boost: serialization: access;/* template
   
    
Void serialize (Archive & ar, const unsigned int version) {ar & BOOST_SERIALIZATION_NVP (name _); ar & BOOST_SERIALIZATION_VP (number _); ar & BOOST_SERIALIZATION_NVP (grade _);} */friend class boost: serialization: access; // The member function member_save (load) of the youyuan class can call our own save and loadtemplate.
    
     
Void save (Archive & ar, const unsigned int version) const {ar <BOOST_SERIALIZATION_NVP (name _); ar <BOOST_SERIALIZATION_NVP (number _); ar <BOOST_SERIALIZATION_NVP (grade _); ar <BOOST_SERIALIZATION_NVP (class _);} template
     
      
Void load (Archive & ar, const unsigned int version) {ar> BOOST_SERIALIZATION_NVP (name _); ar> BOOST_SERIALIZATION_NVP (number _); ar >>> BOOST_SERIALIZATION_NVP (grade _);} BOOST_SERIALIZATION_SPLIT_MEMBER () // must be part of classprivate: std: string name _; std: string number _; std :: string grade _;};
     
    
   


Next, let's take a look at the previous article's legacy problem: the serialize function:
class student_info{public://...private:friend class boost::serialization::access;template
   
    void serialize(Archive& ar, const unsigned int version){ar & BOOST_SERIALIZATION_NVP(name_);ar & BOOST_SERIALIZATION_VP(number_);ar & BOOST_SERIALIZATION_NVP(grade_);}private:std::string name_;std::string number_;std::string grade_;};
   
This function is intrusive. We can also use a non-intrusive:
class student_info{public://...private:template
   
    friend void serialize(Archive& ar, student_info &sdinfo, const unsigned int file_version);private:std::string name_;std::string number_;std::string grade_;};
   
Implement the serialize function outside the class:
template
   
    void serialize(Archive& ar, student_info &sdinfo, const unsigned int file_version){ar & BOOST_SERIALIZATION_NVP(sdinfo.name_);ar & BOOST_SERIALIZATION_NVP(sdinfo.number_);ar & BOOST_SERIALIZATION_NVP(sdinfo.grade_);}
   
In this case, OK.











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.