$include_dir="/home/hyper-archives/boost/include"; include("$include_dir/msg-header.inc") ?>
Subject: Re: [boost] Weak functor: Interested? (How to boost::bind to a weak_ptr)
From: Vladimír Tøebický (vladimir.trebicky_at_[hidden])
Date: 2012-01-11 04:46:42
On Wed, Jan 11, 2012 at 10:38 AM, Roman Perepelitsa
<roman.perepelitsa_at_[hidden]> wrote:
> 2012/1/11 VladimÃr TÅebický <vladimir.trebicky_at_[hidden]>
>
>> On Wed, Jan 11, 2012 at 9:19 AM, Roman Perepelitsa
>> <roman.perepelitsa_at_[hidden]> wrote:
>>
>> > It can be done. In fact, it has already been done. See
>> > http://listarchives.boost.org/boost-users/2011/09/70647.php.
>>
>> Nice. I like this. However, once you declare the get_pointer (plus
>> ptr_adater and invoker) there's no way to specify what should happen
>> if lock() returns empty shared_ptr. In the example "invoker" returns
>> default value:
>>
>> R operator()() const
>> {
>> Â return ci_.first ? (ci_.first->*ci_.second)() : R();
>> }
>>
>> which may (a) not be possible (if R is not default-constructible) or
>> (b) not desired (eg. if user wants an exception to be thrown).
>>
>> Do you think there would be a way to control the behavior?
>
>
> Â In this case one will have to use something like boost::weak_fn or write
> the trampoline function manually.
>
> void do_something(const weak_ptr<foo>& p) {
> Â // Complete freedom here.
> Â if (shared_ptr<foo> sp = p.lock())
> Â Â sp->do_something();
> Â else
> Â Â throw "expired weak_ptr";
> }
>
> bind(do_something, my_weak_ptr)();
Sure, that's always a way. Once you start having a lot of callbacks it
may become messy.
> In practice, however, I only ever used void-returning functors with
> weak_ptr.
Agreed, me to. But typically I need the callbacks to either silently
do nothing or throw.
Anyway, the question is whether such a tool may find place in Boost
for people looking for this kind of behavior, wdyt?
V.