[Scummvm-cvs-logs] SF.net SVN: scummvm:[48334] scummvm/trunk/common/ptr.h
megath at users.sourceforge.net
megath at users.sourceforge.net
Sat Mar 20 21:01:44 CET 2010
Revision: 48334
http://scummvm.svn.sourceforge.net/scummvm/?rev=48334&view=rev
Author: megath
Date: 2010-03-20 20:01:44 +0000 (Sat, 20 Mar 2010)
Log Message:
-----------
added ScopedPtr template
Modified Paths:
--------------
scummvm/trunk/common/ptr.h
Modified: scummvm/trunk/common/ptr.h
===================================================================
--- scummvm/trunk/common/ptr.h 2010-03-20 19:48:21 UTC (rev 48333)
+++ scummvm/trunk/common/ptr.h 2010-03-20 20:01:44 UTC (rev 48334)
@@ -26,6 +26,7 @@
#define COMMON_PTR_H
#include "common/scummsys.h"
+#include "common/noncopyable.h"
namespace Common {
@@ -216,6 +217,60 @@
T *_pointer;
};
+template<typename T>
+class ScopedPtr : Common::NonCopyable {
+protected:
+ T *object;
+
+public:
+ typedef T ValueType;
+ typedef T *PointerType;
+
+ explicit ScopedPtr(T *o = 0): object(o) {}
+
+ T& operator*() const { return *object; }
+ T *operator->() const { return object; }
+ operator T*() const { return object; }
+
+ /**
+ * Implicit conversion operator to bool for convenience, to make
+ * checks like "if (scopedPtr) ..." possible.
+ */
+ operator bool() const { return object != 0; }
+
+ ~ScopedPtr() {
+ delete object;
+ }
+
+ /**
+ * Resets the pointer with the new value. Old object will be destroyed
+ */
+ void reset(T *o = 0) {
+ delete object;
+ object = o;
+ }
+
+ /**
+ * Returns the plain pointer value.
+ *
+ * @return the pointer the ScopedPtr manages
+ */
+ T *get() const { return object; }
+
+ /**
+ * Returns the plain pointer value and releases ScopedPtr.
+ * After release() call you need to delete object yourself
+ *
+ * @return the pointer the ScopedPtr manages
+ */
+ T *release() {
+ T *r = object;
+ object = 0;
+ return r;
+ }
+};
+
+
} // End of namespace Common
#endif
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