简体   繁体   English

boost.serialization和延迟初始化

[英]boost.serialization and lazy initialization

i need to serialize directory tree. 我需要序列化目录树。 i have no trouble with this type: 我没有这种类型的麻烦:

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

but for the serialization the directory tree with the content i need other type: 但是对于序列化目录树与内容我需要其他类型:

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;

how can i initialize the object of type "std::pair" in the moment of its serialization? 我如何在序列化时初始化 “ std :: pair”类型的对象? ie read file piece/calculate crc32 summ. 即读取文件/计算crc32 summ。

up 向上

I dont quite understand the question, but #including "boost/serialization/utility.hpp" gives you the implementation for serialising std::pair. 我不太了解这个问题,但是#include“ boost / serialization / utility.hpp”为您提供了序列化std :: pair的实现。

If you want to load the area of your code later on, then I think the best way would be to create a custom pair class: 如果以后要加载代码区域,那么我认为最好的方法是创建一个自定义对类:

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 );
}

I would replace the std::string in the vector by a custom class, let me say MyFileNames 我将用自定义类替换向量中的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;

And define the save serialization function for MyFileNames by converting the std::string to a 并通过将std :: string转换为MyFileNames来定义MyFileNamessave序列化函数

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
        >
     >
>

and the serialize this type. 并序列化此类型。 This let you evaluate the lazy part only the data is serialized. 这使您可以仅对数据进行序列化来评估惰性部分。 For the load you could ignore the lazy data, as I suppose that this data can be calculated. 对于负载,您可以忽略惰性数据,因为我认为可以计算该数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM