|
Boost Users : |
From: Fernando Cacciola (yg-boost-users_at_[hidden])
Date: 2003-08-07 13:49:48
Peter Dimov <pdimov_at_[hidden]> wrote in message news:001101c35d0a$e1cab440$1d00a8c0_at_pdimov2...
> Fernando Cacciola wrote:
> > That is, instead of directly calling:
> >
> > X x ;
> > Y y ;
> > x.f(1);
> > y.g('a',1.2);
> >
> > I want to do something that would look like:
> >
> > X x;
> > Y y;
> > ActionList actions;
> > actions.push_back( make_closure(&X::f, x,1) );
> > actions.push_back( make_closure(&Y::g, y,'a',1.2) );
> > std::for_each(actions.begin(),actions.end(),execute_action());
>
> typedef std::list< boost::function<void()> > ActionList;
> #define make_closure boost::bind
>
> struct execute_action
> {
> template<class F> void operator()(F & f) const
> {
> f();
> }
> };
>
> ActionList actions;
> actions.push_back( make_closure(&X::f, &x, 1) );
> actions.push_back( make_closure(&Y::g, &y, 'a', 1.2) );
> std::for_each(actions.begin(),actions.end(),execute_action());
>
Beautyful!
Just copy-pasted and it worked just as you wrote it.
> &x and &y since I'm assuming that you don't want x and y copied and stored
> in the closure.
>
Yes...
Thanks
Fernando Cacciola
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