|
| 1 | + |
| 2 | +#ifndef SMART_PTR_H |
| 3 | +#define SMART_PTR_H |
| 4 | + |
| 5 | +#include <algorithm> |
| 6 | + |
| 7 | +namespace wiz { |
| 8 | + |
| 9 | + |
| 10 | + template < class T > |
| 11 | + class SmartPtr |
| 12 | + { |
| 13 | + private: |
| 14 | + class Inner { |
| 15 | + public: |
| 16 | + T* ptr = nullptr; |
| 17 | + int count = 0; |
| 18 | + }; |
| 19 | + private: |
| 20 | + Inner* inner = nullptr; |
| 21 | + int option; |
| 22 | + private: |
| 23 | + void quit() |
| 24 | + { |
| 25 | + if (inner) { |
| 26 | + inner->count--; |
| 27 | + if (0 == inner->count) { |
| 28 | + delete inner; |
| 29 | + } |
| 30 | + inner = nullptr; |
| 31 | + } |
| 32 | + } |
| 33 | + |
| 34 | + void enter(const SmartPtr<T>& sp) |
| 35 | + { |
| 36 | + //wizard::assertNotnullptr( sp ); |
| 37 | + //wizard::assertnullptr( this->ptr ); |
| 38 | + |
| 39 | + if (nullptr == sp.inner) |
| 40 | + { |
| 41 | + this->quit(); |
| 42 | + } |
| 43 | + else if (nullptr == this->inner) |
| 44 | + { |
| 45 | + this->inner = sp.inner; |
| 46 | + sp.inner->count++; |
| 47 | + this->option = sp.option; /// |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + void initFromOther(const SmartPtr<T>& sp) |
| 52 | + { |
| 53 | + //wizard::assertNotEquals( this, &sp ); |
| 54 | + // if( this == sp.getThis() ) { return; } |
| 55 | + |
| 56 | + // delete or quit |
| 57 | + if (this->inner) |
| 58 | + { |
| 59 | + if (isOnlyOne()) // |
| 60 | + { |
| 61 | + remove(); |
| 62 | + } |
| 63 | + else |
| 64 | + { |
| 65 | + quit(); |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + // enter |
| 70 | + enter(sp); |
| 71 | + |
| 72 | + return; |
| 73 | + } |
| 74 | + public: |
| 75 | + |
| 76 | + SmartPtr(T* ptr = nullptr) |
| 77 | + : option(0) |
| 78 | + { |
| 79 | + // ptr <- new T(~~); |
| 80 | + if (ptr) { |
| 81 | + this->inner = new Inner(); |
| 82 | + this->inner->ptr = ptr; |
| 83 | + this->inner->count = 1; |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + SmartPtr(T* ptr, const int option) // option 1,2,.. |
| 88 | + : option(option) |
| 89 | + { |
| 90 | + // ptr <- new T(~~); |
| 91 | + if (ptr) { |
| 92 | + this->inner = new Inner(); |
| 93 | + this->inner->ptr = ptr; |
| 94 | + this->inner->count = 1; |
| 95 | + } |
| 96 | + } |
| 97 | + SmartPtr(const SmartPtr<T>& sp) |
| 98 | + : option(sp.option) |
| 99 | + { |
| 100 | + initFromOther(sp); |
| 101 | + } |
| 102 | + SmartPtr(SmartPtr<T>&& sp) |
| 103 | + :option(sp.option) |
| 104 | + { |
| 105 | + Inner* temp = this->inner; |
| 106 | + this->inner = sp.inner; |
| 107 | + sp.inner = temp; |
| 108 | + } |
| 109 | + virtual ~SmartPtr() /// virtual?? |
| 110 | + { |
| 111 | + if (isOnlyOne()) |
| 112 | + { |
| 113 | + remove(false); |
| 114 | + } |
| 115 | + else |
| 116 | + { |
| 117 | + remove(false); |
| 118 | + } |
| 119 | + } |
| 120 | + public: |
| 121 | + SmartPtr<T>& operator=(T* ptr) { |
| 122 | + if (inner && ptr) { |
| 123 | + inner->ptr = ptr; |
| 124 | + } |
| 125 | + else if (!inner && ptr) { |
| 126 | + inner = new Inner(); |
| 127 | + inner->ptr = ptr; |
| 128 | + inner->count = 1; |
| 129 | + } |
| 130 | + return*this; |
| 131 | + } |
| 132 | + SmartPtr<T>& operator=(const SmartPtr<T>& _sp) |
| 133 | + { |
| 134 | + // temp link |
| 135 | + SmartPtr<T> tempLink(_sp); |
| 136 | + |
| 137 | + initFromOther(tempLink); |
| 138 | + |
| 139 | + return *this; |
| 140 | + } |
| 141 | + T& operator*() |
| 142 | + { |
| 143 | + // wizard::assertNotnullptr( ptr ); |
| 144 | + return (*inner->ptr); |
| 145 | + } |
| 146 | + const T& operator*()const |
| 147 | + { |
| 148 | + // wizard::assertNotnullptr( ptr ); |
| 149 | + return (*inner->ptr); |
| 150 | + } |
| 151 | + public: |
| 152 | + bool isOnlyOne()const |
| 153 | + { |
| 154 | + return this->inner && this->inner->count == 1; |
| 155 | + } |
| 156 | + bool isNULL()const |
| 157 | + { |
| 158 | + return nullptr == this->inner || nullptr == this->inner->ptr; |
| 159 | + } |
| 160 | + bool empty()const |
| 161 | + { |
| 162 | + return nullptr == this->inner; |
| 163 | + } |
| 164 | + /// remove return suceess? |
| 165 | + bool remove() |
| 166 | + { |
| 167 | + return remove(true); |
| 168 | + } |
| 169 | + bool remove(const bool bremove) // make private and delete =true, and make public remove() call remove( true ); - 2012.3.5 todo... |
| 170 | + { |
| 171 | + if (empty()) { return false; } |
| 172 | + if (!bremove && isOnlyOne()) { return false; } /// 2013.08.13 false means "no change"?? |
| 173 | + if (this->inner->ptr && bremove) |
| 174 | + { |
| 175 | + delete this->inner->ptr; this->inner->ptr = nullptr; // no dynamic. |
| 176 | + |
| 177 | + quit(); |
| 178 | + } |
| 179 | + else |
| 180 | + { |
| 181 | + quit(); |
| 182 | + } |
| 183 | + return true; |
| 184 | + } |
| 185 | + public: |
| 186 | + operator T* () { |
| 187 | + if (this->inner) { |
| 188 | + return this->inner->ptr; |
| 189 | + } |
| 190 | + return nullptr; |
| 191 | + } |
| 192 | + |
| 193 | + const T* operator->()const |
| 194 | + { |
| 195 | + //wizard::assertNotnullptr( ptr ); |
| 196 | + return inner->ptr; |
| 197 | + } |
| 198 | + T* operator->() |
| 199 | + { |
| 200 | + //wizard::assertNotnullptr( ptr ); |
| 201 | + return inner->ptr; |
| 202 | + } |
| 203 | + T* operator&() |
| 204 | + { |
| 205 | + return inner->ptr; |
| 206 | + } |
| 207 | + const T* operator&()const |
| 208 | + { |
| 209 | + return inner->ptr; |
| 210 | + } |
| 211 | + /// |
| 212 | + public: |
| 213 | + bool hasSameObject(const SmartPtr<T>& wsp) const |
| 214 | + { |
| 215 | + return this->inner == wsp.inner; |
| 216 | + } |
| 217 | + }; |
| 218 | +} |
| 219 | + |
| 220 | +#endif |
0 commit comments