Rename and expose RefCounted::get_reference_count()

This commit is contained in:
Marc Gilleron 2022-09-19 15:45:45 +01:00
parent 908795301b
commit 474cee7daf
7 changed files with 18 additions and 11 deletions

View File

@ -489,7 +489,7 @@ bool ResourceCache::has(const String &p_path) {
Resource **res = resources.getptr(p_path); Resource **res = resources.getptr(p_path);
if (res && (*res)->reference_get_count() == 0) { if (res && (*res)->get_reference_count() == 0) {
// This resource is in the process of being deleted, ignore its existence. // This resource is in the process of being deleted, ignore its existence.
(*res)->path_cache = String(); (*res)->path_cache = String();
resources.erase(p_path); resources.erase(p_path);

View File

@ -48,9 +48,10 @@ void RefCounted::_bind_methods() {
ClassDB::bind_method(D_METHOD("init_ref"), &RefCounted::init_ref); ClassDB::bind_method(D_METHOD("init_ref"), &RefCounted::init_ref);
ClassDB::bind_method(D_METHOD("reference"), &RefCounted::reference); ClassDB::bind_method(D_METHOD("reference"), &RefCounted::reference);
ClassDB::bind_method(D_METHOD("unreference"), &RefCounted::unreference); ClassDB::bind_method(D_METHOD("unreference"), &RefCounted::unreference);
ClassDB::bind_method(D_METHOD("get_reference_count"), &RefCounted::get_reference_count);
} }
int RefCounted::reference_get_count() const { int RefCounted::get_reference_count() const {
return refcount.get(); return refcount.get();
} }

View File

@ -47,7 +47,7 @@ public:
bool init_ref(); bool init_ref();
bool reference(); // returns false if refcount is at zero and didn't get increased bool reference(); // returns false if refcount is at zero and didn't get increased
bool unreference(); bool unreference();
int reference_get_count() const; int get_reference_count() const;
RefCounted(); RefCounted();
~RefCounted() {} ~RefCounted() {}

View File

@ -13,6 +13,12 @@
<link title="When and how to avoid using nodes for everything">$DOCS_URL/tutorials/best_practices/node_alternatives.html</link> <link title="When and how to avoid using nodes for everything">$DOCS_URL/tutorials/best_practices/node_alternatives.html</link>
</tutorials> </tutorials>
<methods> <methods>
<method name="get_reference_count" qualifiers="const">
<return type="int" />
<description>
Returns the current reference count.
</description>
</method>
<method name="init_ref"> <method name="init_ref">
<return type="bool" /> <return type="bool" />
<description> <description>

View File

@ -1299,7 +1299,7 @@ GDNativeBool CSharpLanguage::_instance_binding_reference_callback(void *p_token,
MonoGCHandleData &gchandle = script_binding.gchandle; MonoGCHandleData &gchandle = script_binding.gchandle;
int refcount = rc_owner->reference_get_count(); int refcount = rc_owner->get_reference_count();
if (!script_binding.inited) { if (!script_binding.inited) {
return refcount == 0; return refcount == 0;
@ -1818,7 +1818,7 @@ void CSharpInstance::refcount_incremented() {
RefCounted *rc_owner = Object::cast_to<RefCounted>(owner); RefCounted *rc_owner = Object::cast_to<RefCounted>(owner);
if (rc_owner->reference_get_count() > 1 && gchandle.is_weak()) { // The managed side also holds a reference, hence 1 instead of 0 if (rc_owner->get_reference_count() > 1 && gchandle.is_weak()) { // The managed side also holds a reference, hence 1 instead of 0
// The reference count was increased after the managed side was the only one referencing our owner. // The reference count was increased after the managed side was the only one referencing our owner.
// This means the owner is being referenced again by the unmanaged side, // This means the owner is being referenced again by the unmanaged side,
// so the owner must hold the managed side alive again to avoid it from being GCed. // so the owner must hold the managed side alive again to avoid it from being GCed.
@ -1849,7 +1849,7 @@ bool CSharpInstance::refcount_decremented() {
RefCounted *rc_owner = Object::cast_to<RefCounted>(owner); RefCounted *rc_owner = Object::cast_to<RefCounted>(owner);
int refcount = rc_owner->reference_get_count(); int refcount = rc_owner->get_reference_count();
if (refcount == 1 && !gchandle.is_weak()) { // The managed side also holds a reference, hence 1 instead of 0 if (refcount == 1 && !gchandle.is_weak()) { // The managed side also holds a reference, hence 1 instead of 0
// If owner owner is no longer referenced by the unmanaged side, // If owner owner is no longer referenced by the unmanaged side,
@ -1995,7 +1995,7 @@ CSharpInstance::~CSharpInstance() {
#ifdef DEBUG_ENABLED #ifdef DEBUG_ENABLED
// The "instance binding" holds a reference so the refcount should be at least 2 before `scope_keep_owner_alive` goes out of scope // The "instance binding" holds a reference so the refcount should be at least 2 before `scope_keep_owner_alive` goes out of scope
CRASH_COND(rc_owner->reference_get_count() <= 1); CRASH_COND(rc_owner->get_reference_count() <= 1);
#endif #endif
} }

View File

@ -62,7 +62,7 @@ Ref<KinematicCollision2D> PhysicsBody2D::_move(const Vector2 &p_distance, bool p
if (move_and_collide(parameters, result, p_test_only)) { if (move_and_collide(parameters, result, p_test_only)) {
// Create a new instance when the cached reference is invalid or still in use in script. // Create a new instance when the cached reference is invalid or still in use in script.
if (motion_cache.is_null() || motion_cache->reference_get_count() > 1) { if (motion_cache.is_null() || motion_cache->get_reference_count() > 1) {
motion_cache.instantiate(); motion_cache.instantiate();
motion_cache->owner = this; motion_cache->owner = this;
} }
@ -1557,7 +1557,7 @@ Ref<KinematicCollision2D> CharacterBody2D::_get_slide_collision(int p_bounce) {
} }
// Create a new instance when the cached reference is invalid or still in use in script. // Create a new instance when the cached reference is invalid or still in use in script.
if (slide_colliders[p_bounce].is_null() || slide_colliders[p_bounce]->reference_get_count() > 1) { if (slide_colliders[p_bounce].is_null() || slide_colliders[p_bounce]->get_reference_count() > 1) {
slide_colliders.write[p_bounce].instantiate(); slide_colliders.write[p_bounce].instantiate();
slide_colliders.write[p_bounce]->owner = this; slide_colliders.write[p_bounce]->owner = this;
} }

View File

@ -100,7 +100,7 @@ Ref<KinematicCollision3D> PhysicsBody3D::_move(const Vector3 &p_distance, bool p
if (move_and_collide(parameters, result, p_test_only)) { if (move_and_collide(parameters, result, p_test_only)) {
// Create a new instance when the cached reference is invalid or still in use in script. // Create a new instance when the cached reference is invalid or still in use in script.
if (motion_cache.is_null() || motion_cache->reference_get_count() > 1) { if (motion_cache.is_null() || motion_cache->get_reference_count() > 1) {
motion_cache.instantiate(); motion_cache.instantiate();
motion_cache->owner = this; motion_cache->owner = this;
} }
@ -1810,7 +1810,7 @@ Ref<KinematicCollision3D> CharacterBody3D::_get_slide_collision(int p_bounce) {
} }
// Create a new instance when the cached reference is invalid or still in use in script. // Create a new instance when the cached reference is invalid or still in use in script.
if (slide_colliders[p_bounce].is_null() || slide_colliders[p_bounce]->reference_get_count() > 1) { if (slide_colliders[p_bounce].is_null() || slide_colliders[p_bounce]->get_reference_count() > 1) {
slide_colliders.write[p_bounce].instantiate(); slide_colliders.write[p_bounce].instantiate();
slide_colliders.write[p_bounce]->owner = this; slide_colliders.write[p_bounce]->owner = this;
} }