1*a12d029eSVictor Perevertkin /* 2*a12d029eSVictor Perevertkin * PROJECT: ReactOS Automatic Testing Utility 3*a12d029eSVictor Perevertkin * LICENSE: GPL-2.0+ (https://spdx.org/licenses/GPL-2.0+) 4*a12d029eSVictor Perevertkin * PURPOSE: Template similar to std::auto_ptr for arrays 5*a12d029eSVictor Perevertkin * COPYRIGHT: Copyright 2009 Colin Finck (colin@reactos.org) 6*a12d029eSVictor Perevertkin */ 7*a12d029eSVictor Perevertkin 8*a12d029eSVictor Perevertkin template<typename Type> 9*a12d029eSVictor Perevertkin class auto_array_ptr 10*a12d029eSVictor Perevertkin { 11*a12d029eSVictor Perevertkin private: 12*a12d029eSVictor Perevertkin Type* m_Ptr; 13*a12d029eSVictor Perevertkin 14*a12d029eSVictor Perevertkin public: 15*a12d029eSVictor Perevertkin typedef Type element_type; 16*a12d029eSVictor Perevertkin 17*a12d029eSVictor Perevertkin /* Construct an auto_array_ptr from a pointer */ throw()18*a12d029eSVictor Perevertkin explicit auto_array_ptr(Type* Ptr = 0) throw() 19*a12d029eSVictor Perevertkin : m_Ptr(Ptr) 20*a12d029eSVictor Perevertkin { 21*a12d029eSVictor Perevertkin } 22*a12d029eSVictor Perevertkin 23*a12d029eSVictor Perevertkin /* Construct an auto_array_ptr from an existing auto_array_ptr */ throw()24*a12d029eSVictor Perevertkin auto_array_ptr(auto_array_ptr<Type>& Right) throw() 25*a12d029eSVictor Perevertkin : m_Ptr(Right.release()) 26*a12d029eSVictor Perevertkin { 27*a12d029eSVictor Perevertkin } 28*a12d029eSVictor Perevertkin 29*a12d029eSVictor Perevertkin /* Destruct the auto_array_ptr and remove the corresponding array from memory */ throw()30*a12d029eSVictor Perevertkin ~auto_array_ptr() throw() 31*a12d029eSVictor Perevertkin { 32*a12d029eSVictor Perevertkin delete[] m_Ptr; 33*a12d029eSVictor Perevertkin } 34*a12d029eSVictor Perevertkin 35*a12d029eSVictor Perevertkin /* Get the pointer address */ get()36*a12d029eSVictor Perevertkin Type* get() const throw() 37*a12d029eSVictor Perevertkin { 38*a12d029eSVictor Perevertkin return m_Ptr; 39*a12d029eSVictor Perevertkin } 40*a12d029eSVictor Perevertkin 41*a12d029eSVictor Perevertkin /* Release the pointer */ release()42*a12d029eSVictor Perevertkin Type* release() throw() 43*a12d029eSVictor Perevertkin { 44*a12d029eSVictor Perevertkin Type* Tmp = m_Ptr; 45*a12d029eSVictor Perevertkin m_Ptr = 0; 46*a12d029eSVictor Perevertkin 47*a12d029eSVictor Perevertkin return Tmp; 48*a12d029eSVictor Perevertkin } 49*a12d029eSVictor Perevertkin 50*a12d029eSVictor Perevertkin /* Reset to a new pointer */ throw()51*a12d029eSVictor Perevertkin void reset(Type* Ptr = 0) throw() 52*a12d029eSVictor Perevertkin { 53*a12d029eSVictor Perevertkin if(Ptr != m_Ptr) 54*a12d029eSVictor Perevertkin delete[] m_Ptr; 55*a12d029eSVictor Perevertkin 56*a12d029eSVictor Perevertkin m_Ptr = Ptr; 57*a12d029eSVictor Perevertkin } 58*a12d029eSVictor Perevertkin 59*a12d029eSVictor Perevertkin /* Simulate all the functionality of real arrays by casting the auto_array_ptr to Type* on demand */ throw()60*a12d029eSVictor Perevertkin operator Type*() const throw() 61*a12d029eSVictor Perevertkin { 62*a12d029eSVictor Perevertkin return m_Ptr; 63*a12d029eSVictor Perevertkin } 64*a12d029eSVictor Perevertkin }; 65