diff options
author | Vladimir Menshakov | 2010-03-20 20:01:44 +0000 |
---|---|---|
committer | Vladimir Menshakov | 2010-03-20 20:01:44 +0000 |
commit | abdfbafa45bb65f7ce315a04411d1a8ffb9e5d50 (patch) | |
tree | 2ee290e1fe0db40067beacb14bc327ff53d27932 /common | |
parent | 3c884abfa6d6f2fe1f99f275d526952783c3962b (diff) | |
download | scummvm-rg350-abdfbafa45bb65f7ce315a04411d1a8ffb9e5d50.tar.gz scummvm-rg350-abdfbafa45bb65f7ce315a04411d1a8ffb9e5d50.tar.bz2 scummvm-rg350-abdfbafa45bb65f7ce315a04411d1a8ffb9e5d50.zip |
added ScopedPtr template
svn-id: r48334
Diffstat (limited to 'common')
-rw-r--r-- | common/ptr.h | 55 |
1 files changed, 55 insertions, 0 deletions
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<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 |