add validate() and SkAutoRef

git-svn-id: http://skia.googlecode.com/svn/trunk@1872 2bbb7eff-a529-9590-31e7-b0007b416f81
This commit is contained in:
reed@google.com 2011-07-15 15:25:22 +00:00
parent e36ddf0131
commit 7f6d6d4571

View File

@ -63,10 +63,46 @@ public:
} }
} }
void validate() const {
SkASSERT(fRefCnt > 0);
}
private: private:
mutable int32_t fRefCnt; 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 <typename T> static inline void SkSafeRef(T* obj) {
if (obj) {
obj->ref();
}
}
/** Check if the argument is non-null, and if so, call obj->unref()
*/
template <typename T> static inline void SkSafeUnref(T* obj) {
if (obj) {
obj->unref();
}
}
///////////////////////////////////////////////////////////////////////////////
/** /**
* Utility class that simply unref's its argument in the destructor. * Utility class that simply unref's its argument in the destructor.
*/ */
@ -98,35 +134,13 @@ public:
SkAutoUnref(SkRefCnt* obj) : SkAutoTUnref<SkRefCnt>(obj) {} SkAutoUnref(SkRefCnt* obj) : SkAutoTUnref<SkRefCnt>(obj) {}
}; };
/////////////////////////////////////////////////////////////////////////////// class SkAutoRef : SkNoncopyable {
public:
/** Helper macro to safely assign one SkRefCnt[TS]* to another, checking for SkAutoRef(SkRefCnt* obj) : fObj(obj) { SkSafeRef(obj); }
null in on each side of the assignment, and ensuring that ref() is called ~SkAutoRef() { SkSafeUnref(fObj); }
before unref(), in case the two pointers point to the same object. private:
*/ SkRefCnt* fObj;
#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 <typename T> static inline void SkSafeRef(T* obj) {
if (obj) {
obj->ref();
}
}
/** Check if the argument is non-null, and if so, call obj->unref()
*/
template <typename T> static inline void SkSafeUnref(T* obj) {
if (obj) {
obj->unref();
}
}
/** Wrapper class for SkRefCnt pointers. This manages ref/unref of a pointer to /** Wrapper class for SkRefCnt pointers. This manages ref/unref of a pointer to
a SkRefCnt (or subclass) object. a SkRefCnt (or subclass) object.