reference.h 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  1. /*************************************************************************/
  2. /* reference.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 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 "object.h"
  33. #include "object_type_db.h"
  34. #include "ref_ptr.h"
  35. #include "safe_refcount.h"
  36. /**
  37. @author Juan Linietsky <reduzio@gmail.com>
  38. */
  39. class Reference : public Object {
  40. OBJ_TYPE(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. void reference();
  50. bool unreference();
  51. int reference_get_count() const;
  52. Reference();
  53. ~Reference();
  54. };
  55. #if 0
  56. class RefBase {
  57. protected:
  58. void ref_inc(Reference *p_reference);
  59. bool ref_dec(Reference *p_reference);
  60. Reference *first_ref(Reference *p_reference);
  61. Reference * get_reference_from_ref(const RefBase &p_base);
  62. virtual Reference * get_reference() const=0;
  63. char * get_refptr_data(const RefPtr &p_refptr) const;
  64. public:
  65. virtual ~RefBase() {}
  66. };
  67. #endif
  68. template <class T>
  69. class Ref {
  70. T *reference;
  71. void ref(const Ref &p_from) {
  72. if (p_from.reference == reference)
  73. return;
  74. unref();
  75. reference = p_from.reference;
  76. if (reference)
  77. reference->reference();
  78. }
  79. void ref_pointer(T *p_ref) {
  80. ERR_FAIL_COND(!p_ref);
  81. if (p_ref->init_ref())
  82. reference = p_ref;
  83. }
  84. //virtual Reference * get_reference() const { return reference; }
  85. public:
  86. _FORCE_INLINE_ bool operator<(const Ref<T> &p_r) const {
  87. return reference < p_r.reference;
  88. }
  89. _FORCE_INLINE_ bool operator==(const Ref<T> &p_r) const {
  90. return reference == p_r.reference;
  91. }
  92. _FORCE_INLINE_ bool operator!=(const Ref<T> &p_r) const {
  93. return reference != p_r.reference;
  94. }
  95. _FORCE_INLINE_ T *operator->() {
  96. return reference;
  97. }
  98. _FORCE_INLINE_ T *operator*() {
  99. return reference;
  100. }
  101. _FORCE_INLINE_ const T *operator->() const {
  102. return reference;
  103. }
  104. _FORCE_INLINE_ const T *ptr() const {
  105. return reference;
  106. }
  107. _FORCE_INLINE_ T *ptr() {
  108. return reference;
  109. }
  110. _FORCE_INLINE_ const T *operator*() const {
  111. return reference;
  112. }
  113. RefPtr get_ref_ptr() const {
  114. RefPtr refptr;
  115. Ref<Reference> *irr = reinterpret_cast<Ref<Reference> *>(refptr.get_data());
  116. *irr = *this;
  117. return refptr;
  118. };
  119. #if 0
  120. // go to RefPtr
  121. operator RefPtr() const {
  122. return get_ref_ptr();
  123. }
  124. #endif
  125. #if 1
  126. operator Variant() const {
  127. return Variant(get_ref_ptr());
  128. }
  129. #endif
  130. void operator=(const Ref &p_from) {
  131. ref(p_from);
  132. }
  133. template <class T_Other>
  134. void operator=(const Ref<T_Other> &p_from) {
  135. Reference *refb = const_cast<Reference *>(static_cast<const Reference *>(p_from.ptr()));
  136. if (!refb) {
  137. unref();
  138. return;
  139. }
  140. Ref r;
  141. r.reference = refb->cast_to<T>();
  142. ref(r);
  143. r.reference = NULL;
  144. }
  145. void operator=(const RefPtr &p_refptr) {
  146. Ref<Reference> *irr = reinterpret_cast<Ref<Reference> *>(p_refptr.get_data());
  147. Reference *refb = irr->ptr();
  148. if (!refb) {
  149. unref();
  150. return;
  151. }
  152. Ref r;
  153. r.reference = refb->cast_to<T>();
  154. ref(r);
  155. r.reference = NULL;
  156. }
  157. void operator=(const Variant &p_variant) {
  158. RefPtr refptr = p_variant;
  159. Ref<Reference> *irr = reinterpret_cast<Ref<Reference> *>(refptr.get_data());
  160. Reference *refb = irr->ptr();
  161. if (!refb) {
  162. unref();
  163. return;
  164. }
  165. Ref r;
  166. r.reference = refb->cast_to<T>();
  167. ref(r);
  168. r.reference = NULL;
  169. }
  170. Ref(const Ref &p_from) {
  171. reference = NULL;
  172. ref(p_from);
  173. }
  174. template <class T_Other>
  175. Ref(const Ref<T_Other> &p_from) {
  176. reference = NULL;
  177. Reference *refb = const_cast<Reference *>(static_cast<const Reference *>(p_from.ptr()));
  178. if (!refb) {
  179. unref();
  180. return;
  181. }
  182. Ref r;
  183. r.reference = refb->cast_to<T>();
  184. ref(r);
  185. r.reference = NULL;
  186. }
  187. Ref(T *p_reference) {
  188. if (p_reference)
  189. ref_pointer(p_reference);
  190. else
  191. reference = NULL;
  192. }
  193. Ref(const Variant &p_variant) {
  194. RefPtr refptr = p_variant;
  195. Ref<Reference> *irr = reinterpret_cast<Ref<Reference> *>(refptr.get_data());
  196. reference = NULL;
  197. Reference *refb = irr->ptr();
  198. if (!refb) {
  199. unref();
  200. return;
  201. }
  202. Ref r;
  203. r.reference = refb->cast_to<T>();
  204. ref(r);
  205. r.reference = NULL;
  206. }
  207. Ref(const RefPtr &p_refptr) {
  208. Ref<Reference> *irr = reinterpret_cast<Ref<Reference> *>(p_refptr.get_data());
  209. reference = NULL;
  210. Reference *refb = irr->ptr();
  211. if (!refb) {
  212. unref();
  213. return;
  214. }
  215. Ref r;
  216. r.reference = refb->cast_to<T>();
  217. ref(r);
  218. r.reference = NULL;
  219. }
  220. inline bool is_valid() const { return reference != NULL; }
  221. inline bool is_null() const { return reference == NULL; }
  222. void unref() {
  223. //TODO this should be moved to mutexes, since this engine does not really
  224. // do a lot of referencing on references and stuff
  225. // mutexes will avoid more crashes?
  226. if (reference && reference->unreference()) {
  227. memdelete(reference);
  228. }
  229. reference = NULL;
  230. }
  231. void instance() {
  232. ref(memnew(T));
  233. }
  234. Ref() {
  235. reference = NULL;
  236. }
  237. ~Ref() {
  238. unref();
  239. }
  240. };
  241. typedef Ref<Reference> REF;
  242. class WeakRef : public Reference {
  243. OBJ_TYPE(WeakRef, Reference);
  244. ObjectID ref;
  245. protected:
  246. static void _bind_methods();
  247. public:
  248. Variant get_ref() const;
  249. void set_obj(Object *p_object);
  250. void set_ref(const REF &p_ref);
  251. WeakRef();
  252. };
  253. #ifdef PTRCALL_ENABLED
  254. template <class T>
  255. struct PtrToArg<Ref<T> > {
  256. _FORCE_INLINE_ static Ref<T> convert(const void *p_ptr) {
  257. return Ref<T>(reinterpret_cast<const T *>(p_ptr));
  258. }
  259. _FORCE_INLINE_ static void encode(Ref<T> p_val, const void *p_ptr) {
  260. *(Ref<Reference> *)p_ptr = p_val;
  261. }
  262. };
  263. template <class T>
  264. struct PtrToArg<const Ref<T> &> {
  265. _FORCE_INLINE_ static Ref<T> convert(const void *p_ptr) {
  266. return Ref<T>(reinterpret_cast<const T *>(p_ptr));
  267. }
  268. };
  269. //this is for RefPtr
  270. template <>
  271. struct PtrToArg<RefPtr> {
  272. _FORCE_INLINE_ static RefPtr convert(const void *p_ptr) {
  273. return Ref<Reference>(reinterpret_cast<const Reference *>(p_ptr)).get_ref_ptr();
  274. }
  275. _FORCE_INLINE_ static void encode(RefPtr p_val, const void *p_ptr) {
  276. Ref<Reference> r = p_val;
  277. *(Ref<Reference> *)p_ptr = r;
  278. }
  279. };
  280. template <>
  281. struct PtrToArg<const RefPtr &> {
  282. _FORCE_INLINE_ static RefPtr convert(const void *p_ptr) {
  283. return Ref<Reference>(reinterpret_cast<const Reference *>(p_ptr)).get_ref_ptr();
  284. }
  285. };
  286. #endif
  287. #endif // REFERENCE_H