2
0

reference.cpp 4.8 KB

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