OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2011 Google Inc. | 2 * Copyright 2011 Google Inc. |
3 * | 3 * |
4 * 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 |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 #ifndef SkTLazy_DEFINED | 8 #ifndef SkTLazy_DEFINED |
9 #define SkTLazy_DEFINED | 9 #define SkTLazy_DEFINED |
10 | 10 |
11 #include "../private/SkTemplates.h" | 11 #include "../private/SkTemplates.h" |
12 #include "SkTypes.h" | 12 #include "SkTypes.h" |
13 #include <new> | 13 #include <new> |
| 14 #include <utility> |
14 | 15 |
15 /** | 16 /** |
16 * Efficient way to defer allocating/initializing a class until it is needed | 17 * Efficient way to defer allocating/initializing a class until it is needed |
17 * (if ever). | 18 * (if ever). |
18 */ | 19 */ |
19 template <typename T> class SkTLazy { | 20 template <typename T> class SkTLazy { |
20 public: | 21 public: |
21 SkTLazy() : fPtr(NULL) {} | 22 SkTLazy() : fPtr(NULL) {} |
22 | 23 |
23 explicit SkTLazy(const T* src) : fPtr(NULL) { | 24 explicit SkTLazy(const T* src) : fPtr(NULL) { |
(...skipping 19 matching lines...) Expand all Loading... |
43 /** | 44 /** |
44 * Return a pointer to an instance of the class initialized with 'args'. | 45 * Return a pointer to an instance of the class initialized with 'args'. |
45 * If a previous instance had been initialized (either from init() or | 46 * If a previous instance had been initialized (either from init() or |
46 * set()) it will first be destroyed, so that a freshly initialized | 47 * set()) it will first be destroyed, so that a freshly initialized |
47 * instance is always returned. | 48 * instance is always returned. |
48 */ | 49 */ |
49 template <typename... Args> T* init(Args&&... args) { | 50 template <typename... Args> T* init(Args&&... args) { |
50 if (this->isValid()) { | 51 if (this->isValid()) { |
51 fPtr->~T(); | 52 fPtr->~T(); |
52 } | 53 } |
53 fPtr = new (SkTCast<T*>(fStorage.get())) T(skstd::forward<Args>(args)...
); | 54 fPtr = new (SkTCast<T*>(fStorage.get())) T(std::forward<Args>(args)...); |
54 return fPtr; | 55 return fPtr; |
55 } | 56 } |
56 | 57 |
57 /** | 58 /** |
58 * Copy src into this, and return a pointer to a copy of it. Note this | 59 * Copy src into this, and return a pointer to a copy of it. Note this |
59 * will always return the same pointer, so if it is called on a lazy that | 60 * will always return the same pointer, so if it is called on a lazy that |
60 * has already been initialized, then this will copy over the previous | 61 * has already been initialized, then this will copy over the previous |
61 * contents. | 62 * contents. |
62 */ | 63 */ |
63 T* set(const T& src) { | 64 T* set(const T& src) { |
(...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
161 operator const T*() const { return fObj; } | 162 operator const T*() const { return fObj; } |
162 | 163 |
163 const T& operator *() const { return *fObj; } | 164 const T& operator *() const { return *fObj; } |
164 | 165 |
165 private: | 166 private: |
166 const T* fObj; | 167 const T* fObj; |
167 SkTLazy<T> fLazy; | 168 SkTLazy<T> fLazy; |
168 }; | 169 }; |
169 | 170 |
170 #endif | 171 #endif |
OLD | NEW |