Boost XML serialization usage

Source: Internet
Author: User
Simple start
The Chinese interpretation of serialization is "serializing", "serialization", or "persistence", which means to save the objects in the memory to the disk, when the program runs again, it reads the files on the disk and restores the original objects. The following is a simple example: # include <fstream> # include <iostream> # Include <boost/archive/text_oarchive.hpp> # Include <boost/archive/text_iarchive.hpp>Class A {PRIVATE ://
To enable the serialized class library to access private members, declare a friend class. Friend class boost: serialization: access;
//
Int A; Double B ;//
A serialized function that saves and restores objects. Template <class archive>
Void serialize (Archive & AR, const unsigned int Version)
{
AR &;

// This is simple. You can also use the AR <a syntax. AR & B;
}
Public: A (int aa, Double BB): A (AA), B (bb) {} A () {} void print () {STD :: cout <A <''<B <STD: Endl ;}}; int main () {STD: ofstream fout (" file.txt ");//
Write the object to the file.txt file. Boost: Archive: text_oarchive OA (fout );
//
An ostream is used to construct a OBJ (1, 2.5 ); OA <OBJ;
//
Save the OBJ object fout. Close ();//
Close the file STD: ifstream fin ("file.txt "); Boost: Archive: text_iarchive Ia (FIN );
//
Text input and archive Class A newobj; IA> newobj;
//
Restore to newobj object newobj. print (); Fin. close (); System ("pause"); Return 0;} From the above we can see that boost uses text_oarchive and text_iarchive classes to complete serialization of an object. The steps for using these two classes are: 1.

The source program contains two files: boost/archive/text_oarchive.hpp and boost/archive/text_iarchive.hpp. 2.

Add a template member function of template <class archive> void serialize (Archive & AR, const unsigned int version) to the class to be serialized. 3.

To include private members in an object, declare the boost: serialization: Access class as a friend. 4.

In the main function, create an output file stream object, use this object to construct a text_oarchive object, and then you can use the <operator to output the object. 5.

Finally, use text_iarchive to restore objects. Inheritance
If you want to serialize a subclass, the methods are different. Example: # Include <boost/serialization/base_object.hpp>
// Be sure to include this header file Class B:{Friend class boost: serialization: access; char C; Template <class archive> void serialize (Archive & AR, const unsigned int version ){ AR & boost: serialization: base_object <A> (* This );
//
Note that AR & C;} public:...}; the serialization steps for sub-classes are: 1.

Include the boost/serialization/base_object.hpp header file 2.

In the serialize template function, use the syntax AR & boost: serialization: base_object <parent class> (* This) to save the data of the parent class, you cannot directly call the serialize function of the parent class. STL container
To serialize an STL container, use the header file that comes with boost. You cannot directly # include <vector> for example: # Include <boost/serialization/list. HPP>
// The specific header file in serialization. The list. HPP file contains the STL list header file class {... List <int> list;Template <class archive> void serialize (Archive & AR, const unsigned int version ){ AR & list;
}...} In serialization, similar header files include vector. HPP string. HPP set. HPP map. HPP slist. HPP and so on. Array and pointer
Arrays and pointers can be serialized directly, for example, Class {... Int A [10]; Int * BTemplate <class archive> void serialize (Archive & AR, const unsigned int version ){ AR &;
AR & B;
}...} Other archive classes
In addition to text_iarchive and text_oarchive, there are other archive classes that can save objects as files of different formats. // A portable text archiveboost: Archive: text_oarchive (ostream & S) // savingboost: Archive: text_iarchive (istream & S) // loading // a portable text archive using a wide character streamboost: Archive: text_woarchive (wostream & S) // savingboost: Archive: text_wiarchive (batch ream & S) // loading // a non-portable Native Binary archiveboost: Archive: binary_oarchive (ostream & S) // savingboost: Archive: binary_iarchive (istream & S) // loading // a portable XML archiveboost: Archive: xml_oarchive (ostream & S) // savingboost: Archive: xml_iarchive (istream & S) // loading // a portable XML archive which uses wide characters-use for UTF-8 outputboost: Archive: xml_woarchive (wostream & S) // savingboost: Archive :: xml_wiarchive (batch ream & S) // Loading

From: http://blog.csdn.net/freerock/archive/2007/08/17/1747928.aspx

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.