Add auto generated getters and setters to GDCLASS and add macro to object.hpp to auto add properties to classes

pull/1401/head
PhobicHD 2024-03-03 18:37:38 -07:00
parent e55b792fea
commit 8f007c6331
2 changed files with 23 additions and 0 deletions

View File

@ -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 internal
} // namespace godot } // namespace godot
@ -369,6 +378,12 @@ public:
_gde_binding_reference_callback, \ _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: private:
// Don't use this for your classes, use GDCLASS() instead. // Don't use this for your classes, use GDCLASS() instead.

View File

@ -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_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_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 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<decltype(p_method)>::object_type::_get_data_member<p_method>; \
auto set = &godot::detail::member_traits<decltype(p_method)>::object_type::_set_data_member<p_method>; \
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) #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 { namespace godot {