impulso. Serialización e inicialización perezosa
-
23-09-2019 - |
Pregunta
Necesito serializar el árbol de directorio. No tengo problemas con este tipo:
std::map<
std::string, // string(path name)
std::vector<std::string> // string array(file names in the path)
> tree;
Pero para la serialización del árbol de directorio con el contenido, necesito otro tipo:
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;
Cómo puedo inicializar ¿El objeto de tipo "std :: par" en el momento de su serialización? es decir, lea el archivo Piece/Calcule CRC32 Summ.
arriba
Solución
Reemplazaría el std::string
En el vector por una clase personalizada, déjame decir 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;
Y defina el save
función de serialización para MyFileNames
convirtiendo el std :: string a un
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
>
>
>
y el serialize este tipo. Esto le permite evaluar la parte perezosa solo los datos se serializan. Para la carga, puede ignorar los datos perezosos, como supongo que estos datos se pueden calcular.
Otros consejos
No entiendo muy bien la pregunta, pero #incluyendo "Boost/Serialization/Utility.HPP" le brinda la implementación para serializar el par std ::.
Si desea cargar el área de su código más adelante, entonces creo que la mejor manera sería crear una clase de par personalizada:
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 );
}