From: reed@google.com Date: Fri, 15 Jul 2011 15:25:22 +0000 (+0000) Subject: add validate() and SkAutoRef X-Git-Tag: accepted/tizen/5.0/unified/20181102.025319~18010 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=7f6d6d4571c0682c81f8508ac4862b2dfea20aec;p=platform%2Fupstream%2FlibSkiaSharp.git add validate() and SkAutoRef git-svn-id: http://skia.googlecode.com/svn/trunk@1872 2bbb7eff-a529-9590-31e7-b0007b416f81 --- diff --git a/include/core/SkRefCnt.h b/include/core/SkRefCnt.h index 36f4249..b186029 100644 --- a/include/core/SkRefCnt.h +++ b/include/core/SkRefCnt.h @@ -63,10 +63,46 @@ public: } } + void validate() const { + SkASSERT(fRefCnt > 0); + } + private: mutable int32_t fRefCnt; }; +/////////////////////////////////////////////////////////////////////////////// + +/** Helper macro to safely assign one SkRefCnt[TS]* to another, checking for + null in on each side of the assignment, and ensuring that ref() is called + before unref(), in case the two pointers point to the same object. + */ +#define SkRefCnt_SafeAssign(dst, src) \ + do { \ + if (src) src->ref(); \ + if (dst) dst->unref(); \ + dst = src; \ + } while (0) + + +/** Check if the argument is non-null, and if so, call obj->ref() + */ +template static inline void SkSafeRef(T* obj) { + if (obj) { + obj->ref(); + } +} + +/** Check if the argument is non-null, and if so, call obj->unref() + */ +template static inline void SkSafeUnref(T* obj) { + if (obj) { + obj->unref(); + } +} + +/////////////////////////////////////////////////////////////////////////////// + /** * Utility class that simply unref's its argument in the destructor. */ @@ -98,35 +134,13 @@ public: SkAutoUnref(SkRefCnt* obj) : SkAutoTUnref(obj) {} }; -/////////////////////////////////////////////////////////////////////////////// - -/** Helper macro to safely assign one SkRefCnt[TS]* to another, checking for - null in on each side of the assignment, and ensuring that ref() is called - before unref(), in case the two pointers point to the same object. -*/ -#define SkRefCnt_SafeAssign(dst, src) \ - do { \ - if (src) src->ref(); \ - if (dst) dst->unref(); \ - dst = src; \ - } while (0) - - -/** Check if the argument is non-null, and if so, call obj->ref() - */ -template static inline void SkSafeRef(T* obj) { - if (obj) { - obj->ref(); - } -} - -/** Check if the argument is non-null, and if so, call obj->unref() - */ -template static inline void SkSafeUnref(T* obj) { - if (obj) { - obj->unref(); - } -} +class SkAutoRef : SkNoncopyable { +public: + SkAutoRef(SkRefCnt* obj) : fObj(obj) { SkSafeRef(obj); } + ~SkAutoRef() { SkSafeUnref(fObj); } +private: + SkRefCnt* fObj; +}; /** Wrapper class for SkRefCnt pointers. This manages ref/unref of a pointer to a SkRefCnt (or subclass) object.