reference.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. /*************************************************************************/
  2. /* reference.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2014 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. Reference();
  51. ~Reference();
  52. };
  53. #if 0
  54. class RefBase {
  55. protected:
  56. void ref_inc(Reference *p_reference);
  57. bool ref_dec(Reference *p_reference);
  58. Reference *first_ref(Reference *p_reference);
  59. Reference * get_reference_from_ref(const RefBase &p_base);
  60. virtual Reference * get_reference() const=0;
  61. char * get_refptr_data(const RefPtr &p_refptr) const;
  62. public:
  63. virtual ~RefBase() {}
  64. };
  65. #endif
  66. template<class T>
  67. class Ref {
  68. T *reference;
  69. void ref( const Ref& p_from ) {
  70. if (p_from.reference==reference)
  71. return;
  72. unref();
  73. reference=p_from.reference;
  74. if (reference)
  75. reference->reference();
  76. }
  77. void ref_pointer( T* p_ref ) {
  78. ERR_FAIL_COND(!p_ref);
  79. if (p_ref->init_ref())
  80. reference=p_ref;
  81. }
  82. //virtual Reference * get_reference() const { return reference; }
  83. public:
  84. _FORCE_INLINE_ bool operator<(const Ref<T>& p_r) const {
  85. return reference<p_r.reference;
  86. }
  87. _FORCE_INLINE_ bool operator==(const Ref<T>& p_r) const {
  88. return reference==p_r.reference;
  89. }
  90. _FORCE_INLINE_ bool operator!=(const Ref<T>& p_r) const {
  91. return reference!=p_r.reference;
  92. }
  93. _FORCE_INLINE_ T* operator->() {
  94. return reference;
  95. }
  96. _FORCE_INLINE_ T* operator*() {
  97. return reference;
  98. }
  99. _FORCE_INLINE_ const T* operator->() const {
  100. return reference;
  101. }
  102. _FORCE_INLINE_ const T* ptr() const {
  103. return reference;
  104. }
  105. _FORCE_INLINE_ T* ptr() {
  106. return reference;
  107. }
  108. _FORCE_INLINE_ const T* operator*() const {
  109. return reference;
  110. }
  111. RefPtr get_ref_ptr() const {
  112. RefPtr refptr;
  113. Ref<Reference> * irr = reinterpret_cast<Ref<Reference>*>( refptr.get_data() );
  114. *irr = *this;
  115. return refptr;
  116. };
  117. #if 0
  118. // go to RefPtr
  119. operator RefPtr() const {
  120. return get_ref_ptr();
  121. }
  122. #endif
  123. #if 1
  124. operator Variant() const {
  125. return Variant( get_ref_ptr() );
  126. }
  127. #endif
  128. void operator=( const Ref& p_from ) {
  129. ref(p_from);
  130. }
  131. template<class T_Other>
  132. void operator=( const Ref<T_Other>& p_from ) {
  133. Reference *refb = const_cast<Reference*>(static_cast<const Reference*>(p_from.ptr()));
  134. if (!refb) {
  135. unref();
  136. return;
  137. }
  138. Ref r;
  139. r.reference=refb->cast_to<T>();
  140. ref(r);
  141. r.reference=NULL;
  142. }
  143. void operator=( const RefPtr& p_refptr ) {
  144. Ref<Reference> * irr = reinterpret_cast<Ref<Reference>*>( p_refptr.get_data() );
  145. Reference *refb = irr->ptr();
  146. if (!refb) {
  147. unref();
  148. return;
  149. }
  150. Ref r;
  151. r.reference=refb->cast_to<T>();
  152. ref(r);
  153. r.reference=NULL;
  154. }
  155. void operator=( const Variant& p_variant ) {
  156. RefPtr refptr=p_variant;
  157. Ref<Reference> * irr = reinterpret_cast<Ref<Reference>*>( refptr.get_data() );
  158. Reference *refb = irr->ptr();
  159. if (!refb) {
  160. unref();
  161. return;
  162. }
  163. Ref r;
  164. r.reference=refb->cast_to<T>();
  165. ref(r);
  166. r.reference=NULL;
  167. }
  168. Ref( const Ref& p_from ) {
  169. reference=NULL;
  170. ref(p_from);
  171. }
  172. template<class T_Other>
  173. Ref( const Ref<T_Other>& p_from ) {
  174. reference=NULL;
  175. Reference *refb = const_cast<Reference*>(static_cast<const Reference*>(p_from.ptr()));
  176. if (!refb) {
  177. unref();
  178. return;
  179. }
  180. Ref r;
  181. r.reference=refb->cast_to<T>();
  182. ref(r);
  183. r.reference=NULL;
  184. }
  185. Ref( T* p_reference ) {
  186. if (p_reference)
  187. ref_pointer(p_reference);
  188. else
  189. reference=NULL;
  190. }
  191. Ref( const Variant& p_variant) {
  192. RefPtr refptr=p_variant;
  193. Ref<Reference> * irr = reinterpret_cast<Ref<Reference>*>( refptr.get_data() );
  194. reference=NULL;
  195. Reference *refb = irr->ptr();
  196. if (!refb) {
  197. unref();
  198. return;
  199. }
  200. Ref r;
  201. r.reference=refb->cast_to<T>();
  202. ref(r);
  203. r.reference=NULL;
  204. }
  205. Ref( const RefPtr& p_refptr) {
  206. Ref<Reference> * irr = reinterpret_cast<Ref<Reference>*>( p_refptr.get_data() );
  207. reference=NULL;
  208. Reference *refb = irr->ptr();
  209. if (!refb) {
  210. unref();
  211. return;
  212. }
  213. Ref r;
  214. r.reference=refb->cast_to<T>();
  215. ref(r);
  216. r.reference=NULL;
  217. }
  218. inline bool is_valid() const { return reference!=NULL; }
  219. inline bool is_null() const { return reference==NULL; }
  220. void unref() {
  221. //TODO this should be moved to mutexes, since this engine does not really
  222. // do a lot of referencing on references and stuff
  223. // mutexes will avoid more crashes?
  224. if (reference && reference->unreference()) {
  225. memdelete(reference);
  226. }
  227. reference=NULL;
  228. }
  229. Ref() {
  230. reference=NULL;
  231. }
  232. ~Ref() {
  233. unref();
  234. }
  235. };
  236. typedef Ref<Reference> REF;
  237. class WeakRef : public Reference {
  238. OBJ_TYPE(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. #endif // REFERENCE_H