• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            woaidongmao

            文章均收錄自他人博客,但不喜標(biāo)題前加-[轉(zhuǎn)貼],因其丑陋,見諒!~
            隨筆 - 1469, 文章 - 0, 評論 - 661, 引用 - 0
            數(shù)據(jù)加載中……

            Boost Serialization 庫教程

            輸出檔案(archive)類似于輸出數(shù)據(jù)流(stream)。數(shù)據(jù)能通過<< 或 & 操作符存儲到檔案(archive)中:

             
            ar << data;
            ar & data;

            輸入檔案(archive)類似于輸入數(shù)據(jù)流(stream)。數(shù)據(jù)能通過>> 或 & 操作符從檔案(archive)中裝載。

             
            ar >> data;
            ar & data;

            對于原始數(shù)據(jù)類型,當(dāng)這些操作調(diào)用的時候,數(shù)據(jù)是簡單的“被存儲/被裝載” “到/從” 檔案(archive)。對于類(class)數(shù)據(jù)類型,類的serialize 函數(shù)被調(diào)用。對上面的操作,每個serialize 函數(shù)用來“存儲/裝載”其數(shù)據(jù)成員。這個處理采用遞歸的方式,直到所有包含在類中的數(shù)據(jù)“被存儲/被裝載”。

             

            一個非常簡單的情形

            通常用serialize 函數(shù)來存儲和裝載類的數(shù)據(jù)成員。

            這個庫包含一個叫 demo.cpp 的程序,用于介紹如何用這個庫。下面,我們從這個demo摘錄代碼,來介紹這個庫應(yīng)用的最簡單情形。

             
            #include <fstream>
             
            // include headers that implement a archive in simple text format
            #include <boost/archive/text_oarchive.hpp>
            #include <boost/archive/text_iarchive.hpp>
             
            /////////////////////////////////////////////////////////////
            // gps coordinate
            //
            // illustrates serialization for a simple type
            //
            class gps_position
            {
            private:
                friend class boost::serialization::access;
                // When the class Archive corresponds to an output archive, the
                // & operator 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;
            public:
                gps_position(){};
                gps_position(int d, int m, float s) :
                    degrees(d), minutes(m), seconds(s)
                {}
            };
             
            int main() {
                // create and open a character archive for output
                std::ofstream ofs("filename");
                boost::archive::text_oarchive oa(ofs);
             
                // create class instance
                const gps_position g(35, 59, 24.567f);
                // write class instance to archive
                oa << g;
                // close archive
                ofs.close();
             
                // ... some time later restore the class instance to its orginal state
                // create and open an archive for input
                std::ifstream ifs("filename", std::ios::binary);
                boost::archive::text_iarchive ia(ifs);
                // read class state from archive
                gps_position newg;
                ia >> newg;
                // close archive
                ifs.close();
                return 0;
            }
             

            對于每個通過序列化“被存儲”的類,必須存在一個函數(shù)去實現(xiàn)“存儲”其所有狀態(tài)數(shù)據(jù)。對于每個通過序列化“被裝載”的類,必須存在一個函數(shù)來實現(xiàn)“裝載”其所有狀態(tài)數(shù)據(jù)。在上面的例子中,這些函數(shù)是模板成員函數(shù)serialize。

             

            非侵入的版本

            在上例是侵入的設(shè)計。類是需要由其實例來序列化,來改變。這在某些情形是困難的。一個等價的可選的設(shè)計如下:

             
            #include <boost/archive/text_oarchive.hpp>
            #include <boost/archive/text_iarchive.hpp>
             
            class gps_position
            {
            public:
                int degrees;
                int minutes;
                float seconds;
                gps_position(){};
                gps_position(int d, int m, float s) :
                    degrees(d), minutes(m), seconds(s)
                {}
            };
             
            namespace boost {
            namespace serialization {
             
            template<class Archive>
            void serialize(Archive & ar, gps_position & g, const unsigned int version)
            {
                ar & g.degrees;
                ar & g.minutes;
                ar & g.seconds;
            }
             
            } // namespace serialization
            } // namespace boost

            這種情況生成的serialize 函數(shù)不是gps_position類的成員函數(shù)。這有異曲同工之妙。

            非侵入序列化主要應(yīng)用在不改變類定義就可實現(xiàn)類的序列化。為實現(xiàn)這種可能,類必須提供足夠的信息來更新類狀態(tài)。在這個例子中,我們假設(shè)類有public成員。僅當(dāng)提供足夠信息來存儲和裝載的類,才能不改變類自身,在外部來序列化類狀態(tài)。

             

            可序列化的成員

            一個可序列化的類,可擁有可序列化的成員,例如:

             
            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;
            protected:
                bus_stop(const gps_position & lat_, const gps_position & long_) :
                latitude(lat_), longitude(long_)
                {}
            public:
                bus_stop(){}
                // See item # 14 in Effective C++ by Scott Meyers.
                // re non-virtual destructors in base classes.
                virtual ~bus_stop(){}
            };

            這里,類類型的成員被序列化,恰如原始類型被序列化一樣。

            注意,類bus_stop的實例“存儲”時,其歸檔(archive)操作符將調(diào)用latitude 和 longitude的serialize 函數(shù)。這將依次調(diào)用定義在gps_position中的serialize 來被“存儲”。這種手法中,通過bus_stop的歸檔(archive)操作符,整個數(shù)據(jù)結(jié)構(gòu)被存儲,bus_stop是它的根條目。

             

            派生類

            派生類應(yīng)包含其基類的序列化。

             
            #include <boost/serialization/base_object.hpp>
             
            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<bus_stop>(*this);
                    ar & street1;
                    ar & street2;
                }
                std::string street1;
                std::string street2;
                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_)
                {}
            };
             

            注意在派生類中不要直接調(diào)用其基類的序列化函數(shù)。這樣做看似工作,實際上繞過跟蹤實例用于存儲來消除冗余的代碼。它也繞過寫到檔案中類的版本信息的代碼。因此,總是聲明serialize 作為私有函數(shù)。聲明friend boost::serialization::access 將運行序列化庫存取私有變量和函數(shù)。

             

            指針

            假設(shè)我們定義了bus route包含一組bus stops。假定:

            1. 我們可以有幾種bus stop的類型(記住bus_stop是一個基類)。
            2. 一個所給的 bus_stop可以展現(xiàn)多于一個的路線。

            一個bus route 用一組指向bus_stop的指針來描述是方便的。

             
            class bus_route
            {
                friend class boost::serialization::access;
                bus_stop * stops[10];
                template<class Archive>
                void serialize(Archive & ar, const unsigned int version)
                {
                    int i;
                    for(i = 0; i < 10; ++i)
                        ar & stops[i];
                }
            public:
                bus_route(){}
            };
             

            數(shù)組stops 的每個成員將被序列化。但是,記住每個成員是個指針。 - 實際含義是什么?序列化整個對象是要求在另一個地方和時間重新構(gòu)造原始數(shù)據(jù)結(jié)構(gòu)。用指針為了完成這些,存儲指針的值是不夠的,指針指向的對象必須存儲。當(dāng)成員最后被裝載,一個新的對象被創(chuàng)建,新的指針被裝載到類的成員中。

            所有這一切是由序列化庫自動完成的。通過指針關(guān)聯(lián)的對象,上述代碼能完成存儲和裝載。

             

            數(shù)組

            事實上上述方案比較復(fù)雜。序列化庫能檢測出被序列化的對象是一個數(shù)組,將產(chǎn)生上述等價的代碼。因此上述代碼能更短的寫為:

             
            class bus_route
            {
                friend class boost::serialization::access;
                bus_stop * stops[10];
                template<class Archive>
                void serialize(Archive & ar, const unsigned int version)
                {
                    ar & stops;
                }
            public:
                bus_route(){}
            };
             
            STL容器

            上面的例子用數(shù)組成員。更多的如此的一個應(yīng)用用STL容器為如此的目的。序列化庫包含為所有STL容器序列化的代碼。因此,下種方案正如我們所預(yù)期的樣子工作。

             
            #include <boost/serialization/list.hpp>
             
            class bus_route
            {
                friend class boost::serialization::access;
                std::list<bus_stop *> stops;
                template<class Archive>
                void serialize(Archive & ar, const unsigned int version)
                {
                    ar & stops;
                }
            public:
                bus_route(){}
            };
             
            類的版本

            假設(shè)我們對bus_route類滿意,在產(chǎn)品中使用它。一段時間后,發(fā)覺bus_route 類需要包含線路駕駛員的名字。因此新版本如下:

             
            #include <boost/serialization/list.hpp>
            #include <boost/serialization/string.hpp>
             
            class bus_route
            {
                friend class boost::serialization::access;
                std::list<bus_stop *> stops;
                std::string driver_name;
                template<class Archive>
                void serialize(Archive & ar, const unsigned int version)
                {
                    ar & driver_name;
                    ar & stops;
                }
            public:
                bus_route(){}
            };
             

            好,完畢!異常...會發(fā)生在讀取舊版本所生成的數(shù)據(jù)文件時。如何考慮版本問題?

            通常,序列化庫為每個被序列化的類在檔案中存儲版本號。缺省值是0。當(dāng)檔案裝載時,存儲的版本號可被讀出。上述代碼可修改如下:

             
            #include <boost/serialization/list.hpp>
            #include <boost/serialization/string.hpp>
            #include <boost/serialization/version.hpp>
             
            class bus_route
            {
                friend class boost::serialization::access;
                std::list<bus_stop *> stops;
                std::string driver_name;
                template<class Archive>
                void serialize(Archive & ar, const unsigned int version)
                {
                    // only save/load driver_name for newer archives
                    if(version > 0)
                        ar & driver_name;
                    ar & stops;
                }
            public:
                bus_route(){}
            };
             
            BOOST_CLASS_VERSION(bus_route, 1)
             

            對每個類通過應(yīng)用的版本,沒有必要維護(hù)一個版本文件。一個文件版本是所有它組成的類的版本的聯(lián)合。系統(tǒng)允許程序和以前版本的程序創(chuàng)建的檔案向下兼容。

             

            把serialize拆分成save/load

            serialize函數(shù)是簡單,簡潔,并且保證類成員按同樣的順序(序列化系統(tǒng)的key)被存儲/被裝載。可是有像這里例子一樣,裝載和存儲不一致的情形。例如,一個類有多個版本的情況發(fā)生。上述情形能重寫為:

             
            #include <boost/serialization/list.hpp>
            #include <boost/serialization/string.hpp>
            #include <boost/serialization/version.hpp>
            #include <boost/serialization/split_member.hpp>
             
            class bus_route
            {
                friend class boost::serialization::access;
                std::list<bus_stop *> stops;
                std::string driver_name;
                template<class Archive>
                void save(Archive & ar, const unsigned int version) const
                {
                    // note, version is always the latest when saving
                    ar  & driver_name;
                    ar  & stops;
                }
                template<class Archive>
                void load(Archive & ar, const unsigned int version)
                {
                    if(version > 0)
                        ar & driver_name;
                    ar  & stops;
                }
                BOOST_SERIALIZATION_SPLIT_MEMBER()
            public:
                bus_route(){}
            };
             
            BOOST_CLASS_VERSION(bus_route, 1)
             

            BOOST_SERIALIZATION_SPLIT_MEMBER() 宏生成調(diào)用 save 或 load的代碼,依賴于是否檔案被用于“存儲”或“裝載”。

             

            檔案

            我們這里討論將聚焦到類的序列化能力上。被序列化的數(shù)據(jù)的實際編碼實現(xiàn)于檔案(archive)類中。被序列化的數(shù)據(jù)流是所選檔案(archive)類的序列化的產(chǎn)物。(鍵)key設(shè)計決定這兩個組件的獨立性。允許任何序列化的規(guī)范可用于任何檔案(archive)。

            在這篇指南中,我們用了一個檔案類-用于存儲的text_oarchive和用于裝載的text_iarchive類。在庫中其他檔案類的接口完全一致。一旦類的序列化已經(jīng)被定義,類能被序列化到任何檔案類型。

            假如當(dāng)前的檔案集不能提供某個屬性,格式,或行為需要特化的應(yīng)用。要么創(chuàng)建一個新的要么從已有的里面衍生一個。將在后繼文檔中描述。

            注意我們的例子save和load程序數(shù)據(jù)在一個程序中,這是為了討論方便而已。通常,被裝載的檔案或許在或許不在同一個程序中。

            T完整的演示程序 - demo.cpp 包括:

            1. 創(chuàng)建各種類別的 stops, routes 和 schedules
            2. 顯示它
            3. 序列化到一個名叫 "testfile.txt"的文件中
            4. 還原到另一個結(jié)構(gòu)中
            5. 顯示被存儲的結(jié)構(gòu)

            這個程序的輸出 分證實了對序列化系統(tǒng)所有的要求,都在這個系統(tǒng)中體現(xiàn)了。對序列化文件是ASCII文本的檔案文件的內(nèi)容 能被顯示。

            posted on 2008-10-19 00:38 肥仔 閱讀(2635) 評論(1)  編輯 收藏 引用 所屬分類: Boost & STL

            評論

            # re: Boost Serialization 庫教程  回復(fù)  更多評論   

            使用 febird.dataio,比二進(jìn)制 boost.serialization 快 50 倍
            http://code.google.com/p/febird/
            http://blog.csdn.net/whinah
            2009-04-08 12:42 | rockeet
            伊色综合久久之综合久久| 一本久久a久久精品亚洲| 久久精品国产69国产精品亚洲| 国产V综合V亚洲欧美久久| 久久精品蜜芽亚洲国产AV| 国产一级持黄大片99久久| 久久九九全国免费| 久久天天躁狠狠躁夜夜不卡| 久久国产精品无码网站| 久久人人爽人人爽人人片av麻烦| 99久久夜色精品国产网站 | 亚洲国产精品无码久久青草| 久久人人爽人人爽人人av东京热| 久久久久无码精品国产| 久久国产美女免费观看精品| 久久99精品国产麻豆| 亚洲国产高清精品线久久 | 久久天天躁狠狠躁夜夜av浪潮| 亚洲中文字幕久久精品无码喷水| 99久久精品免费国产大片| 久久国产欧美日韩精品| 久久成人精品| 国产精品久久99| 人妻无码久久一区二区三区免费| 久久精品亚洲福利| 久久精品国产亚洲欧美| 亚洲AV日韩AV天堂久久| 久久久久久久久波多野高潮| 久久精品国产72国产精福利| 久久综合欧美成人| 国产一久久香蕉国产线看观看 | 久久亚洲精品成人AV| 久久99热这里只有精品66| 久久天天躁狠狠躁夜夜av浪潮 | 亚洲午夜福利精品久久| 久久久精品波多野结衣| 国产三级观看久久| 91精品无码久久久久久五月天| 国产精品久久久久影院色| 国产精品久久波多野结衣| 久久久久中文字幕|