irr_ptr.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. /*
  2. Minetest
  3. Copyright (C) 2018 numzero, Lobachevskiy Vitaliy <numzer0@yandex.ru>
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU Lesser General Public License as published by
  6. the Free Software Foundation; either version 2.1 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public License along
  13. with this program; if not, write to the Free Software Foundation, Inc.,
  14. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  15. */
  16. #pragma once
  17. #include <type_traits>
  18. #include <utility>
  19. namespace irr { class IReferenceCounted; }
  20. /** Shared pointer for IrrLicht objects.
  21. *
  22. * It should only be used for user-managed objects, i.e. those created with
  23. * the @c new operator or @c create* functions, like:
  24. * `irr_ptr<scene::IMeshBuffer> buf{new scene::SMeshBuffer()};`
  25. * The reference counting is *not* balanced as new objects have reference
  26. * count set to one, and the @c irr_ptr constructor (and @c reset) assumes
  27. * ownership of that reference.
  28. *
  29. * It shouldn’t be used for engine-managed objects, including those created
  30. * with @c addTexture and similar methods. Constructing @c irr_ptr directly
  31. * from such object is a bug and may lead to a crash. Indirect construction
  32. * is possible though; see the @c grab free function for details and use cases.
  33. */
  34. template <class ReferenceCounted>
  35. class irr_ptr
  36. {
  37. ReferenceCounted *value = nullptr;
  38. public:
  39. irr_ptr() noexcept = default;
  40. irr_ptr(std::nullptr_t) noexcept {}
  41. irr_ptr(const irr_ptr &b) noexcept { grab(b.get()); }
  42. irr_ptr(irr_ptr &&b) noexcept { reset(b.release()); }
  43. template <typename B,
  44. std::enable_if_t<std::is_convertible_v<B *, ReferenceCounted *>, bool> = true>
  45. irr_ptr(const irr_ptr<B> &b) noexcept
  46. {
  47. grab(b.get());
  48. }
  49. template <typename B,
  50. std::enable_if_t<std::is_convertible_v<B *, ReferenceCounted *>, bool> = true>
  51. irr_ptr(irr_ptr<B> &&b) noexcept
  52. {
  53. reset(b.release());
  54. }
  55. /** Constructs a shared pointer out of a plain one to control object lifetime.
  56. * @param object The object, usually returned by some @c create* function.
  57. * @note Move semantics: reference counter is *not* increased.
  58. * @warning Never wrap any @c add* function with this!
  59. */
  60. explicit irr_ptr(ReferenceCounted *object) noexcept { reset(object); }
  61. ~irr_ptr() { reset(); }
  62. irr_ptr &operator=(const irr_ptr &b) noexcept
  63. {
  64. grab(b.get());
  65. return *this;
  66. }
  67. irr_ptr &operator=(irr_ptr &&b) noexcept
  68. {
  69. reset(b.release());
  70. return *this;
  71. }
  72. template <typename B,
  73. std::enable_if_t<std::is_convertible_v<B *, ReferenceCounted *>, bool> = true>
  74. irr_ptr &operator=(const irr_ptr<B> &b) noexcept
  75. {
  76. grab(b.get());
  77. return *this;
  78. }
  79. template <typename B,
  80. std::enable_if_t<std::is_convertible_v<B *, ReferenceCounted *>, bool> = true>
  81. irr_ptr &operator=(irr_ptr<B> &&b) noexcept
  82. {
  83. reset(b.release());
  84. return *this;
  85. }
  86. ReferenceCounted &operator*() const noexcept { return *value; }
  87. ReferenceCounted *operator->() const noexcept { return value; }
  88. explicit operator ReferenceCounted *() const noexcept { return value; }
  89. explicit operator bool() const noexcept { return !!value; }
  90. /** Returns the stored pointer.
  91. */
  92. ReferenceCounted *get() const noexcept { return value; }
  93. /** Returns the stored pointer, erasing it from this class.
  94. * @note Move semantics: reference counter is not changed.
  95. */
  96. ReferenceCounted *release() noexcept
  97. {
  98. ReferenceCounted *object = value;
  99. value = nullptr;
  100. return object;
  101. }
  102. /** Drops stored pointer replacing it with the given one.
  103. * @note Move semantics: reference counter is *not* increased.
  104. */
  105. void reset(ReferenceCounted *object = nullptr) noexcept
  106. {
  107. static_assert(std::is_base_of_v<irr::IReferenceCounted, ReferenceCounted>,
  108. "Class is not an IReferenceCounted");
  109. if (value)
  110. value->drop();
  111. value = object;
  112. }
  113. /** Drops stored pointer replacing it with the given one.
  114. * @note Copy semantics: reference counter *is* increased.
  115. */
  116. void grab(ReferenceCounted *object) noexcept
  117. {
  118. static_assert(std::is_base_of_v<irr::IReferenceCounted, ReferenceCounted>,
  119. "Class is not an IReferenceCounted");
  120. if (object)
  121. object->grab();
  122. reset(object);
  123. }
  124. };
  125. /** Constructs a shared pointer as a *secondary* reference to an object
  126. *
  127. * This function is intended to make a temporary reference to an object which
  128. * is owned elsewhere so that it is not destroyed too early. To achieve that
  129. * it does balanced reference counting, i.e. reference count is increased
  130. * in this function and decreased when the returned pointer is destroyed.
  131. */
  132. template <class ReferenceCounted>
  133. [[nodiscard]]
  134. irr_ptr<ReferenceCounted> grab(ReferenceCounted *object) noexcept
  135. {
  136. irr_ptr<ReferenceCounted> ptr;
  137. ptr.grab(object);
  138. return ptr;
  139. }
  140. template <typename ReferenceCounted>
  141. bool operator==(const irr_ptr<ReferenceCounted> &a, const irr_ptr<ReferenceCounted> &b) noexcept
  142. {
  143. return a.get() == b.get();
  144. }
  145. template <typename ReferenceCounted>
  146. bool operator==(const irr_ptr<ReferenceCounted> &a, const ReferenceCounted *b) noexcept
  147. {
  148. return a.get() == b;
  149. }
  150. template <typename ReferenceCounted>
  151. bool operator==(const ReferenceCounted *a, const irr_ptr<ReferenceCounted> &b) noexcept
  152. {
  153. return a == b.get();
  154. }
  155. template <typename ReferenceCounted>
  156. bool operator!=(const irr_ptr<ReferenceCounted> &a, const irr_ptr<ReferenceCounted> &b) noexcept
  157. {
  158. return a.get() != b.get();
  159. }
  160. template <typename ReferenceCounted>
  161. bool operator!=(const irr_ptr<ReferenceCounted> &a, const ReferenceCounted *b) noexcept
  162. {
  163. return a.get() != b;
  164. }
  165. template <typename ReferenceCounted>
  166. bool operator!=(const ReferenceCounted *a, const irr_ptr<ReferenceCounted> &b) noexcept
  167. {
  168. return a != b.get();
  169. }
  170. /** Same as std::make_unique<T>, but for irr_ptr.
  171. */
  172. template <class T, class... Args>
  173. irr_ptr<T> make_irr(Args&&... args)
  174. {
  175. return irr_ptr<T>(new T(std::forward<Args>(args)...));
  176. }