Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 /* | 1 /* |
| 2 * Copyright 2006 The Android Open Source Project | 2 * Copyright 2006 The Android Open Source Project |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
| 5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
| 6 */ | 6 */ |
| 7 | 7 |
| 8 #ifndef SkRefCnt_DEFINED | 8 #ifndef SkRefCnt_DEFINED |
| 9 #define SkRefCnt_DEFINED | 9 #define SkRefCnt_DEFINED |
| 10 | 10 |
| (...skipping 209 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 220 SkDEBUGCODE(fRefCnt = 1;) // restore the 1 for our destructor's ass ert | 220 SkDEBUGCODE(fRefCnt = 1;) // restore the 1 for our destructor's ass ert |
| 221 delete (const Derived*)this; | 221 delete (const Derived*)this; |
| 222 } | 222 } |
| 223 } | 223 } |
| 224 void deref() const { this->unref(); } | 224 void deref() const { this->unref(); } |
| 225 | 225 |
| 226 private: | 226 private: |
| 227 mutable int32_t fRefCnt; | 227 mutable int32_t fRefCnt; |
| 228 }; | 228 }; |
| 229 | 229 |
| 230 //////////////////////////////////////////////////////////////////////////////// /////////////////// | |
| 231 | |
| 232 template <typename T> class sk_sp { | |
| 233 public: | |
|
fmalita_google_do_not_use
2016/03/01 05:23:25
Default ctor?
mtklein
2016/03/01 13:25:36
(T* obj = nullptr) serves as the default.
| |
| 234 sk_sp(std::nullptr_t) : fPtr(nullptr) {} | |
| 235 sk_sp(const sk_sp<T>& that) : fPtr(SkSafeRef(that.get())) {} | |
| 236 sk_sp(sk_sp<T>&& that) : fPtr(that.release()) {} | |
| 237 explicit sk_sp(T* obj = nullptr) : fPtr(obj) {} | |
|
fmalita_google_do_not_use
2016/03/01 05:23:25
The adoption is not self documenting, and it's bou
f(malita)
2016/03/01 12:53:49
OTOH this makes sk_sp a drop-in replacment for SkA
mtklein
2016/03/01 13:25:36
Let's plan for very few bare pointers escape into
| |
| 238 ~sk_sp() { | |
| 239 SkSafeUnref(fPtr); | |
| 240 } | |
| 241 | |
| 242 sk_sp<T>& operator=(const sk_sp<T>& that) { | |
| 243 this->reset(SkSafeRef(that.get())); | |
| 244 return *this; | |
| 245 } | |
| 246 sk_sp<T>& operator=(sk_sp<T>&& that) { | |
| 247 this->reset(that.release()); | |
| 248 return *this; | |
| 249 } | |
| 250 | |
| 251 bool operator==(std::nullptr_t) const { return this->get() == nullptr; } | |
| 252 bool operator!=(std::nullptr_t) const { return this->get() != nullptr; } | |
| 253 | |
| 254 bool operator==(const sk_sp<T>& that) const { return this->get() == that.get (); } | |
| 255 bool operator!=(const sk_sp<T>& that) const { return this->get() != that.get (); } | |
| 256 | |
| 257 explicit operator bool() const { return this->get() != nullptr; } | |
| 258 | |
| 259 T* get() const { return fPtr; } | |
| 260 T* operator->() const { return fPtr; } | |
| 261 | |
| 262 void reset(T* ptr) { | |
| 263 SkSafeUnref(fPtr); | |
| 264 fPtr = ptr; | |
| 265 } | |
| 266 | |
| 267 T* release() { | |
| 268 T* ptr = fPtr; | |
| 269 fPtr = nullptr; | |
| 270 return ptr; | |
| 271 } | |
| 272 | |
| 273 private: | |
| 274 T* fPtr; | |
| 275 }; | |
| 276 | |
| 230 #endif | 277 #endif |
| OLD | NEW |