Re: Re: [Fwd: Re: [Boost-users] bind return value]

Darren Vincent Hart <[email protected]> Wed, 10 Sep 2003 09:08:09 -0600
Newsgroups gmane.comp.embedded.stk.gui.devel
Message-ID <1063204235.764.6.camel@sway>
On Wed, 2003-09-10 at 08:08, Marc Str=C3=A4mke wrote:
> Peter Dimov's Solution was basicly what i was thinking of, he's a few=20
> years ahead of me in metaprogramming though *g*
> I see one problem though , the first(simpler) solution makes no=20
> provisions for supporting signal arguments does it? As far as i=20
> understand the limitation of C++'s metaprogramming here we need one=20
> boost_return overload/specialisation for each number of arguments support=
ed.
>=20

I don't think so.  Libstk signals all accept bool(void) slots, so there
are no arguments.  When we use bind to use a method that takes
arguments, it (bind) returns a functor that returns the same thing as
the method, but takes no arguments, so I don't think this is an issue.=20
I am implementing a test case atm (including argument passing to
"bind'ed" slots).

Darren

> Darren Vincent Hart wrote:
> > Here is a response from Peter Dimov (this guy knows his stuff) regardin=
g
> > our signal return type problem.  At least I have been looking in the
> > right place!  I'll look into this and present an STK solution by
> > tomorrow.
> >=20
> > One of the main differences between bind and lambda::bind is that bind
> > allows up to 9 args while lambda::bind is limited to 3 or less.
> >=20
> > Darren
> >=20
> > -----Forwarded Message-----
> > From: Peter Dimov <pdimov-jZ/[email protected]>
> > To: Boost Users mailing list <[email protected]>
> > Subject: Re: [Boost-users] bind return value
> > Date: Wed, 10 Sep 2003 14:21:49 +0300
> >=20
> > Darren Vincent Hart wrote:
> >=20
> >>Boost gurus,
> >>
> >>I am using boost::bind in combination with boost::signals for a GUI
> >>toolkit.  The signals return bool for convenience, but it is often
> >>desireable to bind a void function and connect it to a signal.  Is
> >>there a convenient way to assign a return value for a void function.
> >>
> >>As an example:
> >>
> >>-----------------------------------------------------------------
> >>
> >>struct panel
> >>{
> >>    void print() { std::cout << "print" << std::endl; }
> >>}
> >>
> >>struct widget
> >>{
> >>    boost::signal<bool ()> on_event;
> >>}
> >>
> >>panel p;
> >>widget w;
> >>
> >>// is there some way to do something like this?
> >>w.on_event.connect(boost::bind_return(panel::print, &p, true));
> >=20
> >=20
> > You can do this with Lambda:
> >=20
> > using namespace boost::lambda;
> >=20
> >     w.on_event.connect( (bind(panel::print, &p), true) );
> >=20
> > but not with boost::bind, sorry.
> >=20
> > Not out of the box, anyway.
> >=20
> > template<class F, class V> class override_return_t
> > {
> > private:
> >=20
> >     F f_;
> >     V v_;
> >=20
> > public:
> >=20
> >     typedef V result_type;
> >=20
> >     override_return_t(F const & f, V const & v): f_(f), v_(v)
> >     {
> >     }
> >=20
> >     result_type operator()()
> >     {
> >         f_();
> >         return v_;
> >     }
> >=20
> >     result_type operator()() const
> >     {
> >         f_();
> >         return v_;
> >     }
> > };
> >=20
> > template<class F, class V> override_return_t<F, V> override_return(F co=
nst &
> > f, V const & v)
> > {
> >     return override_return_t<F, V>(f, v);
> > }
> >=20
> > #include <boost/bind.hpp>
> > #include <boost/function.hpp>
> > #include <iostream>
> >=20
> > void g()
> > {
> >     std::cout << "g()\n";
> > }
> >=20
> > int main()
> > {
> >     boost::function<bool()> f =3D override_return(boost::bind(g), true)=
;
> >     std::cout << f() << std::endl;
> > }
> >=20
> > You can even go a bit further and bring boost::bind one step closer to =
its
> > lambda counterpart:
> >=20
> > template<class R, class F, class L, class V>
> > override_return_t<boost::_bi::bind_t<R, F, L>, V>
> > operator,(boost::_bi::bind_t<R, F, L> const & f, V const & v)
> > {
> >     return override_return_t<boost::_bi::bind_t<R, F, L>, V>(f, v);
> > }
> >=20
> > int main()
> > {
> >     boost::function<bool()> f =3D (boost::bind(g), true);
> >     std::cout << f() << std::endl;
> > }
> >=20
> > _______________________________________________
> > Boost-users mailing list
> > [email protected]
> > http://lists.boost.org/mailman/listinfo.cgi/boost-users
>=20
>=20
>=20
>=20
> _______________________________________________
> Libstk mailing list
> [email protected]
> http://www.dvhart.com/cgi-bin/mailman/listinfo/libstk