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;
どのようにi の初期化の型のオブジェクトは、「STD ::ペア」の連載の瞬間にできますか? すなわち、ファイルピースを読み取る計算/ 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を」#includingするあなたの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