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

Create as_member_func in utility.h #379

Open
wants to merge 1 commit 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
1 change: 1 addition & 0 deletions src/rttr/rttr.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
66 changes: 66 additions & 0 deletions src/rttr/utility.h
Original file line number Diff line number Diff line change
@@ -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>("MyClass")
.property_readonly("my_int"
, rttr::as_member_func(+[](const MyClass* self) { return get_my_int(*self); })
)
;
@endcode
*/
template <typename Return, typename Class, typename... Args>
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>("MyClass")
.property("my_int"
, &MyClass::get,
, rttr::as_member_func(+[](MyClass* self, int my_int) { self->get() = my_int; })
)
;
@endcode
*/
template <typename Return, typename Class, typename... Args>
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_