src/misc/autoptr.hpp
changeset 6095 55d5c0e6be3a
child 6294 6c74bf9cc5a4
equal deleted inserted replaced
6094:3fd6237f0d94 6095:55d5c0e6be3a
       
     1 /* $Id:$ */
       
     2 
       
     3 #ifndef AUTOPTR_HPP
       
     4 #define AUTOPTR_HPP
       
     5 
       
     6 /** AutoPtrT - kind of smart pointer that ensures the owned object gets
       
     7  *  deleted when its pointer goes out of scope.
       
     8  *  It is non-invasive smart pointer (no reference counter).
       
     9  *  When copied, the copy takes ownership of underlying object
       
    10  *  and original becomes NULL!
       
    11  *  Can be used also for polymorphic data types (interfaces).
       
    12  */
       
    13 template <class T>
       
    14 class AutoPtrT {
       
    15 public:
       
    16 	typedef T obj_t;
       
    17 
       
    18 protected:
       
    19 	mutable T* m_p; ///< points to the data
       
    20 
       
    21 public:
       
    22 	FORCEINLINE AutoPtrT()
       
    23 	 : m_p(NULL)
       
    24 	{};
       
    25 
       
    26 	FORCEINLINE AutoPtrT(const AutoPtrT<T>& src)
       
    27 	 : m_p(src.m_p)
       
    28 	{
       
    29 		if (m_p != NULL) src.m_p = NULL;
       
    30 	};
       
    31 
       
    32 	FORCEINLINE AutoPtrT(T *p)
       
    33 	 : m_p(p)
       
    34 	{}
       
    35 
       
    36 	FORCEINLINE ~AutoPtrT()
       
    37 	{
       
    38 		if (m_p != NULL) {
       
    39 			delete m_p;
       
    40 			m_p = NULL;
       
    41 		}
       
    42 	}
       
    43 
       
    44 	/** give-up ownership and NULLify the raw pointer */
       
    45 	FORCEINLINE T* Release()
       
    46 	{
       
    47 		T* p = m_p;
       
    48 		m_p = NULL;
       
    49 		return p;
       
    50 	}
       
    51 
       
    52 	/** raw-pointer cast operator (read only) */
       
    53 	FORCEINLINE operator const T* () const
       
    54 	{
       
    55 		return m_p;
       
    56 	}
       
    57 
       
    58 	/** raw-pointer cast operator */
       
    59 	FORCEINLINE operator T* ()
       
    60 	{
       
    61 		return m_p;
       
    62 	}
       
    63 
       
    64 	/** dereference operator (read only) */
       
    65 	FORCEINLINE const T* operator -> () const
       
    66 	{
       
    67 		assert(m_p != NULL);
       
    68 		return m_p;
       
    69 	}
       
    70 
       
    71 	/** dereference operator (read / write) */
       
    72 	FORCEINLINE T* operator -> ()
       
    73 	{
       
    74 		assert(m_p != NULL);
       
    75 		return m_p;
       
    76 	}
       
    77 
       
    78 	/** assignment operator */
       
    79 	FORCEINLINE AutoPtrT& operator = (const AutoPtrT& src)
       
    80 	{
       
    81 		m_p = src.m_p;
       
    82 		if (m_p != NULL) src.m_p = NULL;
       
    83 		return *this;
       
    84 	}
       
    85 
       
    86 	/** forwarding 'lower than' operator to the underlaying items */
       
    87 	FORCEINLINE bool operator < (const AutoPtrT& other) const
       
    88 	{
       
    89 		assert(m_p != NULL);
       
    90 		assert(other.m_p != NULL);
       
    91 		return (*m_p) < (*other.m_p);
       
    92 	}
       
    93 };
       
    94 
       
    95 #endif /* AUTOPTR_HPP */