2016-11-14 07:08:42 +00:00
|
|
|
#pragma once
|
2008-09-18 03:15:49 +00:00
|
|
|
|
2021-03-16 22:55:50 +00:00
|
|
|
// The template class definition for smart pointer
|
2008-09-18 03:15:49 +00:00
|
|
|
template<class _Ty>
|
2016-11-14 07:08:42 +00:00
|
|
|
class AUTO_PTR
|
2008-09-18 03:15:49 +00:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
typedef _Ty element_type;
|
2021-03-16 22:55:50 +00:00
|
|
|
// CTOR
|
2008-09-18 03:15:49 +00:00
|
|
|
explicit AUTO_PTR(_Ty *pVal = 0) throw() :
|
2016-11-14 07:08:42 +00:00
|
|
|
m_Owns(pVal != 0),
|
|
|
|
m_AutoPtr(pVal)
|
2008-09-18 03:15:49 +00:00
|
|
|
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2021-03-16 22:55:50 +00:00
|
|
|
// Copy CTOR
|
2008-09-18 03:15:49 +00:00
|
|
|
AUTO_PTR(const AUTO_PTR<_Ty>& ptrCopy) throw() :
|
2016-11-14 07:08:42 +00:00
|
|
|
m_Owns(ptrCopy.m_Owns),
|
|
|
|
m_AutoPtr(ptrCopy.release())
|
2008-09-18 03:15:49 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2021-03-16 22:55:50 +00:00
|
|
|
// Overloading = operator
|
2008-09-18 03:15:49 +00:00
|
|
|
AUTO_PTR<_Ty>& operator=(AUTO_PTR<_Ty>& ptrCopy) throw()
|
|
|
|
{
|
2016-11-14 07:08:42 +00:00
|
|
|
if (this != &ptrCopy)
|
|
|
|
{
|
|
|
|
if (m_AutoPtr != ptrCopy.get())
|
|
|
|
{
|
|
|
|
if (m_Owns)
|
|
|
|
{
|
|
|
|
delete m_AutoPtr;
|
|
|
|
}
|
|
|
|
m_Owns = ptrCopy.m_Owns;
|
|
|
|
}
|
|
|
|
else if (ptrCopy.m_Owns)
|
|
|
|
{
|
|
|
|
m_Owns = true;
|
|
|
|
}
|
|
|
|
m_AutoPtr = ptrCopy.release();
|
|
|
|
}
|
|
|
|
return (*this);
|
2008-09-18 03:15:49 +00:00
|
|
|
}
|
2021-03-16 22:55:50 +00:00
|
|
|
// DTOR
|
2008-09-18 03:15:49 +00:00
|
|
|
~AUTO_PTR()
|
|
|
|
{
|
2016-11-14 07:08:42 +00:00
|
|
|
if (m_Owns)
|
|
|
|
{
|
|
|
|
delete m_AutoPtr;
|
|
|
|
}
|
2008-09-18 03:15:49 +00:00
|
|
|
}
|
2021-03-16 22:55:50 +00:00
|
|
|
// Overloading * operator
|
2016-11-14 07:08:42 +00:00
|
|
|
_Ty& operator*() const throw()
|
|
|
|
{
|
|
|
|
return (*get());
|
|
|
|
}
|
2008-09-18 03:15:49 +00:00
|
|
|
|
2021-03-16 22:55:50 +00:00
|
|
|
// Overloading -> operator
|
2016-11-14 07:08:42 +00:00
|
|
|
_Ty *operator->() const throw()
|
|
|
|
{
|
|
|
|
return (get());
|
|
|
|
}
|
2008-09-18 03:15:49 +00:00
|
|
|
|
2021-03-16 22:55:50 +00:00
|
|
|
// Function to get the pointer to the class
|
2016-11-14 07:08:42 +00:00
|
|
|
_Ty *get() const throw()
|
|
|
|
{
|
|
|
|
return (m_AutoPtr);
|
|
|
|
}
|
|
|
|
|
2021-03-16 22:55:50 +00:00
|
|
|
// Function to get the pointer to the class and take ownership
|
2016-11-14 07:08:42 +00:00
|
|
|
_Ty *release() const throw()
|
|
|
|
{
|
|
|
|
((AUTO_PTR<_Ty> *)this)->m_Owns = false;
|
|
|
|
return (m_AutoPtr);
|
|
|
|
}
|
2009-12-28 22:22:50 +00:00
|
|
|
|
2016-11-14 07:08:42 +00:00
|
|
|
void reset(_Ty *pVal = 0) throw()
|
|
|
|
{
|
|
|
|
if (m_AutoPtr != pVal)
|
|
|
|
{
|
|
|
|
if (m_Owns)
|
|
|
|
{
|
|
|
|
delete m_AutoPtr;
|
|
|
|
}
|
|
|
|
m_AutoPtr = pVal;
|
|
|
|
m_Owns = pVal != NULL;
|
|
|
|
}
|
|
|
|
}
|
2009-12-28 22:22:50 +00:00
|
|
|
|
2008-09-18 03:15:49 +00:00
|
|
|
private:
|
|
|
|
_Ty * m_AutoPtr;
|
2016-11-14 07:08:42 +00:00
|
|
|
bool m_Owns;
|
2008-09-18 03:15:49 +00:00
|
|
|
};
|