OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2014 Google Inc. | 2 * Copyright 2014 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 SkLazyPtr_DEFINED | 8 #ifndef SkLazyPtr_DEFINED |
9 #define SkLazyPtr_DEFINED | 9 #define SkLazyPtr_DEFINED |
10 | 10 |
(...skipping 129 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
140 // - it has a constructor to zero itself; | 140 // - it has a constructor to zero itself; |
141 // - it has a destructor to clean up; | 141 // - it has a destructor to clean up; |
142 // - get() calls SkNew(T) to create the pointer; | 142 // - get() calls SkNew(T) to create the pointer; |
143 // - get(functor) calls functor to create the pointer. | 143 // - get(functor) calls functor to create the pointer. |
144 template <typename T, void (*Destroy)(T*) = Private::sk_delete<T> > | 144 template <typename T, void (*Destroy)(T*) = Private::sk_delete<T> > |
145 class SkLazyPtr : SkNoncopyable { | 145 class SkLazyPtr : SkNoncopyable { |
146 public: | 146 public: |
147 SkLazyPtr() : fPtr(NULL) {} | 147 SkLazyPtr() : fPtr(NULL) {} |
148 ~SkLazyPtr() { if (fPtr) { Destroy((T*)fPtr); } } | 148 ~SkLazyPtr() { if (fPtr) { Destroy((T*)fPtr); } } |
149 | 149 |
150 T* get() { | 150 T* get() const { |
151 T* ptr = (T*)sk_consume_load(&fPtr); | 151 T* ptr = (T*)sk_consume_load(&fPtr); |
152 return ptr ? ptr : Private::try_cas<T*, Destroy>(&fPtr, SkNEW(T)); | 152 return ptr ? ptr : Private::try_cas<T*, Destroy>(&fPtr, SkNEW(T)); |
153 } | 153 } |
154 | 154 |
155 template <typename Create> | 155 template <typename Create> |
156 T* get(const Create& create) { | 156 T* get(const Create& create) const { |
157 T* ptr = (T*)sk_consume_load(&fPtr); | 157 T* ptr = (T*)sk_consume_load(&fPtr); |
158 return ptr ? ptr : Private::try_cas<T*, Destroy>(&fPtr, create()); | 158 return ptr ? ptr : Private::try_cas<T*, Destroy>(&fPtr, create()); |
159 } | 159 } |
160 | 160 |
161 private: | 161 private: |
162 void* fPtr; | 162 mutable void* fPtr; |
163 }; | 163 }; |
164 | 164 |
165 | 165 |
166 #endif//SkLazyPtr_DEFINED | 166 #endif//SkLazyPtr_DEFINED |
OLD | NEW |