reference.h 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  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 <reduzio@gmail.com>
  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. Ref(const Ref &p_from) {
  156. reference = NULL;
  157. ref(p_from);
  158. }
  159. template <class T_Other>
  160. Ref(const Ref<T_Other> &p_from) {
  161. reference = NULL;
  162. Reference *refb = const_cast<Reference *>(static_cast<const Reference *>(p_from.ptr()));
  163. if (!refb) {
  164. unref();
  165. return;
  166. }
  167. Ref r;
  168. r.reference = Object::cast_to<T>(refb);
  169. ref(r);
  170. r.reference = NULL;
  171. }
  172. Ref(T *p_reference) {
  173. reference = NULL;
  174. if (p_reference)
  175. ref_pointer(p_reference);
  176. }
  177. Ref(const Variant &p_variant) {
  178. RefPtr refptr = p_variant;
  179. Ref<Reference> *irr = reinterpret_cast<Ref<Reference> *>(refptr.get_data());
  180. reference = NULL;
  181. Reference *refb = irr->ptr();
  182. if (!refb) {
  183. unref();
  184. return;
  185. }
  186. Ref r;
  187. r.reference = Object::cast_to<T>(refb);
  188. ref(r);
  189. r.reference = NULL;
  190. }
  191. Ref(const RefPtr &p_refptr) {
  192. Ref<Reference> *irr = reinterpret_cast<Ref<Reference> *>(p_refptr.get_data());
  193. reference = NULL;
  194. Reference *refb = irr->ptr();
  195. if (!refb) {
  196. unref();
  197. return;
  198. }
  199. Ref r;
  200. r.reference = Object::cast_to<T>(refb);
  201. ref(r);
  202. r.reference = NULL;
  203. }
  204. inline bool is_valid() const { return reference != NULL; }
  205. inline bool is_null() const { return reference == NULL; }
  206. void unref() {
  207. //TODO this should be moved to mutexes, since this engine does not really
  208. // do a lot of referencing on references and stuff
  209. // mutexes will avoid more crashes?
  210. if (reference && reference->unreference()) {
  211. memdelete(reference);
  212. }
  213. reference = NULL;
  214. }
  215. void instance() {
  216. ref(memnew(T));
  217. }
  218. Ref() {
  219. reference = NULL;
  220. }
  221. ~Ref() {
  222. unref();
  223. }
  224. };
  225. typedef Ref<Reference> REF;
  226. class WeakRef : public Reference {
  227. GDCLASS(WeakRef, Reference);
  228. ObjectID ref;
  229. protected:
  230. static void _bind_methods();
  231. public:
  232. Variant get_ref() const;
  233. void set_obj(Object *p_object);
  234. void set_ref(const REF &p_ref);
  235. WeakRef();
  236. };
  237. #ifdef PTRCALL_ENABLED
  238. template <class T>
  239. struct PtrToArg<Ref<T> > {
  240. _FORCE_INLINE_ static Ref<T> convert(const void *p_ptr) {
  241. return Ref<T>(const_cast<T *>(reinterpret_cast<const T *>(p_ptr)));
  242. }
  243. _FORCE_INLINE_ static void encode(Ref<T> p_val, const void *p_ptr) {
  244. *(Ref<Reference> *)p_ptr = p_val;
  245. }
  246. };
  247. template <class T>
  248. struct PtrToArg<const Ref<T> &> {
  249. _FORCE_INLINE_ static Ref<T> convert(const void *p_ptr) {
  250. return Ref<T>((T *)p_ptr);
  251. }
  252. };
  253. //this is for RefPtr
  254. template <>
  255. struct PtrToArg<RefPtr> {
  256. _FORCE_INLINE_ static RefPtr convert(const void *p_ptr) {
  257. return Ref<Reference>(const_cast<Reference *>(reinterpret_cast<const Reference *>(p_ptr))).get_ref_ptr();
  258. }
  259. _FORCE_INLINE_ static void encode(RefPtr p_val, const void *p_ptr) {
  260. Ref<Reference> r = p_val;
  261. *(Ref<Reference> *)p_ptr = r;
  262. }
  263. };
  264. template <>
  265. struct PtrToArg<const RefPtr &> {
  266. _FORCE_INLINE_ static RefPtr convert(const void *p_ptr) {
  267. return Ref<Reference>(const_cast<Reference *>(reinterpret_cast<const Reference *>(p_ptr))).get_ref_ptr();
  268. }
  269. };
  270. #endif // PTRCALL_ENABLED
  271. #ifdef DEBUG_METHODS_ENABLED
  272. template <class T>
  273. struct GetTypeInfo<Ref<T> > {
  274. enum { VARIANT_TYPE = Variant::OBJECT };
  275. static inline PropertyInfo get_class_info() {
  276. return PropertyInfo(Variant::OBJECT, String(), PROPERTY_HINT_RESOURCE_TYPE, T::get_class_static());
  277. }
  278. };
  279. template <class T>
  280. struct GetTypeInfo<const Ref<T> &> {
  281. enum { VARIANT_TYPE = Variant::OBJECT };
  282. static inline PropertyInfo get_class_info() {
  283. return PropertyInfo(Variant::OBJECT, String(), PROPERTY_HINT_RESOURCE_TYPE, T::get_class_static());
  284. }
  285. };
  286. #endif // DEBUG_METHODS_ENABLED
  287. #endif // REFERENCE_H