| 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 |
| (...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 124 * thing.writable()->changeMe(); // makes a copy of constThing if we didn't c
all modifyMe() | 124 * thing.writable()->changeMe(); // makes a copy of constThing if we didn't c
all modifyMe() |
| 125 * } | 125 * } |
| 126 * | 126 * |
| 127 * consume_a_thing(thing); // could be constThing or a modified copy. | 127 * consume_a_thing(thing); // could be constThing or a modified copy. |
| 128 */ | 128 */ |
| 129 template <typename T> | 129 template <typename T> |
| 130 class SkTCopyOnFirstWrite { | 130 class SkTCopyOnFirstWrite { |
| 131 public: | 131 public: |
| 132 SkTCopyOnFirstWrite(const T& initial) : fObj(&initial) {} | 132 SkTCopyOnFirstWrite(const T& initial) : fObj(&initial) {} |
| 133 | 133 |
| 134 SkTCopyOnFirstWrite(const T* initial) : fObj(initial) {} |
| 135 |
| 134 // Constructor for delayed initialization. | 136 // Constructor for delayed initialization. |
| 135 SkTCopyOnFirstWrite() : fObj(NULL) {} | 137 SkTCopyOnFirstWrite() : fObj(NULL) {} |
| 136 | 138 |
| 137 // Should only be called once, and only if the default constructor was used. | 139 // Should only be called once, and only if the default constructor was used. |
| 138 void init(const T& initial) { | 140 void init(const T& initial) { |
| 139 SkASSERT(NULL == fObj); | 141 SkASSERT(NULL == fObj); |
| 140 SkASSERT(!fLazy.isValid()); | 142 SkASSERT(!fLazy.isValid()); |
| 141 fObj = &initial; | 143 fObj = &initial; |
| 142 } | 144 } |
| 143 | 145 |
| (...skipping 18 matching lines...) Expand all Loading... |
| 162 operator const T*() const { return fObj; } | 164 operator const T*() const { return fObj; } |
| 163 | 165 |
| 164 const T& operator *() const { return *fObj; } | 166 const T& operator *() const { return *fObj; } |
| 165 | 167 |
| 166 private: | 168 private: |
| 167 const T* fObj; | 169 const T* fObj; |
| 168 SkTLazy<T> fLazy; | 170 SkTLazy<T> fLazy; |
| 169 }; | 171 }; |
| 170 | 172 |
| 171 #endif | 173 #endif |
| OLD | NEW |