add SkRefPtr class (for testing at the moment)

git-svn-id: http://skia.googlecode.com/svn/trunk@555 2bbb7eff-a529-9590-31e7-b0007b416f81
This commit is contained in:
reed@android.com 2010-04-16 20:35:47 +00:00
parent 51709c748e
commit b00cd7258c
2 changed files with 86 additions and 0 deletions

View File

@ -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

View File

@ -1,9 +1,50 @@
#include "Test.h"
#include "SkRandom.h"
#include "SkRefCnt.h"
#include "SkTSearch.h"
#include "SkTSort.h"
#include "SkUtils.h"
class RefClass : public SkRefCnt {
public:
RefClass(int n) : fN(n) {}
int get() const { return fN; }
private:
int fN;
};
static void test_refptr(skiatest::Reporter* reporter) {
RefClass* r0 = new RefClass(0);
SkRefPtr<RefClass> rc0;
REPORTER_ASSERT(reporter, rc0.get() == NULL);
REPORTER_ASSERT(reporter, !rc0);
SkRefPtr<RefClass> rc1;
REPORTER_ASSERT(reporter, rc0 == rc1);
REPORTER_ASSERT(reporter, rc0 != r0);
rc0 = r0;
REPORTER_ASSERT(reporter, rc0);
REPORTER_ASSERT(reporter, rc0 != rc1);
REPORTER_ASSERT(reporter, rc0 == r0);
rc1 = rc0;
REPORTER_ASSERT(reporter, rc1);
REPORTER_ASSERT(reporter, rc0 == rc1);
REPORTER_ASSERT(reporter, rc0 == r0);
rc0 = NULL;
REPORTER_ASSERT(reporter, rc0.get() == NULL);
REPORTER_ASSERT(reporter, !rc0);
REPORTER_ASSERT(reporter, rc0 != rc1);
r0->unref();
}
///////////////////////////////////////////////////////////////////////////////
#define kSEARCH_COUNT 91
static void test_search(skiatest::Reporter* reporter) {
@ -103,6 +144,7 @@ static void TestUTF(skiatest::Reporter* reporter) {
test_utf16(reporter);
test_search(reporter);
test_refptr(reporter);
}
#include "TestClassDef.h"