|
Boost Users : |
Subject: Re: [Boost-users] Serialization of functors and MPL use
From: OvermindDL1 (overminddl1_at_[hidden])
Date: 2010-09-19 20:05:33
On Sun, Sep 19, 2010 at 12:52 PM, Anonymous user <nokiac5eel_at_[hidden]> wrote:
> Hello,
>
> Thanks for the quick response !
>
> It is a good solution, but hhat I try to do is the following:
>
> template<class Object,class Type>
>
> struct my_class
> {
>
> Â boost::function<Type ()> *m_f;
>
> template<typename Functor>
> my_class(Object &obj,Functor &f,Type & newValue)
> {
> Â m_f= boost::bind(f,&obj);
> }
>
> Â ...
> Â void serialize(Archive &ar, const unsigned int version)
> {
> Â Â Â Â Â ar & m_f; //does not compile
> Â }
>
> Â };
>
> Instantiation:
>
> Â MyObject myObject("old value");
> my_class < MyObject,std::string> m(myObject,&MyObject::setName,e,"new
> value");
>
> Thus the idea is to avoid changing the design (no inheritance to create
> functor) and just use boost::function
> to create dynamically functors.
> My problem is to rebuild dynamically the boost::function in the
> serialization step.
> I really needs to avoid using inheritance on my classes just to creates
> functors, thus I really need to keep the current design and just build
> dynamically functors through boost::bind.
>
> One possible idea was to register a pair of types -> object which contains
> the function and the functor.
> Example:
>
> template<class Object,class Type>
>
> struct my_class
> {
>
> typedef boost::mpl::pair<Object,Type> key;
> typedef boost::function<Type ()> value;
>
> typedef boost::mpl::map< key,value > > Functions;
>
> template<typename Functor>
> my_class(Object &obj,Functor &f,Type & newValue)
> {
> Â m_f= boost::bind(f,&obj);
> typedef boost::mpl::insert< Functions,Functor > > t;
> }
>
> ...
>
> template <class Archive>
> void serialize( Archive & ar, const unsigned int version )
> {
> Â Â typedef boost::mpl::at<key>()::type t;
> Â Â ar & t; //not possible -> does not instantiate the real functor address !
> }
>
> };
>
> The problem is the above solution is that we don't store the functor
> address but only it's signature.
>
> If you have an idea to make this example working or another idea to store
> functors which have been created dynamicaly, don't hesitate.
>
>
>
> Thanks again for your help !
As he said, you cannot do it using that method. Do *not* use
boost/std::function or bind anywhere! You have to use a virtual base
with specialized children if you are going to be 'bind'ing things.
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