reference.h 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. /**************************************************************************/
  2. /* reference.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 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. }
  59. unref();
  60. reference = p_from.reference;
  61. if (reference) {
  62. reference->reference();
  63. }
  64. }
  65. void ref_pointer(T *p_ref) {
  66. ERR_FAIL_COND(!p_ref);
  67. if (p_ref->init_ref()) {
  68. reference = p_ref;
  69. }
  70. }
  71. //virtual Reference * get_reference() const { return reference; }
  72. public:
  73. _FORCE_INLINE_ bool operator==(const T *p_ptr) const {
  74. return reference == p_ptr;
  75. }
  76. _FORCE_INLINE_ bool operator!=(const T *p_ptr) const {
  77. return reference != p_ptr;
  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_ bool operator!=(const Ref<T> &p_r) const {
  86. return reference != p_r.reference;
  87. }
  88. _FORCE_INLINE_ T *operator->() {
  89. return reference;
  90. }
  91. _FORCE_INLINE_ T *operator*() {
  92. return reference;
  93. }
  94. _FORCE_INLINE_ const T *operator->() const {
  95. return reference;
  96. }
  97. _FORCE_INLINE_ const T *ptr() const {
  98. return reference;
  99. }
  100. _FORCE_INLINE_ T *ptr() {
  101. return reference;
  102. }
  103. _FORCE_INLINE_ const T *operator*() const {
  104. return reference;
  105. }
  106. RefPtr get_ref_ptr() const {
  107. RefPtr refptr;
  108. Ref<Reference> *irr = reinterpret_cast<Ref<Reference> *>(refptr.get_data());
  109. *irr = *this;
  110. return refptr;
  111. };
  112. operator Variant() const {
  113. return Variant(get_ref_ptr());
  114. }
  115. void operator=(const Ref &p_from) {
  116. ref(p_from);
  117. }
  118. template <class T_Other>
  119. void operator=(const Ref<T_Other> &p_from) {
  120. Reference *refb = const_cast<Reference *>(static_cast<const Reference *>(p_from.ptr()));
  121. if (!refb) {
  122. unref();
  123. return;
  124. }
  125. Ref r;
  126. r.reference = Object::cast_to<T>(refb);
  127. ref(r);
  128. r.reference = nullptr;
  129. }
  130. void operator=(const RefPtr &p_refptr) {
  131. Ref<Reference> *irr = reinterpret_cast<Ref<Reference> *>(p_refptr.get_data());
  132. Reference *refb = irr->ptr();
  133. if (!refb) {
  134. unref();
  135. return;
  136. }
  137. Ref r;
  138. r.reference = Object::cast_to<T>(refb);
  139. ref(r);
  140. r.reference = nullptr;
  141. }
  142. void operator=(const Variant &p_variant) {
  143. RefPtr refptr = p_variant;
  144. Ref<Reference> *irr = reinterpret_cast<Ref<Reference> *>(refptr.get_data());
  145. Reference *refb = irr->ptr();
  146. if (!refb) {
  147. unref();
  148. return;
  149. }
  150. Ref r;
  151. r.reference = Object::cast_to<T>(refb);
  152. ref(r);
  153. r.reference = nullptr;
  154. }
  155. template <class T_Other>
  156. void reference_ptr(T_Other *p_ptr) {
  157. if (reference == p_ptr) {
  158. return;
  159. }
  160. unref();
  161. T *r = Object::cast_to<T>(p_ptr);
  162. if (r) {
  163. ref_pointer(r);
  164. }
  165. }
  166. Ref(const Ref &p_from) {
  167. reference = nullptr;
  168. ref(p_from);
  169. }
  170. template <class T_Other>
  171. Ref(const Ref<T_Other> &p_from) {
  172. reference = nullptr;
  173. Reference *refb = const_cast<Reference *>(static_cast<const Reference *>(p_from.ptr()));
  174. if (!refb) {
  175. unref();
  176. return;
  177. }
  178. Ref r;
  179. r.reference = Object::cast_to<T>(refb);
  180. ref(r);
  181. r.reference = nullptr;
  182. }
  183. Ref(T *p_reference) {
  184. reference = nullptr;
  185. if (p_reference) {
  186. ref_pointer(p_reference);
  187. }
  188. }
  189. Ref(const Variant &p_variant) {
  190. RefPtr refptr = p_variant;
  191. Ref<Reference> *irr = reinterpret_cast<Ref<Reference> *>(refptr.get_data());
  192. reference = nullptr;
  193. Reference *refb = irr->ptr();
  194. if (!refb) {
  195. unref();
  196. return;
  197. }
  198. Ref r;
  199. r.reference = Object::cast_to<T>(refb);
  200. ref(r);
  201. r.reference = nullptr;
  202. }
  203. Ref(const RefPtr &p_refptr) {
  204. Ref<Reference> *irr = reinterpret_cast<Ref<Reference> *>(p_refptr.get_data());
  205. reference = nullptr;
  206. Reference *refb = irr->ptr();
  207. if (!refb) {
  208. unref();
  209. return;
  210. }
  211. Ref r;
  212. r.reference = Object::cast_to<T>(refb);
  213. ref(r);
  214. r.reference = nullptr;
  215. }
  216. inline bool is_valid() const { return reference != nullptr; }
  217. inline bool is_null() const { return reference == nullptr; }
  218. void unref() {
  219. //TODO this should be moved to mutexes, since this engine does not really
  220. // do a lot of referencing on references and stuff
  221. // mutexes will avoid more crashes?
  222. if (reference && reference->unreference()) {
  223. memdelete(reference);
  224. }
  225. reference = nullptr;
  226. }
  227. void instance() {
  228. ref(memnew(T));
  229. }
  230. Ref() {
  231. reference = nullptr;
  232. }
  233. ~Ref() {
  234. unref();
  235. }
  236. };
  237. typedef Ref<Reference> REF;
  238. class WeakRef : public Reference {
  239. GDCLASS(WeakRef, Reference);
  240. ObjectID ref;
  241. protected:
  242. static void _bind_methods();
  243. public:
  244. Variant get_ref() const;
  245. void set_obj(Object *p_object);
  246. void set_ref(const REF &p_ref);
  247. WeakRef();
  248. };
  249. #ifdef PTRCALL_ENABLED
  250. template <class T>
  251. struct PtrToArg<Ref<T>> {
  252. _FORCE_INLINE_ static Ref<T> convert(const void *p_ptr) {
  253. return Ref<T>(const_cast<T *>(reinterpret_cast<const T *>(p_ptr)));
  254. }
  255. _FORCE_INLINE_ static void encode(Ref<T> p_val, const void *p_ptr) {
  256. *(Ref<Reference> *)p_ptr = p_val;
  257. }
  258. };
  259. template <class T>
  260. struct PtrToArg<const Ref<T> &> {
  261. _FORCE_INLINE_ static Ref<T> convert(const void *p_ptr) {
  262. return Ref<T>((T *)p_ptr);
  263. }
  264. };
  265. //this is for RefPtr
  266. template <>
  267. struct PtrToArg<RefPtr> {
  268. _FORCE_INLINE_ static RefPtr convert(const void *p_ptr) {
  269. return Ref<Reference>(const_cast<Reference *>(reinterpret_cast<const Reference *>(p_ptr))).get_ref_ptr();
  270. }
  271. _FORCE_INLINE_ static void encode(RefPtr p_val, const void *p_ptr) {
  272. Ref<Reference> r = p_val;
  273. *(Ref<Reference> *)p_ptr = r;
  274. }
  275. };
  276. template <>
  277. struct PtrToArg<const RefPtr &> {
  278. _FORCE_INLINE_ static RefPtr convert(const void *p_ptr) {
  279. return Ref<Reference>(const_cast<Reference *>(reinterpret_cast<const Reference *>(p_ptr))).get_ref_ptr();
  280. }
  281. };
  282. #endif // PTRCALL_ENABLED
  283. template <class T>
  284. struct GetTypeInfo<Ref<T>> {
  285. static const Variant::Type VARIANT_TYPE = Variant::OBJECT;
  286. static const GodotTypeInfo::Metadata METADATA = GodotTypeInfo::METADATA_NONE;
  287. static inline PropertyInfo get_class_info() {
  288. return PropertyInfo(Variant::OBJECT, String(), PROPERTY_HINT_RESOURCE_TYPE, T::get_class_static());
  289. }
  290. };
  291. template <class T>
  292. struct GetTypeInfo<const Ref<T> &> {
  293. static const Variant::Type VARIANT_TYPE = Variant::OBJECT;
  294. static const GodotTypeInfo::Metadata METADATA = GodotTypeInfo::METADATA_NONE;
  295. static inline PropertyInfo get_class_info() {
  296. return PropertyInfo(Variant::OBJECT, String(), PROPERTY_HINT_RESOURCE_TYPE, T::get_class_static());
  297. }
  298. };
  299. #endif // REFERENCE_H