Boost Serialize-以自定义方式序列化数据

MOn*_*DaR 4 c++ serialization boost

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

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

int main() 
{ 
  boost::archive::text_oarchive oa(std::cout); 
  int i = 1; 
  oa << i; 
}
Run Code Online (Sandbox Code Playgroud)

结果将如下所示:
22 serialization::archive 5 1

现在,我很好奇是否以及如何更改方式,某些数据已序列化。不需要对数据进行反序列化,因此,如果不再可能,则不这样做就不会成为障碍。

可以说,上面的代码应该创建以下输出:(
integer 11
添加了单词integer,并且该值将增加10。将不会集成archive-header。)

那将是可能的,又将如何实现呢?Boost序列化是否能够让用户做到这一点而无需修改序列化的代码库?

PS:
上面的示例代码是从Highscore-Tutorial复制的

Fle*_*exo 5

您可以编写自己的存档,如下所示:

#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){};
};
Run Code Online (Sandbox Code Playgroud)

(改编自文档