ref_counted.h 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. /*************************************************************************/
  2. /* ref_counted.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
  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 reference_get_count() const;
  46. RefCounted();
  47. ~RefCounted() {}
  48. };
  49. template <class T>
  50. class Ref {
  51. T *reference = nullptr;
  52. void ref(const Ref &p_from) {
  53. if (p_from.reference == reference) {
  54. return;
  55. }
  56. unref();
  57. reference = p_from.reference;
  58. if (reference) {
  59. reference->reference();
  60. }
  61. }
  62. void ref_pointer(T *p_ref) {
  63. ERR_FAIL_COND(!p_ref);
  64. if (p_ref->init_ref()) {
  65. reference = p_ref;
  66. }
  67. }
  68. //virtual RefCounted * get_reference() const { return reference; }
  69. public:
  70. _FORCE_INLINE_ bool operator==(const T *p_ptr) const {
  71. return reference == p_ptr;
  72. }
  73. _FORCE_INLINE_ bool operator!=(const T *p_ptr) const {
  74. return reference != p_ptr;
  75. }
  76. _FORCE_INLINE_ bool operator<(const Ref<T> &p_r) const {
  77. return reference < p_r.reference;
  78. }
  79. _FORCE_INLINE_ bool operator==(const Ref<T> &p_r) const {
  80. return reference == p_r.reference;
  81. }
  82. _FORCE_INLINE_ bool operator!=(const Ref<T> &p_r) const {
  83. return reference != p_r.reference;
  84. }
  85. _FORCE_INLINE_ T *operator->() {
  86. return reference;
  87. }
  88. _FORCE_INLINE_ T *operator*() {
  89. return reference;
  90. }
  91. _FORCE_INLINE_ const T *operator->() const {
  92. return reference;
  93. }
  94. _FORCE_INLINE_ const T *ptr() const {
  95. return reference;
  96. }
  97. _FORCE_INLINE_ T *ptr() {
  98. return reference;
  99. }
  100. _FORCE_INLINE_ const T *operator*() const {
  101. return reference;
  102. }
  103. operator Variant() const {
  104. return Variant(reference);
  105. }
  106. void operator=(const Ref &p_from) {
  107. ref(p_from);
  108. }
  109. template <class T_Other>
  110. void operator=(const Ref<T_Other> &p_from) {
  111. RefCounted *refb = const_cast<RefCounted *>(static_cast<const RefCounted *>(p_from.ptr()));
  112. if (!refb) {
  113. unref();
  114. return;
  115. }
  116. Ref r;
  117. r.reference = Object::cast_to<T>(refb);
  118. ref(r);
  119. r.reference = nullptr;
  120. }
  121. void operator=(const Variant &p_variant) {
  122. Object *object = p_variant.get_validated_object();
  123. if (object == reference) {
  124. return;
  125. }
  126. unref();
  127. if (!object) {
  128. return;
  129. }
  130. T *r = Object::cast_to<T>(object);
  131. if (r && r->reference()) {
  132. reference = r;
  133. }
  134. }
  135. template <class T_Other>
  136. void reference_ptr(T_Other *p_ptr) {
  137. if (reference == p_ptr) {
  138. return;
  139. }
  140. unref();
  141. T *r = Object::cast_to<T>(p_ptr);
  142. if (r) {
  143. ref_pointer(r);
  144. }
  145. }
  146. Ref(const Ref &p_from) {
  147. ref(p_from);
  148. }
  149. template <class T_Other>
  150. Ref(const Ref<T_Other> &p_from) {
  151. RefCounted *refb = const_cast<RefCounted *>(static_cast<const RefCounted *>(p_from.ptr()));
  152. if (!refb) {
  153. unref();
  154. return;
  155. }
  156. Ref r;
  157. r.reference = Object::cast_to<T>(refb);
  158. ref(r);
  159. r.reference = nullptr;
  160. }
  161. Ref(T *p_reference) {
  162. if (p_reference) {
  163. ref_pointer(p_reference);
  164. }
  165. }
  166. Ref(const Variant &p_variant) {
  167. Object *object = p_variant.get_validated_object();
  168. if (!object) {
  169. return;
  170. }
  171. T *r = Object::cast_to<T>(object);
  172. if (r && r->reference()) {
  173. reference = r;
  174. }
  175. }
  176. inline bool is_valid() const { return reference != nullptr; }
  177. inline bool is_null() const { return reference == nullptr; }
  178. void unref() {
  179. // TODO: this should be moved to mutexes, since this engine does not really
  180. // do a lot of referencing on references and stuff
  181. // mutexes will avoid more crashes?
  182. if (reference && reference->unreference()) {
  183. memdelete(reference);
  184. }
  185. reference = nullptr;
  186. }
  187. void instantiate() {
  188. ref(memnew(T));
  189. }
  190. Ref() {}
  191. ~Ref() {
  192. unref();
  193. }
  194. };
  195. class WeakRef : public RefCounted {
  196. GDCLASS(WeakRef, RefCounted);
  197. ObjectID ref;
  198. protected:
  199. static void _bind_methods();
  200. public:
  201. Variant get_ref() const;
  202. void set_obj(Object *p_object);
  203. void set_ref(const Ref<RefCounted> &p_ref);
  204. WeakRef() {}
  205. };
  206. template <class T>
  207. struct PtrToArg<Ref<T>> {
  208. _FORCE_INLINE_ static Ref<T> convert(const void *p_ptr) {
  209. return Ref<T>(const_cast<T *>(reinterpret_cast<const T *>(p_ptr)));
  210. }
  211. typedef Ref<T> EncodeT;
  212. _FORCE_INLINE_ static void encode(Ref<T> p_val, const void *p_ptr) {
  213. *(const_cast<Ref<RefCounted> *>(reinterpret_cast<const Ref<RefCounted> *>(p_ptr))) = p_val;
  214. }
  215. };
  216. template <class T>
  217. struct PtrToArg<const Ref<T> &> {
  218. typedef Ref<T> EncodeT;
  219. _FORCE_INLINE_ static Ref<T> convert(const void *p_ptr) {
  220. return Ref<T>((T *)p_ptr);
  221. }
  222. };
  223. template <class T>
  224. struct GetTypeInfo<Ref<T>> {
  225. static const Variant::Type VARIANT_TYPE = Variant::OBJECT;
  226. static const GodotTypeInfo::Metadata METADATA = GodotTypeInfo::METADATA_NONE;
  227. static inline PropertyInfo get_class_info() {
  228. return PropertyInfo(Variant::OBJECT, String(), PROPERTY_HINT_RESOURCE_TYPE, T::get_class_static());
  229. }
  230. };
  231. template <class T>
  232. struct GetTypeInfo<const Ref<T> &> {
  233. static const Variant::Type VARIANT_TYPE = Variant::OBJECT;
  234. static const GodotTypeInfo::Metadata METADATA = GodotTypeInfo::METADATA_NONE;
  235. static inline PropertyInfo get_class_info() {
  236. return PropertyInfo(Variant::OBJECT, String(), PROPERTY_HINT_RESOURCE_TYPE, T::get_class_static());
  237. }
  238. };
  239. #endif // REF_COUNTED_H