Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(164)

Unified Diff: src/core/SkLazyPtr.h

Issue 326413003: SkNEW as default Create() for SkLazyPtr macros. (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: update exemption Created 6 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | src/ports/SkFontConfigInterface_direct.cpp » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: src/core/SkLazyPtr.h
diff --git a/src/core/SkLazyPtr.h b/src/core/SkLazyPtr.h
index 50d8328cd5909e1729d4103ec325d3824bc10be1..c25d3c81202c5657262a476d0056bc8f59e10dc3 100644
--- a/src/core/SkLazyPtr.h
+++ b/src/core/SkLazyPtr.h
@@ -12,23 +12,25 @@
*
* Example usage:
*
- * Foo* CreateFoo() { return SkNEW(Foo); }
* Foo* GetSingletonFoo() {
- * SK_DECLARE_STATIC_LAZY_PTR(Foo, singleton, CreateFoo); // Clean up with SkDELETE.
+ * SK_DECLARE_STATIC_LAZY_PTR(Foo, singleton); // Created with SkNEW, destroyed with SkDELETE.
* return singleton.get();
* }
*
- * These macros take an optional void (*Destroy)(T*) at the end. If not given, we'll use SkDELETE.
- * This option is most useful when T doesn't have a public destructor.
+ * These macros take an optional T* (*Create)() and void (*Destroy)(T*) at the end.
+ * If not given, we'll use SkNEW and SkDELETE.
+ * These options are most useful when T doesn't have a public constructor or destructor.
+ * Create comes first, so you may use a custom Create with a default Destroy, but not vice versa.
*
- * void CustomCleanup(Foo* ptr) { ... }
+ * Foo* CustomCreate() { return ...; }
+ * void CustomDestroy(Foo* ptr) { ... }
* Foo* GetSingletonFooWithCustomCleanup() {
- * SK_DECLARE_STATIC_LAZY_PTR(Foo, singleton, CreateFoo, CustomCleanup);
+ * SK_DECLARE_STATIC_LAZY_PTR(Foo, singleton, CustomCreate, CustomDestroy);
* return singleton.get();
* }
*
* If you have a bunch of related static pointers of the same type, you can
- * declare an array of lazy pointers together:
+ * declare an array of lazy pointers together, and we'll pass the index to Create().
*
* Foo* CreateFoo(int i) { return ...; }
* Foo* GetCachedFoo(Foo::Enum enumVal) {
@@ -48,11 +50,11 @@
* These macros must be used in a global or function scope, not as a class member.
*/
-#define SK_DECLARE_STATIC_LAZY_PTR(T, name, Create, ...) \
- static Private::SkLazyPtr<T, Create, ##__VA_ARGS__> name
+#define SK_DECLARE_STATIC_LAZY_PTR(T, name, ...) \
+ static Private::SkLazyPtr<T, ##__VA_ARGS__> name
-#define SK_DECLARE_STATIC_LAZY_PTR_ARRAY(T, name, N, Create, ...) \
- static Private::SkLazyPtrArray<T, N, Create, ##__VA_ARGS__> name
+#define SK_DECLARE_STATIC_LAZY_PTR_ARRAY(T, name, N, ...) \
+ static Private::SkLazyPtrArray<T, N, ##__VA_ARGS__> name
@@ -63,12 +65,10 @@
#include "SkThreadPriv.h"
// See FIXME below.
-class SkFontConfigInterface;
+class SkFontConfigInterfaceDirect;
namespace Private {
-template <typename T> void sk_delete(T* ptr) { SkDELETE(ptr); }
-
// Set *dst to ptr if *dst is NULL. Returns value of *dst, destroying ptr if not swapped in.
// Issues the same memory barriers as sk_atomic_cas: acquire on failure, release on success.
template <typename P, void (*Destroy)(P)>
@@ -85,8 +85,11 @@ static P try_cas(void** dst, P ptr) {
}
}
+template <typename T> T* sk_new() { return SkNEW(T); }
+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)(), void (*Destroy)(T*) = sk_delete<T> >
+template <typename T, T* (*Create)() = sk_new<T>, void (*Destroy)(T*) = sk_delete<T> >
class SkLazyPtr {
public:
T* get() {
@@ -98,7 +101,7 @@ public:
#ifdef SK_DEVELOPER
// FIXME: We know we leak refs on some classes. For now, let them leak.
- void cleanup(SkFontConfigInterface*) {}
+ void cleanup(SkFontConfigInterfaceDirect*) {}
template <typename U> void cleanup(U* ptr) { Destroy(ptr); }
~SkLazyPtr() {
@@ -111,8 +114,10 @@ private:
void* fPtr;
};
+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), void (*Destroy)(T*) = sk_delete<T> >
+template <typename T, int N, T* (*Create)(int) = sk_new_arg<T>, void (*Destroy)(T*) = sk_delete<T> >
class SkLazyPtrArray {
public:
T* operator[](int i) {
« no previous file with comments | « no previous file | src/ports/SkFontConfigInterface_direct.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698