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

Templated and custom-named classes support and test #1362

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
35 changes: 30 additions & 5 deletions include/godot_cpp/classes/wrapped.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -142,10 +142,9 @@ struct EngineClassRegistration {
#define _GDCLASS_RECREATE(m_class, m_inherits) return nullptr;
#endif

// Use this on top of your own classes.
// Note: the trail of `***` is to keep sane diffs in PRs, because clang-format otherwise moves every `\` which makes
// every line of the macro different
#define GDCLASS(m_class, m_inherits) /***********************************************************************************************************************************************/ \
#define GDCLASS_NAME(m_class, m_inherits, m_class_name) /****************************************************************************************************************************/ \
private: \
void operator=(const m_class &p_rval) {} \
friend class ::godot::ClassDB; \
Expand Down Expand Up @@ -196,9 +195,9 @@ protected:
return (::godot::String(::godot::Wrapped::*)() const) & m_class::_to_string; \
} \
\
template <class T, class B> \
template <class GODOTCPP_T, class GODOTCPP_B> \
static void register_virtuals() { \
m_inherits::register_virtuals<T, B>(); \
m_inherits::register_virtuals<GODOTCPP_T, GODOTCPP_B>(); \
} \
\
public: \
Expand All @@ -218,7 +217,7 @@ public:
} \
\
static ::godot::StringName &get_class_static() { \
static ::godot::StringName string_name = ::godot::StringName(#m_class); \
static ::godot::StringName string_name = ::godot::StringName(m_class_name); \
return string_name; \
} \
\
Expand Down Expand Up @@ -369,6 +368,32 @@ public:
\
private:

// Use this on top of your own classes.
#define GDCLASS(m_class, m_inherits) GDCLASS_NAME(m_class, m_inherits, #m_class)

/* Use this to create templated class
You need to initialize `static const char *_template_class_name` with custom name
outside of class for every template instatiation you use

Example:

// in .h:
template<class T>
class ExampleTemplated : public Object {
GDCLASS_TEMPLATED(ExampleTemplated, Object);
};

// in .cpp:
template<>
const char * ExampleTemplated<int>::_template_class_name = "ExampleTemplatedInt";
template<>
const char * ExampleTemplated<float>::_template_class_name = "ExampleTemplatedFloat";
*/
#define GDCLASS_TEMPLATE(m_class, m_inherits) /*************************/ \
private: \
static const char *_template_class_name; \
GDCLASS_NAME(m_class, m_inherits, _template_class_name)

// Don't use this for your classes, use GDCLASS() instead.
#define GDEXTENSION_CLASS_ALIAS(m_class, m_alias_for, m_inherits) /******************************************************************************************************************/ \
private: \
Expand Down
4 changes: 4 additions & 0 deletions test/project/main.gd
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,10 @@ func _ready():
assert_equal(new_example_ref.was_post_initialized(), true)
assert_equal(example.test_post_initialize(), true)

# Check templated classes
var i = ExampleTemplatedInt.new()
var f = ExampleTemplatedFloat.new()
assert_equal(i.get_number() == 42 and is_equal_approx(f.get_number(), 42), true)
exit_with_status()

func _on_Example_custom_signal(signal_name, value):
Expand Down
18 changes: 18 additions & 0 deletions test/src/example.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -626,3 +626,21 @@ void Example::_input(const Ref<InputEvent> &event) {
emit_custom_signal(String("_input: ") + key_event->get_key_label(), key_event->get_unicode());
}
}

template <>
const char *ExampleTemplated<int>::_template_class_name = "ExampleTemplatedInt";
template class ExampleTemplated<int>;

template <>
const char *ExampleTemplated<float>::_template_class_name = "ExampleTemplatedFloat";
template class ExampleTemplated<float>;

template <class T>
void ExampleTemplated<T>::_bind_methods() {
ClassDB::bind_method(D_METHOD("get_number"), &ExampleTemplated::get_number);
}

template <class T>
T ExampleTemplated<T>::get_number() {
return number;
}
12 changes: 12 additions & 0 deletions test/src/example.h
Original file line number Diff line number Diff line change
Expand Up @@ -205,4 +205,16 @@ class ExampleAbstract : public Object {
static void _bind_methods() {}
};

template <class T>
class ExampleTemplated : public Object {
GDCLASS_TEMPLATE(ExampleTemplated, Object);

T number = (T)42;

protected:
static void _bind_methods();

T get_number();
};

#endif // EXAMPLE_CLASS_H
3 changes: 3 additions & 0 deletions test/src/register_types.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ void initialize_example_module(ModuleInitializationLevel p_level) {
ClassDB::register_class<Example>();
ClassDB::register_class<ExampleVirtual>(true);
ClassDB::register_abstract_class<ExampleAbstract>();

ClassDB::register_class<ExampleTemplated<int>>();
ClassDB::register_class<ExampleTemplated<float>>();
}

void uninitialize_example_module(ModuleInitializationLevel p_level) {
Expand Down