gdb_ref_ptr.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. /* Reference-counted smart pointer class
  2. Copyright (C) 2016-2022 Free Software Foundation, Inc.
  3. This file is part of GDB.
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 3 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 General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  14. #ifndef COMMON_GDB_REF_PTR_H
  15. #define COMMON_GDB_REF_PTR_H
  16. #include <cstddef>
  17. namespace gdb
  18. {
  19. /* An instance of this class either holds a reference to a
  20. reference-counted object or is "NULL". Reference counting is
  21. handled externally by a policy class. If the object holds a
  22. reference, then when the object is destroyed, the reference is
  23. decref'd.
  24. Normally an instance is constructed using a pointer. This sort of
  25. initialization lets this class manage the lifetime of that
  26. reference.
  27. Assignment and copy construction will make a new reference as
  28. appropriate. Assignment from a plain pointer is disallowed to
  29. avoid confusion about whether this acquires a new reference;
  30. instead use the "reset" method -- which, like the pointer
  31. constructor, transfers ownership.
  32. The policy class must provide two static methods:
  33. void incref (T *);
  34. void decref (T *);
  35. */
  36. template<typename T, typename Policy>
  37. class ref_ptr
  38. {
  39. public:
  40. /* Create a new NULL instance. */
  41. ref_ptr ()
  42. : m_obj (NULL)
  43. {
  44. }
  45. /* Create a new NULL instance. Note that this is not explicit. */
  46. ref_ptr (const std::nullptr_t)
  47. : m_obj (NULL)
  48. {
  49. }
  50. /* Create a new instance. OBJ is a reference, management of which
  51. is now transferred to this class. */
  52. explicit ref_ptr (T *obj)
  53. : m_obj (obj)
  54. {
  55. }
  56. /* Copy another instance. */
  57. ref_ptr (const ref_ptr &other)
  58. : m_obj (other.m_obj)
  59. {
  60. if (m_obj != NULL)
  61. Policy::incref (m_obj);
  62. }
  63. /* Transfer ownership from OTHER. */
  64. ref_ptr (ref_ptr &&other) noexcept
  65. : m_obj (other.m_obj)
  66. {
  67. other.m_obj = NULL;
  68. }
  69. /* Destroy this instance. */
  70. ~ref_ptr ()
  71. {
  72. if (m_obj != NULL)
  73. Policy::decref (m_obj);
  74. }
  75. /* Copy another instance. */
  76. ref_ptr &operator= (const ref_ptr &other)
  77. {
  78. /* Do nothing on self-assignment. */
  79. if (this != &other)
  80. {
  81. reset (other.m_obj);
  82. if (m_obj != NULL)
  83. Policy::incref (m_obj);
  84. }
  85. return *this;
  86. }
  87. /* Transfer ownership from OTHER. */
  88. ref_ptr &operator= (ref_ptr &&other)
  89. {
  90. /* Do nothing on self-assignment. */
  91. if (this != &other)
  92. {
  93. reset (other.m_obj);
  94. other.m_obj = NULL;
  95. }
  96. return *this;
  97. }
  98. /* Change this instance's referent. OBJ is a reference, management
  99. of which is now transferred to this class. */
  100. void reset (T *obj)
  101. {
  102. if (m_obj != NULL)
  103. Policy::decref (m_obj);
  104. m_obj = obj;
  105. }
  106. /* Return this instance's referent without changing the state of
  107. this class. */
  108. T *get () const
  109. {
  110. return m_obj;
  111. }
  112. /* Return this instance's referent, and stop managing this
  113. reference. The caller is now responsible for the ownership of
  114. the reference. */
  115. ATTRIBUTE_UNUSED_RESULT T *release ()
  116. {
  117. T *result = m_obj;
  118. m_obj = NULL;
  119. return result;
  120. }
  121. /* Let users refer to members of the underlying pointer. */
  122. T *operator-> () const
  123. {
  124. return m_obj;
  125. }
  126. /* Acquire a new reference and return a ref_ptr that owns it. */
  127. static ref_ptr<T, Policy> new_reference (T *obj)
  128. {
  129. Policy::incref (obj);
  130. return ref_ptr<T, Policy> (obj);
  131. }
  132. private:
  133. T *m_obj;
  134. };
  135. template<typename T, typename Policy>
  136. inline bool operator== (const ref_ptr<T, Policy> &lhs,
  137. const ref_ptr<T, Policy> &rhs)
  138. {
  139. return lhs.get () == rhs.get ();
  140. }
  141. template<typename T, typename Policy>
  142. inline bool operator== (const ref_ptr<T, Policy> &lhs, const T *rhs)
  143. {
  144. return lhs.get () == rhs;
  145. }
  146. template<typename T, typename Policy>
  147. inline bool operator== (const ref_ptr<T, Policy> &lhs, const std::nullptr_t)
  148. {
  149. return lhs.get () == nullptr;
  150. }
  151. template<typename T, typename Policy>
  152. inline bool operator== (const T *lhs, const ref_ptr<T, Policy> &rhs)
  153. {
  154. return lhs == rhs.get ();
  155. }
  156. template<typename T, typename Policy>
  157. inline bool operator== (const std::nullptr_t, const ref_ptr<T, Policy> &rhs)
  158. {
  159. return nullptr == rhs.get ();
  160. }
  161. template<typename T, typename Policy>
  162. inline bool operator!= (const ref_ptr<T, Policy> &lhs,
  163. const ref_ptr<T, Policy> &rhs)
  164. {
  165. return lhs.get () != rhs.get ();
  166. }
  167. template<typename T, typename Policy>
  168. inline bool operator!= (const ref_ptr<T, Policy> &lhs, const T *rhs)
  169. {
  170. return lhs.get () != rhs;
  171. }
  172. template<typename T, typename Policy>
  173. inline bool operator!= (const ref_ptr<T, Policy> &lhs, const std::nullptr_t)
  174. {
  175. return lhs.get () != nullptr;
  176. }
  177. template<typename T, typename Policy>
  178. inline bool operator!= (const T *lhs, const ref_ptr<T, Policy> &rhs)
  179. {
  180. return lhs != rhs.get ();
  181. }
  182. template<typename T, typename Policy>
  183. inline bool operator!= (const std::nullptr_t, const ref_ptr<T, Policy> &rhs)
  184. {
  185. return nullptr != rhs.get ();
  186. }
  187. }
  188. #endif /* COMMON_GDB_REF_PTR_H */