1. 程式人生 > >序列化支援 4 —Boost的序列化庫的強大之處

序列化支援 4 —Boost的序列化庫的強大之處

分享一下我老師大神的人工智慧教程!零基礎,通俗易懂!http://blog.csdn.net/jiangjunshow

也歡迎大家轉載本篇文章。分享知識,造福人民,實現我們中華民族偉大復興!

               

序列化支援(4)—Boost的序列化庫的強大之處

write by 九天雁翎(JTianLing) -- blog.csdn.net/vagrxie

討論新聞組及檔案

1.     
非介入式版本

感覺實際使用中我還沒有碰到過,既然需要完全的public才能還原資料,那麼介入不介入好像影響也不大了,除非碰到一個東西是別的公司寫的,不讓改,我是還沒有碰到這樣的情況。

 

從這裡開始見識Boost序列化庫的強大。。。。。。。。

 

2.      指標的序列化:

下面的例子為文件中例子的簡化,並新增必要部分以方便執行及演示。

// BoostLearn.cpp : 定義控制檯應用程式的入口點。

//

 

#include "stdafx.h"

 

#include

<fstream>

 

// 包含以簡單文字格式實現存檔的標頭檔案

#include <boost/archive/text_oarchive.hpp>

#include <boost/archive/text_iarchive.hpp>

 

/////////////////////////////////////////////////////////////

// gps 座標

//

// 舉例說明簡單型別的序列化

//

class gps_position

{

private:

    friend class boost::serialization::access;

   

// 如果類Archive 是一個輸出存檔,則操作符& 被定義為<<.  同樣,如果類Archive

    // 是一個輸入存檔,則操作符& 被定義為>>.

    template<class Archive>

    void serialize(Archive & ar, const unsigned int version)

    {

       ar & degrees;

       ar & minutes;

       ar & seconds;

    }

    int degrees;

    int minutes;

    float seconds;

public:

    gps_position()

    {

       degrees = 0;

       minutes = 0;

       seconds = 0.0;

    };

    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 & latitude;

       ar & 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(){ }

};

 

class bus_stop_corner : public bus_stop

{

    friend class boost::serialization::access;

    template<class Archive>

    void serialize(Archive & ar, const unsigned int version)

    {

       // 序列化基類資訊

       ar & boost::serialization::base_object<bus_stop>(*this);

       ar & street1;

       ar & street2;

    }

    std::string street1;

    std::string 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_)

    {}

 

    virtual std::string description() const

    {

       return street1 + " and " + street2;

    }

};

 

 

class bus_route

{

    friend class boost::serialization::access;

    bus_stop_corner * stops[2];

    template<class Archive>

    void serialize(Archive & ar, const unsigned int version)

    {

       int i;

       for(i = 0; i < 2; ++i)

           ar & stops[i];

    }

public:

    bus_route(bus_stop_corner *apStop1, bus_stop_corner *apStop2)

    {

       stops[0] = apStop1;

       stops[1] = apStop2;

    }

    bus_route(){}

};

 

 

 

int main() {

    // 建立並開啟一個輸出用的字元存檔

    std::ofstream ofs("bus_route");

 

    // 建立類例項

    const gps_position latitude(1, 2, 3.3f);

    const gps_position longitude(4, 5, 6.6f);

 

    bus_stop_corner *lpStop1 = new bus_stop_corner(latitude, longitude, "corn1", "corn2");

    bus_stop_corner *lpStop2 = new bus_stop_corner(latitude, longitude, "corn3", "corn4");

 

    bus_route route(lpStop1, lpStop2);

 

    // 儲存資料到存檔

    {

       boost::archive::text_oarchive oa(ofs);

       // 將類例項寫出到存檔

       oa << route;

       // 在呼叫解構函式時將關閉存檔和流

    }

 

    // ... 晚些時候,將類例項恢復到原來的狀態

    bus_route new_route;

    {

       // 建立並開啟一個輸入用的存檔

       std::ifstream ifs("bus_route", std::ios::binary);

       boost::archive::text_iarchive ia(ifs);

       // 從存檔中讀取類的狀態

       ia >> new_route;

       // 在呼叫解構函式時將關閉存檔和流

    }