| 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 | |
| 31 | #include "ref_counted.h" |
| 32 | |
| 33 | #include "core/object/script_language.h" |
| 34 | |
| 35 | bool RefCounted::init_ref() { |
| 36 | if (reference()) { |
| 37 | if (!is_referenced() && refcount_init.unref()) { |
| 38 | unreference(); // first referencing is already 1, so compensate for the ref above |
| 39 | } |
| 40 | |
| 41 | return true; |
| 42 | } else { |
| 43 | return false; |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | void RefCounted::_bind_methods() { |
| 48 | ClassDB::bind_method(D_METHOD("init_ref" ), &RefCounted::init_ref); |
| 49 | ClassDB::bind_method(D_METHOD("reference" ), &RefCounted::reference); |
| 50 | ClassDB::bind_method(D_METHOD("unreference" ), &RefCounted::unreference); |
| 51 | ClassDB::bind_method(D_METHOD("get_reference_count" ), &RefCounted::get_reference_count); |
| 52 | } |
| 53 | |
| 54 | int RefCounted::get_reference_count() const { |
| 55 | return refcount.get(); |
| 56 | } |
| 57 | |
| 58 | bool RefCounted::reference() { |
| 59 | uint32_t rc_val = refcount.refval(); |
| 60 | bool success = rc_val != 0; |
| 61 | |
| 62 | if (success && rc_val <= 2 /* higher is not relevant */) { |
| 63 | if (get_script_instance()) { |
| 64 | get_script_instance()->refcount_incremented(); |
| 65 | } |
| 66 | if (_get_extension() && _get_extension()->reference) { |
| 67 | _get_extension()->reference(_get_extension_instance()); |
| 68 | } |
| 69 | |
| 70 | _instance_binding_reference(true); |
| 71 | } |
| 72 | |
| 73 | return success; |
| 74 | } |
| 75 | |
| 76 | bool RefCounted::unreference() { |
| 77 | uint32_t rc_val = refcount.unrefval(); |
| 78 | bool die = rc_val == 0; |
| 79 | |
| 80 | if (rc_val <= 1 /* higher is not relevant */) { |
| 81 | if (get_script_instance()) { |
| 82 | bool script_ret = get_script_instance()->refcount_decremented(); |
| 83 | die = die && script_ret; |
| 84 | } |
| 85 | if (_get_extension() && _get_extension()->unreference) { |
| 86 | _get_extension()->unreference(_get_extension_instance()); |
| 87 | } |
| 88 | |
| 89 | bool binding_ret = _instance_binding_reference(false); |
| 90 | die = die && binding_ret; |
| 91 | } |
| 92 | |
| 93 | return die; |
| 94 | } |
| 95 | |
| 96 | RefCounted::RefCounted() : |
| 97 | Object(true) { |
| 98 | refcount.init(); |
| 99 | refcount_init.init(); |
| 100 | } |
| 101 | |
| 102 | Variant WeakRef::get_ref() const { |
| 103 | if (ref.is_null()) { |
| 104 | return Variant(); |
| 105 | } |
| 106 | |
| 107 | Object *obj = ObjectDB::get_instance(ref); |
| 108 | if (!obj) { |
| 109 | return Variant(); |
| 110 | } |
| 111 | RefCounted *r = cast_to<RefCounted>(obj); |
| 112 | if (r) { |
| 113 | return Ref<RefCounted>(r); |
| 114 | } |
| 115 | |
| 116 | return obj; |
| 117 | } |
| 118 | |
| 119 | void WeakRef::set_obj(Object *p_object) { |
| 120 | ref = p_object ? p_object->get_instance_id() : ObjectID(); |
| 121 | } |
| 122 | |
| 123 | void WeakRef::set_ref(const Ref<RefCounted> &p_ref) { |
| 124 | ref = p_ref.is_valid() ? p_ref->get_instance_id() : ObjectID(); |
| 125 | } |
| 126 | |
| 127 | void WeakRef::_bind_methods() { |
| 128 | ClassDB::bind_method(D_METHOD("get_ref" ), &WeakRef::get_ref); |
| 129 | } |
| 130 | |