序列化variables_map

Jay*_*yen 10 c++ boost boost-program-options boost-serialization

如何序列化/反序列化boost :: program_options :: variables_map?我找不到已经实现的序列化函数,我不知道我可以使用variables_map中的哪些函数来提取和重新组合映射.

rha*_*oto 10

看起来你发现它boost::program_options::variables_map来自于std::map你可以使用它的序列化(但是稍后会看到警告).如果唯一剩下的问题是序列化boost::any它包含的值那么你几乎就在那里.

你无法序列化任意的boost :: any,因为它实际上并不知道如何操纵它所拥有的东西.但是,如果您知道并且可以枚举应用程序使用的类型,则可以进行序列化.例如,如果您知道该boost::any值始终是字符串或int,那么这样的事情应该有效.

要序列化(值是a boost::any):

if (value.type() == typeid(int)) {
   ar << std::string("int");
   ar << boost::any_cast<int>(value);
}
else if (value.type() == typeid(std::string)) {
   ar << std::string("string");
   ar << boost::any_cast<std::string>(value);
}
Run Code Online (Sandbox Code Playgroud)

要反序列化(值是a boost::any):

std::string type;
ar >> type;
if (type == "int") {
   int x;
   ar >> x;
   value = x;
}
else if (type == "string") {
   std::string x;
   ar >> x;
   value = x;
}
Run Code Online (Sandbox Code Playgroud)

显然,您可以在序列化流中使用比"int"和"string"更高效的类型标记,但这为您提供了基本的想法.

编辑:boost::archive对const引用很挑剔,所以我上面写的并不完全编译.这样做,它适用于一个非常简单的测试:

enum {
   TYPE_int,
   TYPE_string,
};

namespace boost {
   namespace serialization {

      template<class Archive>
      void save(Archive& ar, const boost::program_options::variable_value& value, unsigned int version) {
         const boost::any& anyValue = value.value();
         if (anyValue.type() == typeid(int)) {
            int type = static_cast<int>(TYPE_int);
            int typedValue = boost::any_cast<int>(anyValue);
            ar << type << typedValue;
         }
         else if (anyValue.type() == typeid(std::string)) {
            int type = static_cast<int>(TYPE_string);
            std::string typedValue = boost::any_cast<std::string>(anyValue);
            ar << type << typedValue;
         }
      }

      template<class Archive>
      void load(Archive& ar, boost::program_options::variable_value& value, unsigned int version) {
         boost::any anyValue;
         int type;
         ar >> type;
         if (type == TYPE_int) {
            int x;
            ar >> x;
            anyValue = x;
         }
         else if (type == TYPE_string) {
            std::string x;
            ar >> x;
            anyValue = x;
         }

         value = boost::program_options::variable_value(anyValue, false);
      }

      template<class Archive>
      void serialize(Archive& ar, boost::program_options::variables_map& value, unsigned int version) {
         // Probably works but is sloppy and dangerous.  Would be better to
         // deserialize into a temporary std::map and build a variables_map
         // properly.  Left as an exercise.
         ar & static_cast<std::map<std::string, boost::program_options::variable_value>&>(value);
      }
   }
}

BOOST_SERIALIZATION_SPLIT_FREE(boost::program_options::variable_value);
Run Code Online (Sandbox Code Playgroud)

这段代码有几个可能的问题.第一个是load()for variable_value- 最后一个语句variable_value来自a boost::any,我不太确定该bool参数是做什么的(你可能需要序列化所bool代表的任何东西).第二个是你可能会或可能不会variables_map通过转换为std::map引用和反序列化来获得一致性.将反序列化为真实std::map然后variables_mapstd::map内容构建更安全.