"weak" binding to member?
I know that 'bind' (now in std::tr1) will bind pointers to member functions to smart pointers. But is there a simple way to bind to a "weak" pointer, such that the attempted call will realize the smart pointer and if successful perform the call, or if unsuccessful just not call anything without complaint? (In this case, it appears that the return is void) —John
I know that 'bind' (now in std::tr1) will bind pointers to member functions to smart pointers. But is there a simple way to bind to a "weak" pointer, such that the attempted call will realize the smart pointer and if successful perform the call, or if unsuccessful just not call anything without complaint? (In this case, it appears that the return is void)
Not sure you can do this directly, but you can use weak_ptr as a tracking object for the caller that attempts to invoke your binder.
On 9/12/2011 1:18 AM, Igor R wrote:
I know that 'bind' (now in std::tr1) will bind pointers to member functions to smart pointers. But is there a simple way to bind to a "weak" pointer, such that the attempted call will realize the smart pointer and if successful perform the call, or if unsuccessful just not call anything without complaint? (In this case, it appears that the return is void)
Not sure you can do this directly, but you can use weak_ptr as a tracking object for the caller that attempts to invoke your binder.
I don't know what you mean by that.
I don't know what you mean by that.
I mean that you can pass a weak_ptr along with the binder:
void setYourFunctor(const function
On 9/12/2011 12:41 PM, Igor R wrote:
I don't know what you mean by that.
I mean that you can pass a weak_ptr along with the binder:
void setYourFunctor(const function
&func, const weak_ptr<void> &track) { // store "func" and "track" func_ = func; track_ = track; } // Now in the place where "func_" should be called: void call() { shared_ptr<void> p = track_.lock(); if (p) func_(); }
// Now use it this way: shared_ptr<YourClass> ptr = ...; setYourFunctor(bind(&YourClass::doSomething, ptr.get()), ptr);
I see: assume that the raw pointer value doesn't change, and just use the weak ptr to ensure that it's still valid, but it is not necessary to pass the pointer to the call each time.
I know that 'bind' (now in std::tr1) will bind pointers to member functions to smart pointers. But is there a simple way to bind to a "weak" pointer, such that the attempted call will realize the smart pointer and if successful perform the call, or if unsuccessful just not call anything without complaint? (In this case, it appears that the return is void)
Well, I think I've figured out how to solve your problem. Since
Boost.Bind invokes your functor through ->* operator, you just need to
define your own get_pointer() function that returns an adapter that
defines ->* operator, which would do what you want.
The last step is somewhat cumbersome, but you can use the following
Meyers' article:
http://www.aristeia.com/Papers/DDJ_Oct_1999.pdf
I've tested the following code and it seems to work:
#include
On 9/13/2011 4:35 AM, Igor R wrote:
Well, I think I've figured out how to solve your problem. Since Boost.Bind invokes your functor through ->* operator, you just need to define your own get_pointer() function that returns an adapter that defines ->* operator, which would do what you want.
Interesting. I had thought about defining an adaptor that provided operator->, but that would not work with the semantics I want to duplicate. It would be perfect if you intended to throw an exception if the weak ptr was dead. But using ->* gives control over the actual call, so the code can be skipped. I suppose that the inner workings of bind must use operator->* at some point (how else can it work?) this would be portable, even though it's not mentioned in the documentation or the standard. I already wrote an adaptor that is analogous to mem_fn, which is probably about the same complexity as what you show, and doesn't court portability issues. But thank you very much for the idea; I think it is easy to forget about operator->* when looking for solutions. —John
participants (2)
-
Igor R
-
John M. Dlugosz