reference.h 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  1. /*************************************************************************/
  2. /* reference.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2017 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 "class_db.h"
  33. #include "object.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. 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 = NULL;
  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 Ref<T> &p_r) const {
  74. return reference < p_r.reference;
  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_ T *operator->() {
  83. return reference;
  84. }
  85. _FORCE_INLINE_ T *operator*() {
  86. return reference;
  87. }
  88. _FORCE_INLINE_ const T *operator->() const {
  89. return reference;
  90. }
  91. _FORCE_INLINE_ const T *ptr() const {
  92. return reference;
  93. }
  94. _FORCE_INLINE_ T *ptr() {
  95. return reference;
  96. }
  97. _FORCE_INLINE_ const T *operator*() const {
  98. return reference;
  99. }
  100. RefPtr get_ref_ptr() const {
  101. RefPtr refptr;
  102. Ref<Reference> *irr = reinterpret_cast<Ref<Reference> *>(refptr.get_data());
  103. *irr = *this;
  104. return refptr;
  105. };
  106. operator Variant() const {
  107. return Variant(get_ref_ptr());
  108. }
  109. void operator=(const Ref &p_from) {
  110. ref(p_from);
  111. }
  112. template <class T_Other>
  113. void operator=(const Ref<T_Other> &p_from) {
  114. Reference *refb = const_cast<Reference *>(static_cast<const Reference *>(p_from.ptr()));
  115. if (!refb) {
  116. unref();
  117. return;
  118. }
  119. Ref r;
  120. r.reference = Object::cast_to<T>(refb);
  121. ref(r);
  122. r.reference = NULL;
  123. }
  124. void operator=(const RefPtr &p_refptr) {
  125. Ref<Reference> *irr = reinterpret_cast<Ref<Reference> *>(p_refptr.get_data());
  126. Reference *refb = irr->ptr();
  127. if (!refb) {
  128. unref();
  129. return;
  130. }
  131. Ref r;
  132. r.reference = Object::cast_to<T>(refb);
  133. ref(r);
  134. r.reference = NULL;
  135. }
  136. void operator=(const Variant &p_variant) {
  137. RefPtr refptr = p_variant;
  138. Ref<Reference> *irr = reinterpret_cast<Ref<Reference> *>(refptr.get_data());
  139. Reference *refb = irr->ptr();
  140. if (!refb) {
  141. unref();
  142. return;
  143. }
  144. Ref r;
  145. r.reference = Object::cast_to<T>(refb);
  146. ref(r);
  147. r.reference = NULL;
  148. }
  149. Ref(const Ref &p_from) {
  150. reference = NULL;
  151. ref(p_from);
  152. }
  153. template <class T_Other>
  154. Ref(const Ref<T_Other> &p_from) {
  155. reference = NULL;
  156. Reference *refb = const_cast<Reference *>(static_cast<const Reference *>(p_from.ptr()));
  157. if (!refb) {
  158. unref();
  159. return;
  160. }
  161. Ref r;
  162. r.reference = Object::cast_to<T>(refb);
  163. ref(r);
  164. r.reference = NULL;
  165. }
  166. Ref(T *p_reference) {
  167. if (p_reference)
  168. ref_pointer(p_reference);
  169. else
  170. reference = NULL;
  171. }
  172. Ref(const Variant &p_variant) {
  173. RefPtr refptr = p_variant;
  174. Ref<Reference> *irr = reinterpret_cast<Ref<Reference> *>(refptr.get_data());
  175. reference = NULL;
  176. Reference *refb = irr->ptr();
  177. if (!refb) {
  178. unref();
  179. return;
  180. }
  181. Ref r;
  182. r.reference = Object::cast_to<T>(refb);
  183. ref(r);
  184. r.reference = NULL;
  185. }
  186. Ref(const RefPtr &p_refptr) {
  187. Ref<Reference> *irr = reinterpret_cast<Ref<Reference> *>(p_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. inline bool is_valid() const { return reference != NULL; }
  200. inline bool is_null() const { return reference == NULL; }
  201. void unref() {
  202. //TODO this should be moved to mutexes, since this engine does not really
  203. // do a lot of referencing on references and stuff
  204. // mutexes will avoid more crashes?
  205. if (reference && reference->unreference()) {
  206. memdelete(reference);
  207. }
  208. reference = NULL;
  209. }
  210. void instance() {
  211. ref(memnew(T));
  212. }
  213. Ref() {
  214. reference = NULL;
  215. }
  216. ~Ref() {
  217. unref();
  218. }
  219. };
  220. typedef Ref<Reference> REF;
  221. class WeakRef : public Reference {
  222. GDCLASS(WeakRef, Reference);
  223. ObjectID ref;
  224. protected:
  225. static void _bind_methods();
  226. public:
  227. Variant get_ref() const;
  228. void set_obj(Object *p_object);
  229. void set_ref(const REF &p_ref);
  230. WeakRef();
  231. };
  232. #ifdef PTRCALL_ENABLED
  233. template <class T>
  234. struct PtrToArg<Ref<T> > {
  235. _FORCE_INLINE_ static Ref<T> convert(const void *p_ptr) {
  236. return Ref<T>(const_cast<T *>(reinterpret_cast<const T *>(p_ptr)));
  237. }
  238. _FORCE_INLINE_ static void encode(Ref<T> p_val, const void *p_ptr) {
  239. *(Ref<Reference> *)p_ptr = p_val;
  240. }
  241. };
  242. template <class T>
  243. struct PtrToArg<const Ref<T> &> {
  244. _FORCE_INLINE_ static Ref<T> convert(const void *p_ptr) {
  245. return Ref<T>((T *)p_ptr);
  246. }
  247. };
  248. //this is for RefPtr
  249. template <>
  250. struct PtrToArg<RefPtr> {
  251. _FORCE_INLINE_ static RefPtr convert(const void *p_ptr) {
  252. return Ref<Reference>(const_cast<Reference *>(reinterpret_cast<const Reference *>(p_ptr))).get_ref_ptr();
  253. }
  254. _FORCE_INLINE_ static void encode(RefPtr p_val, const void *p_ptr) {
  255. Ref<Reference> r = p_val;
  256. *(Ref<Reference> *)p_ptr = r;
  257. }
  258. };
  259. template <>
  260. struct PtrToArg<const RefPtr &> {
  261. _FORCE_INLINE_ static RefPtr convert(const void *p_ptr) {
  262. return Ref<Reference>(const_cast<Reference *>(reinterpret_cast<const Reference *>(p_ptr))).get_ref_ptr();
  263. }
  264. };
  265. #endif // PTRCALL_ENABLED
  266. #ifdef DEBUG_METHODS_ENABLED
  267. template <class T>
  268. struct GetTypeInfo<Ref<T> > {
  269. enum { VARIANT_TYPE = Variant::OBJECT };
  270. static inline PropertyInfo get_class_info() {
  271. return PropertyInfo(Variant::OBJECT, String(), PROPERTY_HINT_RESOURCE_TYPE, T::get_class_static());
  272. }
  273. };
  274. template <class T>
  275. struct GetTypeInfo<const Ref<T> &> {
  276. enum { VARIANT_TYPE = Variant::OBJECT };
  277. static inline PropertyInfo get_class_info() {
  278. return PropertyInfo(Variant::OBJECT, String(), PROPERTY_HINT_RESOURCE_TYPE, T::get_class_static());
  279. }
  280. };
  281. #endif // DEBUG_METHODS_ENABLED
  282. #endif // REFERENCE_H