From 99c004f6890ad0198b65e63c2ec429cad6a6291e Mon Sep 17 00:00:00 2001 From: Marc Gilleron Date: Sun, 18 Sep 2022 22:23:49 +0100 Subject: [PATCH] Add `Object::cast_to` for `const Object*` --- binding_generator.py | 5 +++++ include/godot_cpp/core/object.hpp | 12 ++++++++++++ 2 files changed, 17 insertions(+) diff --git a/binding_generator.py b/binding_generator.py index 97f1cef..28ec924 100644 --- a/binding_generator.py +++ b/binding_generator.py @@ -1083,10 +1083,15 @@ def generate_engine_class_header(class_api, used_classes, fully_used_classes, us # Special cases. if class_name == "Object": result.append("") + result.append("\ttemplate") result.append("\tstatic T *cast_to(Object *p_object);") + result.append("\ttemplate") + result.append("\tstatic const T *cast_to(const Object *p_object);") + result.append("\tvirtual ~Object() = default;") + elif use_template_get_node and class_name == "Node": result.append("\ttemplate") result.append( diff --git a/include/godot_cpp/core/object.hpp b/include/godot_cpp/core/object.hpp index 3acf827..6feea5a 100644 --- a/include/godot_cpp/core/object.hpp +++ b/include/godot_cpp/core/object.hpp @@ -144,6 +144,18 @@ T *Object::cast_to(Object *p_object) { return reinterpret_cast(internal::gdn_interface->object_get_instance_binding(casted, internal::token, &T::___binding_callbacks)); } +template +const T *Object::cast_to(const Object *p_object) { + if (p_object == nullptr) { + return nullptr; + } + GDNativeObjectPtr casted = internal::gdn_interface->object_cast_to(p_object->_owner, internal::gdn_interface->classdb_get_class_tag(T::get_class_static())); + if (casted == nullptr) { + return nullptr; + } + return reinterpret_cast(internal::gdn_interface->object_get_instance_binding(casted, internal::token, &T::___binding_callbacks)); +} + } // namespace godot #endif // ! GODOT_OBJECT_HPP