reed@google.com | a076e9b | 2011-04-06 20:17:29 +0000 | [diff] [blame] | 1 | /* |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 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. |
reed@google.com | a076e9b | 2011-04-06 20:17:29 +0000 | [diff] [blame] | 6 | */ |
| 7 | |
reed@google.com | a076e9b | 2011-04-06 20:17:29 +0000 | [diff] [blame] | 8 | #ifndef SkTLazy_DEFINED |
| 9 | #define SkTLazy_DEFINED |
| 10 | |
bungeman | f3c15b7 | 2015-08-19 11:56:48 -0700 | [diff] [blame] | 11 | #include "../private/SkTemplates.h" |
reed@google.com | a076e9b | 2011-04-06 20:17:29 +0000 | [diff] [blame] | 12 | #include "SkTypes.h" |
bungeman@google.com | b81be7a | 2011-07-28 16:51:20 +0000 | [diff] [blame] | 13 | #include <new> |
bungeman | 221524d | 2016-01-05 14:59:40 -0800 | [diff] [blame] | 14 | #include <utility> |
reed@google.com | a076e9b | 2011-04-06 20:17:29 +0000 | [diff] [blame] | 15 | |
| 16 | /** |
| 17 | * Efficient way to defer allocating/initializing a class until it is needed |
| 18 | * (if ever). |
| 19 | */ |
| 20 | template <typename T> class SkTLazy { |
| 21 | public: |
Herb Derby | 811a672 | 2019-01-10 13:58:05 -0500 | [diff] [blame^] | 22 | SkTLazy() = default; |
| 23 | explicit SkTLazy(const T* src) : fPtr(src ? new (&fStorage) T(*src) : nullptr) {} |
| 24 | SkTLazy(const SkTLazy& that) { *this = that; } |
| 25 | SkTLazy(SkTLazy&& that) { *this = std::move(that); } |
reed@google.com | a076e9b | 2011-04-06 20:17:29 +0000 | [diff] [blame] | 26 | |
Herb Derby | 811a672 | 2019-01-10 13:58:05 -0500 | [diff] [blame^] | 27 | ~SkTLazy() { this->reset(); } |
reed@google.com | a076e9b | 2011-04-06 20:17:29 +0000 | [diff] [blame] | 28 | |
Ben Wagner | e819e5f | 2017-11-28 16:44:10 -0500 | [diff] [blame] | 29 | SkTLazy& operator=(const SkTLazy& that) { |
| 30 | if (that.isValid()) { |
Herb Derby | 811a672 | 2019-01-10 13:58:05 -0500 | [diff] [blame^] | 31 | this->set(*that); |
Ben Wagner | e819e5f | 2017-11-28 16:44:10 -0500 | [diff] [blame] | 32 | } else { |
| 33 | this->reset(); |
| 34 | } |
| 35 | return *this; |
| 36 | } |
| 37 | |
| 38 | SkTLazy& operator=(SkTLazy&& that) { |
| 39 | if (that.isValid()) { |
Herb Derby | 811a672 | 2019-01-10 13:58:05 -0500 | [diff] [blame^] | 40 | this->set(std::move(*that)); |
fmalita | 0cbe77c | 2016-08-10 16:30:37 -0700 | [diff] [blame] | 41 | } else { |
| 42 | this->reset(); |
| 43 | } |
| 44 | return *this; |
| 45 | } |
| 46 | |
reed@google.com | a076e9b | 2011-04-06 20:17:29 +0000 | [diff] [blame] | 47 | /** |
bungeman | 72440a3 | 2015-08-12 13:37:16 -0700 | [diff] [blame] | 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. |
mike@reedtribe.org | 2c8fc5a | 2011-04-10 00:35:29 +0000 | [diff] [blame] | 52 | */ |
bungeman | 72440a3 | 2015-08-12 13:37:16 -0700 | [diff] [blame] | 53 | template <typename... Args> T* init(Args&&... args) { |
Herb Derby | 811a672 | 2019-01-10 13:58:05 -0500 | [diff] [blame^] | 54 | this->reset(); |
| 55 | fPtr = new (&fStorage) T(std::forward<Args>(args)...); |
mike@reedtribe.org | 2c8fc5a | 2011-04-10 00:35:29 +0000 | [diff] [blame] | 56 | return fPtr; |
| 57 | } |
bsalomon@google.com | 8c2fe99 | 2011-09-13 15:27:18 +0000 | [diff] [blame] | 58 | |
mike@reedtribe.org | 2c8fc5a | 2011-04-10 00:35:29 +0000 | [diff] [blame] | 59 | /** |
reed@google.com | a076e9b | 2011-04-06 20:17:29 +0000 | [diff] [blame] | 60 | * Copy src into this, and return a pointer to a copy of it. Note this |
| 61 | * will always return the same pointer, so if it is called on a lazy that |
| 62 | * has already been initialized, then this will copy over the previous |
| 63 | * contents. |
| 64 | */ |
| 65 | T* set(const T& src) { |
bsalomon@google.com | 8c2fe99 | 2011-09-13 15:27:18 +0000 | [diff] [blame] | 66 | if (this->isValid()) { |
reed@google.com | a076e9b | 2011-04-06 20:17:29 +0000 | [diff] [blame] | 67 | *fPtr = src; |
| 68 | } else { |
Herb Derby | 811a672 | 2019-01-10 13:58:05 -0500 | [diff] [blame^] | 69 | fPtr = new (&fStorage) T(src); |
reed@google.com | a076e9b | 2011-04-06 20:17:29 +0000 | [diff] [blame] | 70 | } |
| 71 | return fPtr; |
| 72 | } |
bsalomon@google.com | 8c2fe99 | 2011-09-13 15:27:18 +0000 | [diff] [blame] | 73 | |
Ben Wagner | e819e5f | 2017-11-28 16:44:10 -0500 | [diff] [blame] | 74 | T* set(T&& src) { |
| 75 | if (this->isValid()) { |
| 76 | *fPtr = std::move(src); |
| 77 | } else { |
Herb Derby | 811a672 | 2019-01-10 13:58:05 -0500 | [diff] [blame^] | 78 | fPtr = new (&fStorage) T(std::move(src)); |
Ben Wagner | e819e5f | 2017-11-28 16:44:10 -0500 | [diff] [blame] | 79 | } |
| 80 | return fPtr; |
| 81 | } |
| 82 | |
bsalomon@google.com | 8c2fe99 | 2011-09-13 15:27:18 +0000 | [diff] [blame] | 83 | /** |
commit-bot@chromium.org | 6f954b9 | 2014-02-27 17:39:46 +0000 | [diff] [blame] | 84 | * Destroy the lazy object (if it was created via init() or set()) |
| 85 | */ |
| 86 | void reset() { |
| 87 | if (this->isValid()) { |
| 88 | fPtr->~T(); |
fmalita | 0cbe77c | 2016-08-10 16:30:37 -0700 | [diff] [blame] | 89 | fPtr = nullptr; |
commit-bot@chromium.org | 6f954b9 | 2014-02-27 17:39:46 +0000 | [diff] [blame] | 90 | } |
| 91 | } |
| 92 | |
| 93 | /** |
bsalomon@google.com | 8c2fe99 | 2011-09-13 15:27:18 +0000 | [diff] [blame] | 94 | * Returns true if a valid object has been initialized in the SkTLazy, |
| 95 | * false otherwise. |
| 96 | */ |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 97 | bool isValid() const { return SkToBool(fPtr); } |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 98 | |
reed@google.com | a076e9b | 2011-04-06 20:17:29 +0000 | [diff] [blame] | 99 | /** |
commit-bot@chromium.org | bb5c465 | 2013-04-01 12:49:31 +0000 | [diff] [blame] | 100 | * Returns the object. This version should only be called when the caller |
| 101 | * knows that the object has been initialized. |
reed@google.com | a076e9b | 2011-04-06 20:17:29 +0000 | [diff] [blame] | 102 | */ |
bsalomon@google.com | 8c2fe99 | 2011-09-13 15:27:18 +0000 | [diff] [blame] | 103 | T* get() const { SkASSERT(this->isValid()); return fPtr; } |
Herb Derby | 811a672 | 2019-01-10 13:58:05 -0500 | [diff] [blame^] | 104 | T* operator->() const { return this->get(); } |
| 105 | T& operator*() const { return *this->get(); } |
skia.committer@gmail.com | 05a2ee0 | 2013-04-02 07:01:34 +0000 | [diff] [blame] | 106 | |
commit-bot@chromium.org | bb5c465 | 2013-04-01 12:49:31 +0000 | [diff] [blame] | 107 | /** |
| 108 | * Like above but doesn't assert if object isn't initialized (in which case |
fmalita | 0cbe77c | 2016-08-10 16:30:37 -0700 | [diff] [blame] | 109 | * nullptr is returned). |
commit-bot@chromium.org | bb5c465 | 2013-04-01 12:49:31 +0000 | [diff] [blame] | 110 | */ |
| 111 | T* getMaybeNull() const { return fPtr; } |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 112 | |
reed@google.com | a076e9b | 2011-04-06 20:17:29 +0000 | [diff] [blame] | 113 | private: |
Herb Derby | 811a672 | 2019-01-10 13:58:05 -0500 | [diff] [blame^] | 114 | typename std::aligned_storage<sizeof(T), alignof(T)>::type fStorage; |
| 115 | T* fPtr{nullptr}; // nullptr or fStorage |
reed@google.com | a076e9b | 2011-04-06 20:17:29 +0000 | [diff] [blame] | 116 | }; |
| 117 | |
bsalomon@google.com | 5dc26b9 | 2012-10-11 19:32:32 +0000 | [diff] [blame] | 118 | /** |
| 119 | * A helper built on top of SkTLazy to do copy-on-first-write. The object is initialized |
| 120 | * with a const pointer but provides a non-const pointer accessor. The first time the |
| 121 | * accessor is called (if ever) the object is cloned. |
| 122 | * |
| 123 | * In the following example at most one copy of constThing is made: |
| 124 | * |
| 125 | * SkTCopyOnFirstWrite<Thing> thing(&constThing); |
| 126 | * ... |
| 127 | * function_that_takes_a_const_thing_ptr(thing); // constThing is passed |
| 128 | * ... |
| 129 | * if (need_to_modify_thing()) { |
| 130 | * thing.writable()->modifyMe(); // makes a copy of constThing |
| 131 | * } |
| 132 | * ... |
| 133 | * x = thing->readSomething(); |
| 134 | * ... |
| 135 | * if (need_to_modify_thing_now()) { |
| 136 | * thing.writable()->changeMe(); // makes a copy of constThing if we didn't call modifyMe() |
| 137 | * } |
| 138 | * |
| 139 | * consume_a_thing(thing); // could be constThing or a modified copy. |
| 140 | */ |
| 141 | template <typename T> |
| 142 | class SkTCopyOnFirstWrite { |
| 143 | public: |
Florin Malita | 8eaf64a | 2018-04-11 11:59:18 -0400 | [diff] [blame] | 144 | explicit SkTCopyOnFirstWrite(const T& initial) : fObj(&initial) {} |
bsalomon@google.com | 5dc26b9 | 2012-10-11 19:32:32 +0000 | [diff] [blame] | 145 | |
Florin Malita | 8eaf64a | 2018-04-11 11:59:18 -0400 | [diff] [blame] | 146 | explicit SkTCopyOnFirstWrite(const T* initial) : fObj(initial) {} |
fmalita | 32cdc32 | 2016-01-12 07:21:11 -0800 | [diff] [blame] | 147 | |
bsalomon@google.com | 45a15f5 | 2012-12-10 19:10:17 +0000 | [diff] [blame] | 148 | // Constructor for delayed initialization. |
fmalita | 0cbe77c | 2016-08-10 16:30:37 -0700 | [diff] [blame] | 149 | SkTCopyOnFirstWrite() : fObj(nullptr) {} |
bsalomon@google.com | 45a15f5 | 2012-12-10 19:10:17 +0000 | [diff] [blame] | 150 | |
Florin Malita | 8eaf64a | 2018-04-11 11:59:18 -0400 | [diff] [blame] | 151 | SkTCopyOnFirstWrite(const SkTCopyOnFirstWrite& that) { *this = that; } |
| 152 | SkTCopyOnFirstWrite( SkTCopyOnFirstWrite&& that) { *this = std::move(that); } |
| 153 | |
| 154 | SkTCopyOnFirstWrite& operator=(const SkTCopyOnFirstWrite& that) { |
| 155 | fLazy = that.fLazy; |
| 156 | fObj = fLazy.isValid() ? fLazy.get() : that.fObj; |
| 157 | return *this; |
| 158 | } |
| 159 | |
| 160 | SkTCopyOnFirstWrite& operator=(SkTCopyOnFirstWrite&& that) { |
| 161 | fLazy = std::move(that.fLazy); |
| 162 | fObj = fLazy.isValid() ? fLazy.get() : that.fObj; |
| 163 | return *this; |
| 164 | } |
| 165 | |
bsalomon@google.com | 45a15f5 | 2012-12-10 19:10:17 +0000 | [diff] [blame] | 166 | // Should only be called once, and only if the default constructor was used. |
| 167 | void init(const T& initial) { |
fmalita | 0cbe77c | 2016-08-10 16:30:37 -0700 | [diff] [blame] | 168 | SkASSERT(nullptr == fObj); |
bsalomon@google.com | 45a15f5 | 2012-12-10 19:10:17 +0000 | [diff] [blame] | 169 | SkASSERT(!fLazy.isValid()); |
| 170 | fObj = &initial; |
| 171 | } |
| 172 | |
bsalomon@google.com | 5dc26b9 | 2012-10-11 19:32:32 +0000 | [diff] [blame] | 173 | /** |
| 174 | * Returns a writable T*. The first time this is called the initial object is cloned. |
| 175 | */ |
| 176 | T* writable() { |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 177 | SkASSERT(fObj); |
bsalomon@google.com | 5dc26b9 | 2012-10-11 19:32:32 +0000 | [diff] [blame] | 178 | if (!fLazy.isValid()) { |
| 179 | fLazy.set(*fObj); |
| 180 | fObj = fLazy.get(); |
| 181 | } |
| 182 | return const_cast<T*>(fObj); |
| 183 | } |
| 184 | |
Florin Malita | 8eaf64a | 2018-04-11 11:59:18 -0400 | [diff] [blame] | 185 | const T* get() const { return fObj; } |
| 186 | |
bsalomon@google.com | 5dc26b9 | 2012-10-11 19:32:32 +0000 | [diff] [blame] | 187 | /** |
| 188 | * Operators for treating this as though it were a const pointer. |
| 189 | */ |
| 190 | |
| 191 | const T *operator->() const { return fObj; } |
| 192 | |
| 193 | operator const T*() const { return fObj; } |
| 194 | |
| 195 | const T& operator *() const { return *fObj; } |
| 196 | |
| 197 | private: |
| 198 | const T* fObj; |
| 199 | SkTLazy<T> fLazy; |
| 200 | }; |
| 201 | |
reed@google.com | a076e9b | 2011-04-06 20:17:29 +0000 | [diff] [blame] | 202 | #endif |