Learning boost 1 serialization

xiaoxiao2021-03-06  53

Learning Boost (1)

Serialization

Simple start

The Chinese explanation of Serialization is "Serialization", "Serialization" or "persistence", which is to save objects in memory to disk, wait until the program is run again read the files in the disk to restore the original object. Let's take a simple example:

#include

#include

#include

#include

Class A

{

Private:

/ / In order to allow the serialization library to access private members, you must declare a friend class.

Friend Class Boost :: Serialization :: Access;

// Object data

Int a;

Double B;

// Serialized function, this function completes the object's save and recovery

Template

Void Serialize (Archive & Ar, Const Unsigned Int Version)

{

Ar & a; // is as simple or use AR << a syntax like this

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); // Text output archive class, use an Ostream to construct

A Obj (1, 2.5);

OA << Obj; // Save OBJ object

Fout.close (); // Close file

Std :: ifstream fin ("file.txt");

Boost :: Archive :: Text_iarchive IA (FIN); // Text input archive class

A newobj;

IA >> Newobj; // Restore to Newobj object

NEWOBJ.PRINT ();

Fin.close ();

System ("pause");

Return 0;

}

As can be seen from the above, Boost is serialized by using the Text_oarchive and the Text_iarchive class to complete the sequence of an object. The steps to use these two classes are:

1. In the source program contains Boost / Archive / Text_oarchive.HPP and Boost / Archive / Text_iarchive.hpp these two files.

2. A template member function of a Template Void Serialize (Archive & Ar, Const Unsigned Int Version) is added to the desired sequence.

3. If you need a private member in the object, you need to 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 an object.

5. Finally, the same, use text_iarchive to restore the object.

inherit

If you want to serialize a subclass, the method is different. Example: #include // must include this header file

Class B: a

{

Friend Class Boost :: Serialization :: Access;

Char C;

Template

Void Serialize (Archive & Ar, Const Unsigned Int Version)

{

Ar & Boost :: Serialization :: Base_Object (* this); // Note

Ar & C;

}

PUBLIC:

...

}

The step of serializing the subclass is:

1. Contains Boost / Serialization / Base_Object.hpp header

2. In the Serialize template function, use the AR & BOOST :: Serialization :: Base_Object (* this) to save the parent class's data, not directly call the Qiqi's serialize function

STL container

If you want to serialize a STL container, use Boost's own header file, you can't directly #include

E.g:

#include // Serialization, in List.hpp already contains STL's list header file

Class A

{

...

List list;

Template

Void Serialize (Archive & Ar, Const Unsigned Int Version)

{

Ar & list;

}

...

}

In Serialization, similar headers also have vector.hpp string.hpp set.hpp map.hpp slist.hpp, and more.

Array and pointers

For arrays and pointers, they can be directly serialized.

Class A

{

...

Int a [10];

INT * B

Template

Void Serialize (Archive & Ar, Const Unsigned Int Version)

{

Ar & A;

Ar & B;

}

...

}

Other Archive class

In addition to Text_iarchive and Text_oarchive, there are other Archive classes that save objects into different formats.

// a Portable Text Archive

Boost :: Archive :: Text_oarchive (Ostream & S) // Saving

Boost :: Archive :: Text_iarchive (Istream & S) // loading

// a portable text archive using a wide character stream

Boost :: Archive :: Text_woarchive (Wostream & S) // Saving

Boost :: Archive :: Text_wiarchive (Wistream & S) // loading

// a non-portable native binary archive

Boost :: Archive :: binary_oarchive (Ostream & S) // Saving

Boost :: Archive :: binary_iarchive (istream & s) // loading // a portable XML Archive

Boost :: Archive :: XML_oarchive (Ostream & S) // Saving

Boost :: Archive :: XML_iarchive (istream & s) // loading

// a portable XML Archive Which Uses Wide Characters - Use for UTF-8 OUTPUT

Boost :: Archive :: Xml_woarchive (Wostream & S) // Saving

Boost :: Archive :: Xml_wiarchive (Wistream & s) // loading


New Post(0)