irr_ptr.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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. #include "irrlichttypes.h"
  20. #include "IReferenceCounted.h"
  21. /** Shared pointer for IrrLicht objects.
  22. *
  23. * It should only be used for user-managed objects, i.e. those created with
  24. * the @c new operator or @c create* functions, like:
  25. * `irr_ptr<scene::IMeshBuffer> buf{new scene::SMeshBuffer()};`
  26. * The reference counting is *not* balanced as new objects have reference
  27. * count set to one, and the @c irr_ptr constructor (and @c reset) assumes
  28. * ownership of that reference.
  29. *
  30. * It shouldn’t be used for engine-managed objects, including those created
  31. * with @c addTexture and similar methods. Constructing @c irr_ptr directly
  32. * from such object is a bug and may lead to a crash. Indirect construction
  33. * is possible though; see the @c grab free function for details and use cases.
  34. */
  35. template <class ReferenceCounted,
  36. class = typename std::enable_if<std::is_base_of<IReferenceCounted,
  37. ReferenceCounted>::value>::type>
  38. class irr_ptr
  39. {
  40. ReferenceCounted *value = nullptr;
  41. public:
  42. irr_ptr() {}
  43. irr_ptr(std::nullptr_t) noexcept {}
  44. irr_ptr(const irr_ptr &b) noexcept { grab(b.get()); }
  45. irr_ptr(irr_ptr &&b) noexcept { reset(b.release()); }
  46. template <typename B, class = typename std::enable_if<std::is_convertible<B *,
  47. ReferenceCounted *>::value>::type>
  48. irr_ptr(const irr_ptr<B> &b) noexcept
  49. {
  50. grab(b.get());
  51. }
  52. template <typename B, class = typename std::enable_if<std::is_convertible<B *,
  53. ReferenceCounted *>::value>::type>
  54. irr_ptr(irr_ptr<B> &&b) noexcept
  55. {
  56. reset(b.release());
  57. }
  58. /** Constructs a shared pointer out of a plain one to control object lifetime.
  59. * @param object The object, usually returned by some @c create* function.
  60. * @note Move semantics: reference counter is *not* increased.
  61. * @warning Never wrap any @c add* function with this!
  62. */
  63. explicit irr_ptr(ReferenceCounted *object) noexcept { reset(object); }
  64. ~irr_ptr() { reset(); }
  65. irr_ptr &operator=(const irr_ptr &b) noexcept
  66. {
  67. grab(b.get());
  68. return *this;
  69. }
  70. irr_ptr &operator=(irr_ptr &&b) noexcept
  71. {
  72. reset(b.release());
  73. return *this;
  74. }
  75. template <typename B, class = typename std::enable_if<std::is_convertible<B *,
  76. ReferenceCounted *>::value>::type>
  77. irr_ptr &operator=(const irr_ptr<B> &b) noexcept
  78. {
  79. grab(b.get());
  80. return *this;
  81. }
  82. template <typename B, class = typename std::enable_if<std::is_convertible<B *,
  83. ReferenceCounted *>::value>::type>
  84. irr_ptr &operator=(irr_ptr<B> &&b) noexcept
  85. {
  86. reset(b.release());
  87. return *this;
  88. }
  89. ReferenceCounted &operator*() const noexcept { return *value; }
  90. ReferenceCounted *operator->() const noexcept { return value; }
  91. explicit operator ReferenceCounted *() const noexcept { return value; }
  92. explicit operator bool() const noexcept { return !!value; }
  93. /** Returns the stored pointer.
  94. */
  95. ReferenceCounted *get() const noexcept { return value; }
  96. /** Returns the stored pointer, erasing it from this class.
  97. * @note Move semantics: reference counter is not changed.
  98. */
  99. ReferenceCounted *release() noexcept
  100. {
  101. ReferenceCounted *object = value;
  102. value = nullptr;
  103. return object;
  104. }
  105. /** Drops stored pointer replacing it with the given one.
  106. * @note Move semantics: reference counter is *not* increased.
  107. */
  108. void reset(ReferenceCounted *object = nullptr) noexcept
  109. {
  110. if (value)
  111. value->drop();
  112. value = object;
  113. }
  114. /** Drops stored pointer replacing it with the given one.
  115. * @note Copy semantics: reference counter *is* increased.
  116. */
  117. void grab(ReferenceCounted *object) noexcept
  118. {
  119. if (object)
  120. object->grab();
  121. reset(object);
  122. }
  123. };
  124. /** Constructs a shared pointer as a *secondary* reference to an object
  125. *
  126. * This function is intended to make a temporary reference to an object which
  127. * is owned elsewhere so that it is not destroyed too early. To achieve that
  128. * it does balanced reference counting, i.e. reference count is increased
  129. * in this function and decreased when the returned pointer is destroyed.
  130. */
  131. template <class ReferenceCounted>
  132. irr_ptr<ReferenceCounted> grab(ReferenceCounted *object) noexcept
  133. {
  134. irr_ptr<ReferenceCounted> ptr;
  135. ptr.grab(object);
  136. return ptr;
  137. }
  138. template <typename ReferenceCounted>
  139. bool operator==(const irr_ptr<ReferenceCounted> &a, const irr_ptr<ReferenceCounted> &b) noexcept
  140. {
  141. return a.get() == b.get();
  142. }
  143. template <typename ReferenceCounted>
  144. bool operator==(const irr_ptr<ReferenceCounted> &a, const ReferenceCounted *b) noexcept
  145. {
  146. return a.get() == b;
  147. }
  148. template <typename ReferenceCounted>
  149. bool operator==(const ReferenceCounted *a, const irr_ptr<ReferenceCounted> &b) noexcept
  150. {
  151. return a == b.get();
  152. }
  153. template <typename ReferenceCounted>
  154. bool operator!=(const irr_ptr<ReferenceCounted> &a, const irr_ptr<ReferenceCounted> &b) noexcept
  155. {
  156. return a.get() != b.get();
  157. }
  158. template <typename ReferenceCounted>
  159. bool operator!=(const irr_ptr<ReferenceCounted> &a, const ReferenceCounted *b) noexcept
  160. {
  161. return a.get() != b;
  162. }
  163. template <typename ReferenceCounted>
  164. bool operator!=(const ReferenceCounted *a, const irr_ptr<ReferenceCounted> &b) noexcept
  165. {
  166. return a != b.get();
  167. }
  168. /** Same as std::make_unique<T>, but for irr_ptr.
  169. */
  170. template <class T, class... Args>
  171. irr_ptr<T> make_irr(Args&&... args)
  172. {
  173. return irr_ptr<T>(new T(std::forward<Args>(args)...));
  174. }