2
0

reference.h 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. /*************************************************************************/
  2. /* reference.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2015 Juan Linietsky, Ariel Manzur. */
  9. /* */
  10. /* Permission is hereby granted, free of charge, to any person obtaining */
  11. /* a copy of this software and associated documentation files (the */
  12. /* "Software"), to deal in the Software without restriction, including */
  13. /* without limitation the rights to use, copy, modify, merge, publish, */
  14. /* distribute, sublicense, and/or sell copies of the Software, and to */
  15. /* permit persons to whom the Software is furnished to do so, subject to */
  16. /* the following conditions: */
  17. /* */
  18. /* The above copyright notice and this permission notice shall be */
  19. /* included in all copies or substantial portions of the Software. */
  20. /* */
  21. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  22. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  23. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  24. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  25. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  26. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  27. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  28. /*************************************************************************/
  29. #ifndef REFERENCE_H
  30. #define REFERENCE_H
  31. #include "object.h"
  32. #include "safe_refcount.h"
  33. #include "ref_ptr.h"
  34. #include "object_type_db.h"
  35. /**
  36. @author Juan Linietsky <[email protected]>
  37. */
  38. class Reference : public Object{
  39. OBJ_TYPE( Reference, Object );
  40. friend class RefBase;
  41. SafeRefCount refcount;
  42. SafeRefCount refcount_init;
  43. protected:
  44. static void _bind_methods();
  45. public:
  46. _FORCE_INLINE_ bool is_referenced() const { return refcount_init.get()<1; }
  47. bool init_ref();
  48. void reference();
  49. bool unreference();
  50. int reference_get_count() const;
  51. Reference();
  52. ~Reference();
  53. };
  54. #if 0
  55. class RefBase {
  56. protected:
  57. void ref_inc(Reference *p_reference);
  58. bool ref_dec(Reference *p_reference);
  59. Reference *first_ref(Reference *p_reference);
  60. Reference * get_reference_from_ref(const RefBase &p_base);
  61. virtual Reference * get_reference() const=0;
  62. char * get_refptr_data(const RefPtr &p_refptr) const;
  63. public:
  64. virtual ~RefBase() {}
  65. };
  66. #endif
  67. template<class T>
  68. class Ref {
  69. T *reference;
  70. void ref( const Ref& p_from ) {
  71. if (p_from.reference==reference)
  72. return;
  73. unref();
  74. reference=p_from.reference;
  75. if (reference)
  76. reference->reference();
  77. }
  78. void ref_pointer( T* p_ref ) {
  79. ERR_FAIL_COND(!p_ref);
  80. if (p_ref->init_ref())
  81. reference=p_ref;
  82. }
  83. //virtual Reference * get_reference() const { return reference; }
  84. public:
  85. _FORCE_INLINE_ bool operator<(const Ref<T>& p_r) const {
  86. return reference<p_r.reference;
  87. }
  88. _FORCE_INLINE_ bool operator==(const Ref<T>& p_r) const {
  89. return reference==p_r.reference;
  90. }
  91. _FORCE_INLINE_ bool operator!=(const Ref<T>& p_r) const {
  92. return reference!=p_r.reference;
  93. }
  94. _FORCE_INLINE_ T* operator->() {
  95. return reference;
  96. }
  97. _FORCE_INLINE_ T* operator*() {
  98. return reference;
  99. }
  100. _FORCE_INLINE_ const T* operator->() const {
  101. return reference;
  102. }
  103. _FORCE_INLINE_ const T* ptr() const {
  104. return reference;
  105. }
  106. _FORCE_INLINE_ T* ptr() {
  107. return reference;
  108. }
  109. _FORCE_INLINE_ const T* operator*() const {
  110. return reference;
  111. }
  112. RefPtr get_ref_ptr() const {
  113. RefPtr refptr;
  114. Ref<Reference> * irr = reinterpret_cast<Ref<Reference>*>( refptr.get_data() );
  115. *irr = *this;
  116. return refptr;
  117. };
  118. #if 0
  119. // go to RefPtr
  120. operator RefPtr() const {
  121. return get_ref_ptr();
  122. }
  123. #endif
  124. #if 1
  125. operator Variant() const {
  126. return Variant( get_ref_ptr() );
  127. }
  128. #endif
  129. void operator=( const Ref& p_from ) {
  130. ref(p_from);
  131. }
  132. template<class T_Other>
  133. void operator=( const Ref<T_Other>& p_from ) {
  134. Reference *refb = const_cast<Reference*>(static_cast<const Reference*>(p_from.ptr()));
  135. if (!refb) {
  136. unref();
  137. return;
  138. }
  139. Ref r;
  140. r.reference=refb->cast_to<T>();
  141. ref(r);
  142. r.reference=NULL;
  143. }
  144. void operator=( const RefPtr& p_refptr ) {
  145. Ref<Reference> * irr = reinterpret_cast<Ref<Reference>*>( p_refptr.get_data() );
  146. Reference *refb = irr->ptr();
  147. if (!refb) {
  148. unref();
  149. return;
  150. }
  151. Ref r;
  152. r.reference=refb->cast_to<T>();
  153. ref(r);
  154. r.reference=NULL;
  155. }
  156. void operator=( const Variant& p_variant ) {
  157. RefPtr refptr=p_variant;
  158. Ref<Reference> * irr = reinterpret_cast<Ref<Reference>*>( refptr.get_data() );
  159. Reference *refb = irr->ptr();
  160. if (!refb) {
  161. unref();
  162. return;
  163. }
  164. Ref r;
  165. r.reference=refb->cast_to<T>();
  166. ref(r);
  167. r.reference=NULL;
  168. }
  169. Ref( const Ref& p_from ) {
  170. reference=NULL;
  171. ref(p_from);
  172. }
  173. template<class T_Other>
  174. Ref( const Ref<T_Other>& p_from ) {
  175. reference=NULL;
  176. Reference *refb = const_cast<Reference*>(static_cast<const Reference*>(p_from.ptr()));
  177. if (!refb) {
  178. unref();
  179. return;
  180. }
  181. Ref r;
  182. r.reference=refb->cast_to<T>();
  183. ref(r);
  184. r.reference=NULL;
  185. }
  186. Ref( T* p_reference ) {
  187. if (p_reference)
  188. ref_pointer(p_reference);
  189. else
  190. reference=NULL;
  191. }
  192. Ref( const Variant& p_variant) {
  193. RefPtr refptr=p_variant;
  194. Ref<Reference> * irr = reinterpret_cast<Ref<Reference>*>( refptr.get_data() );
  195. reference=NULL;
  196. Reference *refb = irr->ptr();
  197. if (!refb) {
  198. unref();
  199. return;
  200. }
  201. Ref r;
  202. r.reference=refb->cast_to<T>();
  203. ref(r);
  204. r.reference=NULL;
  205. }
  206. Ref( const RefPtr& p_refptr) {
  207. Ref<Reference> * irr = reinterpret_cast<Ref<Reference>*>( p_refptr.get_data() );
  208. reference=NULL;
  209. Reference *refb = irr->ptr();
  210. if (!refb) {
  211. unref();
  212. return;
  213. }
  214. Ref r;
  215. r.reference=refb->cast_to<T>();
  216. ref(r);
  217. r.reference=NULL;
  218. }
  219. inline bool is_valid() const { return reference!=NULL; }
  220. inline bool is_null() const { return reference==NULL; }
  221. void unref() {
  222. //TODO this should be moved to mutexes, since this engine does not really
  223. // do a lot of referencing on references and stuff
  224. // mutexes will avoid more crashes?
  225. if (reference && reference->unreference()) {
  226. memdelete(reference);
  227. }
  228. reference=NULL;
  229. }
  230. Ref() {
  231. reference=NULL;
  232. }
  233. ~Ref() {
  234. unref();
  235. }
  236. };
  237. typedef Ref<Reference> REF;
  238. class WeakRef : public Reference {
  239. OBJ_TYPE(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. #endif // REFERENCE_H