ANN: AutoNewPtr (oh yes, yet another smartpointer)
{ since lines in this posting are longer than recommended 70
symbols, you can see some reformatting by the server or by your
newsreading software. there are several links in the text, you
might need to enter them into your browser manually. -mod }
I just coded this up, and hopefully by mentioning it here I'll get some
useful
feedback, like, bugs, shortcomings, improvements...
http://code.google.com/p/alfps/source/browse/trunk/alfs/pointers/AutoNewPtr.
hpp
AutoNewPtr ("new" Berkeley open source license)
* Is not initialized with a raw pointer, but with constructor arguments
for
the to-be referent: AutoNewPtr takes responsibility for 'new'.
* By doing that, it has the ability to add a notifier to the created
object,
so that the smart pointer gets notified when referred object
self-destructs,
if it does (e.g., a window or file closing, or a non-correctable
error).
* After referred object destroyed, pointer reports that it's void and
throws
on access via ->.
* Also, the default constructor sets the pointer to void state.
* Otherwise much like shared_ptr, except customization of deleter is per
type (via c++0x-like std::default_delete) rather than per instance.
Since I haven't yet used this beast, other than testing the basic
functionality,
http://code.google.com/p/alfps/source/browse/trunk/test/pointers/auto_new_pt
r/01/main.cpp,
I really don't know how it fares. I suspect -> access is not the most
efficient
since it's checking for voidness. But that shouldn't be a problem with e.g.
files and windows and such objects where this pointer would be relevant, I
think, and the main objective here is programmer productivity, not
efficiency.
It's not just one file (AutoNewPtr just wraps CheckedPtr, which uses various
things), so I think one would need the whole shebang, which Google says
# Non-members may check out a read-only working copy anonymously over
HTTP.
svn checkout http://alfps.googlecode.com/svn/trunk/ alfps-read-only
In there is also an adapted version of Howard Hinnants C++0x unique_ptr
emulation, and an adapted version of Turkani's (et.al.'s) move_ptr. I'd
hoped
to be able to use unique_ptr instead of e.g. auto_ptr, looking to the
future!
But unfortunately MSVC 7.1 doesn't really like the unique_ptr code, and
neither
adaption has been used by me for anything, or seriously tested by me. I
will
probably remove these smart pointer adaptions. Just didn't think of it
before
putting the code out there.
Praise and lashings and wild ideas all welcome.
Note that this is just freshly coded, it's currently more idea stuff than
anything else, but, might still be useful as-is!
Cheers,
- Alf
PS: Yes I'm aware that adding things to namespace std is formally
prohibited,
except specializations of existing templates. The stuff added is such
things
that will be added by C++0x. I think it's better to just use namespace std
than
to keep changing code as things migrate boost -> tr1 -> std.
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]