From 6887ff83be428d567909baa923b71d9183044c05 Mon Sep 17 00:00:00 2001 From: burak-yildizoz <45322678+burak-yildizoz@users.noreply.github.com> Date: Fri, 29 Nov 2024 09:08:45 +0300 Subject: [PATCH] Create as_member_func in utility.h --- src/rttr/rttr.cmake | 1 + src/rttr/utility.h | 66 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 67 insertions(+) create mode 100644 src/rttr/utility.h diff --git a/src/rttr/rttr.cmake b/src/rttr/rttr.cmake index 0617f884..e2106ea2 100644 --- a/src/rttr/rttr.cmake +++ b/src/rttr/rttr.cmake @@ -51,6 +51,7 @@ set(HEADER_FILES access_levels.h type type.h type_list.h + utility.h variant.h variant_associative_view.h variant_sequential_view.h diff --git a/src/rttr/utility.h b/src/rttr/utility.h new file mode 100644 index 00000000..c6815893 --- /dev/null +++ b/src/rttr/utility.h @@ -0,0 +1,66 @@ +#ifndef RTTR_UTILITY_H_ +#define RTTR_UTILITY_H_ + +namespace rttr +{ + +/** +Register property getter with lambda function. + +@code +int get_my_int(const MyClass& my_class); + +rttr::registration::class_("MyClass") + .property_readonly("my_int" + , rttr::as_member_func(+[](const MyClass* self) { return get_my_int(*self); }) + ) +; +@endcode +*/ +template +auto as_member_func(Return(*func)(const Class*, Args...)) +{ + union { + Return(Class::*member_func)(Args...) const; + void* func_ptr; + } convert; + convert.func_ptr = func; + return convert.member_func; +} + +/** +Register property setter with lambda function. + +@code +class MyClass { +public: + explicit MyClass(int my_int); + + int& get(); + +private: + int my_int; +}; + +rttr::registration::class_("MyClass") + .property("my_int" + , &MyClass::get, + , rttr::as_member_func(+[](MyClass* self, int my_int) { self->get() = my_int; }) + ) +; +@endcode +*/ +template +auto as_member_func(Return(*func)(Class*, Args...)) +{ + union { + Return(Class::*member_func)(Args...); + void* func_ptr; + } convert; + convert.func_ptr = func; + return convert.member_func; +} + +} // end namespace rttr + +#endif // RTTR_UTILITY_H_