SkLazyPtr follow ups

- moves test to LazyPtrTest.cpp
 - adds the ability to use a Create() method with an argument

BUG=skia:

Review URL: https://codereview.chromium.org/669783002
This commit is contained in:
mtklein 2014-10-21 12:20:04 -07:00 committed by Commit bot
parent 183e98559c
commit e9e0dea15b
3 changed files with 60 additions and 15 deletions

View File

@ -130,7 +130,7 @@
'../tests/LListTest.cpp',
'../tests/LayerDrawLooperTest.cpp',
'../tests/LayerRasterizerTest.cpp',
'../tests/LazyPtr.cpp',
'../tests/LazyPtrTest.cpp',
'../tests/MD5Test.cpp',
'../tests/MallocPixelRefTest.cpp',
'../tests/MathTest.cpp',

View File

@ -49,10 +49,10 @@
*/
#define SK_DECLARE_STATIC_LAZY_PTR(T, name, ...) \
namespace {} static Private::SkLazyPtrBase<T, ##__VA_ARGS__> name
namespace {} static Private::SkStaticLazyPtr<T, ##__VA_ARGS__> name
#define SK_DECLARE_STATIC_LAZY_PTR_ARRAY(T, name, N, ...) \
namespace {} static Private::SkLazyPtrArray<T, N, ##__VA_ARGS__> name
namespace {} static Private::SkStaticLazyPtrArray<T, N, ##__VA_ARGS__> name
// namespace {} forces these macros to only be legal in global scopes. Chrome has thread-safety
// problems with them in function-local statics because it uses -fno-threadsafe-statics, and even
@ -102,7 +102,7 @@ template <typename T> void sk_delete(T* ptr) { SkDELETE(ptr); }
// This has no constructor and must be zero-initalized (the macro above does this).
template <typename T, T* (*Create)() = sk_new<T>, void (*Destroy)(T*) = sk_delete<T> >
class SkLazyPtrBase {
class SkStaticLazyPtr {
public:
T* get() {
// If fPtr has already been filled, we need a consume barrier when loading it.
@ -111,7 +111,7 @@ public:
return ptr ? ptr : try_cas<T*, Destroy>(&fPtr, Create());
}
protected:
private:
void* fPtr;
};
@ -119,7 +119,7 @@ template <typename T> T* sk_new_arg(int i) { return SkNEW_ARGS(T, (i)); }
// This has no constructor and must be zero-initalized (the macro above does this).
template <typename T, int N, T* (*Create)(int) = sk_new_arg<T>, void (*Destroy)(T*) = sk_delete<T> >
class SkLazyPtrArray {
class SkStaticLazyPtrArray {
public:
T* operator[](int i) {
SkASSERT(i >= 0 && i < N);
@ -136,16 +136,30 @@ private:
} // namespace Private
// This version is suitable for use as a class member.
// It's the same as above except it has a constructor to zero itself and a destructor to clean up.
template <typename T,
T* (*Create)() = Private::sk_new<T>,
void (*Destroy)(T*) = Private::sk_delete<T> >
class SkLazyPtr : public Private::SkLazyPtrBase<T, Create, Destroy> {
// It's much the same as above except:
// - it has a constructor to zero itself;
// - it has a destructor to clean up;
// - get() calls SkNew(T) to create the pointer;
// - get(functor) calls functor to create the pointer.
template <typename T, void (*Destroy)(T*) = Private::sk_delete<T> >
class SkLazyPtr : SkNoncopyable {
public:
SkLazyPtr() { INHERITED::fPtr = NULL; }
~SkLazyPtr() { if (INHERITED::fPtr) { Destroy((T*)INHERITED::fPtr); } }
SkLazyPtr() : fPtr(NULL) {}
~SkLazyPtr() { if (fPtr) { Destroy((T*)fPtr); } }
T* get() {
T* ptr = (T*)sk_consume_load(&fPtr);
return ptr ? ptr : Private::try_cas<T*, Destroy>(&fPtr, SkNEW(T));
}
template <typename Create>
T* get(const Create& create) {
T* ptr = (T*)sk_consume_load(&fPtr);
return ptr ? ptr : Private::try_cas<T*, Destroy>(&fPtr, create());
}
private:
typedef Private::SkLazyPtrBase<T, Create, Destroy> INHERITED;
void* fPtr;
};

View File

@ -2,14 +2,45 @@
#include "SkLazyPtr.h"
#include "SkTaskGroup.h"
namespace {
struct CreateIntFromFloat {
CreateIntFromFloat(float val) : fVal(val) {}
int* operator()() const { return SkNEW_ARGS(int, ((int)fVal)); }
float fVal;
};
// As a template argument this must have external linkage.
void custom_destroy(int* ptr) { *ptr = 99; }
} // namespace
DEF_TEST(LazyPtr, r) {
// Basic usage: calls SkNEW(int).
SkLazyPtr<int> lazy;
int* ptr = lazy.get();
REPORTER_ASSERT(r, ptr);
REPORTER_ASSERT(r, lazy.get() == ptr);
// Advanced usage: calls a functor.
SkLazyPtr<int> lazyFunctor;
int* six = lazyFunctor.get(CreateIntFromFloat(6.4f));
REPORTER_ASSERT(r, six);
REPORTER_ASSERT(r, 6 == *six);
// Just makes sure this is safe.
SkLazyPtr<double> neverRead;
// SkLazyPtr supports custom destroy methods.
{
SkLazyPtr<int, custom_destroy> customDestroy;
ptr = customDestroy.get();
// custom_destroy called here.
}
REPORTER_ASSERT(r, ptr);
REPORTER_ASSERT(r, 99 == *ptr);
// Since custom_destroy didn't actually delete ptr, we do now.
SkDELETE(ptr);
}
namespace {