Simplify register apis in ClassDB and use macros to register classes instead of functions in example.

pull/1241/head
Daylily-Zeleen 2023-09-11 23:24:51 +08:00
parent 16ffb2795a
commit e291270b42
3 changed files with 27 additions and 33 deletions

View File

@ -103,16 +103,12 @@ private:
static void initialize_class(const ClassInfo &cl); static void initialize_class(const ClassInfo &cl);
static void bind_method_godot(const StringName &p_class_name, MethodBind *p_method); static void bind_method_godot(const StringName &p_class_name, MethodBind *p_method);
template <class T, bool is_abstract> template <class T, bool is_virtual = false, bool is_abstract = false, bool is_exposed = true>
static void _register_class(bool p_virtual = false, bool p_exposed = true); static void _register_class();
public: public:
template <class T> template <class T, bool is_virtual = false, bool is_abstract = false, bool is_exposed = true>
static void register_class(bool p_virtual = false); static void register_class();
template <class T>
static void register_abstract_class();
template <class T>
static void register_internal_class();
template <class T> template <class T>
static void register_engine_class(); static void register_engine_class();
@ -158,8 +154,8 @@ public:
godot::ClassDB::bind_virtual_method(m_class::get_class_static(), #m_method, _call##m_method); \ godot::ClassDB::bind_virtual_method(m_class::get_class_static(), #m_method, _call##m_method); \
} }
template <class T, bool is_abstract> template <class T, bool is_virtual, bool is_abstract, bool is_exposed>
void ClassDB::_register_class(bool p_virtual, bool p_exposed) { void ClassDB::_register_class() {
instance_binding_callbacks[T::get_class_static()] = &T::_gde_binding_callbacks; instance_binding_callbacks[T::get_class_static()] = &T::_gde_binding_callbacks;
// Register this class within our plugin // Register this class within our plugin
@ -177,9 +173,9 @@ void ClassDB::_register_class(bool p_virtual, bool p_exposed) {
// Register this class with Godot // Register this class with Godot
GDExtensionClassCreationInfo2 class_info = { GDExtensionClassCreationInfo2 class_info = {
p_virtual, // GDExtensionBool is_virtual; is_virtual, // GDExtensionBool is_virtual;
is_abstract, // GDExtensionBool is_abstract; is_abstract, // GDExtensionBool is_abstract;
p_exposed, // GDExtensionBool is_exposed; is_exposed, // GDExtensionBool is_exposed;
T::set_bind, // GDExtensionClassSet set_func; T::set_bind, // GDExtensionClassSet set_func;
T::get_bind, // GDExtensionClassGet get_func; T::get_bind, // GDExtensionClassGet get_func;
T::has_get_property_list() ? T::get_property_list_bind : nullptr, // GDExtensionClassGetPropertyList get_property_list_func; T::has_get_property_list() ? T::get_property_list_bind : nullptr, // GDExtensionClassGetPropertyList get_property_list_func;
@ -206,19 +202,9 @@ void ClassDB::_register_class(bool p_virtual, bool p_exposed) {
initialize_class(classes[cl.name]); initialize_class(classes[cl.name]);
} }
template <class T> template <class T, bool is_virtual, bool is_abstract, bool is_exposed>
void ClassDB::register_class(bool p_virtual) { void ClassDB::register_class() {
ClassDB::_register_class<T, false>(p_virtual); ClassDB::_register_class<T, is_virtual, is_exposed, is_exposed>();
}
template <class T>
void ClassDB::register_abstract_class() {
ClassDB::_register_class<T, true>();
}
template <class T>
void ClassDB::register_internal_class() {
ClassDB::_register_class<T, false>(false, false);
} }
template <class T> template <class T>
@ -282,9 +268,9 @@ MethodBind *ClassDB::bind_vararg_method(uint32_t p_flags, StringName p_name, M p
} }
#define GDREGISTER_CLASS(m_class) ClassDB::register_class<m_class>(); #define GDREGISTER_CLASS(m_class) ClassDB::register_class<m_class>();
#define GDREGISTER_VIRTUAL_CLASS(m_class) ClassDB::register_class<m_class>(true); #define GDREGISTER_VIRTUAL_CLASS(m_class) ClassDB::register_class<m_class, true>();
#define GDREGISTER_ABSTRACT_CLASS(m_class) ClassDB::register_abstract_class<m_class>(); #define GDREGISTER_ABSTRACT_CLASS(m_class) ClassDB::register_class<m_class, false, true>();
#define GDREGISTER_INTERNAL_CLASS(m_class) ClassDB::register_internal_class<m_class>(); #define GDREGISTER_INTERNAL_CLASS(m_class) ClassDB::register_class<m_class, false, false, false>();
} // namespace godot } // namespace godot

View File

@ -177,4 +177,11 @@ protected:
static void _bind_methods() {} static void _bind_methods() {}
}; };
class ExampleInternal : public Object {
GDCLASS(ExampleInternal, Object);
protected:
static void _bind_methods() {}
};
#endif // EXAMPLE_CLASS_H #endif // EXAMPLE_CLASS_H

View File

@ -21,11 +21,12 @@ void initialize_example_module(ModuleInitializationLevel p_level) {
return; return;
} }
ClassDB::register_class<ExampleRef>(); GDREGISTER_CLASS(ExampleRef);
ClassDB::register_class<ExampleMin>(); GDREGISTER_CLASS(ExampleMin);
ClassDB::register_class<Example>(); GDREGISTER_CLASS(Example);
ClassDB::register_class<ExampleVirtual>(true); GDREGISTER_VIRTUAL_CLASS(ExampleVirtual);
ClassDB::register_abstract_class<ExampleAbstract>(); GDREGISTER_ABSTRACT_CLASS(ExampleAbstract);
GDREGISTER_INTERNAL_CLASS(ExampleInternal);
} }
void uninitialize_example_module(ModuleInitializationLevel p_level) { void uninitialize_example_module(ModuleInitializationLevel p_level) {