diff options
author | reed@google.com <reed@google.com@2bbb7eff-a529-9590-31e7-b0007b416f81> | 2011-02-25 18:10:29 +0000 |
---|---|---|
committer | reed@google.com <reed@google.com@2bbb7eff-a529-9590-31e7-b0007b416f81> | 2011-02-25 18:10:29 +0000 |
commit | a67573e25faa81ea65e6fc368f66d3f0c0a5f189 (patch) | |
tree | 2ec1768eaf76e7446c3fa9630588d413ed50463b | |
parent | 29f6636a5faad017f07980727eb29a13febfaf3d (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
-rw-r--r-- | include/core/SkRefCnt.h | 49 | ||||
-rw-r--r-- | src/core/SkRefCnt.cpp | 48 | ||||
-rw-r--r-- | src/core/core_files.mk | 1 | ||||
-rw-r--r-- | tests/UtilsTest.cpp | 24 |
4 files changed, 47 insertions, 75 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) {} }; /////////////////////////////////////////////////////////////////////////////// diff --git a/src/core/SkRefCnt.cpp b/src/core/SkRefCnt.cpp deleted file mode 100644 index fea100524f..0000000000 --- a/src/core/SkRefCnt.cpp +++ /dev/null @@ -1,48 +0,0 @@ -/* libs/graphics/sgl/SkRefCnt.cpp -** -** Copyright 2006, The Android Open Source Project -** -** Licensed under the Apache License, Version 2.0 (the "License"); -** you may not use this file except in compliance with the License. -** You may obtain a copy of the License at -** -** http://www.apache.org/licenses/LICENSE-2.0 -** -** Unless required by applicable law or agreed to in writing, software -** distributed under the License is distributed on an "AS IS" BASIS, -** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -** See the License for the specific language governing permissions and -** limitations under the License. -*/ - -#include "SkRefCnt.h" - -SkAutoUnref::~SkAutoUnref() { - if (fObj) { - fObj->unref(); - } -} - -bool SkAutoUnref::ref() { - if (fObj) { - fObj->ref(); - return true; - } - return false; -} - -bool SkAutoUnref::unref() { - if (fObj) { - fObj->unref(); - fObj = NULL; - return true; - } - return false; -} - -SkRefCnt* SkAutoUnref::detach() { - SkRefCnt* obj = fObj; - fObj = NULL; - return obj; -} - diff --git a/src/core/core_files.mk b/src/core/core_files.mk index 63b88336d1..252644e9b5 100644 --- a/src/core/core_files.mk +++ b/src/core/core_files.mk @@ -72,7 +72,6 @@ SOURCE := \ SkQuadClipper.cpp \ SkRasterizer.cpp \ SkRect.cpp \ - SkRefCnt.cpp \ SkRefDict.cpp \ SkRegion.cpp \ SkRegion_rects.cpp \ diff --git a/tests/UtilsTest.cpp b/tests/UtilsTest.cpp index 8ec063e77d..1e11bdcc2d 100644 --- a/tests/UtilsTest.cpp +++ b/tests/UtilsTest.cpp @@ -43,6 +43,27 @@ static void test_refptr(skiatest::Reporter* reporter) { r0->unref(); } +static void test_autounref(skiatest::Reporter* reporter) { + RefClass obj(0); + REPORTER_ASSERT(reporter, 1 == obj.getRefCnt()); + + SkAutoTUnref<RefClass> tmp(&obj); + REPORTER_ASSERT(reporter, &obj == tmp.get()); + REPORTER_ASSERT(reporter, 1 == obj.getRefCnt()); + + REPORTER_ASSERT(reporter, &obj == tmp.detach()); + REPORTER_ASSERT(reporter, 1 == obj.getRefCnt()); + REPORTER_ASSERT(reporter, NULL == tmp.detach()); + REPORTER_ASSERT(reporter, NULL == tmp.get()); + + obj.ref(); + REPORTER_ASSERT(reporter, 2 == obj.getRefCnt()); + { + SkAutoTUnref<RefClass> tmp2(&obj); + } + REPORTER_ASSERT(reporter, 1 == obj.getRefCnt()); +} + /////////////////////////////////////////////////////////////////////////////// #define kSEARCH_COUNT 91 @@ -145,7 +166,8 @@ static void TestUTF(skiatest::Reporter* reporter) { test_utf16(reporter); test_search(reporter); test_refptr(reporter); + test_autounref(reporter); } #include "TestClassDef.h" -DEFINE_TESTCLASS("UTF", UtfTestClass, TestUTF) +DEFINE_TESTCLASS("Utils", UtfTestClass, TestUTF) |