표준 라이브러리 sharedptr 스마트 포인터의 실현

16811 단어 sha
현재 테스트 기능은 정상이다.만약 미비한 점이 있다면 개선하고 있겠지.시간이 늦었으니 자러 가자. 아이고, 땡땡이치면 잡힐 테니 모험하지 마라.늦은 안전 세계
  1 /************************************************************************* 
  2 *my shared_ptr:  share_ptr
  3 *author:ERIC
  4 *blog:http://www.ilovecpp.com
  5 *time:2015-5-28 01:36:43
  6 *************************************************************************/
  7 
  8 template <typename T> class share_ptr {
  9 private:
 10     T* __ptr;
 11     int* __pcounts;
 12 public:
 13     share_ptr(T* p= NULL);
 14     share_ptr(const share_ptr<T>& src);
 15     share_ptr& operator=(share_ptr<T>& src);
 16     ~share_ptr();
 17     operator bool() const;//  if(p)  
 18     T* operator-> () const;
 19     T& operator*() const;
 20     T* get() const;
 21     int use_counts() const;//      
 22     bool unique() const;//          
 23     void swap(share_ptr& rhs);//
 24     template <typename Type>//
 25     friend void swap(share_ptr<Type>& lhs,share_ptr<Type>& rhs);
 26 };
 27 
 28 template<typename T>
 29 share_ptr<T>::share_ptr(T* p)
 30     :__ptr(p),__pcounts(new int(0))
 31 {
 32     if(__ptr)
 33         *__pcounts = 1;
 34 }
 35 
 36 template<typename T>
 37 share_ptr<T>::~share_ptr()
 38 {
 39     --*__pcounts;
 40     if(*__pcounts == 0)
 41     {//       delete  __ptr   ,delete NULL;
 42         delete __pcounts;
 43         delete __ptr;
 44     }
 45 }
 46 
 47 /*__ptr(new T(src))     ,    __ptr(new T)
 48 *   T            
 49 *             
 50 */
 51 template<typename T>
 52 share_ptr<T>::share_ptr (const share_ptr<T>& src)
 53     :__pcounts(new int),__ptr(new T(src))
 54 {
 55     ++*src.__pcounts;
 56     __ptr = src.__ptr;
 57     __pcounts = src.__pcounts;
 58 }
 59 
 60 template <typename T>
 61 share_ptr<T>& share_ptr<T>::operator= (share_ptr<T>& src)
 62 {
 63     --*__pcounts;
 64     //           __pcounts == -1,        
 65     if(*__pcounts == 0 || *__pcounts == -1)
 66         delete __pcounts;
 67     ++*src.__pcounts;
 68     __ptr = src.__ptr;
 69     __pcounts = src.__pcounts;
 70     return *this;
 71 }
 72 
 73 //  if(p)     
 74 template<typename T>
 75 share_ptr<T>::operator bool() const
 76 {
 77     return __ptr;
 78 }
 79 
 80 template<typename T>
 81 T* share_ptr<T>::operator->() const
 82 {
 83     return __ptr;
 84 }
 85 
 86 template<typename T>
 87 T& share_ptr<T>::operator*() const
 88 {
 89     return *__ptr;
 90 }
 91 
 92 
 93 template<typename T>
 94 T* share_ptr<T>::get() const
 95 {
 96     return __ptr;
 97 }
 98 
 99 template<typename T>
100 int share_ptr<T>::use_counts() const
101 {
102     return *__pcounts;
103 }
104 
105 template<typename T>
106 bool share_ptr<T>::unique() const
107 {
108     if(*__pcounts == 1)
109         return true;
110     else
111         return false;
112 }
113 
114 template<typename T>
115 void share_ptr<T>::swap(share_ptr<T>& rhs)
116 {
117     T* tmpPtr = rhs.__ptr;
118     rhs.__ptr = __ptr;
119     __ptr = tmpPtr;
120     int* tmpPcounts = rhs.__pcounts;
121     rhs.__pcounts = __pcounts;
122     __pcounts = tmpPcounts; 
123 }
124 
125 template<typename T>
126 void swap(share_ptr<T>& lhs,share_ptr<T>& rhs)
127 {
128     T* tmpPtr = rhs.__ptr;
129     rhs.__ptr = lhs.__ptr;
130     lhs.__ptr = tmpPtr;
131     int* tmpPcounts = rhs.__pcounts;
132     rhs.__pcounts = lhs.__pcounts;
133     lhs.__pcounts = tmpPcounts; 
134 }
135 
136 //c++11 make_shared<T>(args) 
137 template<typename T>
138 share_ptr<T> make_share(T args)
139 {
140     return new T(args);
141 }

좋은 웹페이지 즐겨찾기