3 * Copyright 2011 Google Inc.
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
11 #ifndef SkTLazy_DEFINED
12 #define SkTLazy_DEFINED
17 template <typename T> class SkTLazy;
18 template <typename T> void* operator new(size_t, SkTLazy<T>* lazy);
21 * Efficient way to defer allocating/initializing a class until it is needed
24 template <typename T> class SkTLazy {
26 SkTLazy() : fPtr(NULL) {}
28 explicit SkTLazy(const T* src) : fPtr(NULL) {
30 fPtr = new (fStorage) T(*src);
34 SkTLazy(const SkTLazy<T>& src) : fPtr(NULL) {
36 fPtr = new (fStorage) T(*src->get());
43 if (this->isValid()) {
49 * Return a pointer to a default-initialized instance of the class. If a
50 * previous instance had been initialized (either from init() or set()) it
51 * will first be destroyed, so that a freshly initialized instance is
55 if (this->isValid()) {
58 fPtr = new (SkTCast<T*>(fStorage)) T;
63 * Copy src into this, and return a pointer to a copy of it. Note this
64 * will always return the same pointer, so if it is called on a lazy that
65 * has already been initialized, then this will copy over the previous
68 T* set(const T& src) {
69 // Diagnoistic. May remove later. See crbug.com/364224
74 if (this->isValid()) {
77 fPtr = new (SkTCast<T*>(fStorage)) T(src);
83 * Destroy the lazy object (if it was created via init() or set())
86 if (this->isValid()) {
93 * Returns true if a valid object has been initialized in the SkTLazy,
96 bool isValid() const { return NULL != fPtr; }
99 * Returns the object. This version should only be called when the caller
100 * knows that the object has been initialized.
102 T* get() const { SkASSERT(this->isValid()); return fPtr; }
105 * Like above but doesn't assert if object isn't initialized (in which case
108 T* getMaybeNull() const { return fPtr; }
111 friend void* operator new<T>(size_t, SkTLazy* lazy);
113 T* fPtr; // NULL or fStorage
114 char fStorage[sizeof(T)];
117 // Use the below macro (SkNEW_IN_TLAZY) rather than calling this directly
118 template <typename T> void* operator new(size_t, SkTLazy<T>* lazy) {
119 SkASSERT(!lazy->isValid());
120 lazy->fPtr = reinterpret_cast<T*>(lazy->fStorage);
124 // Skia doesn't use C++ exceptions but it may be compiled with them enabled. Having an op delete
125 // to match the op new silences warnings about missing op delete when a constructor throws an
127 template <typename T> void operator delete(void*, SkTLazy<T>*) { SK_CRASH(); }
129 // Use this to construct a T inside an SkTLazy using a non-default constructor.
130 #define SkNEW_IN_TLAZY(tlazy_ptr, type_name, args) (new (tlazy_ptr) type_name args)
133 * A helper built on top of SkTLazy to do copy-on-first-write. The object is initialized
134 * with a const pointer but provides a non-const pointer accessor. The first time the
135 * accessor is called (if ever) the object is cloned.
137 * In the following example at most one copy of constThing is made:
139 * SkTCopyOnFirstWrite<Thing> thing(&constThing);
141 * function_that_takes_a_const_thing_ptr(thing); // constThing is passed
143 * if (need_to_modify_thing()) {
144 * thing.writable()->modifyMe(); // makes a copy of constThing
147 * x = thing->readSomething();
149 * if (need_to_modify_thing_now()) {
150 * thing.writable()->changeMe(); // makes a copy of constThing if we didn't call modifyMe()
153 * consume_a_thing(thing); // could be constThing or a modified copy.
155 template <typename T>
156 class SkTCopyOnFirstWrite {
158 SkTCopyOnFirstWrite(const T& initial) : fObj(&initial) {}
160 // Constructor for delayed initialization.
161 SkTCopyOnFirstWrite() : fObj(NULL) {}
163 // Should only be called once, and only if the default constructor was used.
164 void init(const T& initial) {
165 SkASSERT(NULL == fObj);
166 SkASSERT(!fLazy.isValid());
171 * Returns a writable T*. The first time this is called the initial object is cloned.
174 SkASSERT(NULL != fObj);
175 if (!fLazy.isValid()) {
179 return const_cast<T*>(fObj);
183 * Operators for treating this as though it were a const pointer.
186 const T *operator->() const { return fObj; }
188 operator const T*() const { return fObj; }
190 const T& operator *() const { return *fObj; }