/**************************************************************************/1/* ref_counted.cpp */2/**************************************************************************/3/* This file is part of: */4/* GODOT ENGINE */5/* https://godotengine.org */6/**************************************************************************/7/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */8/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */9/* */10/* Permission is hereby granted, free of charge, to any person obtaining */11/* a copy of this software and associated documentation files (the */12/* "Software"), to deal in the Software without restriction, including */13/* without limitation the rights to use, copy, modify, merge, publish, */14/* distribute, sublicense, and/or sell copies of the Software, and to */15/* permit persons to whom the Software is furnished to do so, subject to */16/* the following conditions: */17/* */18/* The above copyright notice and this permission notice shall be */19/* included in all copies or substantial portions of the Software. */20/* */21/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */22/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */23/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */24/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */25/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */26/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */27/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */28/**************************************************************************/2930#include "ref_counted.h"3132#include "core/object/script_language.h"3334bool RefCounted::init_ref() {35if (reference()) {36if (!is_referenced() && refcount_init.unref()) {37unreference(); // first referencing is already 1, so compensate for the ref above38}3940return true;41} else {42return false;43}44}4546void RefCounted::_bind_methods() {47ClassDB::bind_method(D_METHOD("init_ref"), &RefCounted::init_ref);48ClassDB::bind_method(D_METHOD("reference"), &RefCounted::reference);49ClassDB::bind_method(D_METHOD("unreference"), &RefCounted::unreference);50ClassDB::bind_method(D_METHOD("get_reference_count"), &RefCounted::get_reference_count);51}5253int RefCounted::get_reference_count() const {54return refcount.get();55}5657bool RefCounted::reference() {58uint32_t rc_val = refcount.refval();59bool success = rc_val != 0;6061if (success && rc_val <= 2 /* higher is not relevant */) {62if (get_script_instance()) {63get_script_instance()->refcount_incremented();64}65if (_get_extension() && _get_extension()->reference) {66_get_extension()->reference(_get_extension_instance());67}6869_instance_binding_reference(true);70}7172return success;73}7475bool RefCounted::unreference() {76uint32_t rc_val = refcount.unrefval();77bool die = rc_val == 0;7879if (rc_val <= 1 /* higher is not relevant */) {80if (get_script_instance()) {81bool script_ret = get_script_instance()->refcount_decremented();82die = die && script_ret;83}84if (_get_extension() && _get_extension()->unreference) {85_get_extension()->unreference(_get_extension_instance());86}8788bool binding_ret = _instance_binding_reference(false);89die = die && binding_ret;90}9192return die;93}9495RefCounted::RefCounted() :96Object(true) {97refcount.init();98refcount_init.init();99}100101Variant WeakRef::get_ref() const {102if (ref.is_null()) {103return Variant();104}105106Object *obj = ObjectDB::get_instance(ref);107if (!obj) {108return Variant();109}110RefCounted *r = cast_to<RefCounted>(obj);111if (r) {112return Ref<RefCounted>(r);113}114115return obj;116}117118void WeakRef::set_obj(Object *p_object) {119ref = p_object ? p_object->get_instance_id() : ObjectID();120}121122void WeakRef::set_ref(const Ref<RefCounted> &p_ref) {123ref = p_ref.is_valid() ? p_ref->get_instance_id() : ObjectID();124}125126void WeakRef::_bind_methods() {127ClassDB::bind_method(D_METHOD("get_ref"), &WeakRef::get_ref);128}129130131