diff options
author | David Snopek <dsnopek@gmail.com> | 2023-05-24 21:17:12 -0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-05-24 21:17:12 -0500 |
commit | 8052f818b47c163d3e0a19b13282d909ebfa55f2 (patch) | |
tree | 52ec76b9da866251232b46599710a64d54a81dd9 /include/godot_cpp | |
parent | 08bc2ef680cd8b536519d7f59c6f760e96a3d0a8 (diff) | |
parent | 431e30bc3273c83315725f56365845f0b89c0524 (diff) | |
download | redot-cpp-8052f818b47c163d3e0a19b13282d909ebfa55f2.tar.gz |
Merge pull request #1050 from dsnopek/object-correct-class
Ensure GDExtension class is the correct type for the Godot engine class
Diffstat (limited to 'include/godot_cpp')
-rw-r--r-- | include/godot_cpp/classes/ref.hpp | 8 | ||||
-rw-r--r-- | include/godot_cpp/core/class_db.hpp | 11 | ||||
-rw-r--r-- | include/godot_cpp/core/engine_ptrcall.hpp | 4 | ||||
-rw-r--r-- | include/godot_cpp/core/method_ptrcall.hpp | 9 | ||||
-rw-r--r-- | include/godot_cpp/core/object.hpp | 12 | ||||
-rw-r--r-- | include/godot_cpp/godot.hpp | 4 |
6 files changed, 31 insertions, 17 deletions
diff --git a/include/godot_cpp/classes/ref.hpp b/include/godot_cpp/classes/ref.hpp index 301f847..5c10e59 100644 --- a/include/godot_cpp/classes/ref.hpp +++ b/include/godot_cpp/classes/ref.hpp @@ -242,9 +242,7 @@ struct PtrToArg<Ref<T>> { _FORCE_INLINE_ static Ref<T> convert(const void *p_ptr) { // Important: p_ptr is T*, not Ref<T>*, since Object* is what engine gives to ptrcall. ERR_FAIL_NULL_V(p_ptr, Ref<T>()); - return Ref<T>(reinterpret_cast<T *>(godot::internal::gdextension_interface_object_get_instance_binding( - reinterpret_cast<GDExtensionObjectPtr>(const_cast<void *>(p_ptr)), - godot::internal::token, &T::___binding_callbacks))); + return Ref<T>(reinterpret_cast<T *>(godot::internal::get_object_instance_binding(reinterpret_cast<GDExtensionObjectPtr>(const_cast<void *>(p_ptr))))); } typedef Ref<T> EncodeT; @@ -267,9 +265,7 @@ struct PtrToArg<const Ref<T> &> { _FORCE_INLINE_ static Ref<T> convert(const void *p_ptr) { ERR_FAIL_NULL_V(p_ptr, Ref<T>()); - return Ref<T>(reinterpret_cast<T *>(godot::internal::gdextension_interface_object_get_instance_binding( - reinterpret_cast<GDExtensionObjectPtr>(const_cast<void *>(p_ptr)), - godot::internal::token, &T::___binding_callbacks))); + return Ref<T>(reinterpret_cast<T *>(godot::internal::get_object_instance_binding(reinterpret_cast<GDExtensionObjectPtr>(const_cast<void *>(p_ptr))))); } }; diff --git a/include/godot_cpp/core/class_db.hpp b/include/godot_cpp/core/class_db.hpp index 1ac1f91..46fbd42 100644 --- a/include/godot_cpp/core/class_db.hpp +++ b/include/godot_cpp/core/class_db.hpp @@ -104,6 +104,7 @@ public: private: // This may only contain custom classes, not Godot classes static std::unordered_map<StringName, ClassInfo> classes; + static std::unordered_map<StringName, const GDExtensionInstanceBindingCallbacks *> instance_binding_callbacks; static MethodBind *bind_methodfi(uint32_t p_flags, MethodBind *p_bind, const MethodDefinition &method_name, const void **p_defs, int p_defcount); static void initialize_class(const ClassInfo &cl); @@ -117,6 +118,8 @@ public: static void register_class(bool p_virtual = false); template <class T> static void register_abstract_class(); + template <class T> + static void register_engine_class(); template <class N, class M, typename... VarArgs> static MethodBind *bind_method(N p_method_name, M p_method, VarArgs... p_args); @@ -137,6 +140,7 @@ public: static MethodBind *get_method(const StringName &p_class, const StringName &p_method); static GDExtensionClassCallVirtual get_virtual_func(void *p_userdata, GDExtensionConstStringNamePtr p_name); + static const GDExtensionInstanceBindingCallbacks *get_instance_binding_callbacks(const StringName &p_class); static void initialize(GDExtensionInitializationLevel p_level); static void deinitialize(GDExtensionInitializationLevel p_level); @@ -161,6 +165,8 @@ public: template <class T, bool is_abstract> void ClassDB::_register_class(bool p_virtual) { + instance_binding_callbacks[T::get_class_static()] = &T::___binding_callbacks; + // Register this class within our plugin ClassInfo cl; cl.name = T::get_class_static(); @@ -213,6 +219,11 @@ void ClassDB::register_abstract_class() { ClassDB::_register_class<T, true>(); } +template <class T> +void ClassDB::register_engine_class() { + instance_binding_callbacks[T::get_class_static()] = &T::___binding_callbacks; +} + template <class N, class M, typename... VarArgs> MethodBind *ClassDB::bind_method(N p_method_name, M p_method, VarArgs... p_args) { Variant args[sizeof...(p_args) + 1] = { p_args..., Variant() }; // +1 makes sure zero sized arrays are also supported. diff --git a/include/godot_cpp/core/engine_ptrcall.hpp b/include/godot_cpp/core/engine_ptrcall.hpp index a5fd114..69ab196 100644 --- a/include/godot_cpp/core/engine_ptrcall.hpp +++ b/include/godot_cpp/core/engine_ptrcall.hpp @@ -51,7 +51,7 @@ O *_call_native_mb_ret_obj(const GDExtensionMethodBindPtr mb, void *instance, co if (ret == nullptr) { return nullptr; } - return reinterpret_cast<O *>(internal::gdextension_interface_object_get_instance_binding(ret, internal::token, &O::___binding_callbacks)); + return reinterpret_cast<O *>(internal::get_object_instance_binding(ret)); } template <class R, class... Args> @@ -81,7 +81,7 @@ Object *_call_utility_ret_obj(const GDExtensionPtrUtilityFunction func, void *in GodotObject *ret = nullptr; std::array<GDExtensionConstTypePtr, sizeof...(Args)> mb_args = { { (GDExtensionConstTypePtr)args... } }; func(&ret, mb_args.data(), mb_args.size()); - return (Object *)internal::gdextension_interface_object_get_instance_binding(ret, internal::token, &Object::___binding_callbacks); + return (Object *)internal::get_object_instance_binding(ret); } template <class... Args> diff --git a/include/godot_cpp/core/method_ptrcall.hpp b/include/godot_cpp/core/method_ptrcall.hpp index 1c99fd9..283f4b0 100644 --- a/include/godot_cpp/core/method_ptrcall.hpp +++ b/include/godot_cpp/core/method_ptrcall.hpp @@ -33,6 +33,7 @@ #include <godot_cpp/core/defs.hpp> +#include <godot_cpp/core/object.hpp> #include <godot_cpp/godot.hpp> #include <godot_cpp/variant/variant.hpp> @@ -168,9 +169,7 @@ MAKE_PTRARG_BY_REFERENCE(Variant); template <class T> struct PtrToArg<T *> { _FORCE_INLINE_ static T *convert(const void *p_ptr) { - return reinterpret_cast<T *>(godot::internal::gdextension_interface_object_get_instance_binding( - reinterpret_cast<GDExtensionObjectPtr>(const_cast<void *>(p_ptr)), - godot::internal::token, &T::___binding_callbacks)); + return reinterpret_cast<T *>(godot::internal::get_object_instance_binding(reinterpret_cast<GDExtensionObjectPtr>(const_cast<void *>(p_ptr)))); } typedef Object *EncodeT; _FORCE_INLINE_ static void encode(T *p_var, void *p_ptr) { @@ -181,9 +180,7 @@ struct PtrToArg<T *> { template <class T> struct PtrToArg<const T *> { _FORCE_INLINE_ static const T *convert(const void *p_ptr) { - return reinterpret_cast<const T *>(godot::internal::gdextension_interface_object_get_instance_binding( - reinterpret_cast<GDExtensionObjectPtr>(const_cast<void *>(p_ptr)), - godot::internal::token, &T::___binding_callbacks)); + return reinterpret_cast<const T *>(godot::internal::get_object_instance_binding(reinterpret_cast<GDExtensionObjectPtr>(const_cast<void *>(p_ptr)))); } typedef const Object *EncodeT; _FORCE_INLINE_ static void encode(T *p_var, void *p_ptr) { diff --git a/include/godot_cpp/core/object.hpp b/include/godot_cpp/core/object.hpp index 54ca4be..d66e388 100644 --- a/include/godot_cpp/core/object.hpp +++ b/include/godot_cpp/core/object.hpp @@ -52,6 +52,12 @@ namespace godot { +namespace internal { + +Object *get_object_instance_binding(GodotObject *); + +} // namespace internal + struct MethodInfo { StringName name; PropertyInfo return_val; @@ -128,7 +134,7 @@ public: if (obj == nullptr) { return nullptr; } - return reinterpret_cast<Object *>(internal::gdextension_interface_object_get_instance_binding(obj, internal::token, &Object::___binding_callbacks)); + return internal::get_object_instance_binding(obj); } }; @@ -142,7 +148,7 @@ T *Object::cast_to(Object *p_object) { if (casted == nullptr) { return nullptr; } - return reinterpret_cast<T *>(internal::gdextension_interface_object_get_instance_binding(casted, internal::token, &T::___binding_callbacks)); + return dynamic_cast<T *>(internal::get_object_instance_binding(casted)); } template <class T> @@ -155,7 +161,7 @@ const T *Object::cast_to(const Object *p_object) { if (casted == nullptr) { return nullptr; } - return reinterpret_cast<const T *>(internal::gdextension_interface_object_get_instance_binding(casted, internal::token, &T::___binding_callbacks)); + return dynamic_cast<const T *>(internal::get_object_instance_binding(casted)); } } // namespace godot diff --git a/include/godot_cpp/godot.hpp b/include/godot_cpp/godot.hpp index 20e9400..bdcf148 100644 --- a/include/godot_cpp/godot.hpp +++ b/include/godot_cpp/godot.hpp @@ -159,6 +159,7 @@ extern "C" GDExtensionInterfaceGlobalGetSingleton gdextension_interface_global_g extern "C" GDExtensionInterfaceObjectGetInstanceBinding gdextension_interface_object_get_instance_binding; extern "C" GDExtensionInterfaceObjectSetInstanceBinding gdextension_interface_object_set_instance_binding; extern "C" GDExtensionInterfaceObjectSetInstance gdextension_interface_object_set_instance; +extern "C" GDExtensionInterfaceObjectGetClassName gdextension_interface_object_get_class_name; extern "C" GDExtensionInterfaceObjectCastTo gdextension_interface_object_cast_to; extern "C" GDExtensionInterfaceObjectGetInstanceFromId gdextension_interface_object_get_instance_from_id; extern "C" GDExtensionInterfaceObjectGetInstanceId gdextension_interface_object_get_instance_id; @@ -188,6 +189,9 @@ enum ModuleInitializationLevel { }; class GDExtensionBinding { +private: + static void register_engine_classes(); + public: using Callback = void (*)(ModuleInitializationLevel p_level); |