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
Usage help #40
Comments
Hello! I agree that the documentation can be improved. Here is how these operations would translate to refl-cpp. If more flexibility is needed, you can always map the compile-time member metadata to whatever runtime structure is useful for your usecase. // Get member descriptor by index
constexpr refl::trait::get_t<0, refl::member_list<A>> member_first{};
std::cout << "A.members[0]=" << member_first.name.str() << std::endl;
// Get member by name (name must be a constant expression)
constexpr auto x_member = find_one(refl::member_list<A>{}, [](auto m) { return m.name == "x"; });
std::cout << "x_member.name=" << x_member.name.str() << std::endl;
// Get index of member
constexpr auto index_of_x = refl::trait::index_of_v<refl::trait::remove_qualifiers_t<decltype(x_member)>, refl::member_list<A>>;
std::cout << "index_of_x=" << index_of_x << std::endl; You could cast the member pointer constexpr auto members = refl::util::map_to_array<MyMemberInfo>(refl::member_list<A>{}, [](auto m) {
return MyMemberInfo(m);
}); The following resources might be helpful: You can also refer to the Introduction.md. Also take a look at the examples at the bottom of that page. |
I have gotten your code to work but still not very clear how i can use it in code especially in non constexpr code. Interestingly, I reached this code when searching for code capable of constexpr compile time operation. Unfortunately, it is a bit too complex for quick comprehension. I have attached a sample code of what I would like to achieve. It would be much appreciated if you can convert the code to real working code.
|
by using the idex as a template argument for get_t, It is not possible to get the member by a variable index. Is it possible to do something like |
No, it is not. constexpr auto members = refl::util::map_to_array<MyMemberInfo>(refl::member_list<A>{}, [](auto m) {
return MyMemberInfo(m);
}); Example struct MyMemberInfo {
std::string name;
template <typename Member>
MyMemberInfo(Member m)
: name(m.name.str())
{
}
}; Then you would be able to access any property of interest like so |
Could you expand on this? How can a member pointer be converted to offset? Did you mean; cast to size_t and then subtract the value of the pointer to the object that contains the member? |
See the possible implementations in the abstract |
Closing due to inactivity. |
I just came across this library and I must admit that it's bit too complex for an average user. Nevertheless, i appreciate the immense amount of work into it.
I have a particular requirement that brought me here and i would like to know how to achieve them if that is possible.
memcpy(buffer, &myStructAsByteStream[offsetof(mystruct, member)], bytesToSend)
2)Is there a way to get the index of a member of the struct?
3) Is there a way to get the member from index? This is the inverse of the previous.
The text was updated successfully, but these errors were encountered: