From a67573e25faa81ea65e6fc368f66d3f0c0a5f189 Mon Sep 17 00:00:00 2001 From: "reed@google.com" Date: Fri, 25 Feb 2011 18:10:29 +0000 Subject: http://codereview.appspot.com/4233041/ 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 --- include/core/SkRefCnt.h | 49 ++++++++++++++++++++++++------------------------- 1 file changed, 24 insertions(+), 25 deletions(-) (limited to 'include/core') 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 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 { +public: + SkAutoUnref(SkRefCnt* obj) : SkAutoTUnref(obj) {} }; /////////////////////////////////////////////////////////////////////////////// -- cgit v1.2.3