irr_ptr.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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 "irrlichttypes.h"
  19. #include "IReferenceCounted.h"
  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 = typename std::enable_if<std::is_base_of<IReferenceCounted,
  36. ReferenceCounted>::value>::type>
  37. class irr_ptr
  38. {
  39. ReferenceCounted *value = nullptr;
  40. public:
  41. irr_ptr() {}
  42. irr_ptr(std::nullptr_t) noexcept {}
  43. irr_ptr(const irr_ptr &b) noexcept { grab(b.get()); }
  44. irr_ptr(irr_ptr &&b) noexcept { reset(b.release()); }
  45. template <typename B, class = typename std::enable_if<std::is_convertible<B *,
  46. ReferenceCounted *>::value>::type>
  47. irr_ptr(const irr_ptr<B> &b) noexcept
  48. {
  49. grab(b.get());
  50. }
  51. template <typename B, class = typename std::enable_if<std::is_convertible<B *,
  52. ReferenceCounted *>::value>::type>
  53. irr_ptr(irr_ptr<B> &&b) noexcept
  54. {
  55. reset(b.release());
  56. }
  57. /** Constructs a shared pointer out of a plain one to control object lifetime.
  58. * @param object The object, usually returned by some @c create* function.
  59. * @note Move semantics: reference counter is *not* increased.
  60. * @warning Never wrap any @c add* function with this!
  61. */
  62. explicit irr_ptr(ReferenceCounted *object) noexcept { reset(object); }
  63. ~irr_ptr() { reset(); }
  64. irr_ptr &operator=(const irr_ptr &b) noexcept
  65. {
  66. grab(b.get());
  67. return *this;
  68. }
  69. irr_ptr &operator=(irr_ptr &&b) noexcept
  70. {
  71. reset(b.release());
  72. return *this;
  73. }
  74. template <typename B, class = typename std::enable_if<std::is_convertible<B *,
  75. ReferenceCounted *>::value>::type>
  76. irr_ptr &operator=(const irr_ptr<B> &b) noexcept
  77. {
  78. grab(b.get());
  79. return *this;
  80. }
  81. template <typename B, class = typename std::enable_if<std::is_convertible<B *,
  82. ReferenceCounted *>::value>::type>
  83. irr_ptr &operator=(irr_ptr<B> &&b) noexcept
  84. {
  85. reset(b.release());
  86. return *this;
  87. }
  88. ReferenceCounted &operator*() const noexcept { return *value; }
  89. ReferenceCounted *operator->() const noexcept { return value; }
  90. explicit operator ReferenceCounted *() const noexcept { return value; }
  91. explicit operator bool() const noexcept { return !!value; }
  92. /** Returns the stored pointer.
  93. */
  94. ReferenceCounted *get() const noexcept { return value; }
  95. /** Returns the stored pointer, erasing it from this class.
  96. * @note Move semantics: reference counter is not changed.
  97. */
  98. ReferenceCounted *release() noexcept
  99. {
  100. ReferenceCounted *object = value;
  101. value = nullptr;
  102. return object;
  103. }
  104. /** Drops stored pointer replacing it with the given one.
  105. * @note Move semantics: reference counter is *not* increased.
  106. */
  107. void reset(ReferenceCounted *object = nullptr) noexcept
  108. {
  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. if (object)
  119. object->grab();
  120. reset(object);
  121. }
  122. };
  123. // clang-format off
  124. // ^ dislikes long lines
  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 acheive 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. irr_ptr<ReferenceCounted> grab(ReferenceCounted *object) noexcept
  134. {
  135. irr_ptr<ReferenceCounted> ptr;
  136. ptr.grab(object);
  137. return ptr;
  138. }
  139. template <typename ReferenceCounted>
  140. bool operator==(const irr_ptr<ReferenceCounted> &a, const irr_ptr<ReferenceCounted> &b) noexcept
  141. {
  142. return a.get() == b.get();
  143. }
  144. template <typename ReferenceCounted>
  145. bool operator==(const irr_ptr<ReferenceCounted> &a, const ReferenceCounted *b) noexcept
  146. {
  147. return a.get() == b;
  148. }
  149. template <typename ReferenceCounted>
  150. bool operator==(const ReferenceCounted *a, const irr_ptr<ReferenceCounted> &b) noexcept
  151. {
  152. return a == b.get();
  153. }
  154. template <typename ReferenceCounted>
  155. bool operator!=(const irr_ptr<ReferenceCounted> &a, const irr_ptr<ReferenceCounted> &b) noexcept
  156. {
  157. return a.get() != b.get();
  158. }
  159. template <typename ReferenceCounted>
  160. bool operator!=(const irr_ptr<ReferenceCounted> &a, const ReferenceCounted *b) noexcept
  161. {
  162. return a.get() != b;
  163. }
  164. template <typename ReferenceCounted>
  165. bool operator!=(const ReferenceCounted *a, const irr_ptr<ReferenceCounted> &b) noexcept
  166. {
  167. return a != b.get();
  168. }
  169. // clang-format on