From: bsalomon@google.com Date: Fri, 27 Jul 2012 13:27:35 +0000 (+0000) Subject: Specialize SkAutoTUnref on const T so that operator -> works. X-Git-Tag: accepted/tizen/5.0/unified/20181102.025319~15415 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=1448cf8cb6c6cd4866f7c71bf32ad6bca5d683d1;p=platform%2Fupstream%2FlibSkiaSharp.git Specialize SkAutoTUnref on const T so that operator -> works. Review URL: http://codereview.appspot.com/6450054/ git-svn-id: http://skia.googlecode.com/svn/trunk@4802 2bbb7eff-a529-9590-31e7-b0007b416f81 --- diff --git a/include/core/SkRefCnt.h b/include/core/SkRefCnt.h index 79a28ef..eab3c46 100644 --- a/include/core/SkRefCnt.h +++ b/include/core/SkRefCnt.h @@ -135,12 +135,14 @@ template static inline void SkSafeUnref(T* obj) { /////////////////////////////////////////////////////////////////////////////// /** - * Utility class that simply unref's its argument in the destructor. + * SkAutoTUnref is a utility class that simply unref's its argument in the + * destructor. For implementation detail reasons there is a base class, a + * derived general class, and a const-specialized derived class. */ -template class SkAutoTUnref : SkNoncopyable { + +template class SkAutoTUnrefBase : SkNoncopyable { public: - explicit SkAutoTUnref(T* obj = NULL) : fObj(obj) {} - ~SkAutoTUnref() { SkSafeUnref(fObj); } + ~SkAutoTUnrefBase() { SkSafeUnref(fObj); } T* get() const { return fObj; } @@ -161,9 +163,11 @@ public: return obj; } + operator T*() { return fObj; } + /** - * BlockRef is a type which inherits from B, cannot be created, - * and makes ref and unref private. + * BlockRef is a type which inherits from B, cannot be created, and makes + * ref and unref private. */ template class BlockRef : public B { private: @@ -171,19 +175,46 @@ public: void ref() const; void unref() const; }; + +protected: + explicit SkAutoTUnrefBase(T* obj) : fObj(obj) {} + T* fObj; + +private: + SkAutoTUnrefBase(); +}; + +template class SkAutoTUnref : public SkAutoTUnrefBase { +public: + explicit SkAutoTUnref(T* obj = NULL) : SkAutoTUnrefBase(obj) {} + /** * SkAutoTUnref assumes ownership of the ref. As a result, it is an error * for the user to ref or unref through SkAutoTUnref. Therefore * SkAutoTUnref::operator-> returns BlockRef*. This prevents use of * skAutoTUnrefInstance->ref() and skAutoTUnrefInstance->unref(). */ - BlockRef *operator->() const { - return static_cast*>(fObj); + typedef typename SkAutoTUnrefBase::template BlockRef BlockRefT; + + BlockRefT* operator->() const { + return static_cast(this->fObj); } - operator T*() { return fObj; } +}; -private: - T* fObj; +template class SkAutoTUnref : public SkAutoTUnrefBase { +public: + explicit SkAutoTUnref(const T* obj = NULL) : SkAutoTUnrefBase(obj) {} + /** + * SkAutoTUnref assumes ownership of the ref. As a result, it is an error + * for the user to ref or unref through SkAutoTUnref. Therefore + * SkAutoTUnref::operator-> returns BlockRef*. This prevents use of + * skAutoTUnrefInstance->ref() and skAutoTUnrefInstance->unref(). + */ + typedef typename SkAutoTUnrefBase::template BlockRef BlockRefT; + + const BlockRefT* operator->() const { + return static_cast(this->fObj); + } }; class SkAutoUnref : public SkAutoTUnref {