Boost logo

Boost :

Subject: Re: [boost] [smart_ptr] shared_ptr template type
From: Frank Mori Hess (frank.hess_at_[hidden])
Date: 2009-07-14 14:40:51


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On Friday 10 July 2009, Peter Dimov wrote:
> Frank Mori Hess wrote:
> > And also I imagine generalized_shared could be implemented by copying
> > the entire shared_ptr codebase and then making only minor changes.
>
> shared_count can already take an arbitrary P/D pair, so you only need to
> copy shared_ptr itself, not the rest of the codebase.

Ah, ok. Based on your encouragement (at least in the relative sense, since
everyone else's response has been negative :), I've implemented a first pass
at generic_shared. For those just tuning in generic_shared is like a
shared_ptr but can accept a more generic type as the "pointer". It can
accept either a plain old pointer as its template type, or a class which:

1) has operator->() and operator*()
2) either has value_type/reference/pointer member typedefs, or a
specialization of boost::smart_pointer_traits
3) has a is_null_pointer() free function findable by ADL
4) supports (in)equality comparison

generic_shared can also be used by another smart pointer class with similar
requirements, since it fulfills them all itself. So you could have
monitor<generic_shared<T*> > , to bring back the monitor example.

The code is in the file generic_shared.hpp (a fork of shared_ptr.hpp from
trunk) which is located in the sandbox directory:

https://svn.boost.org/trac/boost/browser/sandbox/fmhess/boost/smart_ptr

The file can be copied into the main boost tree (I've been testing it with
trunk). There is also a shared_ptr.hpp in the same directory, which
implements shared_ptr on top of generic_shared. I used that mainly so I
could do some testing of generic_shared using the shared_ptr tests
unmodified. The tests pass, except for those which use
enabled_shared_from_this or weak_ptr. Beyond that, I've played around a
little with a few basic tests that use a user-defined pointer type, and also
use a list iterator as the pointer type (with a deleter that uses the
iterator to erase the element from its list).

So, the current status is: no generic_weak (analogous to weak_ptr) yet, and no
shared_from_this support. Also, there is an unresolved issue with respect to
casting for non-plain old pointer types. generic_shared tries to apply
boost::static/const/dynamic_pointer_cast to its pointer type, but ADL doesn't
work for boost::static/const/dynamic_pointer_cast, and specialization won't
work either. It seems I'll need to add a ADL hook for casts as suggested at
the end of this thread:

http://lists.boost.org/Archives/boost/2006/06/106406.php

so user-defined pointer types can be support casting.

So, before I spend any more time on it, is this something worth pursuing
further as far as boost goes? Does it stand any chance of making it into the
smart_ptr library? Is there a silent majority out there who thinks this is a
worthwhile idea, or is it just me? I might even settle for a vocal
minority :)

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (GNU/Linux)

iEYEARECAAYFAkpc0TMACgkQ5vihyNWuA4VC6ACfW9UfTkCHTbmPc8a+VH2oFU/c
XDEAn2B8wckssSMUL9yK1XUqZPfCyl1r
=u9n9
-----END PGP SIGNATURE-----


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