Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / third_party / skia / include / core / SkTLazy.h
1
2 /*
3  * Copyright 2011 Google Inc.
4  *
5  * Use of this source code is governed by a BSD-style license that can be
6  * found in the LICENSE file.
7  */
8
9
10
11 #ifndef SkTLazy_DEFINED
12 #define SkTLazy_DEFINED
13
14 #include "SkTypes.h"
15 #include <new>
16
17 template <typename T> class SkTLazy;
18 template <typename T> void* operator new(size_t, SkTLazy<T>* lazy);
19
20 /**
21  *  Efficient way to defer allocating/initializing a class until it is needed
22  *  (if ever).
23  */
24 template <typename T> class SkTLazy {
25 public:
26     SkTLazy() : fPtr(NULL) {}
27
28     explicit SkTLazy(const T* src) : fPtr(NULL) {
29         if (src) {
30             fPtr = new (fStorage) T(*src);
31         }
32     }
33
34     SkTLazy(const SkTLazy<T>& src) : fPtr(NULL) {
35         if (src.isValid()) {
36             fPtr = new (fStorage) T(*src->get());
37         } else {
38             fPtr = NULL;
39         }
40     }
41
42     ~SkTLazy() {
43         if (this->isValid()) {
44             fPtr->~T();
45         }
46     }
47
48     /**
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
52      *  always returned.
53      */
54     T* init() {
55         if (this->isValid()) {
56             fPtr->~T();
57         }
58         fPtr = new (SkTCast<T*>(fStorage)) T;
59         return fPtr;
60     }
61
62     /**
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
66      *  contents.
67      */
68     T* set(const T& src) {
69         // Diagnoistic. May remove later. See crbug.com/364224
70         if (NULL == &src) {
71             sk_throw();
72         }
73
74         if (this->isValid()) {
75             *fPtr = src;
76         } else {
77             fPtr = new (SkTCast<T*>(fStorage)) T(src);
78         }
79         return fPtr;
80     }
81
82     /**
83      * Destroy the lazy object (if it was created via init() or set())
84      */
85     void reset() {
86         if (this->isValid()) {
87             fPtr->~T();
88             fPtr = NULL;
89         }
90     }
91
92     /**
93      *  Returns true if a valid object has been initialized in the SkTLazy,
94      *  false otherwise.
95      */
96     bool isValid() const { return NULL != fPtr; }
97
98     /**
99      * Returns the object. This version should only be called when the caller
100      * knows that the object has been initialized.
101      */
102     T* get() const { SkASSERT(this->isValid()); return fPtr; }
103
104     /**
105      * Like above but doesn't assert if object isn't initialized (in which case
106      * NULL is returned).
107      */
108     T* getMaybeNull() const { return fPtr; }
109
110 private:
111     friend void* operator new<T>(size_t, SkTLazy* lazy);
112
113     T*   fPtr; // NULL or fStorage
114     char fStorage[sizeof(T)];
115 };
116
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);
121     return lazy->fPtr;
122 }
123
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
126 // exception.
127 template <typename T> void operator delete(void*, SkTLazy<T>*) { SK_CRASH(); }
128
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)
131
132 /**
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.
136  *
137  * In the following example at most one copy of constThing is made:
138  *
139  * SkTCopyOnFirstWrite<Thing> thing(&constThing);
140  * ...
141  * function_that_takes_a_const_thing_ptr(thing); // constThing is passed
142  * ...
143  * if (need_to_modify_thing()) {
144  *    thing.writable()->modifyMe(); // makes a copy of constThing
145  * }
146  * ...
147  * x = thing->readSomething();
148  * ...
149  * if (need_to_modify_thing_now()) {
150  *    thing.writable()->changeMe(); // makes a copy of constThing if we didn't call modifyMe()
151  * }
152  *
153  * consume_a_thing(thing); // could be constThing or a modified copy.
154  */
155 template <typename T>
156 class SkTCopyOnFirstWrite {
157 public:
158     SkTCopyOnFirstWrite(const T& initial) : fObj(&initial) {}
159
160     // Constructor for delayed initialization.
161     SkTCopyOnFirstWrite() : fObj(NULL) {}
162
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());
167         fObj = &initial;
168     }
169
170     /**
171      * Returns a writable T*. The first time this is called the initial object is cloned.
172      */
173     T* writable() {
174         SkASSERT(NULL != fObj);
175         if (!fLazy.isValid()) {
176             fLazy.set(*fObj);
177             fObj = fLazy.get();
178         }
179         return const_cast<T*>(fObj);
180     }
181
182     /**
183      * Operators for treating this as though it were a const pointer.
184      */
185
186     const T *operator->() const { return fObj; }
187
188     operator const T*() const { return fObj; }
189
190     const T& operator *() const { return *fObj; }
191
192 private:
193     const T*    fObj;
194     SkTLazy<T>  fLazy;
195 };
196
197 #endif