reference.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. /*************************************************************************/
  2. /* reference.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2016 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. #include "reference.h"
  30. #include "script_language.h"
  31. bool Reference::init_ref() {
  32. if (refcount.ref()) {
  33. // this may fail in the scenario of two threads assigning the pointer for the FIRST TIME
  34. // at the same time, which is never likely to happen (would be crazy to do)
  35. // so don't do it.
  36. if (refcount_init.get()>0) {
  37. refcount_init.unref();
  38. refcount.unref(); // first referencing is already 1, so compensate for the ref above
  39. }
  40. return true;
  41. } else {
  42. return false;
  43. }
  44. }
  45. void Reference::_bind_methods() {
  46. ObjectTypeDB::bind_method(_MD("init_ref"),&Reference::init_ref);
  47. ObjectTypeDB::bind_method(_MD("reference"),&Reference::reference);
  48. ObjectTypeDB::bind_method(_MD("unreference"),&Reference::unreference);
  49. }
  50. int Reference::reference_get_count() const {
  51. return refcount.get();
  52. }
  53. void Reference::reference(){
  54. refcount.ref();
  55. if (get_script_instance()) {
  56. get_script_instance()->refcount_incremented();
  57. }
  58. }
  59. bool Reference::unreference(){
  60. bool die = refcount.unref();
  61. if (get_script_instance()) {
  62. die = die && get_script_instance()->refcount_decremented();
  63. }
  64. return die;
  65. }
  66. Reference::Reference() {
  67. refcount.init();
  68. refcount_init.init();
  69. }
  70. Reference::~Reference() {
  71. }
  72. Variant WeakRef::get_ref() const {
  73. if (ref==0)
  74. return Variant();
  75. Object *obj = ObjectDB::get_instance(ref);
  76. if (!obj)
  77. return Variant();
  78. Reference *r = obj->cast_to<Reference>();
  79. if (r) {
  80. return REF(r);
  81. }
  82. return obj;
  83. }
  84. void WeakRef::set_obj(Object *p_object) {
  85. ref=p_object ? p_object->get_instance_ID() : 0;
  86. }
  87. void WeakRef::set_ref(const REF& p_ref) {
  88. ref=p_ref.is_valid() ? p_ref->get_instance_ID() : 0;
  89. }
  90. WeakRef::WeakRef() {
  91. ref=0;
  92. }
  93. void WeakRef::_bind_methods() {
  94. ObjectTypeDB::bind_method(_MD("get_ref:Object"),&WeakRef::get_ref);
  95. }
  96. #if 0
  97. Reference * RefBase::get_reference_from_ref(const RefBase &p_base) {
  98. return p_base.get_reference();
  99. }
  100. void RefBase::ref_inc(Reference *p_reference) {
  101. p_reference->refcount.ref();
  102. }
  103. bool RefBase::ref_dec(Reference *p_reference) {
  104. bool ref = p_reference->refcount.unref();
  105. return ref;
  106. }
  107. Reference *RefBase::first_ref(Reference *p_reference) {
  108. if (p_reference->refcount.ref()) {
  109. // this may fail in the scenario of two threads assigning the pointer for the FIRST TIME
  110. // at the same time, which is never likely to happen (would be crazy to do)
  111. // so don't do it.
  112. if (p_reference->refcount_init.get()>0) {
  113. p_reference->refcount_init.unref();
  114. p_reference->refcount.unref(); // first referencing is already 1, so compensate for the ref above
  115. }
  116. return p_reference;
  117. } else {
  118. return 0;
  119. }
  120. }
  121. char * RefBase::get_refptr_data(const RefPtr &p_refptr) const {
  122. return p_refptr.data;
  123. }
  124. #endif