读书人

运用数组来实现类的序列化 boost

发布时间: 2012-08-28 12:37:01 作者: rapoo

使用数组来实现类的序列化, boost
根据官方文档改写:
http://www.boost.org/doc/libs/1_45_0/libs/serialization/doc/tutorial.html#arrays

http://www.fuzhijie.me/?p=62

#include "stdafx.h"#include <windows.h>#include <process.h>#include <iostream>#include <exception>#include <assert.h>#include <fstream>//include headers that implement a archive in simple text format#include "boost/archive/text_oarchive.hpp"#include "boost/archive/text_iarchive.hpp"#include "boost/archive/binary_iarchive.hpp"#include "boost/archive/binary_oarchive.hpp"#include "boost/serialization/vector.hpp"#include "boost/serialization/string.hpp"#include "boost/iostreams/stream.hpp"#pragma command(lib, "boost_serialization-vc100-mt-1_48.lib")#pragma command(lig, "boost_iostreams-vc100-mt-1_48.lib");using namespace std;/////////////////////////////////////////////////////////////// gps coordinate//// illustrates serialization for a simple type//typedef struct gps_struct{char name[100];char data[2][100];string version;int id;}gps_struct;class gps_position{public:// friend class boost::serialization::access;// // when the class Archive corresponds to an output archive, the// // & operation is defined similar to <<, Likewise, when the class Archive // // is a type of input archive the  & operator is defined similar to >>.// template<class Archive>// void serialize(Archive &ar, const unsigned int version)// {// ar & degrees;// ar & minutes;// ar & seconds;// }int degrees;int minutes;float seconds;vector<string> hellos;gps_struct time;public:gps_position(){};gps_position(int d, int m, float s) :degrees(d), minutes(m), seconds(s){hellos.push_back("test1");hellos.push_back("test2");hellos.push_back("test3");strcpy(time.name, "Kevin He");time.version = "1.0";time.id =100;strcpy(time.data[0], "time1");strcpy(time.data[1], "time2");}};namespace boost {namespace serialization {template<class Archive>void serialize(Archive & ar, gps_struct & g, const unsigned int version){ar & g.id;ar & g.name;ar & g.version;ar & g.data;}template<class Archive>void serialize(Archive & ar, gps_position & g, const unsigned int version){ar & g.degrees;ar & g.minutes;ar & g.seconds;ar & g.hellos;ar & g.time;}} // namespace serialization} // namespace boostint _tmain(int argc, _TCHAR* argv[]){//create and open a character archive for outputstd::ofstream ofs("filename");const gps_position g(35, 59, 24.57);//save data to archivechar buffer[4096];int bufferSize = 4096;string mydata;{//boost::archive::text_oarchive oa(ofs);memset(buffer, 0, bufferSize);boost::iostreams::basic_array<char> sr(buffer, bufferSize);boost::iostreams::stream< boost::iostreams::basic_array<char> > source(sr);boost::archive::text_oarchive oa(source);//write class instance to archiveoa << g;//archive and stream closed when destructors are called;}gps_position newg;{//std::ifstream ifs("filename");//boost::archive::text_iarchive ia(ifs);// read class state from archiveboost::iostreams::basic_array<char> sr(buffer, bufferSize);boost::iostreams::basic_array<char> temp(buffer, bufferSize);boost::iostreams::stream< boost::iostreams::basic_array<char> > newsource(temp);boost::archive::text_iarchive ia(newsource);ia >> newg;}// ... some time late restore the class instance to its orginal state return 0;}

读书人网 >操作系统

热点推荐