From 3db24addcdc09db817c220620138eb463fd8b58d Mon Sep 17 00:00:00 2001 From: Johannes Schickel Date: Sun, 30 Mar 2008 01:26:51 +0000 Subject: - Added support for custom deletion operator (aka deleter) support for SharedPtr. - Removed two failing comparison tests of SharedPtr in our test suite (those were not supported according to our documentation anyway) svn-id: r31312 --- common/ptr.h | 18 ++++++++++++++---- test/common/ptr.h | 23 ++++++++++++++++++----- 2 files changed, 32 insertions(+), 9 deletions(-) diff --git a/common/ptr.h b/common/ptr.h index 30a61a2f5b..a4aa6d0707 100644 --- a/common/ptr.h +++ b/common/ptr.h @@ -51,6 +51,16 @@ private: T *_ptr; }; +template +class SharedPtrDeletionDeleterImpl : public SharedPtrDeletionInternal { +public: + SharedPtrDeletionDeleterImpl(T *ptr, D d) : _ptr(ptr), _deleter(d) {} + ~SharedPtrDeletionDeleterImpl() { _deleter(_ptr); } +private: + T *_ptr; + D _deleter; +}; + /** * A simple shared pointer implementation modelled after boost. * @@ -72,10 +82,9 @@ private: * ->, which does the same as the -> operator on a normal pointer. * * Be sure you are using new to initialize the pointer you want to manage. - * Pointers pointing to memory not allocated by new, will cause undefined - * behavior on deletion. That is for example the case on pointers created - * with malloc (or similar) and new[]. This prevents the use of SharedPtr - * for arrays! + * If you do not use new for allocating, you have to supply a deleter as + * second parameter when creating a SharedPtr object. The deleter has to + * implement operator() which takes the pointer it should free as argument. * * Note that you have to specify the type itself not the pointer type as * template parameter. @@ -105,6 +114,7 @@ public: SharedPtr() : _refCount(0), _deletion(0), _pointer(0) {} template explicit SharedPtr(T2 *p) : _refCount(new RefValue(1)), _deletion(new SharedPtrDeletionImpl(p)), _pointer(p) {} + template SharedPtr(T2 *p, D d) : _refCount(new RefValue(1)), _deletion(new SharedPtrDeletionDeleterImpl(p, d)), _pointer(p) {} SharedPtr(const SharedPtr &r) : _refCount(r._refCount), _deletion(r._deletion), _pointer(r._pointer) { if (_refCount) ++(*_refCount); } template SharedPtr(const SharedPtr &r) : _refCount(r._refCount), _deletion(r._deletion), _pointer(r._pointer) { if (_refCount) ++(*_refCount); } diff --git a/test/common/ptr.h b/test/common/ptr.h index 7102a819cf..b7ed637040 100644 --- a/test/common/ptr.h +++ b/test/common/ptr.h @@ -34,6 +34,24 @@ class PtrTestSuite : public CxxTest::TestSuite TS_ASSERT(p1.unique()); } + template + struct Deleter { + bool *test; + void operator()(T *ptr) { *test = true; delete ptr; } + }; + + void test_deleter() { + Deleter myDeleter; + myDeleter.test = new bool(false); + + { + Common::SharedPtr p(new int(1), myDeleter); + } + + TS_ASSERT_EQUALS(*myDeleter.test, true); + delete myDeleter.test; + } + void test_compare() { Common::SharedPtr p1(new int(1)); Common::SharedPtr p2; @@ -43,10 +61,5 @@ class PtrTestSuite : public CxxTest::TestSuite TS_ASSERT(p1 != 0); TS_ASSERT(p2 == 0); - - // Note: The following two currently do *not* work, contrary to - // what the Doxygen comments of SharedPtr claim. - TS_ASSERT(p1 != (int *)0); - TS_ASSERT(p2 == (int *)0); } }; -- cgit v1.2.3