irr_ptr.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. *
  26. * It should *never* be used for engine-managed objects, including
  27. * those created with @c addTexture and similar methods.
  28. */
  29. template <class ReferenceCounted,
  30. class = typename std::enable_if<std::is_base_of<IReferenceCounted,
  31. ReferenceCounted>::value>::type>
  32. class irr_ptr
  33. {
  34. ReferenceCounted *value = nullptr;
  35. /** Drops stored pointer replacing it with the given one.
  36. * @note Copy semantics: reference counter *is* increased.
  37. */
  38. void grab(ReferenceCounted *object)
  39. {
  40. if (object)
  41. object->grab();
  42. reset(object);
  43. }
  44. public:
  45. irr_ptr() {}
  46. irr_ptr(std::nullptr_t) noexcept {}
  47. irr_ptr(const irr_ptr &b) noexcept { grab(b.get()); }
  48. irr_ptr(irr_ptr &&b) noexcept { reset(b.release()); }
  49. template <typename B, class = typename std::enable_if<std::is_convertible<B *,
  50. ReferenceCounted *>::value>::type>
  51. irr_ptr(const irr_ptr<B> &b) noexcept
  52. {
  53. grab(b.get());
  54. }
  55. template <typename B, class = typename std::enable_if<std::is_convertible<B *,
  56. ReferenceCounted *>::value>::type>
  57. irr_ptr(irr_ptr<B> &&b) noexcept
  58. {
  59. reset(b.release());
  60. }
  61. /** Constructs a shared pointer out of a plain one
  62. * @note Move semantics: reference counter is *not* increased.
  63. */
  64. explicit irr_ptr(ReferenceCounted *object) noexcept { reset(object); }
  65. ~irr_ptr() { reset(); }
  66. irr_ptr &operator=(const irr_ptr &b) noexcept
  67. {
  68. grab(b.get());
  69. return *this;
  70. }
  71. irr_ptr &operator=(irr_ptr &&b) noexcept
  72. {
  73. reset(b.release());
  74. return *this;
  75. }
  76. template <typename B, class = typename std::enable_if<std::is_convertible<B *,
  77. ReferenceCounted *>::value>::type>
  78. irr_ptr &operator=(const irr_ptr<B> &b) noexcept
  79. {
  80. grab(b.get());
  81. return *this;
  82. }
  83. template <typename B, class = typename std::enable_if<std::is_convertible<B *,
  84. ReferenceCounted *>::value>::type>
  85. irr_ptr &operator=(irr_ptr<B> &&b) noexcept
  86. {
  87. reset(b.release());
  88. return *this;
  89. }
  90. ReferenceCounted &operator*() const noexcept { return *value; }
  91. ReferenceCounted *operator->() const noexcept { return value; }
  92. explicit operator ReferenceCounted *() const noexcept { return value; }
  93. explicit operator bool() const noexcept { return !!value; }
  94. /** Returns the stored pointer.
  95. */
  96. ReferenceCounted *get() const noexcept { return value; }
  97. /** Returns the stored pointer, erasing it from this class.
  98. * @note Move semantics: reference counter is not changed.
  99. */
  100. ReferenceCounted *release() noexcept
  101. {
  102. ReferenceCounted *object = value;
  103. value = nullptr;
  104. return object;
  105. }
  106. /** Drops stored pointer replacing it with the given one.
  107. * @note Move semantics: reference counter is *not* increased.
  108. */
  109. void reset(ReferenceCounted *object = nullptr) noexcept
  110. {
  111. if (value)
  112. value->drop();
  113. value = object;
  114. }
  115. };