Boost logo

Boost Users :

Subject: Re: [Boost-users] [boost-users][bind, lambda, ...] binding unused actual parameter
From: Roman Perepelitsa (roman.perepelitsa_at_[hidden])
Date: 2009-04-16 07:37:53


2009/4/16 Igor R <boost.lists_at_[hidden]>

> Hello,
>
> Is it possible somehow to make a binder that stores "unexpected" argument?
>
> Motivation: I have to create & pass a callback handler from within
> intusively ref-counted object (MS-ATL) to an outer asynchronous
> subsystem. If the object were shared-ptr enabled, I'd do this as
> usually:
>
> ThisObj::handler()
> {
> //...
> }
> ThisObj::doSomething()
> {
> asyncSubsystem_->doSomething(&ThisObj::handler, shared_from_this());
> }
>
> However, in my case I have to do something like this:
>
> asyncSubsystem_->doSomething(&ThisObj::handler, this, mySmartPtr); //
> store someOtherSmartPtr in the functor, just to ensure "this" won't
> die before the handler is invoked
>
> Is it possible with bind, lambda or some other library?
>
> Thanks.
>

You can overload boost::get_pointer for your smart pointer.

  namespace boost {
  template <class T> T * get_pointer(CComPtr<T> const& p)
  {
    return p;
  }
  }

  CComPtr<ThisObj> self(this);
  asyncSubsystem_->doSomething(bind(&ThisObj::handler, self));

If you don't want or can't overload get_pointer, you can use double bind
trick.

  CComPtr<ThisObj> self(this);
  asyncSubsystem_->doSomething(bind(bind(&ThisObj::handler, this), self));

First bind creates a function object that accepts any number of arguments,
and second bind creates a function object that stores smart pointer inside
of it.

Roman Perepelitsa.



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