#ifndef RFL_FIND_INDEX_HPP_ #define RFL_FIND_INDEX_HPP_ #include "../Tuple.hpp" #include "StringLiteral.hpp" namespace rfl { namespace internal { template struct FieldWrapper { constexpr static int i_ = _i; }; template constexpr auto operator|(const FieldWrapper& _f1, const FieldWrapper& _f2) { if constexpr (F1::name_ == _field_name) { return _f1; } else { return _f2; } } template constexpr auto find_matching_field(const Head& _head, const Tail&... _tail) { return (_head | ... | _tail); }; template constexpr auto wrap_fields(std::integer_sequence) { return find_matching_field(FieldWrapper, _field_name, _is>{}...) .i_; } /// Finds the index of the field signified by _field_name template constexpr static int find_index() { constexpr int ix = wrap_fields<_field_name, Fields>( std::make_integer_sequence>()); static_assert(rfl::tuple_element_t::name_ == _field_name, "No matching field found."); return ix; } /// Finds the index of the field signified by _field_name or -1. template constexpr static int find_index_or_minus_one() { if constexpr (rfl::tuple_size_v == 0) { return -1; } else { constexpr int ix = wrap_fields<_field_name, Fields>( std::make_integer_sequence>()); if constexpr (rfl::tuple_element_t::name_ == _field_name) { return ix; } else { return -1; } } } } // namespace internal } // namespace rfl #endif