class Vertex {
PRivate:
friend class boost::serialization::access;
template
void serialize(Archive & ar, const unsigned int version)
{
ar & x;
ar & y;
}
double x;
double y;
public:
Vertex() {} // 串行化需要一个缺省的构造器
Vertex(double newX, double newY) : x(newX), y(newY) {}
double getX() const { return x; }
double getY() const { return y; }
void dump() {
cout << x << " " << y << endl;
}
};
ar & x;
ar & y;
Vertex v1(1.5, 2.5);
std::ofstream ofs("myfile.vtx");
boost::archive::text_oarchive oa(ofs);
oa << v1;
ofs.close();
Vertex v2;
std::ifstream ifs("myfile.vtx", std::ios::binary);
boost::archive::text_iarchive ia(ifs);
ia >>v2;
ifs.close();
v2.dump();
ar & vertices;
class Polygon {
private:
vectorvertices;
friend class boost::serialization::access;
template
void serialize(Archive & ar, const unsigned int version)
{
ar & vertices;
}
public:
void addVertex(Vertex *v) {
vertices.push_back(v);
}
void dump() {
for_each(vertices.begin(), vertices.end(), mem_fun(&Vertex::dump));
}
};
ar & vertices;
for_each(vertices.begin(), vertices.end(), mem_fun(&Vertex::dump));
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
class Vertex {
private:
//串行化代码开始
friend class boost::serialization::access;
template
void serialize(Archive & ar, unsigned int version)
{
ar & x;
ar & y;
}
//结束串行化代码
double x;
double y;
public:
Vertex() {} //串行化需要一个缺省的构造器
~Vertex() {}
Vertex(double newX, double newY) : x(newX), y(newY) {}
double getX() const { return x; }
double getY() const { return y; }
void dump() {
cout << x << " " << y << endl;
}
};
void delete_vertex(Vertex *v) { delete v; }
class Polygon {
private:
vectorvertices;
friend class boost::serialization::access;
template
void serialize(Archive & ar, const unsigned int version)
{
ar & vertices;
}
public:
~Polygon() {
for_each(vertices.begin(), vertices.end(), delete_vertex);
}
void addVertex(Vertex *v) {
vertices.push_back(v);
}
void dump() {
for_each(vertices.begin(), vertices.end(), mem_fun(&Vertex::dump));
}
};
void delete_poly(Polygon *p) { delete p; }
class Drawing {
private:
vectorpolygons;
friend class boost::serialization::access;
template
void serialize(Archive & ar, const unsigned int version)
{
ar & polygons;
}
public:
~Drawing() {
for_each(polygons.begin(), polygons.end(), delete_poly);
}
void addPolygon(Polygon *p) {
polygons.push_back(p);
}
void dump() {
for_each(polygons.begin(), polygons.end(), mem_fun(&Polygon::dump));
}
};
string getFileOpen() {
//在实际开发中,这将调用一个各种样的FileOpen 对话框
return "c:/myfile.grp";
}
string getFileSaveAs() {
//在实际开发中,这将调用一个各种样的FileSave 对话框
return "c:/myfile.grp";
}
void saveDocument(Drawing *doc, const string &filename) {
ofstream ofs(filename.c_str());
boost::archive::text_oarchive oa(ofs);
oa << *doc;
ofs.close();
}
Drawing *openDocument(const string &filename) {
Drawing *doc = new Drawing();
std::ifstream ifs(filename.c_str(), std::ios::binary);
boost::archive::text_iarchive ia(ifs);
ia >>*doc;
ifs.close();
return doc;
}
int main()
{
Polygon *poly1 = new Polygon();
poly1->addVertex(new Vertex(0.1,0.2));
poly1->addVertex(new Vertex(1.5,1.5));
poly1->addVertex(new Vertex(0.5,2.9));
Polygon *poly2 = new Polygon();
poly2->addVertex(new Vertex(0,0));
poly2->addVertex(new Vertex(0,1.5));
poly2->addVertex(new Vertex(1.5,1.5));
poly2->addVertex(new Vertex(1.5,0));
Drawing *draw = new Drawing();
draw->addPolygon(poly1);
draw->addPolygon(poly2);
//演示保存一个文档
saveDocument(draw, getFileSaveAs());
// 演示打开一个文档
string filename2 = getFileOpen();
Drawing *doc2 = openDocument(getFileOpen());
doc2->dump();
delete draw;
return 0;
}
新闻热点
疑难解答