SmartPointer.hpp :
/**
* @file SmartPointer.hpp
* @brief 智能指针实现
*
* 修订记录
* @author jack3z
* @version 1.00
* @date 2014-10-30
*
*/
#ifndef __SMARTPOINTER_HPP__
#define __SMARTPOINTER_HPP__
#if defined(__GNUC__)
#elif defined(_MSC_VER)
# include <windows.h>
#endif // _DEBUG
#ifndef __CURRENT_FUNCTION__
# if defined(__GNUC__)
# define __CURRENT_FUNCTION__ __PRETTY_FUNCTION__
# elif defined(_MSC_VER)
# define __CURRENT_FUNCTION__ __FUNCSIG__
# else
# error unsupported compiler!
# endif
#endif
template<typename T>
class SmartPointer
{
public:
SmartPointer()
: m_ptr(NULL), m_pRef(NULL)
{
}
explicit SmartPointer(T* p)
: m_ptr(p)
{
InitRef();
printf("%s>>> m_ptr:%p, m_pRef:%p, *m_pRef:%ld \n", __CURRENT_FUNCTION__,
m_ptr, m_pRef, m_pRef ? *m_pRef : -1);
}
SmartPointer(const SmartPointer& other)
: m_ptr(other.m_ptr), m_pRef(other.m_pRef)
{
this->IncreaseRef();
printf("%s>>> m_ptr:%p, m_pRef:%p, *m_pRef:%ld \n", __CURRENT_FUNCTION__,
m_ptr, m_pRef, m_pRef ? *m_pRef : -1);
}
~SmartPointer()
{
this->DecreaseRef();
if (m_ptr)
{
printf("%s>>> m_ptr:%p, m_pRef:%p, *m_pRef:%ld \n", __CURRENT_FUNCTION__,
m_ptr, m_pRef, m_pRef ? *m_pRef : -1);
}
}
SmartPointer& operator=(SmartPointer& other)
{
if (this == &other)
{
return *this;
}
other.IncreaseRef();
this->DecreaseRef();
this->m_ptr = other.m_ptr;
this->m_pRef = other.m_pRef;
return *this;
}
T& operator*() const
{
return *m_ptr;
}
T* operator->() const
{
return m_ptr;
}
T* get() const
{
return m_ptr;
}
void reset()
{
this->DecreaseRef();
this->m_ptr = NULL;
this->m_pRef = NULL;
}
void reset(T* p)
{
if (p != m_ptr)
{
this->DecreaseRef();
this->m_ptr = p;
InitRef();
}
}
// 重载==操作符.
friend bool operator==(const SmartPointer& a, const SmartPointer& b)
{
return a.m_ptr == b.m_ptr;
}
friend bool operator==(const SmartPointer& a, const T* p)
{
printf("%s>>> a.m_ptr:%p, a.m_pRef:%p, *a.m_pRef:%ld \n", __CURRENT_FUNCTION__,
a.m_ptr, a.m_pRef, a.m_pRef ? *a.m_pRef: -1);
return a.m_ptr == p;
}
friend bool operator==(const T* p, const SmartPointer& b)
{
printf("%s>>> b.m_ptr:%p, b.m_pRef:%p, *b.m_pRef:%ld \n", __CURRENT_FUNCTION__,
b.m_ptr, b.m_pRef, b.m_pRef ? *b.m_pRef : -1);
return p == b.m_ptr;
}
// 重载!=操作符.
friend bool operator!=(const SmartPointer& a, const SmartPointer& b)
{
return a.m_ptr != b.m_ptr;
}
friend bool operator!=(const SmartPointer& a, const T* p)
{
printf("%s>>> a.m_ptr:%p, a.m_pRef:%p, *a.m_pRef:%ld \n", __CURRENT_FUNCTION__,
a.m_ptr, a.m_pRef, a.m_pRef ? *a.m_pRef: -1);
return a.m_ptr != p;
}
friend bool operator!=(const T* p, const SmartPointer& b)
{
printf("%s>>> b.m_ptr:%p, b.m_pRef:%p, *b.m_pRef:%ld \n", __CURRENT_FUNCTION__,
b.m_ptr, b.m_pRef, b.m_pRef ? *b.m_pRef : -1);
return p != b.m_ptr;
}
protected:
void InitRef()
{
m_pRef = m_ptr ? new long(1) : NULL;
}
void IncreaseRef()
{
if (m_ptr)
{
#if defined(__GNUC__)
__sync_fetch_and_add(m_pRef, 1);
#elif defined(_MSC_VER)
::InterlockedIncrement(m_pRef);
#endif
}
}
void DecreaseRef()
{
if (m_ptr)
{
#if defined(__GNUC__)
if (1 == __sync_fetch_and_sub(m_pRef, 1))
#elif defined(_MSC_VER)
if (0 == ::InterlockedDecrement(m_pRef))
#endif
{
printf("%s>>> delete m_ptr:%p, delete m_pRef:%p, *m_pRef:%ld \n", __CURRENT_FUNCTION__,
m_ptr, m_pRef, *m_pRef);
delete m_ptr;
m_ptr = NULL;
delete m_pRef;
m_pRef = NULL;
}
}
}
protected:
T* m_ptr;
long* m_pRef;
};
#endif