Chromium Code Reviews| 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 #include <utility> |
| 15 | 15 |
| 16 /** | 16 /** |
| 17 * 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 |
| 18 * (if ever). | 18 * (if ever). |
| 19 */ | 19 */ |
| 20 template <typename T> class SkTLazy { | 20 template <typename T> class SkTLazy { |
| 21 public: | 21 public: |
| 22 SkTLazy() : fPtr(nullptr) {} | 22 SkTLazy() : fPtr(nullptr) {} |
| 23 | 23 |
| 24 explicit SkTLazy(const T* src) | 24 explicit SkTLazy(const T* src) { |
| 25 : fPtr(src ? new (fStorage.get()) T(*src) : nullptr) {} | 25 // Not in initializer list because it depends on fStorage. |
|
mtklein
2016/08/24 01:19:28
Alternatively, swap the order of the member variab
f(malita)
2016/08/24 13:47:29
Done.
| |
| 26 fPtr = src ? new (fStorage.get()) T(*src) : nullptr; | |
| 27 } | |
| 26 | 28 |
| 27 SkTLazy(const SkTLazy& src) : fPtr(nullptr) { *this = src; } | 29 SkTLazy(const SkTLazy& src) : fPtr(nullptr) { *this = src; } |
| 28 | 30 |
| 29 ~SkTLazy() { | 31 ~SkTLazy() { |
| 30 if (this->isValid()) { | 32 if (this->isValid()) { |
| 31 fPtr->~T(); | 33 fPtr->~T(); |
| 32 } | 34 } |
| 33 } | 35 } |
| 34 | 36 |
| 35 SkTLazy& operator=(const SkTLazy& src) { | 37 SkTLazy& operator=(const SkTLazy& src) { |
| (...skipping 128 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 164 operator const T*() const { return fObj; } | 166 operator const T*() const { return fObj; } |
| 165 | 167 |
| 166 const T& operator *() const { return *fObj; } | 168 const T& operator *() const { return *fObj; } |
| 167 | 169 |
| 168 private: | 170 private: |
| 169 const T* fObj; | 171 const T* fObj; |
| 170 SkTLazy<T> fLazy; | 172 SkTLazy<T> fLazy; |
| 171 }; | 173 }; |
| 172 | 174 |
| 173 #endif | 175 #endif |
| OLD | NEW |