2
0

ref_counted.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /*************************************************************************/
  2. /* ref_counted.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 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 "ref_counted.h"
  31. #include "core/object/script_language.h"
  32. bool RefCounted::init_ref() {
  33. if (reference()) {
  34. if (!is_referenced() && refcount_init.unref()) {
  35. unreference(); // first referencing is already 1, so compensate for the ref above
  36. }
  37. return true;
  38. } else {
  39. return false;
  40. }
  41. }
  42. void RefCounted::_bind_methods() {
  43. ClassDB::bind_method(D_METHOD("init_ref"), &RefCounted::init_ref);
  44. ClassDB::bind_method(D_METHOD("reference"), &RefCounted::reference);
  45. ClassDB::bind_method(D_METHOD("unreference"), &RefCounted::unreference);
  46. }
  47. int RefCounted::reference_get_count() const {
  48. return refcount.get();
  49. }
  50. bool RefCounted::reference() {
  51. uint32_t rc_val = refcount.refval();
  52. bool success = rc_val != 0;
  53. if (success && rc_val <= 2 /* higher is not relevant */) {
  54. if (get_script_instance()) {
  55. get_script_instance()->refcount_incremented();
  56. }
  57. if (_get_extension() && _get_extension()->reference) {
  58. _get_extension()->reference(_get_extension_instance());
  59. }
  60. _instance_binding_reference(true);
  61. }
  62. return success;
  63. }
  64. bool RefCounted::unreference() {
  65. uint32_t rc_val = refcount.unrefval();
  66. bool die = rc_val == 0;
  67. if (rc_val <= 1 /* higher is not relevant */) {
  68. if (get_script_instance()) {
  69. bool script_ret = get_script_instance()->refcount_decremented();
  70. die = die && script_ret;
  71. }
  72. if (_get_extension() && _get_extension()->unreference) {
  73. _get_extension()->unreference(_get_extension_instance());
  74. }
  75. die = die && _instance_binding_reference(false);
  76. }
  77. return die;
  78. }
  79. RefCounted::RefCounted() :
  80. Object(true) {
  81. refcount.init();
  82. refcount_init.init();
  83. }
  84. Variant WeakRef::get_ref() const {
  85. if (ref.is_null()) {
  86. return Variant();
  87. }
  88. Object *obj = ObjectDB::get_instance(ref);
  89. if (!obj) {
  90. return Variant();
  91. }
  92. RefCounted *r = cast_to<RefCounted>(obj);
  93. if (r) {
  94. return REF(r);
  95. }
  96. return obj;
  97. }
  98. void WeakRef::set_obj(Object *p_object) {
  99. ref = p_object ? p_object->get_instance_id() : ObjectID();
  100. }
  101. void WeakRef::set_ref(const REF &p_ref) {
  102. ref = p_ref.is_valid() ? p_ref->get_instance_id() : ObjectID();
  103. }
  104. void WeakRef::_bind_methods() {
  105. ClassDB::bind_method(D_METHOD("get_ref"), &WeakRef::get_ref);
  106. }