ref_counted.h 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. /**************************************************************************/
  2. /* ref_counted.h */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #pragma once
  31. #include "core/object/class_db.h"
  32. #include "core/templates/safe_refcount.h"
  33. class RefCounted : public Object {
  34. GDCLASS(RefCounted, Object);
  35. SafeRefCount refcount;
  36. SafeRefCount refcount_init;
  37. protected:
  38. static void _bind_methods();
  39. public:
  40. _FORCE_INLINE_ bool is_referenced() const { return refcount_init.get() != 1; }
  41. bool init_ref();
  42. bool reference(); // returns false if refcount is at zero and didn't get increased
  43. bool unreference();
  44. int get_reference_count() const;
  45. RefCounted();
  46. ~RefCounted() {}
  47. };
  48. template <typename T>
  49. class Ref {
  50. T *reference = nullptr;
  51. _FORCE_INLINE_ void ref(const Ref &p_from) {
  52. ref_pointer<false>(p_from.reference);
  53. }
  54. template <bool Init>
  55. _FORCE_INLINE_ void ref_pointer(T *p_refcounted) {
  56. if (p_refcounted == reference) {
  57. return;
  58. }
  59. // This will go out of scope and get unref'd.
  60. Ref cleanup_ref;
  61. cleanup_ref.reference = reference;
  62. reference = p_refcounted;
  63. if (reference) {
  64. if constexpr (Init) {
  65. if (!reference->init_ref()) {
  66. reference = nullptr;
  67. }
  68. } else {
  69. if (!reference->reference()) {
  70. reference = nullptr;
  71. }
  72. }
  73. }
  74. }
  75. //virtual RefCounted * get_reference() const { return reference; }
  76. public:
  77. static _FORCE_INLINE_ String get_class_static() {
  78. return T::get_class_static();
  79. }
  80. _FORCE_INLINE_ bool operator==(const T *p_ptr) const {
  81. return reference == p_ptr;
  82. }
  83. _FORCE_INLINE_ bool operator!=(const T *p_ptr) const {
  84. return reference != p_ptr;
  85. }
  86. #ifdef STRICT_CHECKS
  87. // Delete these to prevent raw comparisons with `nullptr`.
  88. bool operator==(std::nullptr_t) const = delete;
  89. bool operator!=(std::nullptr_t) const = delete;
  90. #endif // STRICT_CHECKS
  91. _FORCE_INLINE_ bool operator<(const Ref<T> &p_r) const {
  92. return reference < p_r.reference;
  93. }
  94. _FORCE_INLINE_ bool operator==(const Ref<T> &p_r) const {
  95. return reference == p_r.reference;
  96. }
  97. _FORCE_INLINE_ bool operator!=(const Ref<T> &p_r) const {
  98. return reference != p_r.reference;
  99. }
  100. _FORCE_INLINE_ T *operator*() const {
  101. return reference;
  102. }
  103. _FORCE_INLINE_ T *operator->() const {
  104. return reference;
  105. }
  106. _FORCE_INLINE_ T *ptr() const {
  107. return reference;
  108. }
  109. operator Variant() const {
  110. return Variant(reference);
  111. }
  112. void operator=(const Ref &p_from) {
  113. ref(p_from);
  114. }
  115. void operator=(Ref &&p_from) {
  116. if (reference == p_from.reference) {
  117. return;
  118. }
  119. unref();
  120. reference = p_from.reference;
  121. p_from.reference = nullptr;
  122. }
  123. template <typename T_Other>
  124. void operator=(const Ref<T_Other> &p_from) {
  125. ref_pointer<false>(Object::cast_to<T>(p_from.ptr()));
  126. }
  127. void operator=(T *p_from) {
  128. ref_pointer<true>(p_from);
  129. }
  130. void operator=(const Variant &p_variant) {
  131. Object *object = p_variant.get_validated_object();
  132. if (object == reference) {
  133. return;
  134. }
  135. ref_pointer<false>(Object::cast_to<T>(object));
  136. }
  137. template <typename T_Other>
  138. void reference_ptr(T_Other *p_ptr) {
  139. if (reference == p_ptr) {
  140. return;
  141. }
  142. ref_pointer<true>(Object::cast_to<T>(p_ptr));
  143. }
  144. Ref(const Ref &p_from) {
  145. this->operator=(p_from);
  146. }
  147. Ref(Ref &&p_from) {
  148. reference = p_from.reference;
  149. p_from.reference = nullptr;
  150. }
  151. template <typename T_Other>
  152. Ref(const Ref<T_Other> &p_from) {
  153. this->operator=(p_from);
  154. }
  155. Ref(T *p_from) {
  156. this->operator=(p_from);
  157. }
  158. Ref(const Variant &p_from) {
  159. this->operator=(p_from);
  160. }
  161. inline bool is_valid() const { return reference != nullptr; }
  162. inline bool is_null() const { return reference == nullptr; }
  163. void unref() {
  164. // TODO: this should be moved to mutexes, since this engine does not really
  165. // do a lot of referencing on references and stuff
  166. // mutexes will avoid more crashes?
  167. if (reference) {
  168. // NOTE: `reinterpret_cast` is "safe" here because we know `T` has simple linear
  169. // inheritance to `RefCounted`. This guarantees that `T * == `RefCounted *`, which
  170. // allows us to declare `Ref<T>` with forward declared `T` types.
  171. if (reinterpret_cast<RefCounted *>(reference)->unreference()) {
  172. memdelete(reinterpret_cast<RefCounted *>(reference));
  173. }
  174. reference = nullptr;
  175. }
  176. }
  177. template <typename... VarArgs>
  178. void instantiate(VarArgs... p_params) {
  179. ref(memnew(T(p_params...)));
  180. }
  181. Ref() = default;
  182. ~Ref() {
  183. unref();
  184. }
  185. };
  186. class WeakRef : public RefCounted {
  187. GDCLASS(WeakRef, RefCounted);
  188. ObjectID ref;
  189. protected:
  190. static void _bind_methods();
  191. public:
  192. Variant get_ref() const;
  193. void set_obj(Object *p_object);
  194. void set_ref(const Ref<RefCounted> &p_ref);
  195. WeakRef() {}
  196. };
  197. template <typename T>
  198. struct PtrToArg<Ref<T>> {
  199. _FORCE_INLINE_ static Ref<T> convert(const void *p_ptr) {
  200. if (p_ptr == nullptr) {
  201. return Ref<T>();
  202. }
  203. // p_ptr points to a RefCounted object
  204. return Ref<T>(*reinterpret_cast<T *const *>(p_ptr));
  205. }
  206. typedef Ref<T> EncodeT;
  207. _FORCE_INLINE_ static void encode(Ref<T> p_val, const void *p_ptr) {
  208. // p_ptr points to an EncodeT object which is a Ref<T> object.
  209. *(const_cast<Ref<RefCounted> *>(reinterpret_cast<const Ref<RefCounted> *>(p_ptr))) = p_val;
  210. }
  211. };
  212. template <typename T>
  213. struct GetTypeInfo<Ref<T>> {
  214. static const Variant::Type VARIANT_TYPE = Variant::OBJECT;
  215. static const GodotTypeInfo::Metadata METADATA = GodotTypeInfo::METADATA_NONE;
  216. static inline PropertyInfo get_class_info() {
  217. return PropertyInfo(Variant::OBJECT, String(), PROPERTY_HINT_RESOURCE_TYPE, T::get_class_static());
  218. }
  219. };
  220. template <typename T>
  221. struct VariantInternalAccessor<Ref<T>> {
  222. static _FORCE_INLINE_ Ref<T> get(const Variant *v) { return Ref<T>(*VariantInternal::get_object(v)); }
  223. static _FORCE_INLINE_ void set(Variant *v, const Ref<T> &p_ref) { VariantInternal::object_assign(v, p_ref); }
  224. };
  225. // Zero-constructing Ref initializes reference to nullptr (and thus empty).
  226. template <typename T>
  227. struct is_zero_constructible<Ref<T>> : std::true_type {};
  228. template <typename T>
  229. Ref<T> ObjectDB::get_ref(ObjectID p_instance_id) {
  230. return Ref<T>(get_instance(p_instance_id));
  231. }