Reputation: 8654
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
Upvotes: 4
Views: 2462
Reputation: 88711
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)
Upvotes: 5