https://www.ibm.com/developerworks/cn/aix/library/au-boostserialization/index.html
https://bbs.csdn.net/topics/390947874?utm_medium=distribute.pc_relevant_t0.none-task-discussion_topic-BlogCommendFromBaidu-1.channel_param&depth_1-utm_source=distribute.pc_relevant_t0.none-task-discussion_topic-BlogCommendFromBaidu-1.channel_param
#include<boost/archive/text_oarchive.hpp>
#include <boost/archive/text_iarchive.hpp>
#include <boost/archive/xml_oarchive.hpp>
#include <boost/archive/xml_iarchive.hpp>
#include <boost/serialization/map.hpp>
#include <boost/serialization/assume_abstract.hpp>
#include <fstream>
const int SIZE = 4;
using namespace std;
// 可序列化的成员
// 一个可序列化的类,可拥有可序列化的成员(普通内置类型成员变量)
class gps_position
{
private:
friend class boost::serialization::access;
template<class Archive>
void serialize(Archive & ar, const unsigned int version)
{
ar & BOOST_SERIALIZATION_NVP(degrees);
ar & BOOST_SERIALIZATION_NVP(minutes);
ar & BOOST_SERIALIZATION_NVP(seconds);
}
int degrees; // 度
int minutes; // 分
float seconds; // 秒
public:
gps_position() {};
gps_position(int d, int m, float s) :
degrees(d), minutes(m), seconds(s)
{}
};
// 可序列化的成员
// 一个可序列化的类,可拥有可序列化的成员(类类型成员变量)
class bus_stop
{
friend class boost::serialization::access;
template<class Archive>
void serialize(Archive & ar, const unsigned int version)
{
ar & BOOST_SERIALIZATION_NVP(latitude);
ar & BOOST_SERIALIZATION_NVP(longitude);
}
gps_position latitude; // 经度
gps_position longitude; // 纬度
public:
bus_stop() {}
bus_stop(const gps_position & lat_, const gps_position & long_) :
latitude(lat_), longitude(long_)
{}
virtual ~bus_stop() {}
};
BOOST_SERIALIZATION_ASSUME_ABSTRACT(bus_stop)
// 派生类
// 派生类应包含其基类的序列化
class bus_stop_corner : public bus_stop
{
friend class boost::serialization::access;
template<class Archive>
void serialize(Archive & ar, const unsigned int version)
{
// serialize base class information(序列化基类)
ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(bus_stop);// 注意在派生类中不要直接调用其基类的序列化函数
ar & BOOST_SERIALIZATION_NVP(street1);
ar & BOOST_SERIALIZATION_NVP(street2);
}
std::string street1; // 停靠街区1
std::string street2; // 停靠街区2
virtual std::string description() const
{
return street1 + " and " + street2;
}
public:
bus_stop_corner() {}
bus_stop_corner(const gps_position & lat_, const gps_position &long_,
const std::string & s1_, const std::string & s2_
) :
bus_stop(lat_, long_), street1(s1_), street2(s2_)
{}
};
class bus_stop_destination : public bus_stop
{
friend class boost::serialization::access;
std::string name;
virtual std::string description() const
{
return name;
}
template<class Archive>
void serialize(Archive &ar, const unsigned int version)
{
ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(bus_stop); // 序列化基类对象
ar & BOOST_SERIALIZATION_NVP(name);
}
public:
bus_stop_destination() {}
bus_stop_destination(
const gps_position & _lat, const gps_position & _long, const std::string & _name
) :
bus_stop(_lat, _long), name(_name)
{
}
};
// STL容器
#include <boost/serialization/list.hpp>
// 修改后的类
#include<boost/serialization/version.hpp>
class bus_route2_2
{
public:
friend class boost::serialization::access;
std::list<bus_stop *> stops;
std::string driverName;
//template<class Archive>
//void serialize(Archive & ar, const unsigned int version)
//{
// ar.register_type(static_cast<bus_stop_corner *>(NULL)); // 序列化bus_stop的派生类对象时需要注册其类型,否则报错
// ar.register_type(static_cast<bus_stop_destination *>(NULL));
// // only save/load driver_name for newer archives
// if (version > 0)// version = 1
// {
// ar & driverName;
// }
// ar & stops;
//}
// xml
template<class Archive>
void serialize(Archive& ar, const unsigned int version)
{
// 注意这个宏(变量名存储进去, XML文件的节点将作为节点的名称)
ar.register_type(static_cast<bus_stop_corner *>(NULL)); // 序列化bus_stop的派生类对象时需要注册其类型,否则报错
ar.register_type(static_cast<bus_stop_destination *>(NULL));
ar & BOOST_SERIALIZATION_NVP(driverName);
ar & BOOST_SERIALIZATION_NVP(stops);
}
public:
void Append(bus_stop* bs)
{
stops.push_back(bs);
}
bus_route2_2(){}
};
BOOST_CLASS_VERSION(bus_route2_2, 1)
#include <boost/archive/tmpdir.hpp>
// 保存\加载一条路线
void save_bus_route(const bus_route2_2 &s, const char * filename) {
std::ofstream ofs(filename);
boost::archive::xml_oarchive oa(ofs);
oa << BOOST_SERIALIZATION_NVP(s);
}
void load_bus_route(bus_route2_2 &s, const char * filename)
{
std::ifstream ifs(filename);
boost::archive::xml_iarchive ia(ifs);
ia >> BOOST_SERIALIZATION_NVP(s);
}
int main()
{
{
bus_stop *bs0 = new bus_stop(
gps_position(34, 135, 52.560f),
gps_position(134, 22, 78.30f)
);
bus_stop *bs1 = new bus_stop(
gps_position(35, 137, 23.456f),
gps_position(133, 35, 54.12f)
);
bus_stop *bs2 = new bus_stop_corner(
gps_position(35, 136, 15.456f),
gps_position(133, 32, 15.300f),
"24th Street", "10th Avenue"
);
bus_stop *bs3 = new bus_stop_corner(
gps_position(35, 134, 48.789f),
gps_position(133, 32, 16.230f),
"State street", "Cathedral Vista Lane"
);
bus_stop *bs4 = new bus_stop_destination(
gps_position(35, 134, 48.789f),
gps_position(133, 32, 16.230f),
"Cathedral Vista Lane11111"
);
bus_stop *bs5 = new bus_stop_destination(
gps_position(35, 134, 48.789f),
gps_position(133, 32, 16.230f),
"Cathedral Vista Lane22222"
);
bus_route2_2 saveroute, loadsroute;
saveroute.Append(bs0);
saveroute.Append(bs1);
saveroute.Append(bs2);
saveroute.Append(bs3);
saveroute.Append(bs4);
saveroute.Append(bs5);
saveroute.driverName = "老王";
std::string filename/*(boost::archive::tmpdir())*/;
filename += "filename3.xml";
save_bus_route(saveroute, filename.c_str());
load_bus_route(loadsroute, filename.c_str());
delete bs0; bs0 = NULL;
delete bs1; bs1 = NULL;
delete bs2; bs2 = NULL;
delete bs3; bs3 = NULL;
int ac = 1;
}
return 0;
}