Boosten der Fusionsserialisierung einer Klasse mithilfe von BOOST_FUSION_ADAPT_ADT
Ich versuche ein Serialisierungsmodul für Klassen mit Boost Fusion zu bekommen. Ich habe meine Klasse in eine boost :: fusion-Sequenz umgewandelt. Dieses Beispiel folgt aus den Folien des Vortrags von Michael Caisse auf der boostcon 13
Das von Michael erläuterte Beispiel funktionierte gut für struct type. Dasselbe konnte nicht für Klassentypen angewendet werden. Was vermisse ich hier?
#include <iostream>
#include <typeinfo>
#include <string>
#include <boost/fusion/include/sequence.hpp>
#include <boost/fusion/include/algorithm.hpp>
#include <boost/fusion/include/adapt_struct.hpp>
#include <boost/fusion/include/adapt_adt.hpp>
#include <boost/fusion/include/is_sequence.hpp>
#include <boost/mpl/eval_if.hpp>
#include <boost/lexical_cast.hpp>
#include <boost/type_traits.hpp>
#include <vector>
#include <list>
using namespace std;
template< typename T >
void serialize(T v)
{
boost::fusion::for_each( v, (serial_out()) );
}
struct serial_out
{
template< typename T >
void operator() ( T & v , typename std::enable_if<!boost::fusion::traits::is_sequence<T>::value>::type* = 0 ) const
{
simple::serialize<T>::write(v);
}
template< typename T >
void operator() ( T & v , typename std::enable_if<boost::fusion::traits::is_sequence<T>::value>::type* = 0 ) const
{
serialize(v);
}
template< typename T >
void operator()( std::vector<T> & v ) const
{
simple::serialize<int>::write(v.size());
std::for_each(v.begin(),v.end(),*this);
}
template< typename T >
void operator()( std::list<T> & v ) const
{
simple::serialize<int>::write(v.size());
std::for_each(v.begin(),v.end(),*this);
}
};
namespace simple
{
template<typename T> struct serialize{};
template<> struct serialize<int>
{
static void write(int v) { cout << v << endl; }
};
template<> struct serialize<float>
{
static void write(float v) { cout << v << endl; }
};
template<> struct serialize<std::string>
{
static void write(std::string v)
{
cout << v << endl;
}
};
}
class secret_point
{
public:
secret_point(double x, double y) : x_(x), y_(y) {}
double get_x() const { return x_; }
void set_x(double d) { x_=d; }
double get_y() const { return y_; }
void set_y(double d) { y_=d; }
private:
double x_, y_;
};
BOOST_FUSION_ADAPT_ADT( secret_point, (double, double, obj.get_x(), obj.set_x(val) ) (double, double, obj.get_y(), obj.set_y(val) ) )
int main(int argc, char *argv[])
{
secret_point p(112,233);
serialize(p);
return 0;
}