reference.h 8.8 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-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 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. /**
  37. @author Juan Linietsky <[email protected]>
  38. */
  39. class Reference : public Object {
  40. GDCLASS(Reference, Object);
  41. friend class RefBase;
  42. SafeRefCount refcount;
  43. SafeRefCount refcount_init;
  44. protected:
  45. static void _bind_methods();
  46. public:
  47. _FORCE_INLINE_ bool is_referenced() const { return refcount_init.get() < 1; }
  48. bool init_ref();
  49. bool reference(); // returns false if refcount is at zero and didn't get increased
  50. bool unreference();
  51. int reference_get_count() const;
  52. Reference();
  53. ~Reference();
  54. };
  55. template <class T>
  56. class Ref {
  57. T *reference;
  58. void ref(const Ref &p_from) {
  59. if (p_from.reference == reference)
  60. return;
  61. unref();
  62. reference = p_from.reference;
  63. if (reference)
  64. reference->reference();
  65. }
  66. void ref_pointer(T *p_ref) {
  67. ERR_FAIL_COND(!p_ref);
  68. if (p_ref->init_ref())
  69. reference = p_ref;
  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 = NULL;
  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 = NULL;
  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 = NULL;
  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 = NULL;
  168. ref(p_from);
  169. }
  170. template <class T_Other>
  171. Ref(const Ref<T_Other> &p_from) {
  172. reference = NULL;
  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 = NULL;
  182. }
  183. Ref(T *p_reference) {
  184. reference = NULL;
  185. if (p_reference)
  186. ref_pointer(p_reference);
  187. }
  188. Ref(const Variant &p_variant) {
  189. RefPtr refptr = p_variant;
  190. Ref<Reference> *irr = reinterpret_cast<Ref<Reference> *>(refptr.get_data());
  191. reference = NULL;
  192. Reference *refb = irr->ptr();
  193. if (!refb) {
  194. unref();
  195. return;
  196. }
  197. Ref r;
  198. r.reference = Object::cast_to<T>(refb);
  199. ref(r);
  200. r.reference = NULL;
  201. }
  202. Ref(const RefPtr &p_refptr) {
  203. Ref<Reference> *irr = reinterpret_cast<Ref<Reference> *>(p_refptr.get_data());
  204. reference = NULL;
  205. Reference *refb = irr->ptr();
  206. if (!refb) {
  207. unref();
  208. return;
  209. }
  210. Ref r;
  211. r.reference = Object::cast_to<T>(refb);
  212. ref(r);
  213. r.reference = NULL;
  214. }
  215. inline bool is_valid() const { return reference != NULL; }
  216. inline bool is_null() const { return reference == NULL; }
  217. void unref() {
  218. //TODO this should be moved to mutexes, since this engine does not really
  219. // do a lot of referencing on references and stuff
  220. // mutexes will avoid more crashes?
  221. if (reference && reference->unreference()) {
  222. memdelete(reference);
  223. }
  224. reference = NULL;
  225. }
  226. void instance() {
  227. ref(memnew(T));
  228. }
  229. Ref() {
  230. reference = NULL;
  231. }
  232. ~Ref() {
  233. unref();
  234. }
  235. };
  236. typedef Ref<Reference> REF;
  237. class WeakRef : public Reference {
  238. GDCLASS(WeakRef, Reference);
  239. ObjectID ref;
  240. protected:
  241. static void _bind_methods();
  242. public:
  243. Variant get_ref() const;
  244. void set_obj(Object *p_object);
  245. void set_ref(const REF &p_ref);
  246. WeakRef();
  247. };
  248. #ifdef PTRCALL_ENABLED
  249. template <class T>
  250. struct PtrToArg<Ref<T> > {
  251. _FORCE_INLINE_ static Ref<T> convert(const void *p_ptr) {
  252. return Ref<T>(const_cast<T *>(reinterpret_cast<const T *>(p_ptr)));
  253. }
  254. _FORCE_INLINE_ static void encode(Ref<T> p_val, const void *p_ptr) {
  255. *(Ref<Reference> *)p_ptr = p_val;
  256. }
  257. };
  258. template <class T>
  259. struct PtrToArg<const Ref<T> &> {
  260. _FORCE_INLINE_ static Ref<T> convert(const void *p_ptr) {
  261. return Ref<T>((T *)p_ptr);
  262. }
  263. };
  264. //this is for RefPtr
  265. template <>
  266. struct PtrToArg<RefPtr> {
  267. _FORCE_INLINE_ static RefPtr convert(const void *p_ptr) {
  268. return Ref<Reference>(const_cast<Reference *>(reinterpret_cast<const Reference *>(p_ptr))).get_ref_ptr();
  269. }
  270. _FORCE_INLINE_ static void encode(RefPtr p_val, const void *p_ptr) {
  271. Ref<Reference> r = p_val;
  272. *(Ref<Reference> *)p_ptr = r;
  273. }
  274. };
  275. template <>
  276. struct PtrToArg<const RefPtr &> {
  277. _FORCE_INLINE_ static RefPtr convert(const void *p_ptr) {
  278. return Ref<Reference>(const_cast<Reference *>(reinterpret_cast<const Reference *>(p_ptr))).get_ref_ptr();
  279. }
  280. };
  281. #endif // PTRCALL_ENABLED
  282. #ifdef DEBUG_METHODS_ENABLED
  283. template <class T>
  284. struct GetTypeInfo<Ref<T> > {
  285. enum { VARIANT_TYPE = Variant::OBJECT };
  286. static inline PropertyInfo get_class_info() {
  287. return PropertyInfo(Variant::OBJECT, String(), PROPERTY_HINT_RESOURCE_TYPE, T::get_class_static());
  288. }
  289. };
  290. template <class T>
  291. struct GetTypeInfo<const Ref<T> &> {
  292. enum { VARIANT_TYPE = Variant::OBJECT };
  293. static inline PropertyInfo get_class_info() {
  294. return PropertyInfo(Variant::OBJECT, String(), PROPERTY_HINT_RESOURCE_TYPE, T::get_class_static());
  295. }
  296. };
  297. #endif // DEBUG_METHODS_ENABLED
  298. #endif // REFERENCE_H