summaryrefslogtreecommitdiff
path: root/src/misc
diff options
context:
space:
mode:
authorKUDr <KUDr@openttd.org>2007-01-26 11:38:07 +0000
committerKUDr <KUDr@openttd.org>2007-01-26 11:38:07 +0000
commit55ac8f843a31c196884783117b57ff6faaf5bc37 (patch)
treeac68bde5795292ff114b30e388d7bff3f1eda66e /src/misc
parent1943e8cb8caf49f398fc61420bb7d905bd870c73 (diff)
downloadopenttd-55ac8f843a31c196884783117b57ff6faaf5bc37.tar.xz
(svn r8414) -Codechange: Use own AutoPtrT instead of std::auto_ptr.
-Simplifies assignment from raw pointers -Should be harder to crash the program by incorrect assignment into it. -Should help with MorphOS compilation errors
Diffstat (limited to 'src/misc')
-rw-r--r--src/misc/autoptr.hpp95
1 files changed, 95 insertions, 0 deletions
diff --git a/src/misc/autoptr.hpp b/src/misc/autoptr.hpp
new file mode 100644
index 000000000..eac3129b9
--- /dev/null
+++ b/src/misc/autoptr.hpp
@@ -0,0 +1,95 @@
+/* $Id:$ */
+
+#ifndef AUTOPTR_HPP
+#define AUTOPTR_HPP
+
+/** AutoPtrT - kind of smart pointer that ensures the owned object gets
+ * deleted when its pointer goes out of scope.
+ * It is non-invasive smart pointer (no reference counter).
+ * When copied, the copy takes ownership of underlying object
+ * and original becomes NULL!
+ * Can be used also for polymorphic data types (interfaces).
+ */
+template <class T>
+class AutoPtrT {
+public:
+ typedef T obj_t;
+
+protected:
+ mutable T* m_p; ///< points to the data
+
+public:
+ FORCEINLINE AutoPtrT()
+ : m_p(NULL)
+ {};
+
+ FORCEINLINE AutoPtrT(const AutoPtrT<T>& src)
+ : m_p(src.m_p)
+ {
+ if (m_p != NULL) src.m_p = NULL;
+ };
+
+ FORCEINLINE AutoPtrT(T *p)
+ : m_p(p)
+ {}
+
+ FORCEINLINE ~AutoPtrT()
+ {
+ if (m_p != NULL) {
+ delete m_p;
+ m_p = NULL;
+ }
+ }
+
+ /** give-up ownership and NULLify the raw pointer */
+ FORCEINLINE T* Release()
+ {
+ T* p = m_p;
+ m_p = NULL;
+ return p;
+ }
+
+ /** raw-pointer cast operator (read only) */
+ FORCEINLINE operator const T* () const
+ {
+ return m_p;
+ }
+
+ /** raw-pointer cast operator */
+ FORCEINLINE operator T* ()
+ {
+ return m_p;
+ }
+
+ /** dereference operator (read only) */
+ FORCEINLINE const T* operator -> () const
+ {
+ assert(m_p != NULL);
+ return m_p;
+ }
+
+ /** dereference operator (read / write) */
+ FORCEINLINE T* operator -> ()
+ {
+ assert(m_p != NULL);
+ return m_p;
+ }
+
+ /** assignment operator */
+ FORCEINLINE AutoPtrT& operator = (const AutoPtrT& src)
+ {
+ m_p = src.m_p;
+ if (m_p != NULL) src.m_p = NULL;
+ return *this;
+ }
+
+ /** forwarding 'lower than' operator to the underlaying items */
+ FORCEINLINE bool operator < (const AutoPtrT& other) const
+ {
+ assert(m_p != NULL);
+ assert(other.m_p != NULL);
+ return (*m_p) < (*other.m_p);
+ }
+};
+
+#endif /* AUTOPTR_HPP */