OLD | NEW |
1 | |
2 /* | 1 /* |
3 * Copyright 2011 Google Inc. | 2 * Copyright 2011 Google Inc. |
4 * | 3 * |
5 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
6 * found in the LICENSE file. | 5 * found in the LICENSE file. |
7 */ | 6 */ |
8 | 7 |
9 | |
10 | |
11 #ifndef SkTLazy_DEFINED | 8 #ifndef SkTLazy_DEFINED |
12 #define SkTLazy_DEFINED | 9 #define SkTLazy_DEFINED |
13 | 10 |
| 11 #include "SkTemplates.h" |
14 #include "SkTypes.h" | 12 #include "SkTypes.h" |
15 #include <new> | 13 #include <new> |
16 | 14 |
17 template <typename T> class SkTLazy; | |
18 template <typename T> void* operator new(size_t, SkTLazy<T>* lazy); | |
19 | |
20 /** | 15 /** |
21 * Efficient way to defer allocating/initializing a class until it is needed | 16 * Efficient way to defer allocating/initializing a class until it is needed |
22 * (if ever). | 17 * (if ever). |
23 */ | 18 */ |
24 template <typename T> class SkTLazy { | 19 template <typename T> class SkTLazy { |
25 public: | 20 public: |
26 SkTLazy() : fPtr(NULL) {} | 21 SkTLazy() : fPtr(NULL) {} |
27 | 22 |
28 explicit SkTLazy(const T* src) : fPtr(NULL) { | 23 explicit SkTLazy(const T* src) : fPtr(NULL) { |
29 if (src) { | 24 if (src) { |
30 fPtr = new (fStorage) T(*src); | 25 fPtr = new (fStorage.get()) T(*src); |
31 } | 26 } |
32 } | 27 } |
33 | 28 |
34 SkTLazy(const SkTLazy<T>& src) : fPtr(NULL) { | 29 SkTLazy(const SkTLazy<T>& src) : fPtr(NULL) { |
35 if (src.isValid()) { | 30 if (src.isValid()) { |
36 fPtr = new (fStorage) T(*src->get()); | 31 fPtr = new (fStorage.get()) T(*src->get()); |
37 } else { | 32 } else { |
38 fPtr = NULL; | 33 fPtr = NULL; |
39 } | 34 } |
40 } | 35 } |
41 | 36 |
42 ~SkTLazy() { | 37 ~SkTLazy() { |
43 if (this->isValid()) { | 38 if (this->isValid()) { |
44 fPtr->~T(); | 39 fPtr->~T(); |
45 } | 40 } |
46 } | 41 } |
47 | 42 |
48 /** | 43 /** |
49 * Return a pointer to a default-initialized instance of the class. If a | 44 * Return a pointer to an instance of the class initialized with 'args'. |
50 * previous instance had been initialized (either from init() or set()) it | 45 * If a previous instance had been initialized (either from init() or |
51 * will first be destroyed, so that a freshly initialized instance is | 46 * set()) it will first be destroyed, so that a freshly initialized |
52 * always returned. | 47 * instance is always returned. |
53 */ | 48 */ |
54 T* init() { | 49 template <typename... Args> T* init(Args&&... args) { |
55 if (this->isValid()) { | 50 if (this->isValid()) { |
56 fPtr->~T(); | 51 fPtr->~T(); |
57 } | 52 } |
58 fPtr = new (SkTCast<T*>(fStorage)) T; | 53 fPtr = new (SkTCast<T*>(fStorage.get())) T(skstd::forward<Args>(args)...
); |
59 return fPtr; | 54 return fPtr; |
60 } | 55 } |
61 | 56 |
62 /** | 57 /** |
63 * Copy src into this, and return a pointer to a copy of it. Note this | 58 * Copy src into this, and return a pointer to a copy of it. Note this |
64 * will always return the same pointer, so if it is called on a lazy that | 59 * will always return the same pointer, so if it is called on a lazy that |
65 * has already been initialized, then this will copy over the previous | 60 * has already been initialized, then this will copy over the previous |
66 * contents. | 61 * contents. |
67 */ | 62 */ |
68 T* set(const T& src) { | 63 T* set(const T& src) { |
69 if (this->isValid()) { | 64 if (this->isValid()) { |
70 *fPtr = src; | 65 *fPtr = src; |
71 } else { | 66 } else { |
72 fPtr = new (SkTCast<T*>(fStorage)) T(src); | 67 fPtr = new (SkTCast<T*>(fStorage.get())) T(src); |
73 } | 68 } |
74 return fPtr; | 69 return fPtr; |
75 } | 70 } |
76 | 71 |
77 /** | 72 /** |
78 * Destroy the lazy object (if it was created via init() or set()) | 73 * Destroy the lazy object (if it was created via init() or set()) |
79 */ | 74 */ |
80 void reset() { | 75 void reset() { |
81 if (this->isValid()) { | 76 if (this->isValid()) { |
82 fPtr->~T(); | 77 fPtr->~T(); |
(...skipping 13 matching lines...) Expand all Loading... |
96 */ | 91 */ |
97 T* get() const { SkASSERT(this->isValid()); return fPtr; } | 92 T* get() const { SkASSERT(this->isValid()); return fPtr; } |
98 | 93 |
99 /** | 94 /** |
100 * Like above but doesn't assert if object isn't initialized (in which case | 95 * Like above but doesn't assert if object isn't initialized (in which case |
101 * NULL is returned). | 96 * NULL is returned). |
102 */ | 97 */ |
103 T* getMaybeNull() const { return fPtr; } | 98 T* getMaybeNull() const { return fPtr; } |
104 | 99 |
105 private: | 100 private: |
106 friend void* operator new<T>(size_t, SkTLazy* lazy); | 101 T* fPtr; // NULL or fStorage |
107 | 102 SkAlignedSTStorage<1, T> fStorage; |
108 T* fPtr; // NULL or fStorage | |
109 char fStorage[sizeof(T)]; | |
110 }; | 103 }; |
111 | 104 |
112 // Use the below macro (SkNEW_IN_TLAZY) rather than calling this directly | |
113 template <typename T> void* operator new(size_t, SkTLazy<T>* lazy) { | |
114 SkASSERT(!lazy->isValid()); | |
115 lazy->fPtr = reinterpret_cast<T*>(lazy->fStorage); | |
116 return lazy->fPtr; | |
117 } | |
118 | |
119 // Skia doesn't use C++ exceptions but it may be compiled with them enabled. Hav
ing an op delete | |
120 // to match the op new silences warnings about missing op delete when a construc
tor throws an | |
121 // exception. | |
122 template <typename T> void operator delete(void*, SkTLazy<T>*) { SK_CRASH(); } | |
123 | |
124 // Use this to construct a T inside an SkTLazy using a non-default constructor. | |
125 #define SkNEW_IN_TLAZY(tlazy_ptr, type_name, args) (new (tlazy_ptr) type_name ar
gs) | |
126 | |
127 /** | 105 /** |
128 * A helper built on top of SkTLazy to do copy-on-first-write. The object is ini
tialized | 106 * A helper built on top of SkTLazy to do copy-on-first-write. The object is ini
tialized |
129 * with a const pointer but provides a non-const pointer accessor. The first tim
e the | 107 * with a const pointer but provides a non-const pointer accessor. The first tim
e the |
130 * accessor is called (if ever) the object is cloned. | 108 * accessor is called (if ever) the object is cloned. |
131 * | 109 * |
132 * In the following example at most one copy of constThing is made: | 110 * In the following example at most one copy of constThing is made: |
133 * | 111 * |
134 * SkTCopyOnFirstWrite<Thing> thing(&constThing); | 112 * SkTCopyOnFirstWrite<Thing> thing(&constThing); |
135 * ... | 113 * ... |
136 * function_that_takes_a_const_thing_ptr(thing); // constThing is passed | 114 * function_that_takes_a_const_thing_ptr(thing); // constThing is passed |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
183 operator const T*() const { return fObj; } | 161 operator const T*() const { return fObj; } |
184 | 162 |
185 const T& operator *() const { return *fObj; } | 163 const T& operator *() const { return *fObj; } |
186 | 164 |
187 private: | 165 private: |
188 const T* fObj; | 166 const T* fObj; |
189 SkTLazy<T> fLazy; | 167 SkTLazy<T> fLazy; |
190 }; | 168 }; |
191 | 169 |
192 #endif | 170 #endif |
OLD | NEW |