Skip to content

Commit

Permalink
Fix deriving a custom class with virtual methods
Browse files Browse the repository at this point in the history
  • Loading branch information
Zylann committed Sep 20, 2022
1 parent a330342 commit 24deaa3
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 6 deletions.
11 changes: 8 additions & 3 deletions binding_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -1071,17 +1071,22 @@ def generate_engine_class_header(class_api, used_classes, fully_used_classes, us
result.append(method_signature + ";")

result.append("protected:")
result.append("\ttemplate <class T>")
# T is the custom class we want to register (from which the call initiates, going up the inheritance chain),
# B is its base class (can be a custom class too, that's why we pass it).
result.append("\ttemplate <class T, class B>")
result.append("\tstatic void register_virtuals() {")
if class_name != "Object":
result.append(f"\t\t{inherits}::register_virtuals<T>();")
result.append(f"\t\t{inherits}::register_virtuals<T, B>();")
if "methods" in class_api:
for method in class_api["methods"]:
if not method["is_virtual"]:
continue
method_name = escape_identifier(method["name"])
result.append(
f"\t\tif constexpr (!std::is_same_v<decltype(&{class_name}::{method_name}),decltype(&T::{method_name})>) {{"
# If the method is different from the base class, it means T overrides it, so it needs to be bound.
# Note that with an `if constexpr`, the code inside the `if` will not even be compiled if the
# condition returns false (in such cases it can't compile due to ambiguity).
f"\t\tif constexpr (!std::is_same_v<decltype(&B::{method_name}),decltype(&T::{method_name})>) {{"
)
result.append(f"\t\t\tBIND_VIRTUAL_METHOD(T, {method_name});")
result.append("\t\t}")
Expand Down
6 changes: 3 additions & 3 deletions include/godot_cpp/classes/wrapped.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -145,9 +145,9 @@ protected:
return (::godot::String(::godot::Wrapped::*)()) & m_class::_to_string; \
} \
\
template <class T> \
template <class T, class B> \
static void register_virtuals() { \
m_inherits::register_virtuals<T>(); \
m_inherits::register_virtuals<T, B>(); \
} \
\
public: \
Expand All @@ -159,7 +159,7 @@ public:
m_inherits::initialize_class(); \
if (m_class::_get_bind_methods() != m_inherits::_get_bind_methods()) { \
_bind_methods(); \
m_inherits::register_virtuals<m_class>(); \
m_inherits::register_virtuals<m_class, m_inherits>(); \
} \
initialized = true; \
} \
Expand Down

0 comments on commit 24deaa3

Please sign in to comment.