序列化variables_map

时间:2010-11-06 05:52:04

标签: c++ boost boost-serialization boost-program-options

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

1 个答案:

答案 0 :(得分:9)

看起来您发现boost::program_options::variables_map派生自std::map,因此您可以使用其序列化(但请参阅此后的警告)。如果唯一剩下的问题是序列化它包含的boost::any值,那么你几乎就在那里。

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

要序列化(值为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);
}

要反序列化(值为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;
}

显然,您可以在序列化流中使用比“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);

此代码存在两个可能的问题。第一个是load() variable_value - 最后一个语句从variable_value发出boost::any,我不太清楚bool论证做了什么(您可能需要序列化bool代表的任何内容。第二个是,您可能会或可能不会通过转换为variables_map引用和反序列化来获得一致std::map。将反序列化为真实std::map然后从variables_map内容构建std::map会更安全。

相关问题