�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<Object,Function> container;serialize(){boost::function<Type ()>�m_getter=mpl::at<�container,Object>;}��*Here is the problem that �I'm trying to resolve:�MyObject�myObject("before");SetCommand<std::string,MyObject> SetCommand(myObject,&MyObject::setName,&MyObject::getName,"after");�SetCommand.do(); � // call functor 'MyObject::setName("after")'SetCommand.undo();�// call functor 'MyObject::setName("before")' template<typename Type,typename Object>class�SetCommand : public Command{ ��public:���� � �template�<typename�Setter,�typename�Getter>�� � � �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<Type ()>�m_getter�;� � � ��boost::function<void�(Type)>�m_setter�;�� � � � Type�initialValue�;� � � � Type�finalValue�;}; Thanks in advance. -------------------------------------------------------------- Ovi Mail: Making email access easy http://mail.ovi.com
Anonymous user wrote:
Hello everybody,
I'm currently trying to serialize functors. ... Thanks in advance.
Without going through your specific situation, I can say that this is an easy problem. Here are a couple of ways to do it. a) use boost::variant Make your functors: f, g, h make a variant boost::variant<f, g, h> v; v = f or g or h ... #include boost/serialization/variant.hpp serialize v b) make your functors f, g, h all derived from a base class - functors make the base class polymorphic by making at least one member virtual EXPORT f, g, and h functor *fx; fx = &f, or &g, or &h serialize fx c) There might be a way to do it with boost::any also. Robert Ramey
participants (2)
-
Anonymous user
-
Robert Ramey