1 #ifndef LVAUTOPTR_H 2 #define LVAUTOPTR_H 3 4 /// auto pointer 5 template <class T > 6 class LVAutoPtr { 7 T * p; LVAutoPtr(const LVAutoPtr & v)8 LVAutoPtr( const LVAutoPtr & v ) { 9 CR_UNUSED(v); 10 } // no copy allowed 11 LVAutoPtr & operator = (const LVAutoPtr & v) { 12 CR_UNUSED(v); 13 return *this; 14 } // no copy 15 public: LVAutoPtr()16 LVAutoPtr() 17 : p(NULL) 18 { 19 } LVAutoPtr(T * ptr)20 explicit LVAutoPtr( T* ptr ) 21 : p(ptr) 22 { 23 } isNull()24 bool isNull() const { 25 return p == NULL; 26 } 27 bool operator !() const { return p == NULL; } clear()28 inline void clear() 29 { 30 if (p) 31 delete p; 32 p = NULL; 33 } ~LVAutoPtr()34 ~LVAutoPtr() 35 { 36 clear(); 37 } 38 inline T * operator -> () 39 { 40 return p; 41 } 42 inline const T * operator -> () const 43 { 44 return p; 45 } 46 inline T & operator [] (int index) { return p[index]; } get()47 inline T * get() const { return p; } 48 inline T & operator * () 49 { 50 return *p; 51 } 52 inline const T & operator * () const 53 { 54 return *p; 55 } 56 inline LVAutoPtr & operator = ( T* ptr ) 57 { 58 if ( p==ptr ) 59 return *this; 60 if ( p ) 61 delete p; 62 p = ptr; 63 return *this; 64 } 65 }; 66 67 /// unique pointer 68 template <class T > 69 class LVUniquePtr { 70 T * p; 71 public: LVUniquePtr()72 LVUniquePtr() 73 : p(NULL) 74 { 75 } LVUniquePtr(T * ptr)76 explicit LVUniquePtr( T* ptr ) 77 : p(ptr) 78 { 79 } LVUniquePtr(const LVUniquePtr & v)80 LVUniquePtr( const LVUniquePtr & v ) { p = v.p; v.p = NULL; } 81 LVUniquePtr & operator = (const LVUniquePtr & v) { 82 clear(); 83 p = v.p; v.p = NULL; 84 return *this; 85 } // no copy isNull()86 bool isNull() const { 87 return p == NULL; 88 } 89 bool operator !() const { return p == NULL; } clear()90 inline void clear() 91 { 92 if (p) 93 delete p; 94 p = NULL; 95 } ~LVUniquePtr()96 ~LVUniquePtr() 97 { 98 clear(); 99 } 100 inline T * operator -> () 101 { 102 return p; 103 } 104 inline const T * operator -> () const 105 { 106 return p; 107 } get()108 inline T * get() const { return p; } 109 inline T & operator * () 110 { 111 return *p; 112 } 113 inline const T & operator * () const 114 { 115 return *p; 116 } 117 inline LVUniquePtr & operator = ( T* ptr ) 118 { 119 if ( p==ptr ) 120 return *this; 121 if ( p ) 122 delete p; 123 p = ptr; 124 return *this; 125 } 126 }; 127 128 /// unique pointer 129 template <class T > 130 class LVClonePtr { 131 T * p; 132 public: LVClonePtr()133 LVClonePtr() 134 : p(NULL) 135 { 136 } LVClonePtr(T * ptr)137 explicit LVClonePtr( T* ptr ) 138 : p(ptr ? (T*)ptr->clone() : NULL) 139 { 140 } LVClonePtr(const LVClonePtr & v)141 LVClonePtr( const LVClonePtr & v ) { p = v.p ? (T*)v.p->clone() : NULL; } 142 LVClonePtr & operator = (const LVClonePtr & v) { 143 clear(); 144 p = v.p ? (T*)v.p->clone() : NULL; 145 return *this; 146 } // no copy isNull()147 bool isNull() const { 148 return p == NULL; 149 } 150 bool operator !() const { return p == NULL; } clear()151 inline void clear() 152 { 153 if (p) 154 delete p; 155 p = NULL; 156 } ~LVClonePtr()157 ~LVClonePtr() 158 { 159 clear(); 160 } 161 inline T * operator -> () 162 { 163 return p; 164 } 165 inline const T * operator -> () const 166 { 167 return p; 168 } 169 inline T & operator [] (int index) { return p[index]; } get()170 inline T * get() const { return p; } 171 inline T & operator * () 172 { 173 return *p; 174 } 175 inline const T & operator * () const 176 { 177 return *p; 178 } 179 inline LVClonePtr & operator = ( T* ptr ) 180 { 181 if ( p==ptr ) 182 return *this; 183 if ( p ) 184 delete p; 185 p = ptr ? (T*)ptr->clone() : NULL; 186 return *this; 187 } 188 }; 189 190 191 #endif // LVAUTOPTR_H 192