Merge pull request #849 from Zylann/const_cast_to

cast_to` for `const Object*`
pull/853/head
Rémi Verschelde 2022-09-19 09:29:51 +02:00
commit d4cef0bd43
2 changed files with 17 additions and 0 deletions

View File

@ -1083,10 +1083,15 @@ def generate_engine_class_header(class_api, used_classes, fully_used_classes, us
# Special cases. # Special cases.
if class_name == "Object": if class_name == "Object":
result.append("") result.append("")
result.append("\ttemplate<class T>") result.append("\ttemplate<class T>")
result.append("\tstatic T *cast_to(Object *p_object);") result.append("\tstatic T *cast_to(Object *p_object);")
result.append("\ttemplate<class T>")
result.append("\tstatic const T *cast_to(const Object *p_object);")
result.append("\tvirtual ~Object() = default;") result.append("\tvirtual ~Object() = default;")
elif use_template_get_node and class_name == "Node": elif use_template_get_node and class_name == "Node":
result.append("\ttemplate<class T>") result.append("\ttemplate<class T>")
result.append( result.append(

View File

@ -144,6 +144,18 @@ T *Object::cast_to(Object *p_object) {
return reinterpret_cast<T *>(internal::gdn_interface->object_get_instance_binding(casted, internal::token, &T::___binding_callbacks)); return reinterpret_cast<T *>(internal::gdn_interface->object_get_instance_binding(casted, internal::token, &T::___binding_callbacks));
} }
template <class T>
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<const T *>(internal::gdn_interface->object_get_instance_binding(casted, internal::token, &T::___binding_callbacks));
}
} // namespace godot } // namespace godot
#endif // ! GODOT_OBJECT_HPP #endif // ! GODOT_OBJECT_HPP