This flaw exists when I read several serialization frameworks.
1. boost is too big to learn
2. eternity does not support non-intrusive methods. serialization methods must be added to the class. Nesting such as map <int, vector <my_class> is not supported.
3. s11n is too complex and does not support binary storage, resulting in large files.
So I tried to implement the following functions.
1. Non-Intrusive
2. Supports native pod types, such as int, double, and pointer.
3. Support for stl containers
4. Binary Storage
Example:
[Cpp]
# Include <windows. h>
# Include <iostream>
# Include <fstream>
# Include <map>
# Include <vector>
# Include <algorithm>
Using namespace std;
# Include "serialization. h"
# Include "stl/vector. h"
# Include "stl/map. h"
# Include "stl/string. h"
Struct packet {
Int x;
String name;
};
Typedef map <int, vector <packet> vmap;
Vmap m;
Template <class Stream>
Void serialize (Stream & stream, packet & p ){
Serialize (stream, p. x );
Serialize (stream, p. name );
}
Template <class Stream>
Void de_serialize (Stream & stream, packet & p ){
De_serialize (stream, p. x );
De_serialize (stream, p. name );
}
Vector <packet *> vv;
Int main (){
Packet * p1 = new packet;
P1-> x = 10; p1-> name = "p1 ";
Packet * p2 = new packet;
P2-> x = 20; p2-> name = "p2 ";
Vv. push_back (p1 );
Vv. push_back (p2 );
Ofstream ofss;
Ofss. open ("pppppppppp.txt", ios: binary );
Serialize (cout, vv );
Ofss. close ();
Vv. clear ();
Ifstream ifss;
Ifss. open ("pppppppppp.txt", ios: binary );
De_serialize (ifss, vv );
Ifss. close ();
Printf ("vv. size = % d \ n", vv. size ());
For_each (vv. begin (), vv. end (), [] (packet * pp ){
Printf ("x: % d, name: % s \ n", pp-> x, pp-> name. c_str ());
});
Printf ("=========================\ n ");
Packet p;
P. x = 3; p. name = "name1 ";
M [1]. push_back (p );
P. x = 4; p. name = "name2 ";
M [2]. push_back (p );
Ofstream ofs;
Ofs. open ("zzzzzzzzzzzzzzzzzzzzz.txt", ios: binary );
Serialize (ofs, m );
Ofs. close ();
// ZeroMemory (& p, sizeof (p ));
M. clear ();
Ifstream ifs;
Ifs. open ("zzzzzzzzzzzzzzzzzzzzz.txt", ios: binary );
De_serialize (ifs, m );
Ifs. close ();
Printf ("m. size = % d \ n", m. size ());
For_each (m. begin (), m. end (), [] (pair <int, vector <packet> p ){
Printf ("m [% d]. size = % d \ n", p. first, p. second. size ());
For_each (p. second. begin (), p. second. end (), [] (packet p ){
Printf ("x = % d, name = % s \ n", p. x, p. name. c_str ());
});
});
}