
Hello,
Thanks for the quick response !
It is a good solution, but hhat I try to do is the following:
template
----- Original message ----- From: "Robert Ramey"
To: boost-users@lists.boost.org Subject: Re: [Boost-users] Serialization of functors and MPL use Date: Sun, 19 Sep 2010 11:06:43 -0800 Anonymous user wrote:
Hello,
Thanks for the response.
Actually, I need to create dynamically functosr, because I call a lot of different methods coming from several classes. I don't want to implement the operator () inside each object used, because it is not appropriate in my code (too much classes).
Can you please , give me an example of registration of functors ?
I could if I had nothing else to do right now. I've got on my list to make a "case study" in the documentation on how to do this. But for now I'll just give a short explanation off the top of my head.
A functor is just a structure. You can serialize just as you can any other structure.
The problem is that you want serialize a data variable which refers to any one of a number of differently defined structures. This is a specific instance of something that we want to do in C++ all the time. The most common way of doing this is to use a pointer to a virtual base class. e.g.
struct functor_interface { virtual int /* or other return type */ operator()(int /* other other return types */ x, ...); // virtual makes it polymorphic // operator() ... makes a functor };
So now we can define any number of functors which use the same interface:
struct f1 : public functor_interface { virtual int operator()(int x){ ... whatever template<class Archive> void serialize(Archive &ar, const unsigned int version){ //.. this functor has no parameters so nothing to do. // .. serialization still needs this defined though } };
struct f2 : public functor_interface { int m_a; // member variable which the functor uses at runtime virtual int operator()(int x){ ... whatever - something different than f1 void serialize(Archive &ar, const unsigned int version){ ar & m_a; } };
... as many as you want.
Register all the functors BOOST_EXPORT_CLASS(f1) ...
The above makes the functors exportable - that is associates the functor f1 with the external string name "f1".
Now you can use a variable
struct my_class { functor_interface *m_f; ... void serialize(Archive &ar, const unsigned int version){ ar & m_f; } };
Which uses the m_f variable as a "variable function". You call it with m_f->operator(112) or maybe with something slicker like (*m_f)(112) // or something similar
Now you're done - just serialize m_class like anyother. when you de-serialize it, it has the same state it did before (whatever that was).
Home this is helpful.
Robert Ramey
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
-------------------------------------------------------------- Ovi Mail: Making email access easy http://mail.ovi.com