I have a View class that has some widgets, and a Controller class
that controls the view, so the View class has a GetController()
member function that returns a reference to its controller.
When the View's constructor is called, GetController() would return
a bad ref, but it would return a valid reference by the time a
button is clicked... so what I'm trying to do is, inside the View
constructor:
my_button.on_click = bind( (_1 ->* &A::GetController).DoSomething(),
this );
but that obviously doesn't work... I'm even having trouble just
saving the first part:
my_button.on_click = bind
whoops, sorry about the typos. any A:: here is really View::
--- In Boost-Users@y..., "khuroth"
I have a View class that has some widgets, and a Controller class that controls the view, so the View class has a GetController() member function that returns a reference to its controller. When the View's constructor is called, GetController() would return a bad ref, but it would return a valid reference by the time a button is clicked... so what I'm trying to do is, inside the View constructor:
my_button.on_click = bind( (_1 ->* &A::GetController).DoSomething (), this );
but that obviously doesn't work... I'm even having trouble just saving the first part:
my_button.on_click = bind
(_1 ->* &A::GetController, this); also doesn't work... I tried defining the on_click member of the button as a boost::function<void> or a boost::function0<void> in the first example, and a boost::function
or boost::function0 in the second snip... I get immense compiler errors, so I'm not sure where I messed up. What am I doing wrong?
--- In Boost-Users@y..., "Tanton Gibbs"
my_button.on_click = bind( (_1 ->* &A::GetController).DoSomething (), this );
I think what you want is something like bind( &Controller::DoSomething, bind( &A::GetController, _1 ) );
That didn't work when I tried earlier, either because bind() produces a function instead of a pointer to a Controller or because GetController returns a reference instead of a pointer.
I think what you want is something like bind( &Controller::DoSomething, bind( &A::GetController, _1 ) );
That didn't work when I tried earlier, either because bind() produces a function instead of a pointer to a Controller or because GetController returns a reference instead of a pointer.
When binding member pointers, LL accepts either pointers or references as the object argument, and the above should thus work. This example works for me: #include "boost/lambda/bind.hpp" #include "boost/lambda/lambda.hpp" #include <iostream> using namespace boost; using namespace boost::lambda; class Controller { public: void DoSomething() { std::cout << "Hello World!"; }; }; class View { Controller& contr; public: View(Controller& c) : contr(c) {}; Controller& GetController() { return contr; } }; int main () { Controller c; View v(c); bind(&Controller::DoSomething, bind(&View::GetController, _1))(v); }; /Jaakko
Yahoo! Groups Sponsor ADVERTISEMENT
Info: http://www.boost.org Wiki: http://www.crystalclearsoftware.com/cgi-bin/boost_wiki/wiki.pl Unsubscribe: mailto:boost-users-unsubscribe@yahoogroups.com
Your use of Yahoo! Groups is subject to the Yahoo! Terms of Service.
-- -- -- Jaakko Järvi email: jajarvi@cs.indiana.edu -- Post Doctoral Fellow phone: +1 (812) 855-3608 -- Pervasive Technology Labs fax: +1 (812) 855-4829 -- Indiana University, Bloomington
Oh, that works, thanks!
To clarify though, for a member function, bind can be passed either
an object pointer, an object reference, a function that returns an
object pointer (the result of another bind), or a function that
returns an object reference?
I'm definitely a fan of this library now =)
--- In Boost-Users@y..., Jaakko Jarvi
I think what you want is something like bind( &Controller::DoSomething, bind( &A::GetController, _1 ) );
That didn't work when I tried earlier, either because bind() produces a function instead of a pointer to a Controller or because GetController returns a reference instead of a pointer.
When binding member pointers, LL accepts either pointers or references as the object argument, and the above should thus work.
This example works for me:
#include "boost/lambda/bind.hpp" #include "boost/lambda/lambda.hpp"
#include <iostream>
using namespace boost; using namespace boost::lambda;
class Controller { public: void DoSomething() { std::cout << "Hello World!"; }; };
class View { Controller& contr; public: View(Controller& c) : contr(c) {}; Controller& GetController() { return contr; } };
int main () {
Controller c; View v(c);
bind(&Controller::DoSomething, bind(&View::GetController, _1))(v);
};
/Jaakko
Yahoo! Groups Sponsor ADVERTISEMENT
Info: http://www.boost.org Wiki: <http://www.crystalclearsoftware.com/cgi-
bin/boost_wiki/wiki.pl>
Unsubscribe: mailto:boost-users-unsubscribe@y...
Your use of Yahoo! Groups is subject to the Yahoo! Terms of Service.
-- -- -- Jaakko Järvi email: jajarvi@c... -- Post Doctoral Fellow phone: +1 (812) 855-3608 -- Pervasive Technology Labs fax: +1 (812) 855-4829 -- Indiana University, Bloomington
Oh, that works, thanks! To clarify though, for a member function, bind can be passed either an object pointer, an object reference, a function that returns an object pointer (the result of another bind), or a function that returns an object reference?
Yes, here are some of the cases: #include "boost/lambda/bind.hpp" #include "boost/lambda/lambda.hpp" #include <iostream> using namespace boost; using namespace boost::lambda; class Controller { public: void DoSomething() { std::cout << "Hello World!"; }; }; class View { Controller& contr; public: View(Controller& c) : contr(c) {}; Controller& GetController() { return contr; } Controller* GetControllerPtr() { return &contr; } }; int main () { Controller c; View v(c); View* vp = &v; bind(&Controller::DoSomething, bind(&View::GetController, _1))(v); bind(&Controller::DoSomething, bind(&View::GetController, _1))(vp); bind(&Controller::DoSomething, bind(&View::GetControllerPtr, _1))(vp); }; // ------------------------ Cheers, Jaakko
I'm not exactly sure what you are trying to do, but here's some
explanation on bind and ->*
First, The dot operator cannot be overloaded, so you do this:
(_1 ->* &A::GetController).DoSomething
// DoSomething is not defined for (_1 ->* &A::GetController)
The semantics of ->* in general is rather tricky, and the Lambda Library
mimics that:
Here's a snippet of the LL docs:
For a built-in call like this, the result is kind of a delayed
member function call. Such an expression must be followed by a function
argument list, with which the delayed member function call is performed.
For example:
struct B { int foo(int); };
B* b = new B();
...
(b ->* &B::foo) // returns a delayed call to b->foo
// a function argument list must follow
(b ->* &B::foo)(1) // ok, calls b->foo(1)
(_1 ->* &B::foo)(b); // returns a delayed call to b->foo,
// no effect as such
(_1 ->* &B::foo)(b)(1); // calls b->foo(1)
The LL can figure out the return type when binding a pointer to member
function so you do no have to say bind
whoops, sorry about the typos. any A:: here is really View::
--- In Boost-Users@y..., "khuroth"
wrote: I have a View class that has some widgets, and a Controller class that controls the view, so the View class has a GetController() member function that returns a reference to its controller. When the View's constructor is called, GetController() would return a bad ref, but it would return a valid reference by the time a button is clicked... so what I'm trying to do is, inside the View constructor:
my_button.on_click = bind( (_1 ->* &A::GetController).DoSomething (), this );
but that obviously doesn't work... I'm even having trouble just saving the first part:
my_button.on_click = bind
(_1 ->* &A::GetController, this); also doesn't work... I tried defining the on_click member of the button as a boost::function<void> or a boost::function0<void> in the first example, and a boost::function
or boost::function0 in the second snip... I get immense compiler errors, so I'm not sure where I messed up. What am I doing wrong?
Yahoo! Groups Sponsor ADVERTISEMENT [ericsson_300x250.jpg] Info: http://www.boost.org Wiki: http://www.crystalclearsoftware.com/cgi-bin/boost_wiki/wiki.pl Unsubscribe: mailto:boost-users-unsubscribe@yahoogroups.com
Your use of Yahoo! Groups is subject to the Yahoo! Terms of Service.
-- -- -- Jaakko Järvi email: jajarvi@cs.indiana.edu -- Post Doctoral Fellow phone: +1 (812) 855-3608 -- Pervasive Technology Labs fax: +1 (812) 855-4829 -- Indiana University, Bloomington
I'm not exactly sure what you are trying to do, but here's some explanation on bind and ->*
First, The dot operator cannot be overloaded, so you do this:
(_1 ->* &A::GetController).DoSomething
// DoSomething is not defined for (_1 ->* &A::GetController)
The semantics of ->* in general is rather tricky, and the Lambda Library mimics that:
Here's a snippet of the LL docs:
For a built-in call like this, the result is kind of a delayed member function call. Such an expression must be followed by a function argument list, with which the delayed member function call is
Yeah, I'm not sure why I used _1 in the example, but ultimately my
goal is to delay the call to the DoSomething method of Controller.
something like this would feel kind of natural:
boost::function0
For example:
struct B { int foo(int); }; B* b = new B(); ... (b ->* &B::foo) // returns a delayed call to b->foo // a function argument list must follow (b ->* &B::foo)(1) // ok, calls b->foo(1)
(_1 ->* &B::foo)(b); // returns a delayed call to b->foo, // no effect as such (_1 ->* &B::foo)(b)(1); // calls b->foo(1)
The LL can figure out the return type when binding a pointer to member function so you do no have to say bind
, just bind is enugh. If you just try to bind a member function call, leaving the object open, you would write:
bind(&View::GetController, _1);
Could be called as:
View w; bind(&View::GetController, _1)(v);
Cheers, Jaakko
On Tue, 27 Aug 2002, khuroth wrote:
whoops, sorry about the typos. any A:: here is really View::
--- In Boost-Users@y..., "khuroth"
wrote: I have a View class that has some widgets, and a Controller class that controls the view, so the View class has a GetController() member function that returns a reference to its controller. When the View's constructor is called, GetController() would return a bad ref, but it would return a valid reference by the time a button is clicked... so what I'm trying to do is, inside the View constructor:
my_button.on_click = bind( (_1 ->* &A::GetController).DoSomething (), this );
but that obviously doesn't work... I'm even having trouble just saving the first part:
my_button.on_click = bind
(_1 ->* &A::GetController, this); also doesn't work... I tried defining the on_click member of the button as a boost::function<void> or a boost::function0<void> in the first example, and a boost::function
or boost::function0 in the second snip... I get immense compiler errors, so I'm not sure where I messed up. What am I doing wrong?
Yahoo! Groups Sponsor ADVERTISEMENT [ericsson_300x250.jpg] Info: http://www.boost.org Wiki: <http://www.crystalclearsoftware.com/cgi- bin/boost_wiki/wiki.pl> Unsubscribe: mailto:boost-users-unsubscribe@y...
Your use of Yahoo! Groups is subject to the Yahoo! Terms of Service.
-- -- -- Jaakko Järvi email: jajarvi@c... -- Post Doctoral Fellow phone: +1 (812) 855-3608 -- Pervasive Technology Labs fax: +1 (812) 855-4829 -- Indiana University, Bloomington
participants (3)
-
Jaakko Jarvi
-
khuroth
-
Tanton Gibbs