2010-03-31 114 views
0

我需要序列化目錄樹。 我有這種類型的無故障:boost.serialization和延遲初始化

std::map< 
    std::string, // string(path name) 
    std::vector<std::string> // string array(file names in the path) 
> tree; 

但對於系列化與內容的目錄樹,我需要其他類型的:

std::map< 
    std::string, // string(path name) 
    std::vector< // files array 
     std::pair< 
     std::string, // file name 
     std::vector< // array of file pieces 
      std::pair< // <<<<<<<<<<<<<<<<<<<<<< for this i need lazy initialization 
       std::string, // piece buf 
       boost::uint32_t // crc32 summ on piece 
      > 
     > 
     > 
    > 
> tree; 

我怎麼能初始化類型的對象「STD ::對「在序列化的時刻? 即讀取文件塊/計算crc32總和。

回答

2

我會通過自定義類載體代替std::string,讓我說MyFileNames

class MyFileNames : std::string 
{ 
// add forward constructors as needed 

}; 

std::map< 
    std::string, // string(path name) 
    std::vector<MyFileNames> // string array(file names in the path) 
> tree; 

而受的std :: string轉換爲

std::pair< 
    std::string, // file name 
    std::vector< // array of file pieces 
     std::pair< // <<<<<<<<<<<<<<<<<<<<<< for this i need lazy initialization 
      std::string, // piece buf 
      boost::uint32_t // crc32 summ on piece 
     > 
    > 
> 
定義 MyFileNamessave序列化功能

和序列化這種類型。 這讓您評估懶惰部分只有數據被序列化。對於負載,您可以忽略惰性數據,因爲我認爲可以計算這些數據。

2

我不太明白的問題,但#包括「升壓/系列化/ utility.hpp」給你連載的std ::對實施。

如果你想以後加載代碼的區域,那麼我認爲最好的方法是創建一個自定義對類:

class custom_pair : std::pair< std::string, // piece buf 
       boost::uint32_t > // crc32 summ on piece 
{ 

}; 

//... 
     std::vector< // array of file pieces 
      custom_pair // <<<<<<<<<<<<<<<<<<<<<< for this i need lazy initialization 
     > 
//... 

template< class Archive > 
void serialize(Archive & ar, custom_pair & p, const unsigned int version) { 
    ar & boost::serialization::make_nvp("std::pair", std::pair<...>(p)); 
} 

template<class Archive> 
inline void load_construct_data(Archive & ar, custom_pair * p, const unsigned int file_version) { 
    std::string first; 
    boost::uint32_t second; 
    ar & boost::serialization::make_nvp("first", first_); 
    ar & boost::serialization::make_nvp("second", second_); 
    ::new(t)custom_pair; 
    //... 
} 

template<class Archive> 
inline void save_construct_data(Archive & ar, const custom_pair * p, const unsigned int file_version) { 
    ar & boost::serialization::make_nvp("first", t->first); 
    ar & boost::serialization::make_nvp("second", t->second); 
} 
0

我不明白的問題提出。

#include <boost/archive/text_oarchive.hpp> 
#include <boost/serialization/map.hpp> 
#include <boost/serialization/vector.hpp> 

如果我把一個元素在樹中去:

boost::archive::text_iarchive ia(std::cout); 
oa << tree; 

它輸出:

22系列化::存檔7 0 0 1 0 0 0 3 FOO 0 0 1 0 0 0 3巴0 0 1 0 0 0 3 巴茲27

我我確定你可以反序列化。

(用升壓1.42測試)

啊。現在你將'serialize'改爲'initialize'。沒有任何鉤子可以隨時更改您的數據,因此您必須在之前和之後執行此操作。