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;
как я могу инициализировать Объект типа «std :: pare» в момент его сериализации? т.е. прочтете файл файл/вычислить Summ CRC32.
вверх
Решение
Я бы заменил 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 ::
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
>
>
>
и сериализуйте этот тип. Это позволяет оценить ленивую часть, только данные сериализованы. Для нагрузки вы можете игнорировать ленивые данные, как я полагаю, эти данные могут быть рассчитаны.
Другие советы
Я не совсем понимаю вопрос, но #including "boost/serialization/utility.hpp" дает вам реализацию для сериализации std :: pare.
Если вы хотите загрузить область вашего кода позже, то я думаю, что лучший способ - создать пользовательский пара класса:
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 );
}