问题描述
如果我使用升压序列化序列化一个整数:
If I'm using Boost Serialization to serialize an Integer:
#include <boost/archive/text_oarchive.hpp>
#include <iostream>
int main()
{
boost::archive::text_oarchive oa(std::cout);
int i = 1;
oa << i;
}
其结果将是如下:结果 22 ::系列化存档5 1
现在我我是否以及如何可以改变的方式好奇,某些数据序列化。
这些数据不需要反序列化,所以如果这是不可能的了,这不是一个阻值理由不这样做。
Now I'm curious if and how I could change the way, certain data is serialized.The data does not need to be deserialized, so if that is not possible anymore, it's not a hindering reason to not doing that.
比方说上面的code应创建以下的输出:结果 11的整数
结果
(单词整数,并将该值将由10.归档头不会被集成而增加。)
Lets say the above code should create the following output:integer 11
(The word integer is added and the value will be increased by 10. The archive-header will not be integrated.)
请问可不可以和一个如何做到这一点?就是boost序列化能够让用户做到这一点,而无需修改序列化的codeBase的?
Would that be possible and how could one achieve that? Is Boost Serialization able to let a user do that without modifying the codebase of Serialization?
PS:结果
该示例 - code以上从
推荐答案
您可以写你自己的存档,像这样:
You can write your own archive, something like this:
#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){};
};
(从改编>)
这篇关于升压序列化 - 在自定义序列化方式的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!