diff options
author | reed@android.com <reed@android.com@2bbb7eff-a529-9590-31e7-b0007b416f81> | 2010-04-16 20:35:47 +0000 |
---|---|---|
committer | reed@android.com <reed@android.com@2bbb7eff-a529-9590-31e7-b0007b416f81> | 2010-04-16 20:35:47 +0000 |
commit | b00cd7258c5ccf856c0dc72840e082306251b278 (patch) | |
tree | 478434cc090185cf4c7f4c5eb43d4951a5291a6f /include/core | |
parent | 51709c748e32bb115103b1468eb507f4ccaa7ee8 (diff) |
add SkRefPtr class (for testing at the moment)
git-svn-id: http://skia.googlecode.com/svn/trunk@555 2bbb7eff-a529-9590-31e7-b0007b416f81
Diffstat (limited to 'include/core')
-rw-r--r-- | include/core/SkRefCnt.h | 44 |
1 files changed, 44 insertions, 0 deletions
diff --git a/include/core/SkRefCnt.h b/include/core/SkRefCnt.h index 7e325e0002..956397a832 100644 --- a/include/core/SkRefCnt.h +++ b/include/core/SkRefCnt.h @@ -147,5 +147,49 @@ template <typename T> static inline void SkSafeUnref(T* obj) { } } +template <typename T> +class SkRefPtr { +public: + SkRefPtr() : fObj(NULL) {} + SkRefPtr(T* obj) : fObj(obj) { SkSafeRef(fObj); } + SkRefPtr(const SkRefPtr& o) : fObj(o.fObj) { SkSafeRef(fObj); } + ~SkRefPtr() { SkSafeUnref(fObj); } + + SkRefPtr& operator=(const SkRefPtr& rp) { + SkRefCnt_SafeAssign(fObj, rp.fObj); + return *this; + } + SkRefPtr& operator=(T* obj) { + SkRefCnt_SafeAssign(fObj, obj); + return *this; + } + + bool operator==(const SkRefPtr& rp) const { return fObj == rp.fObj; } + bool operator==(const T* obj) const { return fObj == obj; } + bool operator!=(const SkRefPtr& rp) const { return fObj != rp.fObj; } + bool operator!=(const T* obj) const { return fObj != obj; } + + T* get() const { return fObj; } + T& operator*() const { return *fObj; } + T* operator->() const { return fObj; } + bool operator!() const { return !fObj; } + + typedef T* SkRefPtr::*unspecified_bool_type; + operator unspecified_bool_type() const { return fObj ? &SkRefPtr::fObj : NULL; } + +private: + T* fObj; +}; + +template <typename T> +inline bool operator==(T* obj, const SkRefPtr<T>& rp) { + return obj == rp.get(); +} + +template <typename T> +inline bool operator!=(T* obj, const SkRefPtr<T>& rp) { + return obj != rp.get(); +} + #endif |