|
Boost Users : |
From: Ruediger Berlich (ruediger.berlich_at_[hidden])
Date: 2007-12-08 13:45:14
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<shared_ptr<functionObject> > .
Best,
Ruediger Berlich
/********************************************************************/
#include <iostream>
#include <sstream>
#include <boost/function.hpp>
#include <boost/bind.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/random.hpp>
#include <boost/archive/xml_oarchive.hpp>
#include <boost/archive/xml_iarchive.hpp>
#include <boost/archive/text_oarchive.hpp>
#include <boost/archive/text_iarchive.hpp>
#include <boost/serialization/nvp.hpp>
#include <boost/serialization/vector.hpp>
#include <boost/serialization/shared_ptr.hpp>
#include <boost/serialization/base_object.hpp>
#include <boost/serialization/utility.hpp>
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<shared_ptr<functionObject> > 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<function<void(void)> > B;
for(int i=1; i<4; ++i) B.push_back(functionObject(i));
vector<function<void(void)> >::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<void ()(), std::allocator<void> >â
// has no member named âserializeâ
}
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