reference.h 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  1. /*************************************************************************/
  2. /* reference.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 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 REFERENCE_H
  31. #define REFERENCE_H
  32. #include "core/class_db.h"
  33. #include "core/object.h"
  34. #include "core/ref_ptr.h"
  35. #include "core/safe_refcount.h"
  36. class Reference : public Object {
  37. GDCLASS(Reference, Object);
  38. friend class RefBase;
  39. SafeRefCount refcount;
  40. SafeRefCount refcount_init;
  41. protected:
  42. static void _bind_methods();
  43. public:
  44. _FORCE_INLINE_ bool is_referenced() const { return refcount_init.get() != 1; }
  45. bool init_ref();
  46. bool reference(); // returns false if refcount is at zero and didn't get increased
  47. bool unreference();
  48. int reference_get_count() const;
  49. Reference();
  50. ~Reference();
  51. };
  52. template <class T>
  53. class Ref {
  54. T *reference;
  55. void ref(const Ref &p_from) {
  56. if (p_from.reference == reference)
  57. return;
  58. unref();
  59. reference = p_from.reference;
  60. if (reference)
  61. reference->reference();
  62. }
  63. void ref_pointer(T *p_ref) {
  64. ERR_FAIL_COND(!p_ref);
  65. if (p_ref->init_ref())
  66. reference = p_ref;
  67. }
  68. //virtual Reference * 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. RefPtr get_ref_ptr() const {
  104. RefPtr refptr;
  105. Ref<Reference> *irr = reinterpret_cast<Ref<Reference> *>(refptr.get_data());
  106. *irr = *this;
  107. return refptr;
  108. };
  109. operator Variant() const {
  110. return Variant(get_ref_ptr());
  111. }
  112. void operator=(const Ref &p_from) {
  113. ref(p_from);
  114. }
  115. template <class T_Other>
  116. void operator=(const Ref<T_Other> &p_from) {
  117. Reference *refb = const_cast<Reference *>(static_cast<const Reference *>(p_from.ptr()));
  118. if (!refb) {
  119. unref();
  120. return;
  121. }
  122. Ref r;
  123. r.reference = Object::cast_to<T>(refb);
  124. ref(r);
  125. r.reference = NULL;
  126. }
  127. void operator=(const RefPtr &p_refptr) {
  128. Ref<Reference> *irr = reinterpret_cast<Ref<Reference> *>(p_refptr.get_data());
  129. Reference *refb = irr->ptr();
  130. if (!refb) {
  131. unref();
  132. return;
  133. }
  134. Ref r;
  135. r.reference = Object::cast_to<T>(refb);
  136. ref(r);
  137. r.reference = NULL;
  138. }
  139. void operator=(const Variant &p_variant) {
  140. RefPtr refptr = p_variant;
  141. Ref<Reference> *irr = reinterpret_cast<Ref<Reference> *>(refptr.get_data());
  142. Reference *refb = irr->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 = NULL;
  151. }
  152. template <class T_Other>
  153. void reference_ptr(T_Other *p_ptr) {
  154. if (reference == p_ptr) {
  155. return;
  156. }
  157. unref();
  158. T *r = Object::cast_to<T>(p_ptr);
  159. if (r) {
  160. ref_pointer(r);
  161. }
  162. }
  163. Ref(const Ref &p_from) {
  164. reference = NULL;
  165. ref(p_from);
  166. }
  167. template <class T_Other>
  168. Ref(const Ref<T_Other> &p_from) {
  169. reference = NULL;
  170. Reference *refb = const_cast<Reference *>(static_cast<const Reference *>(p_from.ptr()));
  171. if (!refb) {
  172. unref();
  173. return;
  174. }
  175. Ref r;
  176. r.reference = Object::cast_to<T>(refb);
  177. ref(r);
  178. r.reference = NULL;
  179. }
  180. Ref(T *p_reference) {
  181. reference = NULL;
  182. if (p_reference)
  183. ref_pointer(p_reference);
  184. }
  185. Ref(const Variant &p_variant) {
  186. RefPtr refptr = p_variant;
  187. Ref<Reference> *irr = reinterpret_cast<Ref<Reference> *>(refptr.get_data());
  188. reference = NULL;
  189. Reference *refb = irr->ptr();
  190. if (!refb) {
  191. unref();
  192. return;
  193. }
  194. Ref r;
  195. r.reference = Object::cast_to<T>(refb);
  196. ref(r);
  197. r.reference = NULL;
  198. }
  199. Ref(const RefPtr &p_refptr) {
  200. Ref<Reference> *irr = reinterpret_cast<Ref<Reference> *>(p_refptr.get_data());
  201. reference = NULL;
  202. Reference *refb = irr->ptr();
  203. if (!refb) {
  204. unref();
  205. return;
  206. }
  207. Ref r;
  208. r.reference = Object::cast_to<T>(refb);
  209. ref(r);
  210. r.reference = NULL;
  211. }
  212. inline bool is_valid() const { return reference != NULL; }
  213. inline bool is_null() const { return reference == NULL; }
  214. void unref() {
  215. //TODO this should be moved to mutexes, since this engine does not really
  216. // do a lot of referencing on references and stuff
  217. // mutexes will avoid more crashes?
  218. if (reference && reference->unreference()) {
  219. memdelete(reference);
  220. }
  221. reference = NULL;
  222. }
  223. void instance() {
  224. ref(memnew(T));
  225. }
  226. Ref() {
  227. reference = NULL;
  228. }
  229. ~Ref() {
  230. unref();
  231. }
  232. };
  233. typedef Ref<Reference> REF;
  234. class WeakRef : public Reference {
  235. GDCLASS(WeakRef, Reference);
  236. ObjectID ref;
  237. protected:
  238. static void _bind_methods();
  239. public:
  240. Variant get_ref() const;
  241. void set_obj(Object *p_object);
  242. void set_ref(const REF &p_ref);
  243. WeakRef();
  244. };
  245. #ifdef PTRCALL_ENABLED
  246. template <class T>
  247. struct PtrToArg<Ref<T> > {
  248. _FORCE_INLINE_ static Ref<T> convert(const void *p_ptr) {
  249. return Ref<T>(const_cast<T *>(reinterpret_cast<const T *>(p_ptr)));
  250. }
  251. _FORCE_INLINE_ static void encode(Ref<T> p_val, const void *p_ptr) {
  252. *(Ref<Reference> *)p_ptr = p_val;
  253. }
  254. };
  255. template <class T>
  256. struct PtrToArg<const Ref<T> &> {
  257. _FORCE_INLINE_ static Ref<T> convert(const void *p_ptr) {
  258. return Ref<T>((T *)p_ptr);
  259. }
  260. };
  261. //this is for RefPtr
  262. template <>
  263. struct PtrToArg<RefPtr> {
  264. _FORCE_INLINE_ static RefPtr convert(const void *p_ptr) {
  265. return Ref<Reference>(const_cast<Reference *>(reinterpret_cast<const Reference *>(p_ptr))).get_ref_ptr();
  266. }
  267. _FORCE_INLINE_ static void encode(RefPtr p_val, const void *p_ptr) {
  268. Ref<Reference> r = p_val;
  269. *(Ref<Reference> *)p_ptr = r;
  270. }
  271. };
  272. template <>
  273. struct PtrToArg<const RefPtr &> {
  274. _FORCE_INLINE_ static RefPtr convert(const void *p_ptr) {
  275. return Ref<Reference>(const_cast<Reference *>(reinterpret_cast<const Reference *>(p_ptr))).get_ref_ptr();
  276. }
  277. };
  278. #endif // PTRCALL_ENABLED
  279. #ifdef DEBUG_METHODS_ENABLED
  280. template <class T>
  281. struct GetTypeInfo<Ref<T> > {
  282. static const Variant::Type VARIANT_TYPE = Variant::OBJECT;
  283. static const GodotTypeInfo::Metadata METADATA = GodotTypeInfo::METADATA_NONE;
  284. static inline PropertyInfo get_class_info() {
  285. return PropertyInfo(Variant::OBJECT, String(), PROPERTY_HINT_RESOURCE_TYPE, T::get_class_static());
  286. }
  287. };
  288. template <class T>
  289. struct GetTypeInfo<const Ref<T> &> {
  290. static const Variant::Type VARIANT_TYPE = Variant::OBJECT;
  291. static const GodotTypeInfo::Metadata METADATA = GodotTypeInfo::METADATA_NONE;
  292. static inline PropertyInfo get_class_info() {
  293. return PropertyInfo(Variant::OBJECT, String(), PROPERTY_HINT_RESOURCE_TYPE, T::get_class_static());
  294. }
  295. };
  296. #endif // DEBUG_METHODS_ENABLED
  297. #endif // REFERENCE_H