Boost logo

Boost :

Subject: Re: [boost] intrusive_ptr design question
From: Gottlob Frege (gottlobfrege_at_[hidden])
Date: 2009-07-06 13:51:15


On Mon, Jul 6, 2009 at 11:07 AM, Frank Mori Hess<frank.hess_at_[hidden]> wrote:
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
>
> On Monday 06 July 2009, Zachary Turner wrote:
>> You shouldn't have to store 2 function pointers for every
>> intrusive_ptr object.  You should only have to store 2 function
>> pointers per instance pointed to, and then 1 reference to some shared
>> structure in each intrusive_ptr object.  Much like shared_ptr
>> currently does.
>
> You'd still need a pointer in every intrusive_ptr object to point at the
> shared_structure, plus you'd have to do dynamic allocation of the shared
> object.  That is never going to fly.
>
> - From the "main reasons to use intrusive_ptr" section of its documentation:
>
> "The memory footprint of intrusive_ptr is the same as the corresponding raw
> pointer;"
>

I'm not sure if this does what the OP wants, but the ref counting
could be a policy:

template <typename T> default_intrusive_ptr_refcount_policy
{
   static void add_ref(T * t)
   {
      // by default call the global function
      intrusive_ptr_add_ref(t);
   }
   static void remove_ref(T * t)
   {
      // by default call the global function
      intrusive_ptr_remove_ref(t);
   }
};

template intrusive_ptr<typename T, typename RefCountPolicy =
default_intrusive_ptr_refcount_policy<T> >
{
   // intrusive_ptr details...
   // on add ref, we call RefCountPolicy::add_ref(t)
   // on remove ref, we call RefCountPolicy::remove_ref(t)
};

// examples:
struct Foo { ... };
struct Bar { ... };

intrusive_ptr<Foo> pFoo; // usual intrusive_ptr, same as always

struct BarRefCountPolicy1
{
   static add_ref(Bar * bar)
   {
       // add ref to bar some way
   };
   static remove_ref(Bar * bar)
   {
       // remove ref some way
   }
};

struct BarRefCountPolicy2
{
   static add_ref(Bar * bar)
   {
       // add ref to bar some *pther* way
   };
   static remove_ref(Bar * bar)
   {
       // remove ref some *other* way
   }
};

intrusive_ptr<Bar, BarRefCountPolicy1> pBar1;
intrusive_ptr<Bar, BarRefCountPolicy2> pBar2;

For better or worse...

?

Tony


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk