سؤال

أحتاج إلى تسلسل شجرة الدليل. ليس لدي مشكلة في هذا النوع:

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 :: pair" في لحظة التسلسل؟ أي قراءة قطعة الملف/حساب 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;

وتحديد save وظيفة التسلسل ل MyFileNames عن طريق تحويل Std :: String إلى

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" يمنحك تنفيذ Serialising 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 );
}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top