2010-03-31 135 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'。没有任何钩子可以随时更改您的数据,因此您必须在之前和之后执行此操作。