michael@0: michael@0: /* michael@0: * Copyright 2011 Google Inc. michael@0: * michael@0: * Use of this source code is governed by a BSD-style license that can be michael@0: * found in the LICENSE file. michael@0: */ michael@0: michael@0: michael@0: michael@0: #ifndef SkTLazy_DEFINED michael@0: #define SkTLazy_DEFINED michael@0: michael@0: #include "SkTypes.h" michael@0: #include michael@0: michael@0: template class SkTLazy; michael@0: template void* operator new(size_t, SkTLazy* lazy); michael@0: michael@0: /** michael@0: * Efficient way to defer allocating/initializing a class until it is needed michael@0: * (if ever). michael@0: */ michael@0: template class SkTLazy { michael@0: public: michael@0: SkTLazy() : fPtr(NULL) {} michael@0: michael@0: explicit SkTLazy(const T* src) : fPtr(NULL) { michael@0: if (src) { michael@0: fPtr = new (fStorage) T(*src); michael@0: } michael@0: } michael@0: michael@0: SkTLazy(const SkTLazy& src) : fPtr(NULL) { michael@0: if (src.isValid()) { michael@0: fPtr = new (fStorage) T(*src->get()); michael@0: } else { michael@0: fPtr = NULL; michael@0: } michael@0: } michael@0: michael@0: ~SkTLazy() { michael@0: if (this->isValid()) { michael@0: fPtr->~T(); michael@0: } michael@0: } michael@0: michael@0: /** michael@0: * Return a pointer to a default-initialized instance of the class. If a michael@0: * previous instance had been initialized (either from init() or set()) it michael@0: * will first be destroyed, so that a freshly initialized instance is michael@0: * always returned. michael@0: */ michael@0: T* init() { michael@0: if (this->isValid()) { michael@0: fPtr->~T(); michael@0: } michael@0: fPtr = new (SkTCast(fStorage)) T; michael@0: return fPtr; michael@0: } michael@0: michael@0: /** michael@0: * Copy src into this, and return a pointer to a copy of it. Note this michael@0: * will always return the same pointer, so if it is called on a lazy that michael@0: * has already been initialized, then this will copy over the previous michael@0: * contents. michael@0: */ michael@0: T* set(const T& src) { michael@0: if (this->isValid()) { michael@0: *fPtr = src; michael@0: } else { michael@0: fPtr = new (SkTCast(fStorage)) T(src); michael@0: } michael@0: return fPtr; michael@0: } michael@0: michael@0: /** michael@0: * Destroy the lazy object (if it was created via init() or set()) michael@0: */ michael@0: void reset() { michael@0: if (this->isValid()) { michael@0: fPtr->~T(); michael@0: fPtr = NULL; michael@0: } michael@0: } michael@0: michael@0: /** michael@0: * Returns true if a valid object has been initialized in the SkTLazy, michael@0: * false otherwise. michael@0: */ michael@0: bool isValid() const { return NULL != fPtr; } michael@0: michael@0: /** michael@0: * Returns the object. This version should only be called when the caller michael@0: * knows that the object has been initialized. michael@0: */ michael@0: T* get() const { SkASSERT(this->isValid()); return fPtr; } michael@0: michael@0: /** michael@0: * Like above but doesn't assert if object isn't initialized (in which case michael@0: * NULL is returned). michael@0: */ michael@0: T* getMaybeNull() const { return fPtr; } michael@0: michael@0: private: michael@0: friend void* operator new(size_t, SkTLazy* lazy); michael@0: michael@0: T* fPtr; // NULL or fStorage michael@0: char fStorage[sizeof(T)]; michael@0: }; michael@0: michael@0: // Use the below macro (SkNEW_IN_TLAZY) rather than calling this directly michael@0: template void* operator new(size_t, SkTLazy* lazy) { michael@0: SkASSERT(!lazy->isValid()); michael@0: lazy->fPtr = reinterpret_cast(lazy->fStorage); michael@0: return lazy->fPtr; michael@0: } michael@0: michael@0: // Skia doesn't use C++ exceptions but it may be compiled with them enabled. Having an op delete michael@0: // to match the op new silences warnings about missing op delete when a constructor throws an michael@0: // exception. michael@0: template void operator delete(void*, SkTLazy*) { SK_CRASH(); } michael@0: michael@0: // Use this to construct a T inside an SkTLazy using a non-default constructor. michael@0: #define SkNEW_IN_TLAZY(tlazy_ptr, type_name, args) (new (tlazy_ptr) type_name args) michael@0: michael@0: /** michael@0: * A helper built on top of SkTLazy to do copy-on-first-write. The object is initialized michael@0: * with a const pointer but provides a non-const pointer accessor. The first time the michael@0: * accessor is called (if ever) the object is cloned. michael@0: * michael@0: * In the following example at most one copy of constThing is made: michael@0: * michael@0: * SkTCopyOnFirstWrite thing(&constThing); michael@0: * ... michael@0: * function_that_takes_a_const_thing_ptr(thing); // constThing is passed michael@0: * ... michael@0: * if (need_to_modify_thing()) { michael@0: * thing.writable()->modifyMe(); // makes a copy of constThing michael@0: * } michael@0: * ... michael@0: * x = thing->readSomething(); michael@0: * ... michael@0: * if (need_to_modify_thing_now()) { michael@0: * thing.writable()->changeMe(); // makes a copy of constThing if we didn't call modifyMe() michael@0: * } michael@0: * michael@0: * consume_a_thing(thing); // could be constThing or a modified copy. michael@0: */ michael@0: template michael@0: class SkTCopyOnFirstWrite { michael@0: public: michael@0: SkTCopyOnFirstWrite(const T& initial) : fObj(&initial) {} michael@0: michael@0: // Constructor for delayed initialization. michael@0: SkTCopyOnFirstWrite() : fObj(NULL) {} michael@0: michael@0: // Should only be called once, and only if the default constructor was used. michael@0: void init(const T& initial) { michael@0: SkASSERT(NULL == fObj); michael@0: SkASSERT(!fLazy.isValid()); michael@0: fObj = &initial; michael@0: } michael@0: michael@0: /** michael@0: * Returns a writable T*. The first time this is called the initial object is cloned. michael@0: */ michael@0: T* writable() { michael@0: SkASSERT(NULL != fObj); michael@0: if (!fLazy.isValid()) { michael@0: fLazy.set(*fObj); michael@0: fObj = fLazy.get(); michael@0: } michael@0: return const_cast(fObj); michael@0: } michael@0: michael@0: /** michael@0: * Operators for treating this as though it were a const pointer. michael@0: */ michael@0: michael@0: const T *operator->() const { return fObj; } michael@0: michael@0: operator const T*() const { return fObj; } michael@0: michael@0: const T& operator *() const { return *fObj; } michael@0: michael@0: private: michael@0: const T* fObj; michael@0: SkTLazy fLazy; michael@0: }; michael@0: michael@0: #endif