boost.serialization e inizializzazione differita
-
23-09-2019 - |
Domanda
ho bisogno di serializzare albero di directory. Non ho problemi con questo tipo:
std::map<
std::string, // string(path name)
std::vector<std::string> // string array(file names in the path)
> tree;
ma per la serializzazione l'albero delle directory con il contenuto ho bisogno di un altro 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;
Come posso di inizializzazione l'oggetto di tipo "std :: coppia" nel momento della sua serializzazione? vale a dire leggere pezzo file / calcolo crc32 summ.
up
Soluzione
Vorrei sostituire il std::string
nel vettore da una classe personalizzata, lasciatemi dire 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;
E definire la funzione save
serializzazione per MyFileNames
convertendo lo 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
>
>
>
e il serialize questo tipo. Questo consente di valutare la parte pigra solo i dati è serializzato. Per il carico si potrebbe ignorare i dati pigri, come suppongo che questi dati può essere calcolato.
Altri suggerimenti
I dont abbastanza capire la domanda, ma #including "boost / serializzazione / utility.hpp" ti dà l'implementazione per serializzazione std :: coppia.
Se si desidera caricare l'area del codice in seguito, quindi penso che il modo migliore sarebbe quello di creare una classe paio personalizzato:
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 );
}