【问题标题】:Boost class serialization, change in member typesBoost类序列化,成员类型变化
【发布时间】:2014-06-07 11:08:10
【问题描述】:

如何处理切换序列化成员的类型,同时保持与以前存档的兼容性?例如。我想把float/int改成double/size_t

我知道我可以增加版本号,但这会使代码变得混乱。有不同的方法来处理吗?如果有区别,成员将通过MAKE_NVP 进行序列化。

【问题讨论】:

  • 名称-值对没有区别。

标签: c++ serialization boost boost-serialization


【解决方案1】:

您可以使用version 参数来进行版本化。文档给出了这个例子:http://www.boost.org/doc/libs/1_55_0/libs/serialization/doc/tutorial.html#versioning

注意序列化期间的版本是如何由BOOST_CLASS_VERSION 宏指定的。

这是使用以下数据结构版本的概念证明:See it Live On Coliru

struct DataV0
{
    float f = 3.14;
    int   i = 42;

    template<class Archive>
    void serialize(Archive & ar, const unsigned int version)
    {
        if (0 != version) 
            throw std::runtime_error("Unsupported version");

        ar & f;
        ar & i;
    }
};

现在,在您的 V1 序列化格式中,您已经更改了问题中的类型:

struct DataV1
{
    double d  = 3.14;
    size_t ul = 42;

    template<class Archive>
    void serialize(Archive & ar, const unsigned int version)
    {
        switch(version)
        {
            case 0:
                { 
                    DataV0 old;
                    ar & old.f;
                    ar & old.i;

                    d = old.f;
                    assert(old.i >= std::numeric_limits<size_t>::min());
                    assert(old.i <= std::numeric_limits<size_t>::max());
                    ul = static_cast<size_t>(old.i);
                }
                break;
            case 1:
                ar & d;
                ar & ul;
                break;
        }
    }
};

BOOST_CLASS_VERSION(DataV1, 1)

完整的测试程序:

template <typename Data>
std::string to_string(Data d)
{
    std::ostringstream oss;
    boost::archive::text_oarchive oa(oss);

    oa << d;

    return oss.str();
}

template <typename Data>
bool verify(std::string const& text)
{
    std::istringstream iss(text);
    boost::archive::text_iarchive ia(iss);

    Data d;
    ia >> d;

    return text == to_string(d);
}

int main()
{
    std::string v0text = to_string(DataV0());
    std::string v1text = to_string(DataV1());

    std::cout << v0text << '\n';
    std::cout << v1text << '\n';

    std::cout << "v0 as v0: " << std::boolalpha << verify<DataV0>(v0text) << "\n";
    std::cout << "v0 as v1: " << std::boolalpha << verify<DataV1>(v0text) << "\n";

    std::cout << "v1 as v1: " << std::boolalpha << verify<DataV1>(v1text) << "\n";
    try {
        std::cout << "v1 as v0: " << std::boolalpha << verify<DataV0>(v1text) << "\n";
    } catch (std::exception const& e)
    {
        std::cerr << "Threw the expected '" << e.what() << "'\n";
    }

}

打印:

22 serialization::archive 10 0 0 3.1400001 42
22 serialization::archive 10 0 1 3.1400000000000001 42
v0 as v0: true
v0 as v1: false
v1 as v1: true
v1 as v0: Threw the expected 'Unsupported version'

【讨论】:

  • 只是为了扩展这个很好的答案,如果您的结构是另一个类的私有/受保护成员,它也可以使用宏进行版本控制,但使用类范围:BOOST_CLASS_VERSION(MyClass, 1); BOOST_CLASS_VERSION(MyClass::MyPrivateStruct, 2);
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-02-19
  • 2012-08-11
  • 1970-01-01
  • 2013-04-08
  • 2018-11-22
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多