| 1 |
/* |
| 2 |
* Copyright 2011 Google Inc. |
| 3 |
* |
| 4 |
* Use of this source code is governed by a BSD-style license that can be |
| 5 |
* found in the LICENSE file. |
| 6 |
*/ |
| 7 |
|
| 8 |
#ifndef SkTLazy_DEFINED |
| 9 |
#define SkTLazy_DEFINED |
| 10 |
|
| 11 |
#include "include/private/base/SkAssert.h" |
| 12 |
|
| 13 |
#include <optional> |
| 14 |
#include <utility> |
| 15 |
|
| 16 |
template <typename T> T* SkOptAddressOrNull(std::optional<T>& object) { |
| 17 |
return object.has_value() ? &object.value() : nullptr; |
| 18 |
} |
| 19 |
|
| 20 |
template <typename T> const T* SkOptAddressOrNull(const std::optional<T>& object) { |
| 21 |
return object.has_value() ? &object.value() : nullptr; |
| 22 |
} |
| 23 |
|
| 24 |
/** |
| 25 |
* Efficient way to defer allocating/initializing a class until it is needed |
| 26 |
* (if ever). |
| 27 |
*/ |
| 28 |
template <typename T> class SkTLazy { |
| 29 |
public: |
| 30 |
SkTLazy() = default; |
| 31 |
explicit SkTLazy(const T* src) : fValue(src ? std::optional<T>(*src) : std::nullopt) {} |
| 32 |
SkTLazy(const SkTLazy& that) : fValue(that.fValue) {} |
| 33 |
SkTLazy(SkTLazy&& that) : fValue(std::move(that.fValue)) {} |
| 34 |
|
| 35 |
~SkTLazy() = default; |
| 36 |
|
| 37 |
SkTLazy& operator=(const SkTLazy& that) { |
| 38 |
fValue = that.fValue; |
| 39 |
return *this; |
| 40 |
} |
| 41 |
|
| 42 |
SkTLazy& operator=(SkTLazy&& that) { |
| 43 |
fValue = std::move(that.fValue); |
| 44 |
return *this; |
| 45 |
} |
| 46 |
|
| 47 |
/** |
| 48 |
* Return a pointer to an instance of the class initialized with 'args'. |
| 49 |
* If a previous instance had been initialized (either from init() or |
| 50 |
* set()) it will first be destroyed, so that a freshly initialized |
| 51 |
* instance is always returned. |
| 52 |
*/ |
| 53 |
template <typename... Args> T* init(Args&&... args) { |
| 54 |
fValue.emplace(std::forward<Args>(args)...); |
| 55 |
return this->get(); |
| 56 |
} |
| 57 |
|
| 58 |
/** |
| 59 |
* Copy src into this, and return a pointer to a copy of it. Note this |
| 60 |
* will always return the same pointer, so if it is called on a lazy that |
| 61 |
* has already been initialized, then this will copy over the previous |
| 62 |
* contents. |
| 63 |
*/ |
| 64 |
T* set(const T& src) { |
| 65 |
fValue = src; |
| 66 |
return this->get(); |
| 67 |
} |
| 68 |
|
| 69 |
T* set(T&& src) { |
| 70 |
fValue = std::move(src); |
| 71 |
return this->get(); |
| 72 |
} |
| 73 |
|
| 74 |
/** |
| 75 |
* Destroy the lazy object (if it was created via init() or set()) |
| 76 |
*/ |
| 77 |
void reset() { |
| 78 |
fValue.reset(); |
| 79 |
} |
| 80 |
|
| 81 |
/** |
| 82 |
* Returns true if a valid object has been initialized in the SkTLazy, |
| 83 |
* false otherwise. |
| 84 |
*/ |
| 85 |
bool isValid() const { return fValue.has_value(); } |
| 86 |
|
| 87 |
/** |
| 88 |
* Returns the object. This version should only be called when the caller |
| 89 |
* knows that the object has been initialized. |
| 90 |
*/ |
| 91 |
T* get() { |
| 92 |
SkASSERT(fValue.has_value()); |
| 93 |
return &fValue.value(); |
| 94 |
} |
| 95 |
const T* get() const { |
| 96 |
SkASSERT(fValue.has_value()); |
| 97 |
return &fValue.value(); |
| 98 |
} |
| 99 |
|
| 100 |
T* operator->() { return this->get(); } |
| 101 |
const T* operator->() const { return this->get(); } |
| 102 |
|
| 103 |
T& operator*() { |
| 104 |
SkASSERT(fValue.has_value()); |
| 105 |
return *fValue; |
| 106 |
} |
| 107 |
const T& operator*() const { |
| 108 |
SkASSERT(fValue.has_value()); |
| 109 |
return *fValue; |
| 110 |
} |
| 111 |
|
| 112 |
/** |
| 113 |
* Like above but doesn't assert if object isn't initialized (in which case |
| 114 |
* nullptr is returned). |
| 115 |
*/ |
| 116 |
const T* getMaybeNull() const { return fValue.has_value() ? this->get() : nullptr; } |
| 117 |
T* getMaybeNull() { return fValue.has_value() ? this->get() : nullptr; } |
| 118 |
|
| 119 |
private: |
| 120 |
std::optional<T> fValue; |
| 121 |
}; |
| 122 |
|
| 123 |
/** |
| 124 |
* A helper built on top of std::optional to do copy-on-first-write. The object is initialized |
| 125 |
* with a const pointer but provides a non-const pointer accessor. The first time the |
| 126 |
* accessor is called (if ever) the object is cloned. |
| 127 |
* |
| 128 |
* In the following example at most one copy of constThing is made: |
| 129 |
* |
| 130 |
* SkTCopyOnFirstWrite<Thing> thing(&constThing); |
| 131 |
* ... |
| 132 |
* function_that_takes_a_const_thing_ptr(thing); // constThing is passed |
| 133 |
* ... |
| 134 |
* if (need_to_modify_thing()) { |
| 135 |
* thing.writable()->modifyMe(); // makes a copy of constThing |
| 136 |
* } |
| 137 |
* ... |
| 138 |
* x = thing->readSomething(); |
| 139 |
* ... |
| 140 |
* if (need_to_modify_thing_now()) { |
| 141 |
* thing.writable()->changeMe(); // makes a copy of constThing if we didn't call modifyMe() |
| 142 |
* } |
| 143 |
* |
| 144 |
* consume_a_thing(thing); // could be constThing or a modified copy. |
| 145 |
*/ |
| 146 |
template <typename T> |
| 147 |
class SkTCopyOnFirstWrite { |
| 148 |
public: |
| 149 |
explicit SkTCopyOnFirstWrite(const T& initial) : fObj(&initial) {} |
| 150 |
|
| 151 |
explicit SkTCopyOnFirstWrite(const T* initial) : fObj(initial) {} |
| 152 |
|
| 153 |
// Constructor for delayed initialization. |
| 154 |
SkTCopyOnFirstWrite() : fObj(nullptr) {} |
| 155 |
|
| 156 |
SkTCopyOnFirstWrite(const SkTCopyOnFirstWrite& that) { *this = that; } |
| 157 |
SkTCopyOnFirstWrite( SkTCopyOnFirstWrite&& that) { *this = std::move(that); } |
| 158 |
|
| 159 |
SkTCopyOnFirstWrite& operator=(const SkTCopyOnFirstWrite& that) { |
| 160 |
fLazy = that.fLazy; |
| 161 |
fObj = fLazy.has_value() ? &fLazy.value() : that.fObj; |
| 162 |
return *this; |
| 163 |
} |
| 164 |
|
| 165 |
SkTCopyOnFirstWrite& operator=(SkTCopyOnFirstWrite&& that) { |
| 166 |
fLazy = std::move(that.fLazy); |
| 167 |
fObj = fLazy.has_value() ? &fLazy.value() : that.fObj; |
| 168 |
return *this; |
| 169 |
} |
| 170 |
|
| 171 |
// Should only be called once, and only if the default constructor was used. |
| 172 |
void init(const T& initial) { |
| 173 |
SkASSERT(!fObj); |
| 174 |
SkASSERT(!fLazy.has_value()); |
| 175 |
fObj = &initial; |
| 176 |
} |
| 177 |
|
| 178 |
// If not already initialized, in-place instantiates the writable object |
| 179 |
template <typename... Args> |
| 180 |
void initIfNeeded(Args&&... args) { |
| 181 |
if (!fObj) { |
| 182 |
SkASSERT(!fLazy.has_value()); |
| 183 |
fObj = &fLazy.emplace(std::forward<Args>(args)...); |
| 184 |
} |
| 185 |
} |
| 186 |
|
| 187 |
/** |
| 188 |
* Returns a writable T*. The first time this is called the initial object is cloned. |
| 189 |
*/ |
| 190 |
T* writable() { |
| 191 |
SkASSERT(fObj); |
| 192 |
if (!fLazy.has_value()) { |
| 193 |
fLazy = *fObj; |
| 194 |
fObj = &fLazy.value(); |
| 195 |
} |
| 196 |
return &fLazy.value(); |
| 197 |
} |
| 198 |
|
| 199 |
const T* get() const { return fObj; } |
| 200 |
|
| 201 |
/** |
| 202 |
* Operators for treating this as though it were a const pointer. |
| 203 |
*/ |
| 204 |
|
| 205 |
const T *operator->() const { return fObj; } |
| 206 |
|
| 207 |
operator const T*() const { return fObj; } |
| 208 |
|
| 209 |
const T& operator *() const { return *fObj; } |
| 210 |
|
| 211 |
private: |
| 212 |
const T* fObj; |
| 213 |
std::optional<T> fLazy; |
| 214 |
}; |
| 215 |
|
| 216 |
#endif |