[Core] Add ClassDB functions to retrieve/construct extensions.

Calling the constructor alone is not enough if the class to be
instantiated is not a base class.

This commit adds two functions, one for retrieving the the extension
class reference, the other to construct an instance using the
constructor and the extension class reference.
This commit is contained in:
Fabio Alessandrelli 2021-09-21 03:54:50 +02:00
parent a4b80cdad9
commit f724bd1880
4 changed files with 25 additions and 0 deletions

View file

@ -862,6 +862,18 @@ static GDNativeClassConstructor gdnative_classdb_get_constructor(const char *p_c
return nullptr; return nullptr;
} }
static GDNativeExtensionPtr gdnative_classdb_get_extension(const char *p_classname) {
ClassDB::ClassInfo *class_info = ClassDB::classes.getptr(StringName(p_classname));
if (class_info) {
return (GDNativeExtensionPtr)class_info->native_extension;
}
return nullptr;
}
static GDNativeObjectPtr gdnative_classdb_construct_extended(GDNativeClassConstructor p_constructor, GDNativeExtensionPtr p_extension) {
return (GDNativeObjectPtr)ClassDB::construct_extended((Object * (*)()) p_constructor, (ObjectNativeExtension *)p_extension);
}
static void *gdnative_classdb_get_class_tag(const char *p_classname) { static void *gdnative_classdb_get_class_tag(const char *p_classname) {
ClassDB::ClassInfo *class_info = ClassDB::classes.getptr(p_classname); ClassDB::ClassInfo *class_info = ClassDB::classes.getptr(p_classname);
return class_info ? class_info->class_ptr : nullptr; return class_info ? class_info->class_ptr : nullptr;
@ -1010,6 +1022,8 @@ void gdnative_setup_interface(GDNativeInterface *p_interface) {
/* CLASSDB */ /* CLASSDB */
gdni.classdb_get_constructor = gdnative_classdb_get_constructor; gdni.classdb_get_constructor = gdnative_classdb_get_constructor;
gdni.classdb_get_extension = gdnative_classdb_get_extension;
gdni.classdb_construct_extended = gdnative_classdb_construct_extended;
gdni.classdb_get_method_bind = gdnative_classdb_get_method_bind; gdni.classdb_get_method_bind = gdnative_classdb_get_method_bind;
gdni.classdb_get_class_tag = gdnative_classdb_get_class_tag; gdni.classdb_get_class_tag = gdnative_classdb_get_class_tag;

View file

@ -137,6 +137,7 @@ typedef void *GDNativeStringNamePtr;
typedef void *GDNativeStringPtr; typedef void *GDNativeStringPtr;
typedef void *GDNativeObjectPtr; typedef void *GDNativeObjectPtr;
typedef void *GDNativeTypePtr; typedef void *GDNativeTypePtr;
typedef void *GDNativeExtensionPtr;
typedef void *GDNativeMethodBindPtr; typedef void *GDNativeMethodBindPtr;
typedef int64_t GDNativeInt; typedef int64_t GDNativeInt;
typedef uint8_t GDNativeBool; typedef uint8_t GDNativeBool;
@ -432,6 +433,8 @@ typedef struct {
/* CLASSDB */ /* CLASSDB */
GDNativeClassConstructor (*classdb_get_constructor)(const char *p_classname); GDNativeClassConstructor (*classdb_get_constructor)(const char *p_classname);
GDNativeExtensionPtr (*classdb_get_extension)(const char *p_classname);
GDNativeObjectPtr (*classdb_construct_extended)(GDNativeClassConstructor p_constructor, GDNativeExtensionPtr p_extension);
GDNativeMethodBindPtr (*classdb_get_method_bind)(const char *p_classname, const char *p_methodname, GDNativeInt p_hash); GDNativeMethodBindPtr (*classdb_get_method_bind)(const char *p_classname, const char *p_methodname, GDNativeInt p_hash);
void *(*classdb_get_class_tag)(const char *p_classname); void *(*classdb_get_class_tag)(const char *p_classname);

View file

@ -545,6 +545,13 @@ Object *ClassDB::instantiate(const StringName &p_class) {
return ti->creation_func(); return ti->creation_func();
} }
Object *ClassDB::construct_extended(Object *(*p_create_func)(), ObjectNativeExtension *p_extension) {
initializing_with_extension = true;
initializing_extension = p_extension;
initializing_extension_instance = p_extension->create_instance(p_extension->class_userdata);
return p_create_func();
}
bool ClassDB::can_instantiate(const StringName &p_class) { bool ClassDB::can_instantiate(const StringName &p_class) {
OBJTYPE_RLOCK; OBJTYPE_RLOCK;

View file

@ -234,6 +234,7 @@ public:
static bool is_parent_class(const StringName &p_class, const StringName &p_inherits); static bool is_parent_class(const StringName &p_class, const StringName &p_inherits);
static bool can_instantiate(const StringName &p_class); static bool can_instantiate(const StringName &p_class);
static Object *instantiate(const StringName &p_class); static Object *instantiate(const StringName &p_class);
static Object *construct_extended(Object *(*p_create_func)(), ObjectNativeExtension *p_extension);
static void instance_get_native_extension_data(ObjectNativeExtension **r_extension, GDExtensionClassInstancePtr *r_extension_instance, Object *p_base); static void instance_get_native_extension_data(ObjectNativeExtension **r_extension, GDExtensionClassInstancePtr *r_extension_instance, Object *p_base);
static APIType get_api_type(const StringName &p_class); static APIType get_api_type(const StringName &p_class);