本文共 8515 字,大约阅读时间需要 28 分钟。
class Vertex { private: friend class boost::serialization::access; template<class Archive> 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: vector<Vertex *> vertices; friend class boost::serialization::access; template<class Archive> 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 <iostream> #include <vector> #include <functional> #include <algorithm> #include <fstream> #include <boost/archive/text_oarchive.hpp> #include <boost/archive/text_iarchive.hpp> #include <boost/serialization/vector.hpp> using namespace std; class Vertex { private: //串行化代码开始 friend class boost::serialization::access; template<class Archive> 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: vector<Vertex *> vertices; friend class boost::serialization::access; template<class Archive> 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: vector<Polygon *> polygons; friend class boost::serialization::access; template<class Archive> 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; } |
要使用串行化库,你最少需要得到该库的1.32.0版本(早期的版本不包括串行化库)。注意,在此我不是向你介绍如何安装Boost库;网站上提供详细 步骤说明如何安装该库。如果你使用该串行化库,你还需要编译另外几个该库需要的.cpp源文件-你可以在boost_1_32_0\libs\ serialization\src文件夹下找到它们。还有一个boost_1_32_0\libs\serialization\build 库,它使用了一种新的创建(build)系统,称为jamfile,你可以用它来把源文件创建成一个库。或者,你可以仅把这些源文件添加到你的工程的 src目录下。
本文转自朱先忠老师51CTO博客,原文链接: http://blog.51cto.com/zhuxianzhong/60103,如需转载请自行联系原作者