Skip to content

Commit 1d829f2

Browse files
authored
Merge pull request #1448 from dsnopek/require-bind-methods
Give compile-time error if registering a class without its own `_bind_methods()` function
2 parents e23b117 + ca46ef4 commit 1d829f2

File tree

3 files changed

+16
-0
lines changed

3 files changed

+16
-0
lines changed

include/godot_cpp/classes/wrapped.hpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,7 @@ protected:
214214
\
215215
public: \
216216
typedef m_class self_type; \
217+
typedef m_inherits parent_type; \
217218
\
218219
static void initialize_class() { \
219220
static bool initialized = false; \
@@ -381,6 +382,7 @@ public:
381382
private: \
382383
inline static ::godot::internal::EngineClassRegistration<m_class> _gde_engine_class_registration_helper; \
383384
void operator=(const m_class &p_rval) {} \
385+
friend class ::godot::ClassDB; \
384386
\
385387
protected: \
386388
virtual const GDExtensionInstanceBindingCallbacks *_get_bindings_callbacks() const override { \
@@ -390,6 +392,8 @@ protected:
390392
m_class(const char *p_godot_class) : m_inherits(p_godot_class) {} \
391393
m_class(GodotObject *p_godot_object) : m_inherits(p_godot_object) {} \
392394
\
395+
static void _bind_methods() {} \
396+
\
393397
static void (*_get_bind_methods())() { \
394398
return nullptr; \
395399
} \
@@ -432,6 +436,7 @@ protected:
432436
\
433437
public: \
434438
typedef m_class self_type; \
439+
typedef m_inherits parent_type; \
435440
\
436441
static void initialize_class() {} \
437442
\

include/godot_cpp/core/class_db.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,7 @@ class ClassDB {
203203
template <typename T, bool is_abstract>
204204
void ClassDB::_register_class(bool p_virtual, bool p_exposed, bool p_runtime) {
205205
static_assert(TypesAreSame<typename T::self_type, T>::value, "Class not declared properly, please use GDCLASS.");
206+
static_assert(!FunctionsAreSame<T::self_type::_bind_methods, T::parent_type::_bind_methods>::value, "Class must declare 'static void _bind_methods'.");
206207
static_assert(!std::is_abstract_v<T> || is_abstract, "Class is abstract, please use GDREGISTER_ABSTRACT_CLASS.");
207208
instance_binding_callbacks[T::get_class_static()] = &T::_gde_binding_callbacks;
208209

include/godot_cpp/core/type_info.hpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,16 @@ struct TypesAreSame<A, A> {
5858
static bool const value = true;
5959
};
6060

61+
template <auto A, auto B>
62+
struct FunctionsAreSame {
63+
static bool const value = false;
64+
};
65+
66+
template <auto A>
67+
struct FunctionsAreSame<A, A> {
68+
static bool const value = true;
69+
};
70+
6171
template <typename B, typename D>
6272
struct TypeInherits {
6373
static D *get_d();

0 commit comments

Comments
 (0)