From 8f007c63318cf2bc25b4d050fe20037ee16f3e2c Mon Sep 17 00:00:00 2001 From: PhobicHD Date: Sun, 3 Mar 2024 18:37:38 -0700 Subject: [PATCH 1/2] Add auto generated getters and setters to GDCLASS and add macro to object.hpp to auto add properties to classes --- include/godot_cpp/classes/wrapped.hpp | 15 +++++++++++++++ include/godot_cpp/core/object.hpp | 8 ++++++++ 2 files changed, 23 insertions(+) diff --git a/include/godot_cpp/classes/wrapped.hpp b/include/godot_cpp/classes/wrapped.hpp index 25a93dfc7..12040509c 100644 --- a/include/godot_cpp/classes/wrapped.hpp +++ b/include/godot_cpp/classes/wrapped.hpp @@ -149,6 +149,15 @@ struct EngineClassRegistration { } }; +template +struct MemberPointerTraits; + +template +struct MemberPointerTraits { + using result_type = Result; + using object_type = Object; +}; + } // namespace internal } // namespace godot @@ -369,6 +378,12 @@ public: _gde_binding_reference_callback, \ }; \ \ + template \ + typename internal::MemberPointerTraits::result_type _get_data_member() { return (this->*T); } \ + \ + template \ + void _set_data_member(typename internal::MemberPointerTraits::result_type t) { (this->*T) = t; } \ + \ private: // Don't use this for your classes, use GDCLASS() instead. diff --git a/include/godot_cpp/core/object.hpp b/include/godot_cpp/core/object.hpp index c1f106922..3670bd4bd 100644 --- a/include/godot_cpp/core/object.hpp +++ b/include/godot_cpp/core/object.hpp @@ -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::detail::member_traits::object_type::_get_data_member; \ + auto set = &godot::detail::member_traits::object_type::_set_data_member; \ + ClassDB::bind_method(D_METHOD(&#p_method[1]), get); \ + ClassDB::bind_method(D_METHOD(&#p_method[1]), set); \ + ClassDB::add_property(get_class_static(), p_property, &#p_method[1], &#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 { From 3c7f44435330119e7506f23a7711014f1753d6c3 Mon Sep 17 00:00:00 2001 From: PhobicHD Date: Sun, 3 Mar 2024 18:58:27 -0700 Subject: [PATCH 2/2] Fix naming --- include/godot_cpp/core/object.hpp | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/include/godot_cpp/core/object.hpp b/include/godot_cpp/core/object.hpp index 3670bd4bd..0780364ab 100644 --- a/include/godot_cpp/core/object.hpp +++ b/include/godot_cpp/core/object.hpp @@ -51,13 +51,13 @@ #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::detail::member_traits::object_type::_get_data_member; \ - auto set = &godot::detail::member_traits::object_type::_set_data_member; \ - ClassDB::bind_method(D_METHOD(&#p_method[1]), get); \ - ClassDB::bind_method(D_METHOD(&#p_method[1]), set); \ - ClassDB::add_property(get_class_static(), p_property, &#p_method[1], &#p_method[1]); \ +#define AUTO_ADD_PROPERTY(p_property, p_method) \ + { \ + auto get = &godot::internal::MemberPointerTraits::object_type::_get_data_member; \ + auto set = &godot::internal::MemberPointerTraits::object_type::_set_data_member; \ + 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)