Boost logo

Boost :

From: hicks (hicks_at_[hidden])
Date: 2002-05-11 08:46:07

Topic: new module eigen_ptr,
Keywords: custom_deleters, shared_ptr, weak_ptr, dangling pointers,

eigen_ptr class

The eigen_ptr class makes use of boost::shared_ptr,
boost::weak_ptr, and inhertiance to achieve inverted object
management in an "upside down" approach.
It is useful as a way to improve object management in existing code
with a minimum of reprogramming as it is relatively lighweight and

The basic concept is as follows:
1. Base class "eigen_ptr" is derived from by client class T.
2. "eigen_ptr" has a "shared_ptr<T>" member which it constructs with
a pointer to it's client class instance (&t) and empty deletor functor.
3. "eigen_ptr" provides and interface for obtaining weak pointers
of type boost::weak_ptr<T> which are constructed from
"eigen_ptr"'s member shared_ptr.
4. The weak pointers "*" and "->"-evaluate to t while t is alive,
and "get()" -evaluate to NULL after t is destroyed (along with the
eigen pointer). (This functionality already exists in
the wonderful weak/shared pointer library).
5. Copy constructor and assignment operator of "eigen_ptr" are
made to reinstantiate member shared_ptr, so that it does not
remain referring to the previous instance.

embedding in a class is done as follows
class T : public eigen_ptr<T> {....}

Public interface: (inherited by T)

typedef boost::weak_ptr<T> weak_ptr_type;
weak_ptr_type make_weak_ptr();

It was developed for the following situation::
A pool of bald object pointers was already managed by existing
code which would have been really hard to convert to smart pointer approach.
New code was needed for which the bald pointers approach posed problems,
like are they dangling or not.
Adding "eigen_ptr" to the objects as a base class meant the existing I/F
did not have to be touched, while new code could obtain weak-pointers
which tell if an object has been deleted.

Is there any interest? The code is very short.
Its really just a light frosting with a twist of lemon on shared and
weak pointers,
which are great.

(I had to modify shared_counter.hpp slightly to get weak pointers to
compile under Borland Builder C++ 5.
Anybody interested in that?)

> Re: shared_ptr and custom deleters (Darin Adler)
> This actually deserves a response. It seems that custom deleters are often
> misunderstood or overemphasized. They are _not_ a central point of
> shared_ptr's interface; there are other reasons for keeping a deleter along
> with the pointer. Custom deleter support falls out for free from the
> implementation, which is why we added the appropriate constructor.

"eigen_ptr" uses a custom null deleter. I was glad the custom deleter was there.

Craig Hicks

Boost list run by bdawes at, gregod at, cpdaniel at, john at