|
Boost Users : |
Subject: Re: [Boost-users] Serialization of functors and MPL use
From: OvermindDL1 (overminddl1_at_[hidden])
Date: 2010-09-17 02:22:11
On Thu, Sep 16, 2010 at 2:43 PM, Anonymous user <nokiac5eel_at_[hidden]> 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<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.
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 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