| OLD | NEW |
| 1 | 1 |
| 2 /* | 2 /* |
| 3 * Copyright 2006 The Android Open Source Project | 3 * Copyright 2006 The Android Open Source Project |
| 4 * | 4 * |
| 5 * Use of this source code is governed by a BSD-style license that can be | 5 * Use of this source code is governed by a BSD-style license that can be |
| 6 * found in the LICENSE file. | 6 * found in the LICENSE file. |
| 7 */ | 7 */ |
| 8 | 8 |
| 9 | 9 |
| 10 #ifndef SkRefCnt_DEFINED | 10 #ifndef SkRefCnt_DEFINED |
| (...skipping 190 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 201 * The reference count is not modified, and the internal ptr is set to NULL | 201 * The reference count is not modified, and the internal ptr is set to NULL |
| 202 * so unref() will not be called in our destructor. A subsequent call to | 202 * so unref() will not be called in our destructor. A subsequent call to |
| 203 * detach() will do nothing and return null. | 203 * detach() will do nothing and return null. |
| 204 */ | 204 */ |
| 205 T* detach() { | 205 T* detach() { |
| 206 T* obj = fObj; | 206 T* obj = fObj; |
| 207 fObj = NULL; | 207 fObj = NULL; |
| 208 return obj; | 208 return obj; |
| 209 } | 209 } |
| 210 | 210 |
| 211 /** | 211 T* operator->() const { return fObj; } |
| 212 * BlockRef<B> is a type which inherits from B, cannot be created, | |
| 213 * cannot be deleted, and makes ref and unref private. | |
| 214 */ | |
| 215 template<typename B> class BlockRef : public B { | |
| 216 private: | |
| 217 BlockRef(); | |
| 218 ~BlockRef(); | |
| 219 void ref() const; | |
| 220 void unref() const; | |
| 221 }; | |
| 222 | |
| 223 /** If T is const, the type returned from operator-> will also be const. */ | |
| 224 typedef typename SkTConstType<BlockRef<T>, SkTIsConst<T>::value>::type Block
RefType; | |
| 225 | |
| 226 /** | |
| 227 * SkAutoTUnref assumes ownership of the ref. As a result, it is an error | |
| 228 * for the user to ref or unref through SkAutoTUnref. Therefore | |
| 229 * SkAutoTUnref::operator-> returns BlockRef<T>*. This prevents use of | |
| 230 * skAutoTUnrefInstance->ref() and skAutoTUnrefInstance->unref(). | |
| 231 */ | |
| 232 BlockRefType *operator->() const { | |
| 233 return static_cast<BlockRefType*>(fObj); | |
| 234 } | |
| 235 operator T*() const { return fObj; } | 212 operator T*() const { return fObj; } |
| 236 | 213 |
| 237 private: | 214 private: |
| 238 T* fObj; | 215 T* fObj; |
| 239 }; | 216 }; |
| 240 // Can't use the #define trick below to guard a bare SkAutoTUnref(...) because i
t's templated. :( | 217 // Can't use the #define trick below to guard a bare SkAutoTUnref(...) because i
t's templated. :( |
| 241 | 218 |
| 242 class SkAutoUnref : public SkAutoTUnref<SkRefCnt> { | 219 class SkAutoUnref : public SkAutoTUnref<SkRefCnt> { |
| 243 public: | 220 public: |
| 244 SkAutoUnref(SkRefCnt* obj) : SkAutoTUnref<SkRefCnt>(obj) {} | 221 SkAutoUnref(SkRefCnt* obj) : SkAutoTUnref<SkRefCnt>(obj) {} |
| (...skipping 21 matching lines...) Expand all Loading... |
| 266 SkDELETE((const Derived*)this); | 243 SkDELETE((const Derived*)this); |
| 267 } | 244 } |
| 268 } | 245 } |
| 269 void deref() const { this->unref(); } | 246 void deref() const { this->unref(); } |
| 270 | 247 |
| 271 private: | 248 private: |
| 272 mutable int32_t fRefCnt; | 249 mutable int32_t fRefCnt; |
| 273 }; | 250 }; |
| 274 | 251 |
| 275 #endif | 252 #endif |
| OLD | NEW |