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

Add automatic generation of getters and setters to GDCLASS and a helper macro for using them #1401

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
15 changes: 15 additions & 0 deletions include/godot_cpp/classes/wrapped.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,15 @@ struct EngineClassRegistration {
}
};

template <typename>
struct MemberPointerTraits;

template <typename Result, typename Object>
struct MemberPointerTraits<Result (Object::*)> {
using result_type = Result;
using object_type = Object;
};

} // namespace internal

} // namespace godot
Expand Down Expand Up @@ -369,6 +378,12 @@ public:
_gde_binding_reference_callback, \
}; \
\
template <auto T> \
typename internal::MemberPointerTraits<decltype(T)>::result_type _get_data_member() { return (this->*T); } \
\
template <auto T> \
void _set_data_member(typename internal::MemberPointerTraits<decltype(T)>::result_type t) { (this->*T) = t; } \
\
private:

// Don't use this for your classes, use GDCLASS() instead.
Expand Down
8 changes: 8 additions & 0 deletions include/godot_cpp/core/object.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,14 @@
#define ADD_GROUP(m_name, m_prefix) godot::ClassDB::add_property_group(get_class_static(), m_name, m_prefix)
#define ADD_SUBGROUP(m_name, m_prefix) godot::ClassDB::add_property_subgroup(get_class_static(), m_name, m_prefix)
#define ADD_PROPERTY(m_property, m_setter, m_getter) godot::ClassDB::add_property(get_class_static(), m_property, m_setter, m_getter)
#define AUTO_ADD_PROPERTY(p_property, p_method) \
{ \
auto get = &godot::internal::MemberPointerTraits<decltype(p_method)>::object_type::_get_data_member<p_method>; \
auto set = &godot::internal::MemberPointerTraits<decltype(p_method)>::object_type::_set_data_member<p_method>; \
ClassDB::bind_method(D_METHOD(StringName("get") + StringName(&#p_method[1])), get); \
ClassDB::bind_method(D_METHOD(StringName("set") + StringName(&#p_method[1])), set); \
ClassDB::add_property(get_class_static(), p_property, StringName("set") + StringName(&#p_method[1]), StringName("get") + StringName(&#p_method[1])); \
}
#define ADD_PROPERTYI(m_property, m_setter, m_getter, m_index) godot::ClassDB::add_property(get_class_static(), m_property, m_setter, m_getter, m_index)

namespace godot {
Expand Down