aboutsummaryrefslogtreecommitdiffhomepage
path: root/include/core/SkTLazy.h
diff options
context:
space:
mode:
authorGravatar bungeman <bungeman@google.com>2015-08-12 13:37:16 -0700
committerGravatar Commit bot <commit-bot@chromium.org>2015-08-12 13:37:17 -0700
commit72440a3785c13b8ec539d7e11bea1124eeddecbd (patch)
treeb326003afce201c5cc68e9dc3c78d110ece72ca3 /include/core/SkTLazy.h
parent65bd7ae3741847d1f01bcf5266f42ff9e371952d (diff)
Use forwarding with SkTLazy::init.
This allows removal the difficult to use (and so currently unused) placement new and related macros to allow any constructor of T to be used to initilize the storage of SkTLazy. This also properly aligns the SkTLazy storage. Review URL: https://codereview.chromium.org/1283183003
Diffstat (limited to 'include/core/SkTLazy.h')
-rw-r--r--include/core/SkTLazy.h46
1 files changed, 12 insertions, 34 deletions
diff --git a/include/core/SkTLazy.h b/include/core/SkTLazy.h
index a1dc0013fc..61c2b181ac 100644
--- a/include/core/SkTLazy.h
+++ b/include/core/SkTLazy.h
@@ -1,4 +1,3 @@
-
/*
* Copyright 2011 Google Inc.
*
@@ -6,17 +5,13 @@
* found in the LICENSE file.
*/
-
-
#ifndef SkTLazy_DEFINED
#define SkTLazy_DEFINED
+#include "SkTemplates.h"
#include "SkTypes.h"
#include <new>
-template <typename T> class SkTLazy;
-template <typename T> void* operator new(size_t, SkTLazy<T>* lazy);
-
/**
* Efficient way to defer allocating/initializing a class until it is needed
* (if ever).
@@ -27,13 +22,13 @@ public:
explicit SkTLazy(const T* src) : fPtr(NULL) {
if (src) {
- fPtr = new (fStorage) T(*src);
+ fPtr = new (fStorage.get()) T(*src);
}
}
SkTLazy(const SkTLazy<T>& src) : fPtr(NULL) {
if (src.isValid()) {
- fPtr = new (fStorage) T(*src->get());
+ fPtr = new (fStorage.get()) T(*src->get());
} else {
fPtr = NULL;
}
@@ -46,16 +41,16 @@ public:
}
/**
- * Return a pointer to a default-initialized instance of the class. If a
- * previous instance had been initialized (either from init() or set()) it
- * will first be destroyed, so that a freshly initialized instance is
- * always returned.
+ * Return a pointer to an instance of the class initialized with 'args'.
+ * If a previous instance had been initialized (either from init() or
+ * set()) it will first be destroyed, so that a freshly initialized
+ * instance is always returned.
*/
- T* init() {
+ template <typename... Args> T* init(Args&&... args) {
if (this->isValid()) {
fPtr->~T();
}
- fPtr = new (SkTCast<T*>(fStorage)) T;
+ fPtr = new (SkTCast<T*>(fStorage.get())) T(skstd::forward<Args>(args)...);
return fPtr;
}
@@ -69,7 +64,7 @@ public:
if (this->isValid()) {
*fPtr = src;
} else {
- fPtr = new (SkTCast<T*>(fStorage)) T(src);
+ fPtr = new (SkTCast<T*>(fStorage.get())) T(src);
}
return fPtr;
}
@@ -103,27 +98,10 @@ public:
T* getMaybeNull() const { return fPtr; }
private:
- friend void* operator new<T>(size_t, SkTLazy* lazy);
-
- T* fPtr; // NULL or fStorage
- char fStorage[sizeof(T)];
+ T* fPtr; // NULL or fStorage
+ SkAlignedSTStorage<1, T> fStorage;
};
-// Use the below macro (SkNEW_IN_TLAZY) rather than calling this directly
-template <typename T> void* operator new(size_t, SkTLazy<T>* lazy) {
- SkASSERT(!lazy->isValid());
- lazy->fPtr = reinterpret_cast<T*>(lazy->fStorage);
- return lazy->fPtr;
-}
-
-// Skia doesn't use C++ exceptions but it may be compiled with them enabled. Having an op delete
-// to match the op new silences warnings about missing op delete when a constructor throws an
-// exception.
-template <typename T> void operator delete(void*, SkTLazy<T>*) { SK_CRASH(); }
-
-// Use this to construct a T inside an SkTLazy using a non-default constructor.
-#define SkNEW_IN_TLAZY(tlazy_ptr, type_name, args) (new (tlazy_ptr) type_name args)
-
/**
* A helper built on top of SkTLazy to do copy-on-first-write. The object is initialized
* with a const pointer but provides a non-const pointer accessor. The first time the