reference.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /*************************************************************************/
  2. /* reference.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 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 "core/script_language.h"
  32. bool Reference::init_ref() {
  33. if (reference()) {
  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. unreference(); // 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. ClassDB::bind_method(D_METHOD("init_ref"), &Reference::init_ref);
  48. ClassDB::bind_method(D_METHOD("reference"), &Reference::reference);
  49. ClassDB::bind_method(D_METHOD("unreference"), &Reference::unreference);
  50. }
  51. int Reference::reference_get_count() const {
  52. return refcount.get();
  53. }
  54. bool Reference::reference() {
  55. bool success = refcount.ref();
  56. if (success && refcount.get() <= 2 /* higher is not relevant */) {
  57. if (get_script_instance()) {
  58. get_script_instance()->refcount_incremented();
  59. }
  60. if (instance_binding_count > 0) {
  61. for (int i = 0; i < MAX_SCRIPT_INSTANCE_BINDINGS; i++) {
  62. if (_script_instance_bindings[i]) {
  63. ScriptServer::get_language(i)->refcount_incremented_instance_binding(this);
  64. }
  65. }
  66. }
  67. }
  68. return success;
  69. }
  70. bool Reference::unreference() {
  71. bool die = refcount.unref();
  72. if (refcount.get() <= 1 /* higher is not relevant */) {
  73. if (get_script_instance()) {
  74. bool script_ret = get_script_instance()->refcount_decremented();
  75. die = die && script_ret;
  76. }
  77. if (instance_binding_count > 0) {
  78. for (int i = 0; i < MAX_SCRIPT_INSTANCE_BINDINGS; i++) {
  79. if (_script_instance_bindings[i]) {
  80. bool script_ret = ScriptServer::get_language(i)->refcount_decremented_instance_binding(this);
  81. die = die && script_ret;
  82. }
  83. }
  84. }
  85. }
  86. return die;
  87. }
  88. Reference::Reference() {
  89. refcount.init();
  90. refcount_init.init();
  91. }
  92. Reference::~Reference() {
  93. }
  94. Variant WeakRef::get_ref() const {
  95. if (ref == 0)
  96. return Variant();
  97. Object *obj = ObjectDB::get_instance(ref);
  98. if (!obj)
  99. return Variant();
  100. Reference *r = cast_to<Reference>(obj);
  101. if (r) {
  102. return REF(r);
  103. }
  104. return obj;
  105. }
  106. void WeakRef::set_obj(Object *p_object) {
  107. ref = p_object ? p_object->get_instance_id() : 0;
  108. }
  109. void WeakRef::set_ref(const REF &p_ref) {
  110. ref = p_ref.is_valid() ? p_ref->get_instance_id() : 0;
  111. }
  112. WeakRef::WeakRef() :
  113. ref(0) {
  114. }
  115. void WeakRef::_bind_methods() {
  116. ClassDB::bind_method(D_METHOD("get_ref"), &WeakRef::get_ref);
  117. }