如果我使用Boost Serialization来序列化一个Integer:
#include <boost/archive/text_oarchive.hpp>
#include <iostream>
int main()
{
boost::archive::text_oarchive oa(std::cout);
int i = 1;
oa << i;
}结果如下所示:
22 serialization::archive 5 1
现在我很好奇是否以及如何改变这种方式,某些数据是序列化的。不需要对数据进行反序列化,因此如果不能再进行反序列化,也不会成为不进行反序列化的障碍。
假设上面的代码应该创建以下输出:
integer 11
(添加整型字,值增加10,不会集成archive-header。)
这可能吗?如何才能做到这一点?Boost Serialization能够让用户在不修改序列化代码库的情况下做到这一点吗?
PS:
上面的示例代码是从Highscore-Tutorial复制的
发布于 2012-02-25 04:44:56
您可以编写自己的归档文件,如下所示:
#include <cstddef> // std::size_t
#include <string>
#include <typeid>
template <typename T>
std::string printName() {
// Unmangle for your platform or just specialise for types you care about:
return typeid(T).name();
}
//////////////////////////////////////////////////////////////
// class trivial_oarchive
class trivial_oarchive {
public:
//////////////////////////////////////////////////////////
// public interface used by programs that use the
// serialization library
typedef boost::mpl::bool_<true> is_saving;
typedef boost::mpl::bool_<false> is_loading;
template<class T> register_type(){}
template<class T> trivial_oarchive & operator<<(const T & t){
return *this;
}
template<class T> trivial_oarchive & operator&(const T & t){
// No idea why you'd want to add 10, but this does it:
return *this << printName<T>() << " " << (t+10);
}
void save_binary(void *address, std::size_t count){};
};(改编自the documentation)
https://stackoverflow.com/questions/3931535
复制相似问题