SkTLazy.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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. #ifndef SkTLazy_DEFINED
  8. #define SkTLazy_DEFINED
  9. #include "include/core/SkTypes.h"
  10. #include <new>
  11. #include <type_traits>
  12. #include <utility>
  13. /**
  14. * Efficient way to defer allocating/initializing a class until it is needed
  15. * (if ever).
  16. */
  17. template <typename T> class SkTLazy {
  18. public:
  19. SkTLazy() = default;
  20. explicit SkTLazy(const T* src) : fPtr(src ? new (&fStorage) T(*src) : nullptr) {}
  21. SkTLazy(const SkTLazy& that) : fPtr(that.fPtr ? new (&fStorage) T(*that.fPtr) : nullptr) {}
  22. SkTLazy(SkTLazy&& that) : fPtr(that.fPtr ? new (&fStorage) T(std::move(*that.fPtr)) : nullptr){}
  23. ~SkTLazy() { this->reset(); }
  24. SkTLazy& operator=(const SkTLazy& that) {
  25. if (that.isValid()) {
  26. this->set(*that);
  27. } else {
  28. this->reset();
  29. }
  30. return *this;
  31. }
  32. SkTLazy& operator=(SkTLazy&& that) {
  33. if (that.isValid()) {
  34. this->set(std::move(*that));
  35. } else {
  36. this->reset();
  37. }
  38. return *this;
  39. }
  40. /**
  41. * Return a pointer to an instance of the class initialized with 'args'.
  42. * If a previous instance had been initialized (either from init() or
  43. * set()) it will first be destroyed, so that a freshly initialized
  44. * instance is always returned.
  45. */
  46. template <typename... Args> T* init(Args&&... args) {
  47. this->reset();
  48. fPtr = new (&fStorage) T(std::forward<Args>(args)...);
  49. return fPtr;
  50. }
  51. /**
  52. * Copy src into this, and return a pointer to a copy of it. Note this
  53. * will always return the same pointer, so if it is called on a lazy that
  54. * has already been initialized, then this will copy over the previous
  55. * contents.
  56. */
  57. T* set(const T& src) {
  58. if (this->isValid()) {
  59. *fPtr = src;
  60. } else {
  61. fPtr = new (&fStorage) T(src);
  62. }
  63. return fPtr;
  64. }
  65. T* set(T&& src) {
  66. if (this->isValid()) {
  67. *fPtr = std::move(src);
  68. } else {
  69. fPtr = new (&fStorage) T(std::move(src));
  70. }
  71. return fPtr;
  72. }
  73. /**
  74. * Destroy the lazy object (if it was created via init() or set())
  75. */
  76. void reset() {
  77. if (this->isValid()) {
  78. fPtr->~T();
  79. fPtr = nullptr;
  80. }
  81. }
  82. /**
  83. * Returns true if a valid object has been initialized in the SkTLazy,
  84. * false otherwise.
  85. */
  86. bool isValid() const { return SkToBool(fPtr); }
  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() const { SkASSERT(this->isValid()); return fPtr; }
  92. T* operator->() const { return this->get(); }
  93. T& operator*() const { return *this->get(); }
  94. /**
  95. * Like above but doesn't assert if object isn't initialized (in which case
  96. * nullptr is returned).
  97. */
  98. T* getMaybeNull() const { return fPtr; }
  99. private:
  100. typename std::aligned_storage<sizeof(T), alignof(T)>::type fStorage;
  101. T* fPtr{nullptr}; // nullptr or fStorage
  102. };
  103. /**
  104. * A helper built on top of SkTLazy to do copy-on-first-write. The object is initialized
  105. * with a const pointer but provides a non-const pointer accessor. The first time the
  106. * accessor is called (if ever) the object is cloned.
  107. *
  108. * In the following example at most one copy of constThing is made:
  109. *
  110. * SkTCopyOnFirstWrite<Thing> thing(&constThing);
  111. * ...
  112. * function_that_takes_a_const_thing_ptr(thing); // constThing is passed
  113. * ...
  114. * if (need_to_modify_thing()) {
  115. * thing.writable()->modifyMe(); // makes a copy of constThing
  116. * }
  117. * ...
  118. * x = thing->readSomething();
  119. * ...
  120. * if (need_to_modify_thing_now()) {
  121. * thing.writable()->changeMe(); // makes a copy of constThing if we didn't call modifyMe()
  122. * }
  123. *
  124. * consume_a_thing(thing); // could be constThing or a modified copy.
  125. */
  126. template <typename T>
  127. class SkTCopyOnFirstWrite {
  128. public:
  129. explicit SkTCopyOnFirstWrite(const T& initial) : fObj(&initial) {}
  130. explicit SkTCopyOnFirstWrite(const T* initial) : fObj(initial) {}
  131. // Constructor for delayed initialization.
  132. SkTCopyOnFirstWrite() : fObj(nullptr) {}
  133. SkTCopyOnFirstWrite(const SkTCopyOnFirstWrite& that) { *this = that; }
  134. SkTCopyOnFirstWrite( SkTCopyOnFirstWrite&& that) { *this = std::move(that); }
  135. SkTCopyOnFirstWrite& operator=(const SkTCopyOnFirstWrite& that) {
  136. fLazy = that.fLazy;
  137. fObj = fLazy.isValid() ? fLazy.get() : that.fObj;
  138. return *this;
  139. }
  140. SkTCopyOnFirstWrite& operator=(SkTCopyOnFirstWrite&& that) {
  141. fLazy = std::move(that.fLazy);
  142. fObj = fLazy.isValid() ? fLazy.get() : that.fObj;
  143. return *this;
  144. }
  145. // Should only be called once, and only if the default constructor was used.
  146. void init(const T& initial) {
  147. SkASSERT(nullptr == fObj);
  148. SkASSERT(!fLazy.isValid());
  149. fObj = &initial;
  150. }
  151. /**
  152. * Returns a writable T*. The first time this is called the initial object is cloned.
  153. */
  154. T* writable() {
  155. SkASSERT(fObj);
  156. if (!fLazy.isValid()) {
  157. fLazy.set(*fObj);
  158. fObj = fLazy.get();
  159. }
  160. return const_cast<T*>(fObj);
  161. }
  162. const T* get() const { return fObj; }
  163. /**
  164. * Operators for treating this as though it were a const pointer.
  165. */
  166. const T *operator->() const { return fObj; }
  167. operator const T*() const { return fObj; }
  168. const T& operator *() const { return *fObj; }
  169. private:
  170. const T* fObj;
  171. SkTLazy<T> fLazy;
  172. };
  173. #endif