Hi there,
Hmmm ...
i have used non-intrusive serialization on a function object without
problems.
So I don't see a problem here so I'm clearly missing something.
Robert Ramey
See below for a working example. The code that doesn't work is commented out
at the end. If boost::function would provide an access method for the
objects stored in it, writing the serialization code would be no big deal,
I would assume. As it is, however, I think I'll have to stick to the
vector .
Best,
Ruediger Berlich
/********************************************************************/
#include <iostream>
#include <sstream>
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
using namespace boost;
class functionObject{
///////////////////////////////////////////////////////////////
friend class boost::serialization::access;
template<class Archive>
void serialize(Archive & ar, const unsigned int version){
using boost::serialization::make_nvp;
ar & make_nvp("innerState_", innerState_);
}
///////////////////////////////////////////////////////////////
public:
functionObject(void){
innerState_=0;
}
functionObject(int is){
innerState_=is;
}
void operator()(void){
cout << "Inner state is " << innerState_ << endl;
}
private:
int innerState_;
};
main(){
vector A;
for(int i=1; i<4; ++i){
shared_ptr<functionObject> p(new functionObject(i));
A.push_back(p);
}
// Serialization works
ostringstream oss;
{
boost::archive::xml_oarchive oa(oss);
oa << boost::serialization::make_nvp("functionObjectContainer", A);
}
cout << oss.str() << endl;
// Now the same with boost::function()
vector > B;
for(int i=1; i<4; ++i) B.push_back(functionObject(i));
vector >::iterator it;
// Calls functionObject::operator(), works
for(it=B.begin(); it!=B.end(); ++it) (*it)();
// *** Does not work: ***
// ostringstream oss2;
// {
// boost::archive::xml_oarchive oa(oss2);
// oa << boost::serialization::make_nvp("functionContainer", B);
// }
// cout << oss2.str() << endl;
//
// Compilation fails with error message:
// error: ‘struct boost::function’
// has no member named ‘serialize’
}