boost.serialization和延迟初始化
-
23-09-2019 - |
题
我需要序列目录树。 我有这种类型的没有麻烦:
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;
如何初始化对象类型的“标准::对”在其序列的时刻? 即读文件片/计算CRC32 SUMM。
达
解决方案
我将取代由一个自定义类的载体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 ::到
定义save
的MyFileNames
系列化功能
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
>
>
>
和序列化这种类型。 这让你评价懒部分只有数据被序列化。用于负载你可以忽略该懒惰数据,我想,该数据可以计算出来。
其他提示
我不很明白的问题,但#包括“升压/系列化/ 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 );
}
不隶属于 StackOverflow