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

6 篇文章 0 订阅
根据官方文档改写:
[url]http://www.boost.org/doc/libs/1_45_0/libs/serialization/doc/tutorial.html#arrays[/url]

[url]http://www.fuzhijie.me/?p=62[/url]



#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 boost


int _tmain(int argc, _TCHAR* argv[])
{
//create and open a character archive for output
std::ofstream ofs("filename");
const gps_position g(35, 59, 24.57);

//save data to archive
char 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 archive
oa << 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 archive
boost::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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值