Re: [Boost-users] Serialization of functors and MPL use
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 ? Thanks in advance. Regards.
----- Original message ----- From: "OvermindDL1"
To: boost-users@lists.boost.org Subject: Re: [Boost-users] Serialization of functors and MPL use Date: Fri, 17 Sep 2010 00:22:11 -0600 On Thu, Sep 16, 2010 at 2:43 PM, Anonymous user
wrote: Hello everybody,
I'm currently trying to serialize functors.
Apparently, it is not possible to serialize boost::function objects,
is it possible to find a way to serialize pointer to function ? (in order to make working the example below*)
If it is definitively not possible, is there a way to 'register' a relation betwwen objects and functor ? (using MPL library).
The idea is to rebuild the boost function according to the 'Object and 'Type' class of the template'.
Example:
mpl::pair
serialize()
{
boost::function
m_getter=mpl::at< container,Object>; }
*Here is the problem that� I'm trying to resolve:
MyObject myObject("before");
SetCommandstd::string,MyObject SetCommand(myObject,&MyObject::setName,&MyObject::getName,"after");
SetCommand.do(); � // call functor 'MyObject::setName("after")'
SetCommand.undo(); // call functor 'MyObject::setName("before")'
template
class SetCommand : public Command
{
� public:
� � � � template
� � � � SetCommand(Object& o, const Setter& setter, const Getter& getter, const Type& value) :
� � � � � � Command(),
� � � � � � m_getter(boost::bind(getter, &o)),
� � m_setter(boost::bind(setter, &o, _1)),
� � � � � � initialValue(m_getter()),
� � � � � � finalValue(value)
� � � � {
� � � � }
virtual void undo()
� � � � {
� � � � � � m_setter(initialValue) ;
� � � � }
� � � � virtual void do()
� � � � {
� � � � � � m_setter(finalValue) ;
� � � � }
�� private:
friend class boost::serialization::access;
SetCommand(){}
template <class Archive>
void serialize( Archive & ar, const unsigned int version )
{
� ar & m_getter; ERROR -> does not compile !!!
� ar & m_setter; ERROR -> does not compile !!!
� ar & initialValue;
� ar & finalValue;
}
boost::function
m_getter ; � � � � boost::function
m_setter ; � � � � Type initialValue ;
� � � � Type finalValue ;
};
Thanks in advance.
You need to somehow register the function in there, if you know what a limited set of types it might be, an integer that can rebuild it would work, but the most common way is to just make a base class with an operator() and have a (shared) pointer to that be your 'function', then base classes (that can be properly serialized) to act as the functions. Not pretty, not the easiest, but it works well and is expandable. _______________________________________________ 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
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
participants (2)
-
Anonymous user
-
Robert Ramey