boost.serialization 和延迟初始化

发布于 2024-08-27 09:19:35 字数 818 浏览 10 评论 0原文

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

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::pair”类型的对象? 即读取文件片段/计算crc32 summ。

向上

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?
i.e. read file piece/calculate crc32 summ.

up

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

剪不断理还乱 2024-09-03 09:19:35

我将用自定义类替换向量中的 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 序列化函数定义>MyFileNames 通过将 std::string 转换为 a

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

并序列化此类型。
这让您可以仅在数据被序列化时评估惰性部分。对于负载,您可以忽略惰性数据,因为我认为可以计算该数据。

I would replace the std::string in the vector by a custom class, let me say 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::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.

优雅的叶子 2024-09-03 09:19:35

我不太明白这个问题,但是 #include "boost/serialization/utility.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 );
}

I dont quite understand the question, but #including "boost/serialization/utility.hpp" gives you the implementation for serialising 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 );
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文