我正在尝试使用boost fusion获取类的序列化模块。我已将我的类转换为boost :: fusion序列。这个例子是关于Michael Caisse在boostcon 13上的演讲的幻灯片。
迈克尔解释的例子对结构类型很有用。同样不能应用于类类型。我在这里缺少什么?
#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;
}