diff options
author | bungeman <bungeman@google.com> | 2015-10-01 12:28:49 -0700 |
---|---|---|
committer | Commit bot <commit-bot@chromium.org> | 2015-10-01 12:28:49 -0700 |
commit | 77a53de20d723ca21cc824fd97e68aaa60e022ea (patch) | |
tree | f5fc67e8b4c3ca465e4c6fe820c945d138af80d1 /include/core | |
parent | 52f8deba607fd67558b57551cd43406c8b743072 (diff) |
Base SkAutoTUnref on skstd::unique_ptr.
To further consolidate the various unique owning classes, this bases
SkAutoTUnref on skstd::unique_ptr. Users are updated because of two
breaking changes, swap now takes a reference and reset no longer
returns its argument.
Review URL: https://codereview.chromium.org/1370803002
Diffstat (limited to 'include/core')
-rw-r--r-- | include/core/SkRefCnt.h | 47 |
1 files changed, 11 insertions, 36 deletions
diff --git a/include/core/SkRefCnt.h b/include/core/SkRefCnt.h index 1ba59b33c0..0e8d857722 100644 --- a/include/core/SkRefCnt.h +++ b/include/core/SkRefCnt.h @@ -9,6 +9,7 @@ #define SkRefCnt_DEFINED #include "../private/SkAtomics.h" +#include "../private/SkUniquePtr.h" #include "SkTypes.h" /** \class SkRefCntBase @@ -143,7 +144,7 @@ class SK_API SkRefCnt : public SkRefCntBase { }; } while (0) -/** Call obj->ref() and return obj. The obj must not be NULL. +/** Call obj->ref() and return obj. The obj must not be nullptr. */ template <typename T> static inline T* SkRef(T* obj) { SkASSERT(obj); @@ -171,51 +172,25 @@ template <typename T> static inline void SkSafeUnref(T* obj) { template<typename T> static inline void SkSafeSetNull(T*& obj) { if (obj) { obj->unref(); - obj = NULL; + obj = nullptr; } } /////////////////////////////////////////////////////////////////////////////// +template <typename T> struct SkTUnref { + void operator()(T* t) { t->unref(); } +}; + /** * Utility class that simply unref's its argument in the destructor. */ -template <typename T> class SkAutoTUnref : SkNoncopyable { +template <typename T> class SkAutoTUnref : public skstd::unique_ptr<T, SkTUnref<T>> { public: - explicit SkAutoTUnref(T* obj = NULL) : fObj(obj) {} - ~SkAutoTUnref() { SkSafeUnref(fObj); } - - T* get() const { return fObj; } - - T* reset(T* obj) { - SkSafeUnref(fObj); - fObj = obj; - return obj; - } - - void swap(SkAutoTUnref* other) { - T* tmp = fObj; - fObj = other->fObj; - other->fObj = tmp; - } + explicit SkAutoTUnref(T* obj = nullptr) : skstd::unique_ptr<T, SkTUnref<T>>(obj) {} - /** - * Return the hosted object (which may be null), transferring ownership. - * The reference count is not modified, and the internal ptr is set to NULL - * so unref() will not be called in our destructor. A subsequent call to - * detach() will do nothing and return null. - */ - T* detach() { - T* obj = fObj; - fObj = NULL; - return obj; - } - - T* operator->() const { return fObj; } - operator T*() const { return fObj; } - -private: - T* fObj; + T* detach() { return this->release(); } + operator T*() const { return this->get(); } }; // Can't use the #define trick below to guard a bare SkAutoTUnref(...) because it's templated. :( |