From 77a53de20d723ca21cc824fd97e68aaa60e022ea Mon Sep 17 00:00:00 2001 From: bungeman Date: Thu, 1 Oct 2015 12:28:49 -0700 Subject: 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 --- include/core/SkRefCnt.h | 47 +++++++++++------------------------------------ 1 file changed, 11 insertions(+), 36 deletions(-) (limited to 'include/core/SkRefCnt.h') 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 static inline T* SkRef(T* obj) { SkASSERT(obj); @@ -171,51 +172,25 @@ template static inline void SkSafeUnref(T* obj) { template static inline void SkSafeSetNull(T*& obj) { if (obj) { obj->unref(); - obj = NULL; + obj = nullptr; } } /////////////////////////////////////////////////////////////////////////////// +template struct SkTUnref { + void operator()(T* t) { t->unref(); } +}; + /** * Utility class that simply unref's its argument in the destructor. */ -template class SkAutoTUnref : SkNoncopyable { +template class SkAutoTUnref : public skstd::unique_ptr> { 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>(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. :( -- cgit v1.2.3