ref_counted.h 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  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 <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*() const {
  86. return reference;
  87. }
  88. _FORCE_INLINE_ T *operator->() const {
  89. return reference;
  90. }
  91. _FORCE_INLINE_ T *ptr() const {
  92. return reference;
  93. }
  94. operator Variant() const {
  95. return Variant(reference);
  96. }
  97. void operator=(const Ref &p_from) {
  98. ref(p_from);
  99. }
  100. template <class T_Other>
  101. void operator=(const Ref<T_Other> &p_from) {
  102. RefCounted *refb = const_cast<RefCounted *>(static_cast<const RefCounted *>(p_from.ptr()));
  103. if (!refb) {
  104. unref();
  105. return;
  106. }
  107. Ref r;
  108. r.reference = Object::cast_to<T>(refb);
  109. ref(r);
  110. r.reference = nullptr;
  111. }
  112. void operator=(const Variant &p_variant) {
  113. Object *object = p_variant.get_validated_object();
  114. if (object == reference) {
  115. return;
  116. }
  117. unref();
  118. if (!object) {
  119. return;
  120. }
  121. T *r = Object::cast_to<T>(object);
  122. if (r && r->reference()) {
  123. reference = r;
  124. }
  125. }
  126. template <class T_Other>
  127. void reference_ptr(T_Other *p_ptr) {
  128. if (reference == p_ptr) {
  129. return;
  130. }
  131. unref();
  132. T *r = Object::cast_to<T>(p_ptr);
  133. if (r) {
  134. ref_pointer(r);
  135. }
  136. }
  137. Ref(const Ref &p_from) {
  138. ref(p_from);
  139. }
  140. template <class T_Other>
  141. Ref(const Ref<T_Other> &p_from) {
  142. RefCounted *refb = const_cast<RefCounted *>(static_cast<const RefCounted *>(p_from.ptr()));
  143. if (!refb) {
  144. unref();
  145. return;
  146. }
  147. Ref r;
  148. r.reference = Object::cast_to<T>(refb);
  149. ref(r);
  150. r.reference = nullptr;
  151. }
  152. Ref(T *p_reference) {
  153. if (p_reference) {
  154. ref_pointer(p_reference);
  155. }
  156. }
  157. Ref(const Variant &p_variant) {
  158. Object *object = p_variant.get_validated_object();
  159. if (!object) {
  160. return;
  161. }
  162. T *r = Object::cast_to<T>(object);
  163. if (r && r->reference()) {
  164. reference = r;
  165. }
  166. }
  167. inline bool is_valid() const { return reference != nullptr; }
  168. inline bool is_null() const { return reference == nullptr; }
  169. void unref() {
  170. // TODO: this should be moved to mutexes, since this engine does not really
  171. // do a lot of referencing on references and stuff
  172. // mutexes will avoid more crashes?
  173. if (reference && reference->unreference()) {
  174. memdelete(reference);
  175. }
  176. reference = nullptr;
  177. }
  178. void instantiate() {
  179. ref(memnew(T));
  180. }
  181. Ref() {}
  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 <class 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>(const_cast<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 <class T>
  213. struct PtrToArg<const Ref<T> &> {
  214. typedef Ref<T> EncodeT;
  215. _FORCE_INLINE_ static Ref<T> convert(const void *p_ptr) {
  216. if (p_ptr == nullptr) {
  217. return Ref<T>();
  218. }
  219. // p_ptr points to a RefCounted object
  220. return Ref<T>(*((T *const *)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. template <class T>
  240. struct VariantInternalAccessor<Ref<T>> {
  241. static _FORCE_INLINE_ Ref<T> get(const Variant *v) { return Ref<T>(*VariantInternal::get_object(v)); }
  242. static _FORCE_INLINE_ void set(Variant *v, const Ref<T> &p_ref) { VariantInternal::refcounted_object_assign(v, p_ref.ptr()); }
  243. };
  244. template <class T>
  245. struct VariantInternalAccessor<const Ref<T> &> {
  246. static _FORCE_INLINE_ Ref<T> get(const Variant *v) { return Ref<T>(*VariantInternal::get_object(v)); }
  247. static _FORCE_INLINE_ void set(Variant *v, const Ref<T> &p_ref) { VariantInternal::refcounted_object_assign(v, p_ref.ptr()); }
  248. };
  249. #endif // REF_COUNTED_H