Boost logo

Boost Users :

Subject: Re: [Boost-users] [boost] [shared_ptr] A smarter smart pointer proposal for dyn
From: Emil Dotchevski (emildotchevski_at_[hidden])
Date: 2008-12-24 11:42:58


On Wed, Dec 24, 2008 at 3:06 AM, Max <loadcom_at_[hidden]> wrote:
> Hello Dotchevski,
>
> Could you please elaborate?

Let's say you have a shared_ptr<void const> that keeps the DLL afloat,
and another shared_ptr<T> which requires the module to remain loaded.
You could do:

template <class T>
struct wrapper
{
  shared_ptr<T> obj_;
  shared_ptr<void const> lifetime_;

  wrapper(
    shared_ptr<T> const & obj,
    shared_ptr<void const> const & lifetime ):
      obj_(obj),
      lifetime_(lifetime)
  {
  }
};

template <class T>
shared_ptr<T>
inject_lifetime(
  shared_ptr<T> const & obj,
  shared_ptr<void const> const & lifetime )
{
  shared_ptr< wrapper<T> > w( new wrapper<T>(obj,lifetime) );
  return shared_ptr<T>(w,w.obj_->get());
}

Emil Dotchevski
Reverge Studios, Inc.
http://www.revergestudios.com/reblog/index.php?n=ReCode


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