序列化:序列化(Serialization)库教程来源: 发布时间:星期日, 2009年8月16日 浏览:188次 评论:0
1.
输出档案(archive)类似于输出数据流(stream) ar << data; ar & data; 输入档案(archive)类似于输入数据流(stream) ar >> data; ar & data; 对于原始数据类型 通常用serialize 这个库包含 # // # # ///////////////////////////////////////////////////////////// // gps coordinate // // illustrates serialization for a simple type // { private: friend // When the // & operator is // is a type of input archive the & operator is // 这个模板 template< void serialize(Archive & ar, const unsigned { ar & degrees; ar & minutes; ar & seconds; } float seconds; public: gps_position gps_position( degrees(d), minutes(m), seconds(s) {} }; { // create and open a character archive for output std::ofstream ofs("filename"); boost::archive::text_oarchive oa(ofs); // create const gps_position g(35, 59, 24.567f); // write oa << g; // close archive ofs.close // ... some time later restore the // create and open an archive for input std:: boost::archive::text_iarchive ia( // read gps_position ia >> // close archive } 对于每个通过序列化“被存储” 2. 非侵入 在上例是侵入 # # { public: float seconds; gps_position gps_position( degrees(d), minutes(m), seconds(s) {} }; // 注意命名空间 template< void serialize(Archive & ar, gps_position & g, const unsigned { ar & g.degrees; ar & g.minutes; ar & g.seconds; } } // } // 这种情况生成 非侵入序列化主要应用在不改变类定义就可实现类 3. 可序列化 { friend template< void serialize(Archive & ar, const unsigned { ar & latitude; ar & longitude; } gps_position latitude; gps_position longitude; protected: bus_stop(const gps_position & lat_, const gps_position & long_) : latitude(lat_), longitude(long_) {} public: bus_stop // See item # 14 in Effective C // re non-virtual destructors in base virtual ~bus_stop }; 这里 注意 4. 派生类 派生类应包含其基类 # { friend template< void serialize(Archive & ar, const unsigned { // serialize base // 基类 ar & boost::serialization::base_object<bus_stop>(*this); ar & street1; ar & street2; } std:: std:: virtual std:: { } public: bus_stop_corner bus_stop_corner(const gps_position & lat_, const gps_position & long_, const std:: ) : bus_stop(lat_, long_), street1(s1_), street2(s2_) {} }; 注意在派生类中不要直接 5. 指针 假设我们定义了bus route包含 我们可以有几种bus stop { friend bus_stop * stops[10]; template< void serialize(Archive & ar, const unsigned { for(i = 0; i < 10; ar & stops[i]; } public: bus_route }; 所有这 6. 事实上上述方案比较复杂 { friend bus_stop * stops[10]; template< void serialize(Archive & ar, const unsigned { ar & stops; } public: bus_route }; 7. STL容器 上面 # { friend std::list<bus_stop *> stops; template< void serialize(Archive & ar, const unsigned { ar & stops; } public: bus_route }; 8. 类 假设我们对bus_route类满意 # # { friend std::list<bus_stop *> stops; std:: template< void serialize(Archive & ar, const unsigned { ar & driver_name; ar & stops; } public: bus_route }; 好 通常 # # # { friend std::list<bus_stop *> stops; std:: template< void serialize(Archive & ar, const unsigned { // only save/load driver_name for ar & driver_name; ar & stops; } public: bus_route }; BOOST_CLASS_VERSION(bus_route, 1) 对每个类通过应用 9. 把serialize拆分成save/load serialize # # # # { friend std::list<bus_stop *> stops; std:: template< void save(Archive & ar, const unsigned { // note, version is always the latest when saving ar & driver_name; ar & stops; } template< void load(Archive & ar, const unsigned { ar & driver_name; ar & stops; } BOOST_SERIALIZATION_SPLIT_MEMBER public: bus_route }; BOOST_CLASS_VERSION(bus_route, 1) BOOST_SERIALIZATION_SPLIT_MEMBER 10. 档案 我们这里讨论将聚焦到类 在这篇指南中 假如当前 注意我们 T完整 创建各种类别 显示它 序列化到 还原到另 显示被存储 这个 0
相关文章
读者评论发表评论 |
