Boost logo

Boost Users :

Subject: Re: [Boost-users] Serialization of functors and MPL use
From: Anonymous user (nokiac5eel_at_[hidden])
Date: 2010-09-19 14:52:53


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, constunsignedint 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 !

Regards.

> ----- Original message -----
> From: "Robert Ramey‎" <ramey_at_[hidden]>
> To: boost-users_at_[hidden]
> 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_at_[hidden]
> http://lists.boost.org/mailman/listinfo.cgi/boost-users
>

--------------------------------------------------------------
Ovi Mail: Making email access easy
http://mail.ovi.com



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