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. #ifndef REF_COUNTED_H
  31. #define REF_COUNTED_H
  32. #include "core/object/class_db.h"
  33. #include "core/templates/safe_refcount.h"
  34. class RefCounted : public Object {
  35. GDCLASS(RefCounted, Object);
  36. SafeRefCount refcount;
  37. SafeRefCount refcount_init;
  38. protected:
  39. static void _bind_methods();
  40. public:
  41. _FORCE_INLINE_ bool is_referenced() const { return refcount_init.get() != 1; }
  42. bool init_ref();
  43. bool reference(); // returns false if refcount is at zero and didn't get increased
  44. bool unreference();
  45. int get_reference_count() const;
  46. RefCounted();
  47. ~RefCounted() {}
  48. };
  49. template <typename T>
  50. class Ref {
  51. T *reference = nullptr;
  52. _FORCE_INLINE_ void ref(const Ref &p_from) {
  53. ref_pointer<false>(p_from.reference);
  54. }
  55. template <bool Init>
  56. _FORCE_INLINE_ void ref_pointer(T *p_refcounted) {
  57. if (p_refcounted == reference) {
  58. return;
  59. }
  60. // This will go out of scope and get unref'd.
  61. Ref cleanup_ref;
  62. cleanup_ref.reference = reference;
  63. reference = p_refcounted;
  64. if (reference) {
  65. if constexpr (Init) {
  66. if (!reference->init_ref()) {
  67. reference = nullptr;
  68. }
  69. } else {
  70. if (!reference->reference()) {
  71. reference = nullptr;
  72. }
  73. }
  74. }
  75. }
  76. //virtual RefCounted * get_reference() const { return reference; }
  77. public:
  78. _FORCE_INLINE_ bool operator==(const T *p_ptr) const {
  79. return reference == p_ptr;
  80. }
  81. _FORCE_INLINE_ bool operator!=(const T *p_ptr) const {
  82. return reference != p_ptr;
  83. }
  84. #ifdef STRICT_CHECKS
  85. // Delete these to prevent raw comparisons with `nullptr`.
  86. bool operator==(std::nullptr_t) const = delete;
  87. bool operator!=(std::nullptr_t) const = delete;
  88. #endif // STRICT_CHECKS
  89. _FORCE_INLINE_ bool operator<(const Ref<T> &p_r) const {
  90. return reference < p_r.reference;
  91. }
  92. _FORCE_INLINE_ bool operator==(const Ref<T> &p_r) const {
  93. return reference == p_r.reference;
  94. }
  95. _FORCE_INLINE_ bool operator!=(const Ref<T> &p_r) const {
  96. return reference != p_r.reference;
  97. }
  98. _FORCE_INLINE_ T *operator*() const {
  99. return reference;
  100. }
  101. _FORCE_INLINE_ T *operator->() const {
  102. return reference;
  103. }
  104. _FORCE_INLINE_ T *ptr() const {
  105. return reference;
  106. }
  107. operator Variant() const {
  108. return Variant(reference);
  109. }
  110. void operator=(const Ref &p_from) {
  111. ref(p_from);
  112. }
  113. template <typename T_Other>
  114. void operator=(const Ref<T_Other> &p_from) {
  115. ref_pointer<false>(Object::cast_to<T>(p_from.ptr()));
  116. }
  117. void operator=(T *p_from) {
  118. ref_pointer<true>(p_from);
  119. }
  120. void operator=(const Variant &p_variant) {
  121. Object *object = p_variant.get_validated_object();
  122. if (object == reference) {
  123. return;
  124. }
  125. ref_pointer<false>(Object::cast_to<T>(object));
  126. }
  127. template <typename T_Other>
  128. void reference_ptr(T_Other *p_ptr) {
  129. if (reference == p_ptr) {
  130. return;
  131. }
  132. ref_pointer<true>(Object::cast_to<T>(p_ptr));
  133. }
  134. Ref(const Ref &p_from) {
  135. this->operator=(p_from);
  136. }
  137. template <typename T_Other>
  138. Ref(const Ref<T_Other> &p_from) {
  139. this->operator=(p_from);
  140. }
  141. Ref(T *p_from) {
  142. this->operator=(p_from);
  143. }
  144. Ref(const Variant &p_from) {
  145. this->operator=(p_from);
  146. }
  147. inline bool is_valid() const { return reference != nullptr; }
  148. inline bool is_null() const { return reference == nullptr; }
  149. void unref() {
  150. // TODO: this should be moved to mutexes, since this engine does not really
  151. // do a lot of referencing on references and stuff
  152. // mutexes will avoid more crashes?
  153. if (reference && reference->unreference()) {
  154. memdelete(reference);
  155. }
  156. reference = nullptr;
  157. }
  158. template <typename... VarArgs>
  159. void instantiate(VarArgs... p_params) {
  160. ref(memnew(T(p_params...)));
  161. }
  162. Ref() = default;
  163. ~Ref() {
  164. unref();
  165. }
  166. };
  167. class WeakRef : public RefCounted {
  168. GDCLASS(WeakRef, RefCounted);
  169. ObjectID ref;
  170. protected:
  171. static void _bind_methods();
  172. public:
  173. Variant get_ref() const;
  174. void set_obj(Object *p_object);
  175. void set_ref(const Ref<RefCounted> &p_ref);
  176. WeakRef() {}
  177. };
  178. template <typename T>
  179. struct PtrToArg<Ref<T>> {
  180. _FORCE_INLINE_ static Ref<T> convert(const void *p_ptr) {
  181. if (p_ptr == nullptr) {
  182. return Ref<T>();
  183. }
  184. // p_ptr points to a RefCounted object
  185. return Ref<T>(const_cast<T *>(*reinterpret_cast<T *const *>(p_ptr)));
  186. }
  187. typedef Ref<T> EncodeT;
  188. _FORCE_INLINE_ static void encode(Ref<T> p_val, const void *p_ptr) {
  189. // p_ptr points to an EncodeT object which is a Ref<T> object.
  190. *(const_cast<Ref<RefCounted> *>(reinterpret_cast<const Ref<RefCounted> *>(p_ptr))) = p_val;
  191. }
  192. };
  193. template <typename T>
  194. struct PtrToArg<const Ref<T> &> {
  195. typedef Ref<T> EncodeT;
  196. _FORCE_INLINE_ static Ref<T> convert(const void *p_ptr) {
  197. if (p_ptr == nullptr) {
  198. return Ref<T>();
  199. }
  200. // p_ptr points to a RefCounted object
  201. return Ref<T>(*((T *const *)p_ptr));
  202. }
  203. };
  204. template <typename T>
  205. struct GetTypeInfo<Ref<T>> {
  206. static const Variant::Type VARIANT_TYPE = Variant::OBJECT;
  207. static const GodotTypeInfo::Metadata METADATA = GodotTypeInfo::METADATA_NONE;
  208. static inline PropertyInfo get_class_info() {
  209. return PropertyInfo(Variant::OBJECT, String(), PROPERTY_HINT_RESOURCE_TYPE, T::get_class_static());
  210. }
  211. };
  212. template <typename T>
  213. struct GetTypeInfo<const 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. template <typename T>
  226. struct VariantInternalAccessor<const Ref<T> &> {
  227. static _FORCE_INLINE_ Ref<T> get(const Variant *v) { return Ref<T>(*VariantInternal::get_object(v)); }
  228. static _FORCE_INLINE_ void set(Variant *v, const Ref<T> &p_ref) { VariantInternal::object_assign(v, p_ref); }
  229. };
  230. #endif // REF_COUNTED_H