代码之家  ›  专栏  ›  技术社区  ›  niXman

boost.serialization和延迟初始化

  •  0
  • niXman  · 技术社区  · 15 年前

    我需要序列化目录树。 我对这种类型没有问题:

    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 回复  |  直到 15 年前
        1
  •  2
  •   Vicente Botet Escriba    15 年前

    我会替换 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;
    

    并定义 save 的序列化函数 文件名 通过将STD::字符串转换为

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

    以及序列化此类型。 这让您只计算数据序列化的延迟部分。对于负载,可以忽略惰性数据,因为我认为可以计算这些数据。

        2
  •  2
  •   ashleysmithgpu    15 年前

    我不太理解这个问题,但是包括“Boosi/Serial/Uptudio .HPP”的介绍,为您提供了序列化STD::配对的实现。

    如果以后要加载代码区域,我认为最好的方法是创建一个自定义pair类:

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