8 #ifndef SkTLazy_DEFINED
9 #define SkTLazy_DEFINED
11 #include "../private/SkTemplates.h"
25 : fPtr(src ?
new (fStorage.get()) T(*src) :
nullptr) {}
50 template <
typename... Args> T*
init(Args&&... args) {
54 fPtr =
new (SkTCast<T*>(fStorage.get())) T(std::forward<Args>(args)...);
64 T*
set(
const T& src) {
68 fPtr =
new (SkTCast<T*>(fStorage.get())) T(src);
93 T*
get()
const { SkASSERT(this->
isValid());
return fPtr; }
102 SkAlignedSTStorage<1, T> fStorage;
129 template <
typename T>
140 void init(
const T& initial) {
141 SkASSERT(
nullptr == fObj);
142 SkASSERT(!fLazy.isValid());
151 if (!fLazy.isValid()) {
155 return const_cast<T*
>(fObj);
164 operator const T*()
const {
return fObj; }
166 const T& operator *()
const {
return *fObj; }
A helper built on top of SkTLazy to do copy-on-first-write.
Definition: SkTLazy.h:130
Efficient way to defer allocating/initializing a class until it is needed (if ever).
Definition: SkTLazy.h:20
T * writable()
Returns a writable T*.
Definition: SkTLazy.h:149
void reset()
Destroy the lazy object (if it was created via init() or set())
Definition: SkTLazy.h:76
bool isValid() const
Returns true if a valid object has been initialized in the SkTLazy, false otherwise.
Definition: SkTLazy.h:87
const T * operator->() const
Operators for treating this as though it were a const pointer.
Definition: SkTLazy.h:162
T * get() const
Returns the object.
Definition: SkTLazy.h:93
#define SkToBool(cond)
Returns 0 or 1 based on the condition.
Definition: SkTypes.h:227
T * getMaybeNull() const
Like above but doesn't assert if object isn't initialized (in which case nullptr is returned)...
Definition: SkTLazy.h:99
T * init(Args &&...args)
Return a pointer to an instance of the class initialized with 'args'.
Definition: SkTLazy.h:50
T * set(const T &src)
Copy src into this, and return a pointer to a copy of it.
Definition: SkTLazy.h:64