Boost logo

Boost :

From: Andy Glew (glew_at_[hidden])
Date: 1999-09-17 14:49:40


Brief
------

I wish there were a versioned_ptr<T> type in Boost,
a template type that checks a version number on dereferencig
a pointer against a version number in the pointee.

Announcement: I have such a template, in case others want to
look at it / bang at it.

Wish: I'd like to find a better resolution to the intrusiveness
/ non-intrusiveness issue for smart pointers.

Detail
--------

A while back Beman asked for suggested additions to Boost.

At the same time as I suggested live_ptr<T>, which Greg Colvin
made into weak_ptr<T>, (i.e. a smart pointer which is set to 0
when the object it points to retires), I also suggested a number of
other smart pointers.

This week, I wrote and started using versioned_ptr<T>,
a pointer which remembers a "version number" which is
also associated with the object it points to.

Please note that I say "associated with"; my present implementation
is intrusive, but just as for live_ptr<T> I anticipate that the same
indirection that Greg used to make weak_ptr<T> non-intrusive
could be used. See note below.

Anyway, I am presently using versioned_ptr<T> as a debugging
tool (since Purify doesn't seem to be available on my system).

I have used similar versioned pointers in the past
as a fast way of implementing live_ptr<T> / weak_ptr<T>;
my present implementation of live_ptr<T> uses a "registry"
{which I have encapsulated in a type regd_ptr<T>}
associated with the pointee that points back to the pointers
pointing to the pointee, and requires O(N) work at destruction,
where N is the number of pointers pointing to the pointee;
versioned_ptr<T> requires O(1) work at destruction,
but is more intrusive in the data structure.

---
Intrusiveness:
I'd like to find a better resolution to the intrusiveness issue
for smart pointer / pointee data types.
There are a number of such types where it is desirable to
have data "associated with" the pointee. Similarly, there
are a number of places where it is desired to have a
callback to the smart pointer on pointee events (e.g. a
pointee that causes an exception if it is deallocated
while a smart pointer to it is outstanding).
Having the pointee inherit from a base type is intrusive.
E.g. if the pointee type is written somewhere else...
Greg Colvin's weak_ptr<T> implementation uses a level
of indirection. This has several weaknesses:
    a) some of us *think* that we cannot afford that extra level of
indirection. (I say "think" because I have been forced to swallow
my words in similar circumstances. It's just that, as a hardware guy,
I know that extra levels of pointers are the worst possible thing.)
    b) it can only be used with dynamically allocated storage.
You can't have such pointers to such pointees if the pointee is stack
allocated.
I really hope that someone can suggest some middle ground.
E.g. perhaps a smart_ptee<T> template that wraps the pointee
base, so that smart_ptr<T> can only be allocated to a smart_ptee<T>,
not to an unadorned T.

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