Chromium Code Reviews| Index: include/core/SkRefCnt.h |
| diff --git a/include/core/SkRefCnt.h b/include/core/SkRefCnt.h |
| index 9d1e5f1f02c252c13c2091208903457403c0a825..6da6a277c175e3eaa2ea12f8887566c14f986896 100644 |
| --- a/include/core/SkRefCnt.h |
| +++ b/include/core/SkRefCnt.h |
| @@ -227,4 +227,51 @@ private: |
| mutable int32_t fRefCnt; |
| }; |
| +/////////////////////////////////////////////////////////////////////////////////////////////////// |
| + |
| +template <typename T> class sk_sp { |
| +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.
|
| + sk_sp(std::nullptr_t) : fPtr(nullptr) {} |
| + sk_sp(const sk_sp<T>& that) : fPtr(SkSafeRef(that.get())) {} |
| + sk_sp(sk_sp<T>&& that) : fPtr(that.release()) {} |
| + 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
|
| + ~sk_sp() { |
| + SkSafeUnref(fPtr); |
| + } |
| + |
| + sk_sp<T>& operator=(const sk_sp<T>& that) { |
| + this->reset(SkSafeRef(that.get())); |
| + return *this; |
| + } |
| + sk_sp<T>& operator=(sk_sp<T>&& that) { |
| + this->reset(that.release()); |
| + return *this; |
| + } |
| + |
| + bool operator==(std::nullptr_t) const { return this->get() == nullptr; } |
| + bool operator!=(std::nullptr_t) const { return this->get() != nullptr; } |
| + |
| + bool operator==(const sk_sp<T>& that) const { return this->get() == that.get(); } |
| + bool operator!=(const sk_sp<T>& that) const { return this->get() != that.get(); } |
| + |
| + explicit operator bool() const { return this->get() != nullptr; } |
| + |
| + T* get() const { return fPtr; } |
| + T* operator->() const { return fPtr; } |
| + |
| + void reset(T* ptr) { |
| + SkSafeUnref(fPtr); |
| + fPtr = ptr; |
| + } |
| + |
| + T* release() { |
| + T* ptr = fPtr; |
| + fPtr = nullptr; |
| + return ptr; |
| + } |
| + |
| +private: |
| + T* fPtr; |
| +}; |
| + |
| #endif |