如何处理切换序列化成员的类型,同时保持以前存档的可压缩性?我想把float/int改成double/size_t。
我知道我可以增加版本号,但这会使代码变得混乱。有什么不同的方法吗?如果存在差异,则通过MAKE_NVP序列化成员。
发布于 2014-04-22 21:54:51
您可以使用version参数进行版本。文档给出了这个例子:0/libs/serialization/doc/tutorial.html#versioning
请注意BOOST_CLASS_VERSION宏如何指定序列化期间的版本。
以下是使用以下版本的数据结构的概念证明: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'https://stackoverflow.com/questions/23230369
复制相似问题