/** @file SmartPointer.h @brief 文件打包:包文件类 第一版测试
*	Copyright (c) 2007,北京金刚游科技有限公司
*	All rights reserved.
*
*	当前版本:2009-3-
*	作    者:KingKong
*	完成日期:2009-3-
*
*	取代版本:
*	作    者:
*	完成日期:
*/ #pragma once #include "Api.h" #include class PACKAGE_ENTRY CRefObject { public: CRefObject(); virtual ~CRefObject(); void IncRefCount(); void DecRefCount(); unsigned int GetRefCount() const; protected: virtual void DeleteThis(); private: unsigned int m_uiRefCount; }; inline CRefObject::CRefObject() { m_uiRefCount = 0; } inline CRefObject::~CRefObject() { } inline void CRefObject::IncRefCount() { InterlockedIncrement((long*)&m_uiRefCount); } inline void CRefObject::DecRefCount() { if (InterlockedDecrement((long*)&m_uiRefCount) == 0) DeleteThis(); } inline unsigned int CRefObject::GetRefCount() const { return m_uiRefCount; } //------------------------------------------------------------------ template class PACKAGE_ENTRY CPointer { public: // construction and destruction CPointer(T* pObject = (T*) 0); CPointer(const CPointer& ptr); ~CPointer(); // implicit conversions operator T*() const; T& operator*() const; T* operator->() const; // assignment CPointer& operator=(const CPointer& ptr); CPointer& operator=(T* pObject); // comparisons bool operator==(T* pObject) const; bool operator!=(T* pObject) const; bool operator==(const CPointer& ptr) const; bool operator!=(const CPointer& ptr) const; protected: // the managed pointer T* m_pObject; }; #define SmartPointerDef(classname) \ class classname; \ typedef CPointer classname##Ptr #define CSmartPointerCast(type, smartptr) ((type*) (void*) (smartptr)) //--------------------------------------------------------------------------- template inline CPointer::CPointer(T* pObject) { m_pObject = pObject; if (m_pObject) m_pObject->IncRefCount(); } //--------------------------------------------------------------------------- template inline CPointer::CPointer(const CPointer& ptr) { m_pObject = ptr.m_pObject; if (m_pObject) m_pObject->IncRefCount(); } //--------------------------------------------------------------------------- template inline CPointer::~CPointer() { if (m_pObject) m_pObject->DecRefCount(); } //--------------------------------------------------------------------------- template inline CPointer::operator T*() const { return m_pObject; } //--------------------------------------------------------------------------- template inline T& CPointer::operator*() const { return *m_pObject; } //--------------------------------------------------------------------------- template inline T* CPointer::operator->() const { return m_pObject; } //--------------------------------------------------------------------------- template inline CPointer& CPointer::operator=(const CPointer& ptr) { if (m_pObject != ptr.m_pObject) { if (m_pObject) m_pObject->DecRefCount(); m_pObject = ptr.m_pObject; if (m_pObject) m_pObject->IncRefCount(); } return *this; } //--------------------------------------------------------------------------- template inline CPointer& CPointer::operator=(T* pObject) { if (m_pObject != pObject) { if (m_pObject) m_pObject->DecRefCount(); m_pObject = pObject; if (m_pObject) m_pObject->IncRefCount(); } return *this; } //--------------------------------------------------------------------------- template inline bool CPointer::operator==(T* pObject) const { return (m_pObject == pObject); } //--------------------------------------------------------------------------- template inline bool CPointer::operator!=(T* pObject) const { return (m_pObject != pObject); } //--------------------------------------------------------------------------- template inline bool CPointer::operator==(const CPointer& ptr) const { return (m_pObject == ptr.m_pObject); } //--------------------------------------------------------------------------- template inline bool CPointer::operator!=(const CPointer& ptr) const { return (m_pObject != ptr.m_pObject); } //---------------------------------------------------------------------------