Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(4)

Unified Diff: include/core/SkRefCnt.h

Issue 1370803002: Base SkAutoTUnref on skstd::unique_ptr. (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: Rebase Created 5 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | src/core/SkPath.cpp » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: include/core/SkRefCnt.h
diff --git a/include/core/SkRefCnt.h b/include/core/SkRefCnt.h
index 1ba59b33c0578cd06d742ad4f0cf7c28795ad53b..0e8d857722d2678d8620fe923267fff82b7e197e 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. :(
« no previous file with comments | « no previous file | src/core/SkPath.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698