Skip to content

Commit

Permalink
[reflection] Add customization point for users to supply reflection info
Browse files Browse the repository at this point in the history
This is useful e.g. in really big structs that can have reflection
info provided by codegen
  • Loading branch information
alexkaratarakis committed Dec 10, 2024
1 parent 26ff0bf commit 36438b2
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 6 deletions.
15 changes: 10 additions & 5 deletions include/fixed_containers/reflection.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -211,11 +211,16 @@ constexpr auto field_names_of_impl(const T& instance)
return output;
}

template <typename T>
inline constexpr auto FIELD_NAMES =
field_names_of_impl<field_count_of_impl(std::decay_t<T>{})>(std::decay_t<T>{});
} // namespace fixed_containers::reflection_detail

namespace fixed_containers::reflection::customize
{
template <typename T>
inline constexpr auto FIELD_NAMES = fixed_containers::reflection_detail::field_names_of_impl<
fixed_containers::reflection_detail::field_count_of_impl(std::decay_t<T>{})>(std::decay_t<T>{});

} // namespace fixed_containers::reflection::customize

namespace fixed_containers::reflection
{
template <typename T>
Expand All @@ -225,14 +230,14 @@ template <typename T>
requires(Reflectable<std::decay_t<T>>)
constexpr std::size_t field_count_of()
{
return reflection_detail::FIELD_NAMES<std::decay_t<T>>.size();
return fixed_containers::reflection::customize::FIELD_NAMES<std::decay_t<T>>.size();
}

template <typename T>
requires(Reflectable<std::decay_t<T>>)
constexpr const auto& field_names_of()
{
return reflection_detail::FIELD_NAMES<std::decay_t<T>>;
return fixed_containers::reflection::customize::FIELD_NAMES<std::decay_t<T>>;
}

template <typename T, typename Func>
Expand Down
11 changes: 10 additions & 1 deletion include/fixed_containers/tuples.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,22 @@
#include <tuple>
#include <utility>

namespace fixed_containers::tuples
namespace fixed_containers::tuples::customize
{
template <std::size_t FIELD_COUNT, typename T>
constexpr auto as_tuple_view(T& data)
{
return as_tuple_view_detail::as_tuple_view<FIELD_COUNT, T>(data);
}
} // namespace fixed_containers::tuples::customize

namespace fixed_containers::tuples
{
template <std::size_t FIELD_COUNT, typename T>
constexpr auto as_tuple_view(T& data)
{
return fixed_containers::tuples::customize::as_tuple_view<FIELD_COUNT, T>(data);
}

template <typename Tuple, typename Func>
requires(std::tuple_size_v<std::decay_t<Tuple>> == 0)
Expand Down
25 changes: 25 additions & 0 deletions test/reflection_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -605,4 +605,29 @@ TEST(Reflection, MockFailingAddressOfOperator)

} // namespace fixed_containers

struct MyCustomStruct
{
int a{};
int b{};
int c{};
double d{}; // Customization will ignore this field to show the customization is applied
};

template <>
constexpr auto fixed_containers::tuples::customize::as_tuple_view<3, MyCustomStruct>(
MyCustomStruct& data)
{
return std::tie(data.a, data.b, data.c);
}

template <>
inline constexpr auto fixed_containers::reflection::customize::FIELD_NAMES<MyCustomStruct> =
make_fixed_vector<std::string_view>({
"a",
"b",
"c",
});

static_assert(fixed_containers::reflection::field_names_of<MyCustomStruct>().size() == 3);

#endif

0 comments on commit 36438b2

Please sign in to comment.