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

Reflection customization #175

Open
wants to merge 3 commits into
base: main
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
36 changes: 26 additions & 10 deletions include/fixed_containers/reflection.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -211,11 +211,30 @@ 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>{});

template <typename T>
struct ReflectionHelper
{
template <typename T2, typename Func>
static constexpr void for_each_field(T2&& instance, Func&& func)
{
auto tuple_view = tuples::as_tuple_view<FIELD_NAMES<std::decay_t<T2>>.size()>(instance);
tuples::for_each_entry(
tuple_view,
[&func]<typename Field>(std::size_t index, Field&& field)
{ func(FIELD_NAMES<std::decay_t<T2>>.at(index), std::forward<Field>(field)); });
}
};

} // namespace fixed_containers::reflection::customize

namespace fixed_containers::reflection
{
template <typename T>
Expand All @@ -225,25 +244,22 @@ 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>
requires(Reflectable<std::decay_t<T>>)
constexpr void for_each_field(T&& instance, Func&& func)
{
constexpr const auto& FIELD_NAMES = field_names_of<T>();
auto tuple_view = tuples::as_tuple_view<FIELD_NAMES.size()>(instance);
tuples::for_each_entry(tuple_view,
[&func]<typename Field>(std::size_t index, Field&& field)
{ func(FIELD_NAMES.at(index), std::forward<Field>(field)); });
fixed_containers::reflection::customize::ReflectionHelper<std::decay_t<T>>::for_each_field(
std::forward<T>(instance), std::forward<Func>(func));
alexkaratarakis marked this conversation as resolved.
Show resolved Hide resolved
}

} // namespace fixed_containers::reflection
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
Loading
Loading