Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose is_class_ptr to GDNative for dynamic casts #34688

Merged
merged 1 commit into from
Jan 3, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions core/class_db.h
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ class ClassDB {

APIType api;
ClassInfo *inherits_ptr;
void *class_ptr;
HashMap<StringName, MethodBind *> method_map;
HashMap<StringName, int> constant_map;
HashMap<StringName, List<StringName> > enum_map;
Expand Down Expand Up @@ -177,6 +178,7 @@ class ClassDB {
ERR_FAIL_COND(!t);
t->creation_func = &creator<T>;
t->exposed = true;
t->class_ptr = T::get_class_ptr_static();
T::register_custom_data_to_otdb();
}

Expand All @@ -188,6 +190,7 @@ class ClassDB {
ClassInfo *t = classes.getptr(T::get_class_static());
ERR_FAIL_COND(!t);
t->exposed = true;
t->class_ptr = T::get_class_ptr_static();
//nothing
}

Expand All @@ -206,6 +209,7 @@ class ClassDB {
ERR_FAIL_COND(!t);
t->creation_func = &_create_ptr_func<T>;
t->exposed = true;
t->class_ptr = T::get_class_ptr_static();
T::register_custom_data_to_otdb();
}

Expand Down
13 changes: 13 additions & 0 deletions modules/gdnative/gdnative/gdnative.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,19 @@ bool GDAPI godot_is_instance_valid(const godot_object *p_object) {
return ObjectDB::instance_validate((Object *)p_object);
}

void *godot_get_class_tag(const godot_string_name *p_class) {
StringName class_name = *(StringName *)p_class;
ClassDB::ClassInfo *class_info = ClassDB::classes.getptr(class_name);
return class_info ? class_info->class_ptr : NULL;
}

godot_object *godot_object_cast_to(const godot_object *p_object, void *p_class_tag) {
if (!p_object) return NULL;
Object *o = (Object *)p_object;

return o->is_class_ptr(p_class_tag) ? (godot_object *)o : NULL;
}

#ifdef __cplusplus
}
#endif
15 changes: 15 additions & 0 deletions modules/gdnative/gdnative_api.json
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,21 @@
"arguments": [
["const godot_pool_color_array *", "p_self"]
]
},
{
"name": "godot_get_class_tag",
"return_type": "void *",
"arguments": [
["const godot_string_name *", "p_class"]
]
},
{
"name": "godot_object_cast_to",
"return_type": "godot_object *",
"arguments": [
["const godot_object *", "p_object"],
["void *", "p_class_tag"]
]
}
]
},
Expand Down
4 changes: 4 additions & 0 deletions modules/gdnative/include/gdnative/gdnative.h
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,10 @@ void GDAPI godot_print(const godot_string *p_message);

bool GDAPI godot_is_instance_valid(const godot_object *p_object);

//tags used for safe dynamic casting
void GDAPI *godot_get_class_tag(const godot_string_name *p_class);
godot_object GDAPI *godot_object_cast_to(const godot_object *p_object, void *p_class_tag);

#ifdef __cplusplus
}
#endif
Expand Down