From abdfbafa45bb65f7ce315a04411d1a8ffb9e5d50 Mon Sep 17 00:00:00 2001 From: Vladimir Menshakov Date: Sat, 20 Mar 2010 20:01:44 +0000 Subject: added ScopedPtr template svn-id: r48334 --- common/ptr.h | 55 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) (limited to 'common') diff --git a/common/ptr.h b/common/ptr.h index bceba2787d..09ef6693cd 100644 --- a/common/ptr.h +++ b/common/ptr.h @@ -26,6 +26,7 @@ #define COMMON_PTR_H #include "common/scummsys.h" +#include "common/noncopyable.h" namespace Common { @@ -216,6 +217,60 @@ private: T *_pointer; }; +template +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 -- cgit v1.2.3