New shared pointer
Frerich Raabe
raabe at kde.org
Fri Sep 9 23:02:34 BST 2005
Moin,
the current shared pointer implementation in KShared/KSharedPtr is a little
too simplistic IMHO:
- it requires all classes with onto whose objects you want to point with a
KSharedPtr to provide the interface given by KShared; in practice, this means
that you might have to change the code of the target class because you either
need to make it inherit KShared, or you add the ref()/deref() functions
- it provides an implicit conversion to T* which is very evil. For instance,
this works:
KSharedPtr<Widget> p( new Foo );
KSharedPtr<Widget> q = p;
delete p;
q->doStuff(); // boom!
This is especially likely to happen when you're porting code to KSharedPtr
because you might forget to remove a delete call. I noticed that somebody
added my name to the corresponding entry in kdelibs/TODO which points these
things out so here's a try at a slightly less simplistic (yet by no means
comprehensive) shoot at a shared pointer. It's very much like the old
KSharedPtr, except that
- you don't have to inherit KShared anymore, you can use it with any type.
- it doesn't have an implicit conversion to T* so you cannot accidentally
delete the internal object. You can still use the get() function to retrieve
the raw pointer if you really need to.
- the data() function had a weird name - get() is not much better I guess, but
a few other shared pointer implementations use that name, so I went for it.
Feel free to suggest something more appropriate.
Note that this new KSharedPtr is not like QSharedDataPointer because it does
not do copy-on-write (it doesn't automatically detach() when using the
non-const * and -> operators) - it features a copy() function though, so you
can have explicitely shared objects.
Please have a look and point stuff out which is worth being pointed out. It's
still kinda rough (I only tested it with a few code snippets), but you'd get
the idea what's supposed to happen. I'm very well aware that this is a great
bikeshed topic but please refrain from endless theoretical discussions. I'd
appreciate proper C++ code which breaks when using this class or straight
"Looks okay, we can give it a try and see what breaks", tho.
- Frerich
P.S.: I considered using QSharedData/QSharedDataPointer but that duo has
exactly the same two problems as KShared/KSharedPtr I mentioned above. I also
considered boost::shared_ptr but noticed that it pulls in a lot of other
funky template magic (which, as the honest tears of a certain SUSE engineer
agree with, might be a little too much for the compiler of choice).
-------------- next part --------------
A non-text attachment was scrubbed...
Name: ksharedptr.h
Type: text/x-c++hdr
Size: 4612 bytes
Desc: not available
URL: <http://mail.kde.org/pipermail/kde-core-devel/attachments/20050909/343d749b/attachment.h>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 187 bytes
Desc: not available
URL: <http://mail.kde.org/pipermail/kde-core-devel/attachments/20050909/343d749b/attachment.sig>
More information about the kde-core-devel
mailing list