Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[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
alexkaratarakis committed Dec 5, 2024
1 parent cc81a05 commit 621dfe0
Showing 3 changed files with 61 additions and 6 deletions.
22 changes: 17 additions & 5 deletions include/fixed_containers/reflection.hpp
Original file line number Diff line number Diff line change
@@ -211,11 +211,22 @@ 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 <std::size_t FIELD_COUNT, typename T>
using TuplesHelper = fixed_containers::tuples::customize::TuplesViewHelper<FIELD_COUNT, T>;

template <typename T>
struct ReflectionHelper
{
static 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>
@@ -225,14 +236,15 @@ 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::ReflectionHelper<std::decay_t<T>>::FIELD_NAMES
.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::ReflectionHelper<std::decay_t<T>>::FIELD_NAMES;
}

template <typename T, typename Func>
15 changes: 14 additions & 1 deletion include/fixed_containers/tuples.hpp
Original file line number Diff line number Diff line change
@@ -7,12 +7,25 @@
#include <tuple>
#include <utility>

namespace fixed_containers::tuples::customize
{
template <std::size_t FIELD_COUNT, typename T>
struct TuplesViewHelper
{
static 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 as_tuple_view_detail::as_tuple_view<FIELD_COUNT, T>(data);
return fixed_containers::tuples::customize::TuplesViewHelper<FIELD_COUNT, T>::as_tuple_view(
data);
}

template <typename Tuple, typename Func>
30 changes: 30 additions & 0 deletions test/reflection_test.cpp
Original file line number Diff line number Diff line change
@@ -605,4 +605,34 @@ 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 <>
struct fixed_containers::tuples::customize::TuplesViewHelper<3, MyCustomStruct>
{
static constexpr auto as_tuple_view(MyCustomStruct& data)
{
return std::tie(data.a, data.b, data.c);
}
};

template <>
struct fixed_containers::reflection::customize::ReflectionHelper<MyCustomStruct>
{
static constexpr auto FIELD_NAMES = 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 621dfe0

Please sign in to comment.