Boost Serialize - 以自定义方式对数据进行序列化 [英] Boost Serialize - Serialize data in a custom way

查看:742
本文介绍了Boost Serialize - 以自定义方式对数据进行序列化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我使用Boost序列化来序列化整数:

  #include< boost / archive / text_oarchive.hpp> ; 
#include< iostream>

int main()
{
boost :: archive :: text_oarchive oa(std :: cout);
int i = 1;
oa<<一世;
}

结果如下:

22 serialization :: archive 5 1



现在我很好奇如果我可以改变方式,某些数据序列化。
数据不需要反序列化,所以如果这不可能,这不是一个阻碍的原因,不这样做。



让我们说上面的代码应创建以下输出:

整数11

(添加字整数,值将增加10 。)



这是否可能,如何实现?是Boost序列化能够让用户这样做而不修改序列化的代码库?



PS:

上面的示例代码是从 Highscore-Tutorial

解决方案

您可以编写自己的归档文件,如下所示:

  #include< cstddef& // std :: size_t 
#include< string>
#include< typeid>

template< typename T>
std :: string printName(){
//为您的平台解构或专门为您关心的类型:
return typeid(T).name();
}

////////////////////////////////////// /////////////////////////
// class trivial_oarchive
class trivial_oarchive {
public:
////////////////////////////////////////////////// ////////
//使用
//序列化库的程序使用的公共接口
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&运算符&(const T& t){
//不知道为什么你想添加10,但是这样做:
return * this< printName< T>()<< < (t + 10);
}
void save_binary(void * address,std :: size_t count){};
};

(改编自文档


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; 
}

The result will be the following:
22 serialization::archive 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.

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.)

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:
The example-code above is copied from the Highscore-Tutorial

解决方案

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){};
};

(Adapted from the documentation)

这篇关于Boost Serialize - 以自定义方式对数据进行序列化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆