summaryrefslogtreecommitdiff
path: root/include/mts/MTS_AutoPtr.h
diff options
context:
space:
mode:
Diffstat (limited to 'include/mts/MTS_AutoPtr.h')
-rw-r--r--include/mts/MTS_AutoPtr.h115
1 files changed, 115 insertions, 0 deletions
diff --git a/include/mts/MTS_AutoPtr.h b/include/mts/MTS_AutoPtr.h
new file mode 100644
index 0000000..68b82cf
--- /dev/null
+++ b/include/mts/MTS_AutoPtr.h
@@ -0,0 +1,115 @@
+/*
+ * Copyright (C) 2015 by Multi-Tech Systems
+ *
+ * This file is part of libmts.
+ *
+ * libmts is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License as published by
+ * the Free Software Foundation, either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * libmts is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with libmts. If not, see <http://www.gnu.org/licenses/>.
+ *
+ */
+
+/*! \file MTS_AutoPtr.h
+ \brief An autopointer
+ \date 15JUN11
+ \author Sean Godinez
+
+ An autopointer.
+ */
+
+#ifndef MTS_AUTOPTR_H
+#define MTS_AUTOPTR_H
+
+#include <mts/MTS_NonCopyable.h>
+#include <mts/MTS_Stdint.h>
+
+namespace MTS {
+
+ //! An autopointer
+ /*!
+ An autopointer cleans up after itself.
+ */
+ template<class T> class AutoPtr: NonCopyable {
+
+ public:
+ enum MallocType {
+ SINGLE, ARRAY
+ };
+
+ explicit AutoPtr(T* ptr = 0, MallocType type = SINGLE); //!< Explicitly Constructs the AutoPtr to reference ptr
+ ~AutoPtr(); //!< Destructs the AutoPtr deleting T
+
+ T& operator*() const; //!< Returns T
+ T* operator->() const; //!< Returns address of T
+ T& operator[](const uint32_t& index) const; //!< Returns T at index
+ T* get() const; //!< Returns address of T
+ T* release(); //!< Removes reference to T (does NOT delete T) and returns address
+ void reset(T* ptr = 0, MallocType type = SINGLE); //!< Deletes old T and assigns internal reference to ptr
+ bool isNull() const; //!< Returns true if pointer is NULL
+
+ private:
+ T* m_pObj; //!< Reference to class T
+ MallocType m_eType; //!< Describes memory allocated as a single T or block of Ts
+
+ };
+
+ template<class T> AutoPtr<T>::AutoPtr(T* ptr, MallocType type)
+ : m_pObj(ptr), m_eType(type) {
+ }
+
+ template<class T> AutoPtr<T>::~AutoPtr() {
+ if (m_eType == SINGLE) {
+ delete m_pObj;
+ } else {
+ delete[] m_pObj;
+ }
+ }
+
+ template<class T> T& AutoPtr<T>::operator*() const {
+ return *m_pObj;
+ }
+
+ template<class T> T* AutoPtr<T>::operator->() const {
+ return m_pObj;
+ }
+
+ template<class T> T& AutoPtr<T>::operator[](const uint32_t& index) const {
+ return m_pObj[index];
+ }
+
+ template<class T> T* AutoPtr<T>::get() const {
+ return m_pObj;
+ }
+
+ template<class T> T* AutoPtr<T>::release() {
+ T* temp = m_pObj;
+ m_pObj = 0;
+ return temp;
+ }
+
+ template<class T> void AutoPtr<T>::reset(T* ptr, MallocType type) {
+ T* temp = m_pObj;
+ m_pObj = ptr;
+ if (m_eType == SINGLE) {
+ delete temp;
+ } else {
+ delete[] temp;
+ }
+ m_eType = type;
+ }
+
+ template<class T> bool AutoPtr<T>::isNull() const {
+ return (m_pObj == 0);
+ }
+}
+
+#endif