在此特别感谢renwind提供的FTP空间
文章请访问下面的链接(PDF格式请使用Acrobat 5或以上版本打开)
http://www.zsws.org/renwind/smartPtr.pdf
代码:
/** NOTE ********************************************************************
filename: RefObject.h
creator: noslopforever
more author: noslopforever
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
本文件是GamerClass工程不可分割的一部分,GamerClass以及本文件的作者noslopforever(天堂里的死神)[Email:[email protected]]在此声明:
在法律所允许范围内,您可以自由、免费使用本文件,但您不能更改此文件自由、共享的本质,不能将文件及其内容私有化。
在任何情况下,您都必须不经修改地保留此Note,即便您在使用过程中人为修改了本文件,仍应保留此Note。
此Note中,只有more author部分才是您可以修改的部分,但必须保留这一部分的原始信息。
让我们共同维护公平公正、合法合理的代码共享环境,好的环境将让我们彼此受益,谢谢大家。
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
purpose: TSafePtr NULL保护智能指针
TSharePtr RefCount智能指针(必须和CRefObject的派生类搭配使用)
TSharePtrAutoValid 自删除性质的RefCount智能指针(必须和CRefObject的派生类搭配使用)
****************************************************************************/
#ifndef _RefObject_H_
#define _RefObject_H_
#include "NF3DTypes.h"
StartNamespace(NF3D)
/** 在需要INVALID Define的类结构体内写这一句,为cls传入这个类的类名 */
#define DEF_INVALID( cls ) static cls##* INVALID;
/** 在相应cpp里面写这一句,为cls传入这个类的类名,为value传入相应非法Class的地址 */
#define IMPL_INVALID( cls , value ) cls##* cls##::INVALID =##value##;
/** 或者,为cls传入这个类的类名,为inv_cls传入相应非法Class名 */
#define IMPL_CLS_INVALID( cls , inv_cls ) /
inv_cls g_##inv_cls; /
IMPL_INVALID( cls , &g_##inv_cls ) /
template < class T >
class TSharePtr;
//------------------------------------------------------------------------
/**
* /brief
*/
class NF3D_API CRefObject
{
public:
CRefObject() : m_un32RefCount(0)
{ }
virtual ~CRefObject()
{
#ifdef _DEBUG
if ( m_un32RefCount != 0 )
{ assert(false); }
#endif
}
DEF_INVALID(CRefObject)
private:
template < class T > friend class TSharePtr;
template < class T > friend class TSharePtrAutoValid;
// 引用计数
uint32 m_un32RefCount;
private:
inline void AddRef()
{ ++m_un32RefCount; }
inline void Release()
{
if ( --m_un32RefCount == 0 )
{ delete this; }
}
public:
inline uint32 GetRefCount()
{ return m_un32RefCount; }
template< class T >
static void AddRef( T* t )
{
if ( t )
{
t->AddRef();
}
}
template< class T >
static void Release( T* t )
{
if ( t )
{
t->Release();
}
}
};
//------------------------------------------------------------------------
//------------------------------------------------------------------------
/**
* @brief
*/
template < class T >
class TSharePtr
{
private:
/** 内部接口 */
T* m_pInner;
inline T* _ReturnInner() const
{
if ( !m_pInner )
{
// @todo throw or log error
return T::INVALID;
}
return m_pInner;
}
public:
inline T* __real_ptr() const
{ return m_pInner; }
inline operator bool() const
{ return m_pInner!=NULL; }
public:
/** cast为内部接口 */
inline T* ptr() const
{ return _ReturnInner(); }
inline const T* cptr() const
{ return _ReturnInner(); }
inline const T* operator->() const
{ return _ReturnInner(); }
inline T* operator->()
{ return _ReturnInner(); }
public:
/** 默认构造函数 */
TSharePtr()
{
m_pInner = NULL;
}
/** 通过内部接口构造 */
TSharePtr( const T* pInner )
{
m_pInner = const_cast<T*>(pInner);
if ( m_pInner )
{ m_pInner->AddRef(); }
}
/** 拷贝构造函数 */
template < class U >
TSharePtr( TSharePtr<U> const& rhs )
{
m_pInner = static_cast<T*>(rhs.__real_ptr());
if ( m_pInner ) { m_pInner->AddRef(); }
}
TSharePtr( TSharePtr<T> const& rhs )
{
m_pInner = rhs.__real_ptr();
if ( m_pInner )
{ m_pInner->AddRef(); }
}
/** 析构函数 */
~TSharePtr()
{
if ( m_pInner )
{
static_cast<CRefObject*>(m_pInner)->Release();
m_pInner = NULL;
}
}
/** 赋值函数 */
template < class U >
inline TSharePtr<T>& operator= ( TSharePtr<U> const& rhs )
{
SafeRelease(m_pInner);
m_pInner = static_cast<T*>(rhs.__real_ptr());
if ( m_pInner )
{ m_pInner->AddRef(); }
return *this;
}
inline TSharePtr<T>& operator= ( TSharePtr<T> const& rhs )
{
SafeRelease(m_pInner);
m_pInner = rhs.__real_ptr();
if ( m_pInner )
{ m_pInner->AddRef(); }
return *this;
}
/** 比较函数 */
template <class U>
inline bool operator== ( TSharePtr<U> const& rhs ) const
{ return m_pInner == rhs.__real_ptr(); }
template <class U>
inline bool operator!= ( TSharePtr<U> const& rhs ) const
{ return m_pInner != rhs.__real_ptr(); }
template <class U>
inline bool operator== ( U* rhs ) const
{ return m_pInner == rhs; }
template <class U>
inline bool operator!= ( U* rhs ) const
{ return m_pInner != rhs; }
inline bool operator== ( T* rhs ) const
{ return m_pInner == rhs; }
inline bool operator!= ( T* rhs ) const
{ return m_pInner != rhs; }
static const TSharePtr<T>& NULLPTR()
{
static TSharePtr<T> null_handle;
return null_handle;
}
};
//------------------------------------------------------------------------
//------------------------------------------------------------------------
/**
* @brief
*/
template < class T >
class TSafePtr
{
private:
/** 内部接口 */
T* m_pInner;
inline T* _ReturnInner() const
{
if ( !m_pInner )
{
// @todo throw or log error
return T::INVALID;
}
return m_pInner;
}
public:
inline T* __real_ptr() const
{ return m_pInner; }
/** cast为内部接口 */
inline T* ptr() const
{
return _ReturnInner();
}
inline const T* cptr() const
{ return _ReturnInner(); }
inline operator bool() const
{ return m_pInner!=NULL; }
inline const T* operator->() const
{
return _ReturnInner();
}
inline T* operator->()
{
return _ReturnInner();
}
public:
/** 默认构造函数 */
TSafePtr()
{
m_pInner = NULL;
}
/** 通过内部接口构造 */
TSafePtr( const T* pInner )
{
m_pInner = const_cast<T*>(pInner);
}
/** 拷贝构造函数 */
template < class U >
TSafePtr( TSafePtr<U> const& rhs )
{
m_pInner = static_cast<T*>(rhs.__real_ptr());
}
TSafePtr( TSafePtr<T> const& rhs )
{
m_pInner = rhs.__real_ptr();
}
/** 析构函数 */
~TSafePtr()
{
}
/** 赋值函数 */
template < class U >
inline TSafePtr<T>& operator= ( TSafePtr<U> const& rhs )
{
m_pInner = static_cast<T*>(rhs.__real_ptr());
return *this;
}
inline TSafePtr<T>& operator= ( TSafePtr<T> const& rhs )
{
m_pInner = rhs.__real_ptr();
return *this;
}
/** 比较函数 */
template <class U>
inline bool operator== ( TSafePtr<U> const& rhs ) const
{ return m_pInner == rhs.__real_ptr(); }
template <class U>
inline bool operator!= ( TSafePtr<U> const& rhs ) const
{ return m_pInner != rhs.__real_ptr(); }
template <class U>
inline bool operator== ( U* rhs ) const
{ return m_pInner == rhs; }
template <class U>
inline bool operator!= ( U* rhs ) const
{ return m_pInner != rhs; }
inline bool operator== ( T* rhs ) const
{ return m_pInner == rhs; }
inline bool operator!= ( T* rhs ) const
{ return m_pInner != rhs; }
static const TSafePtr<T>& NULLPTR()
{
static TSafePtr<T> null_handle;
return null_handle;
}
};
//------------------------------------------------------------------------
//------------------------------------------------------------------------
/**
* @brief
*/
template < class T >
class TSharePtrAutoValid
{
private:
/** 内部接口 */
T* m_pInner;
inline void _Validate() const
{
if ( m_pInner )
{
if ( m_pInner->IsDestroyed() ) { SafeRelease(const_cast<TSharePtrAutoValid<T>*>(this)->m_pInner); }
}
}
inline T* _ReturnInner() const
{
if ( !m_pInner )
{
//assert(false);
return T::INVALID;
}
return m_pInner;
}
public:
inline T* __real_ptr() const
{
_Validate();
return m_pInner;
}
inline operator bool() const
{ return m_pInner!=NULL; }
public:
/** cast为内部接口 */
inline T* ptr() const
{ _Validate(); return _ReturnInner(); }
inline const T* cptr() const
{ _Validate(); return _ReturnInner(); }
inline const T* operator->() const
{ _Validate(); return _ReturnInner(); }
inline T* operator->()
{ _Validate(); return _ReturnInner(); }
public:
/** 默认构造函数 */
TSharePtrAutoValid()
{
m_pInner = NULL;
}
/** 通过内部接口构造 */
TSharePtrAutoValid( const T* pInner )
{
m_pInner = const_cast<T*>(pInner);
if ( m_pInner )
{ m_pInner->AddRef(); }
}
/** 拷贝构造函数 */
template < class U >
TSharePtrAutoValid( TSharePtrAutoValid<U> const& rhs )
{
m_pInner = static_cast<T*>(rhs.__real_ptr());
if ( m_pInner ) { m_pInner->AddRef(); }
}
TSharePtrAutoValid( TSharePtrAutoValid<T> const& rhs )
{
m_pInner = rhs.__real_ptr();
if ( m_pInner )
{ m_pInner->AddRef(); }
}
/** 析构函数 */
~TSharePtrAutoValid()
{
if ( m_pInner )
{
static_cast<CRefObject*>(m_pInner)->Release();
m_pInner = NULL;
}
}
/** 赋值函数 */
template < class U >
inline TSharePtrAutoValid<T>& operator= ( TSharePtrAutoValid<U> const& rhs )
{
SafeRelease(m_pInner);
m_pInner = static_cast<T*>(rhs.__real_ptr());
if ( m_pInner )
{ m_pInner->AddRef(); }
return *this;
}
inline TSharePtrAutoValid<T>& operator= ( TSharePtrAutoValid<T> const& rhs )
{
SafeRelease(m_pInner);
m_pInner = rhs.__real_ptr();
if ( m_pInner )
{ m_pInner->AddRef(); }
return *this;
}
/** 比较函数 */
template <class U>
inline bool operator== ( TSharePtrAutoValid<U> const& rhs ) const
{ _Validate();return m_pInner == rhs.__real_ptr(); }
template <class U>
inline bool operator!= ( TSharePtrAutoValid<U> const& rhs ) const
{ _Validate();return m_pInner != rhs.__real_ptr(); }
template <class U>
inline bool operator== ( U* rhs ) const
{ _Validate();return m_pInner == rhs; }
template <class U>
inline bool operator!= ( U* rhs ) const
{ _Validate();return m_pInner != rhs; }
inline bool operator== ( T* rhs ) const
{ _Validate();return m_pInner == rhs; }
inline bool operator!= ( T* rhs ) const
{ _Validate();return m_pInner != rhs; }
static const TSharePtrAutoValid<T>& NULLPTR()
{
static TSharePtrAutoValid<T> null_handle;
return null_handle;
}
};
//------------------------------------------------------------------------
//------------------------------------------------------------------------
typedef TSharePtr<CRefObject> REFOBJ_PTR;
//------------------------------------------------------------------------
EndNamespace
#endif//_RefObject_H_