Closed grandseiken closed 5 months ago
I think a helper function like this would be a good addition, but there are a couple of things I would do differently:
1) I would design it as a method called .apply(...)
on the NamedTuple.
https://github.com/getml/reflect-cpp/blob/main/include/rfl/NamedTuple.hpp
(rfl::view(...)
just returns a named tuple with pointers to the original fields.)
.apply(...)
should also have a const version.
2) I would design it in such a way that the function F expects an actual field. So in your example I would rewrite print_ints
as follows:
auto print_ints =
[](const auto& field) {
if constexpr(std::is_same_v<typename Field::Type, *int>) {
std::cout << field.name() << ": " << *field.value() << "\n";
}
};
// Use as follows:
rfl::view(foo{ 1, 2.0f, 3 }).apply(print_ints);
(field.name()
will have to be added as well. I already have an implementation, but it is in a feature branch. It works as follows:
struct Field {
...
static std::string name() { return Name().str(); }
}
https://github.com/getml/reflect-cpp/blob/main/include/rfl/Field.hpp
This works because Field::Name is a Literal and literals have this method:
https://github.com/getml/reflect-cpp/blob/main/include/rfl/Literal.hpp
If you want to do it (you've basically already implemented it), a PR is welcome. If not, I will just do it myself. It's not a lot of work.
@grandseiken , just merged your PR. Thank you so much!
The library claims to support reflective programming in general, but found that I had to do a bit more work to really make it convenient to iterate over fields of a struct with type information and field names, e.g.
Did I miss something that would have made this easier? Do you think helper functions like this would make sense to include in the library, or maybe in example documentation?
I'm also not sure if I missed a better way to get the name of an
rfl::Field
as a compile-time value, I had to use the internalname_
member above.