通用智能指針模板類及基礎對象類定義

    通過引用計數實現內存的智能管理的核心思想是通過記錄指向某塊已經申請的內存的地址,並記錄該內存被引用的次數;該內存每被引用一次,引用計數就需要添加一,相應的每減少一次引用,引用計數就要被減一,當引用計數爲零的時候就表示該內存已經不再使用,此內存可以被刪除,在進行智能指針類定義的時候需要重點考慮賦值和引用等傳遞類的操作,因爲這類操作是引發引用計數變化的關鍵,具體可以參見智能指針模版類定義部分詳細代碼。

  • 基礎對象類定義
  1. /**  
  2.       * 基類   
  3.       * @versiont     v 1.0.0  
  4.       * @author       順 哥 
  5.       */  
  6.     class  Object  
  7.     {  
  8.     private:  
  9.         /**引用計數*/  
  10.         volatile long m_nRefCount;  
  11.   
  12.     public:  
  13.         Object();  
  14.         virtual ~Object();  
  15.   
  16.     public:  
  17.       /**  
  18.         * 增加引用計數  
  19.         * @versiont     v 1.0.0  
  20.         * @author       順 哥 
  21.         */  
  22.       virtual void Object::AddRef() 
  23. { 
  24. InterlockedIncrement(&m_nRefCount);  
  25. } 
  26.       /**  
  27.         * 減少引用計數,當引用計數爲0 的時候刪除自身  
  28.         * @versiont     v 1.0.0  
  29.         * @author       順 哥 
  30.         */  
  31.       virtual void Object::Release() 
  32. { 
  33.  
  34.          if( InterlockedDecrement(&m_nRefCount) == 0 )  
  35.      { 
  36.          delete this
  37.      } 
  38. } 
  39.  
  40.   
  41. };  
  • 智能指針類定義如下
  1. /** 
  2.       * 智能指針模版類聲明,通過引用技術自動管理指針所指向的內 
  3.       * @versiont     v 1.0.0 
  4.       * @author       順 哥
  5.       */ 
  6.     template<typename T> 
  7.     class  Pointer  
  8.     { 
  9.     private
  10.         T *m_ptrObject; 
  11.  
  12.     public
  13.         Pointer() 
  14.         { 
  15.             m_ptrObject = NULL; 
  16.         } 
  17.         Pointer(T *t) 
  18.         { 
  19.             if( t!= NULL) 
  20.             { 
  21.                 _Bind(t); 
  22.             } 
  23.             else 
  24.             { 
  25.                 m_ptrObject = NULL; 
  26.             } 
  27.         } 
  28.         template<typename U> 
  29.         Pointer(U *p) 
  30.         { 
  31.             if(p!= NULL) 
  32.             { 
  33.                 _Bind(p); 
  34.             } 
  35.         } 
  36.  
  37.         Pointer(const Pointer<T> &t) 
  38.         { 
  39.             _Bind(t.Get()); 
  40.         } 
  41.         template<typename U> 
  42.         Pointer(const Pointer<U> &u) 
  43.         { 
  44.             _Bind(u.Get()); 
  45.         } 
  46.  
  47.         ~Pointer() 
  48.         {        
  49.             _Unbind(); 
  50.         } 
  51.  
  52.         Pointer<T> &operator=(T *t) 
  53.         { 
  54.             if( t!= NULL) 
  55.             { 
  56.                 _Unbind(); 
  57.                 _Bind(t); 
  58.             } 
  59.             return *this
  60.         } 
  61.         template<typename U> 
  62.         Pointer<T> &operator=(U *p) 
  63.         { 
  64.             if(p!=NULL) 
  65.             { 
  66.                 _Unbind(); 
  67.                 _Bind(p); 
  68.             } 
  69.             return *this
  70.         } 
  71.  
  72.         Pointer<T> & operator=(const Pointer<T> &t) 
  73.         { 
  74.             if(t.Get() != NULL) 
  75.             { 
  76.                 _Unbind(); 
  77.                 _Bind(t.Get()); 
  78.             } 
  79.             return *this
  80.         } 
  81.         template<typename U> 
  82.         Pointer<T> & operator=(const Pointer<U> &u) 
  83.         { 
  84.             if( m_ptrObject != u.Get() )  
  85.             { 
  86.                 _Unbind(); 
  87.                 _Bind(u.Get()); 
  88.             } 
  89.  
  90.             return *this
  91.         } 
  92.  
  93.         T &operator*() 
  94.         { 
  95.             return *m_ptrObject; 
  96.         } 
  97.         T &operator*() const 
  98.         { 
  99.             return *m_ptrObject; 
  100.         } 
  101.         T *operator->() 
  102.         { 
  103.             return m_ptrObject; 
  104.         } 
  105.         T *operator->() const 
  106.         { 
  107.             return m_ptrObject; 
  108.         } 
  109.  
  110.         T *Get() 
  111.         { 
  112.             return m_ptrObject; 
  113.         } 
  114.         T *Get() const 
  115.         { 
  116.             return m_ptrObject; 
  117.         } 
  118.  
  119.         bool operator==(const Pointer<T> &t) const 
  120.         { 
  121.             return m_ptrObject == t.Get(); 
  122.         } 
  123.  
  124.         bool operator!=(const Pointer<T> &t) const 
  125.         { 
  126.             return m_ptrObject != t.Get(); 
  127.         } 
  128.  
  129.         template<typename U> 
  130.         bool operator==(const Pointer<U> &u) const 
  131.         { 
  132.             return m_ptrObject == u.Get(); 
  133.         } 
  134.  
  135.         template<typename U> 
  136.         bool operator!=(const Pointer<U> &u) const 
  137.         { 
  138.             return m_ptrObject != u.Get(); 
  139.         } 
  140.  
  141.     private
  142.         void _Bind(T *p) 
  143.         { 
  144.             if(p!=NULL) 
  145.             { 
  146.                 p->AddRef(); 
  147.                 m_ptrObject = p; 
  148.             } 
  149.             else 
  150.             { 
  151.                 m_ptrObject = NULL; 
  152.             } 
  153.         } 
  154.         template<typename U> 
  155.         void _Bind(U *p) 
  156.         { 
  157.             if(p != NULL) 
  158.             { 
  159.                 p->AddRef(); 
  160.                 m_ptrObject = reinterpret_cast<T *>(p); 
  161.             } 
  162.             else 
  163.             { 
  164.                 m_ptrObject = NULL; 
  165.             } 
  166.  
  167.         } 
  168.  
  169.         void _Unbind() 
  170.         {        
  171.             if(m_ptrObject != NULL) 
  172.             { 
  173.                 m_ptrObject->Release(); 
  174.             } 
  175.         } 
  176.  
  177.     }; 
  •  使用方式如下
  1.   /**  
  2.     * VC 屬性擴展,保證類的靜態成員變量在頭文件中鏈接正常  
  3.    * @versiont     v 1.0.0  
  4.     * @author       順 哥 
  5.     */  
  6.    __declspec(selectany) ObjectPtr nothing(new Object);  
  7.   /**
  8.     * 智能指針類型宏定義  
  9.     * @versiont     v 1.0.0  
  10.     * @author       順 哥 
  11.     */  
  12.    typedef Pointer<Object>  ObjectPtr;  
  13. /**  
  14.      * 可以如此放肆的 new 而不用 自己去 delete 
  15.     * @versiont     v 1.0.0  
  16.      * @author       順 哥 
  17.      */  
  18.     ObjectPtr = new Object();  

    在應用中通過繼承Object使得子類繼承 AddRef  和 Release 方法,根據需要可以在子類將此二方法重寫;通過採用Pointer 對 Object或其子類進行包裝可以獲得對指向被包裝類類型的指針所指向的內存進行自動維護的能力。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章