|
Boost Users : |
Subject: [Boost-users] Serialize a boost function (actually a member function pointer)
From: Fabian Fritz (fabianfuture_at_[hidden])
Date: 2010-06-18 19:48:31
Hi,
I want to use boost serialization to serialize a boost function. In other
places I learned that I need to use non-intrusive serialization and the
target<>() method, but I still don't get it to work:
#include <vector>
#include <iostream>
#include <fstream>
#include <boost/archive/text_oarchive.hpp>
#include <boost/archive/text_iarchive.hpp>
#include <boost/function.hpp>
class A {
public:
void func1() {
std::cout << "Hello World";
}
};
typedef void (A::*fPointerType)();
class FuncPointerWrapper {
boost::function<void (A*)> f;
friend class boost::serialization::access;
template<typename Archive>void serialize(Archive & ar, const unsigned
int version) {
ar & f;
}
public:
FuncPointerWrapper() {}
FuncPointerWrapper(fPointerType fun) { f = fun; }
boost::function<void (A*)> getFuncPointer() {return f;}
};
//non-intrusive
namespace boost { namespace serialization {
template<typename Archive>void serialize(Archive &
ar,boost::function<void (A*)> & f, const unsigned int version) {
ar & (f.template target<fPointerType>());
//error: no match for 'operator&' in 'ar &
((boost::function_base*)(+f))->boost::function_base::target [with Functor =
void (A::*)()]()'|
// but how do I have to use it then?
}
}
}
int main() {
FuncPointerWrapper fpw(&A::func1);
std::ofstream ofs("filename");
{
boost::archive::text_oarchive oa(ofs);
// write class instance to archive
oa << fpw;
// archive and stream closed when destructors are called
}
A a;
fpw.getFuncPointer()(&a);
// Some other place, some other time:
FuncPointerWrapper fpw2;
{
std::ifstream ifs("filename");
boost::archive::text_iarchive ia(ifs);
ia >> fpw2;
}
A b;
fpw2.getFuncPointer()(&b);
}
Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net