OLD | NEW |
1 | 1 |
2 /* | 2 /* |
3 * Copyright 2011 Google Inc. | 3 * Copyright 2011 Google Inc. |
4 * | 4 * |
5 * Use of this source code is governed by a BSD-style license that can be | 5 * Use of this source code is governed by a BSD-style license that can be |
6 * found in the LICENSE file. | 6 * found in the LICENSE file. |
7 */ | 7 */ |
8 | 8 |
9 | 9 |
10 | 10 |
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
81 if (this->isValid()) { | 81 if (this->isValid()) { |
82 fPtr->~T(); | 82 fPtr->~T(); |
83 fPtr = NULL; | 83 fPtr = NULL; |
84 } | 84 } |
85 } | 85 } |
86 | 86 |
87 /** | 87 /** |
88 * Returns true if a valid object has been initialized in the SkTLazy, | 88 * Returns true if a valid object has been initialized in the SkTLazy, |
89 * false otherwise. | 89 * false otherwise. |
90 */ | 90 */ |
91 bool isValid() const { return NULL != fPtr; } | 91 bool isValid() const { return SkToBool(fPtr); } |
92 | 92 |
93 /** | 93 /** |
94 * Returns the object. This version should only be called when the caller | 94 * Returns the object. This version should only be called when the caller |
95 * knows that the object has been initialized. | 95 * knows that the object has been initialized. |
96 */ | 96 */ |
97 T* get() const { SkASSERT(this->isValid()); return fPtr; } | 97 T* get() const { SkASSERT(this->isValid()); return fPtr; } |
98 | 98 |
99 /** | 99 /** |
100 * Like above but doesn't assert if object isn't initialized (in which case | 100 * Like above but doesn't assert if object isn't initialized (in which case |
101 * NULL is returned). | 101 * NULL is returned). |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
159 void init(const T& initial) { | 159 void init(const T& initial) { |
160 SkASSERT(NULL == fObj); | 160 SkASSERT(NULL == fObj); |
161 SkASSERT(!fLazy.isValid()); | 161 SkASSERT(!fLazy.isValid()); |
162 fObj = &initial; | 162 fObj = &initial; |
163 } | 163 } |
164 | 164 |
165 /** | 165 /** |
166 * Returns a writable T*. The first time this is called the initial object i
s cloned. | 166 * Returns a writable T*. The first time this is called the initial object i
s cloned. |
167 */ | 167 */ |
168 T* writable() { | 168 T* writable() { |
169 SkASSERT(NULL != fObj); | 169 SkASSERT(fObj); |
170 if (!fLazy.isValid()) { | 170 if (!fLazy.isValid()) { |
171 fLazy.set(*fObj); | 171 fLazy.set(*fObj); |
172 fObj = fLazy.get(); | 172 fObj = fLazy.get(); |
173 } | 173 } |
174 return const_cast<T*>(fObj); | 174 return const_cast<T*>(fObj); |
175 } | 175 } |
176 | 176 |
177 /** | 177 /** |
178 * Operators for treating this as though it were a const pointer. | 178 * Operators for treating this as though it were a const pointer. |
179 */ | 179 */ |
180 | 180 |
181 const T *operator->() const { return fObj; } | 181 const T *operator->() const { return fObj; } |
182 | 182 |
183 operator const T*() const { return fObj; } | 183 operator const T*() const { return fObj; } |
184 | 184 |
185 const T& operator *() const { return *fObj; } | 185 const T& operator *() const { return *fObj; } |
186 | 186 |
187 private: | 187 private: |
188 const T* fObj; | 188 const T* fObj; |
189 SkTLazy<T> fLazy; | 189 SkTLazy<T> fLazy; |
190 }; | 190 }; |
191 | 191 |
192 #endif | 192 #endif |
OLD | NEW |