aboutsummaryrefslogtreecommitdiffhomepage
path: root/include/core
diff options
context:
space:
mode:
authorGravatar reed@google.com <reed@google.com@2bbb7eff-a529-9590-31e7-b0007b416f81>2011-02-25 18:10:29 +0000
committerGravatar reed@google.com <reed@google.com@2bbb7eff-a529-9590-31e7-b0007b416f81>2011-02-25 18:10:29 +0000
commita67573e25faa81ea65e6fc368f66d3f0c0a5f189 (patch)
tree2ec1768eaf76e7446c3fa9630588d413ed50463b /include/core
parent29f6636a5faad017f07980727eb29a13febfaf3d (diff)
Add templated version of SkAutoTUnref. Add unittests for it. Remove unused helper apis on SkAutoUnref. git-svn-id: http://skia.googlecode.com/svn/trunk@858 2bbb7eff-a529-9590-31e7-b0007b416f81
Diffstat (limited to 'include/core')
-rw-r--r--include/core/SkRefCnt.h49
1 files changed, 24 insertions, 25 deletions
diff --git a/include/core/SkRefCnt.h b/include/core/SkRefCnt.h
index f109ead4c7..2024e08c9b 100644
--- a/include/core/SkRefCnt.h
+++ b/include/core/SkRefCnt.h
@@ -67,36 +67,35 @@ private:
mutable int32_t fRefCnt;
};
-/** \class SkAutoUnref
-
- SkAutoUnref is a stack-helper class that will automatically call unref() on
- the object it points to when the SkAutoUnref object goes out of scope.
- If obj is null, do nothing.
-*/
-class SkAutoUnref : SkNoncopyable {
+/**
+ * Utility class that simply unref's its argument in the destructor.
+ */
+template <typename T> class SkAutoTUnref : SkNoncopyable {
public:
- SkAutoUnref(SkRefCnt* obj) : fObj(obj) {}
- ~SkAutoUnref();
-
- SkRefCnt* get() const { return fObj; }
+ SkAutoTUnref(T* obj) : fObj(obj) {}
+ ~SkAutoTUnref() { SkSafeUnref(fObj); }
- /** If the hosted object is null, do nothing and return false, else call
- ref() on it and return true
- */
- bool ref();
-
- /** If the hosted object is null, do nothing and return false, else call
- unref() on it, set its reference to null, and return true
- */
- bool unref();
+ T* get() const { return fObj; }
- /** If the hosted object is null, do nothing and return NULL, else call
- unref() on it, set its reference to null, and return the object
- */
- SkRefCnt* detach();
+ /**
+ * 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;
+ }
private:
- SkRefCnt* fObj;
+ T* fObj;
+};
+
+class SkAutoUnref : public SkAutoTUnref<SkRefCnt> {
+public:
+ SkAutoUnref(SkRefCnt* obj) : SkAutoTUnref<SkRefCnt>(obj) {}
};
///////////////////////////////////////////////////////////////////////////////