ref_counted.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /**************************************************************************/
  2. /* ref_counted.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  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. ClassDB::bind_method(D_METHOD("get_reference_count"), &RefCounted::get_reference_count);
  47. }
  48. int RefCounted::get_reference_count() const {
  49. return refcount.get();
  50. }
  51. bool RefCounted::reference() {
  52. uint32_t rc_val = refcount.refval();
  53. bool success = rc_val != 0;
  54. if (success && rc_val <= 2 /* higher is not relevant */) {
  55. if (get_script_instance()) {
  56. get_script_instance()->refcount_incremented();
  57. }
  58. if (_get_extension() && _get_extension()->reference) {
  59. _get_extension()->reference(_get_extension_instance());
  60. }
  61. _instance_binding_reference(true);
  62. }
  63. return success;
  64. }
  65. bool RefCounted::unreference() {
  66. uint32_t rc_val = refcount.unrefval();
  67. bool die = rc_val == 0;
  68. if (rc_val <= 1 /* higher is not relevant */) {
  69. if (get_script_instance()) {
  70. bool script_ret = get_script_instance()->refcount_decremented();
  71. die = die && script_ret;
  72. }
  73. if (_get_extension() && _get_extension()->unreference) {
  74. _get_extension()->unreference(_get_extension_instance());
  75. }
  76. bool binding_ret = _instance_binding_reference(false);
  77. die = die && binding_ret;
  78. }
  79. return die;
  80. }
  81. RefCounted::RefCounted() :
  82. Object(true) {
  83. refcount.init();
  84. refcount_init.init();
  85. }
  86. Variant WeakRef::get_ref() const {
  87. if (ref.is_null()) {
  88. return Variant();
  89. }
  90. Object *obj = ObjectDB::get_instance(ref);
  91. if (!obj) {
  92. return Variant();
  93. }
  94. RefCounted *r = cast_to<RefCounted>(obj);
  95. if (r) {
  96. return Ref<RefCounted>(r);
  97. }
  98. return obj;
  99. }
  100. void WeakRef::set_obj(Object *p_object) {
  101. ref = p_object ? p_object->get_instance_id() : ObjectID();
  102. }
  103. void WeakRef::set_ref(const Ref<RefCounted> &p_ref) {
  104. ref = p_ref.is_valid() ? p_ref->get_instance_id() : ObjectID();
  105. }
  106. void WeakRef::_bind_methods() {
  107. ClassDB::bind_method(D_METHOD("get_ref"), &WeakRef::get_ref);
  108. }