[Scummvm-cvs-logs] SF.net SVN: scummvm: [31312] scummvm/trunk

lordhoto at users.sourceforge.net lordhoto at users.sourceforge.net
Sun Mar 30 03:26:51 CEST 2008


Revision: 31312
          http://scummvm.svn.sourceforge.net/scummvm/?rev=31312&view=rev
Author:   lordhoto
Date:     2008-03-29 18:26:51 -0700 (Sat, 29 Mar 2008)

Log Message:
-----------
- 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)

Modified Paths:
--------------
    scummvm/trunk/common/ptr.h
    scummvm/trunk/test/common/ptr.h

Modified: scummvm/trunk/common/ptr.h
===================================================================
--- scummvm/trunk/common/ptr.h	2008-03-30 01:09:05 UTC (rev 31311)
+++ scummvm/trunk/common/ptr.h	2008-03-30 01:26:51 UTC (rev 31312)
@@ -51,6 +51,16 @@
 	T *_ptr;
 };
 
+template<class T, class D>
+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 @@
  * ->, 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 @@
 
 	SharedPtr() : _refCount(0), _deletion(0), _pointer(0) {}
 	template<class T2> explicit SharedPtr(T2 *p) : _refCount(new RefValue(1)), _deletion(new SharedPtrDeletionImpl<T2>(p)), _pointer(p) {}
+	template<class T2, class D> SharedPtr(T2 *p, D d) : _refCount(new RefValue(1)), _deletion(new SharedPtrDeletionDeleterImpl<T2, D>(p, d)), _pointer(p) {}
 
 	SharedPtr(const SharedPtr &r) : _refCount(r._refCount), _deletion(r._deletion), _pointer(r._pointer) { if (_refCount) ++(*_refCount); }
 	template<class T2> SharedPtr(const SharedPtr<T2> &r) : _refCount(r._refCount), _deletion(r._deletion), _pointer(r._pointer) { if (_refCount) ++(*_refCount); }

Modified: scummvm/trunk/test/common/ptr.h
===================================================================
--- scummvm/trunk/test/common/ptr.h	2008-03-30 01:09:05 UTC (rev 31311)
+++ scummvm/trunk/test/common/ptr.h	2008-03-30 01:26:51 UTC (rev 31312)
@@ -34,6 +34,24 @@
 		TS_ASSERT(p1.unique());
 	}
 
+	template<class T>
+	struct Deleter {
+		bool *test;
+		void operator()(T *ptr) { *test = true; delete ptr; }
+	};
+
+	void test_deleter() {
+		Deleter<int> myDeleter;
+		myDeleter.test = new bool(false);
+		
+		{
+			Common::SharedPtr<int> p(new int(1), myDeleter);
+		}
+
+		TS_ASSERT_EQUALS(*myDeleter.test, true);
+		delete myDeleter.test;
+	}
+
 	void test_compare() {
 		Common::SharedPtr<int> p1(new int(1));
 		Common::SharedPtr<int> p2;
@@ -43,10 +61,5 @@
 
 		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);
 	}
 };


This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.




More information about the Scummvm-git-logs mailing list