Skip to content
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
74 changes: 39 additions & 35 deletions include/rfl/NamedTuple.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
#define RFL_NAMEDTUPLE_HPP_

#include <algorithm>
#include <array>
#include <cstddef>
#include <string_view>
#include <type_traits>
#include <utility>
Expand All @@ -20,6 +22,32 @@

namespace rfl {

namespace internal {

/// Finds the position of rfl::ExtraFields in a field pack, returning -1 if none
/// is present or -2 if more than one is present.
template <class... FieldTypes>
consteval int find_extra_fields() noexcept {
if constexpr (sizeof...(FieldTypes) == 0) {
return -1;
} else {
constexpr std::array<bool, sizeof...(FieldTypes)> flags{
is_extra_fields_v<typename std::remove_cvref_t<FieldTypes>::Type>...};
int idx = -1;
for (std::size_t i = 0; i < flags.size(); ++i) {
if (flags[i]) {
if (idx != -1) {
return -2;
}
idx = static_cast<int>(i);
}
}
return idx;
}
}

} // namespace internal

/// A named tuple behaves like a tuple,
/// but the fields have explicit names, which
/// allows for reflection.
Expand Down Expand Up @@ -547,29 +575,6 @@ class NamedTuple {
}
}

/// @brief Finds the position of the extra fields, or -1 if there aren't any.
/// @tparam _i Current index.
/// @tparam _idx Index of ExtraFields found so far.
/// @return Position of ExtraFields or -1.
template <int _i = 0, int _idx = -1>
constexpr static int find_extra_fields() {
if constexpr (_i == size()) {
return _idx;
} else {
using FieldType = internal::nth_element_t<_i, FieldTypes...>;
constexpr bool is_extra_fields =
internal::is_extra_fields_v<typename FieldType::Type>;
static_assert(_idx == -1 || !is_extra_fields,
"There can only be one rfl::ExtraFields in any struct or "
"named tuple.");
if constexpr (is_extra_fields) {
return find_extra_fields<_i + 1, _i>();
} else {
return find_extra_fields<_i + 1, _idx>();
}
}
}

/// @brief Generates the fields for the NamedTuple (rvalue overload).
/// @tparam _is Indices of the fields.
/// @tparam AdditionalArgs Additional arguments to append.
Expand All @@ -578,12 +583,9 @@ class NamedTuple {
template <int... _is, class... AdditionalArgs>
auto make_fields(std::integer_sequence<int, _is...>,
AdditionalArgs&&... _args) && {
const auto wrap = [this]<int _i>(Index<_i>) {
using FieldType = internal::nth_element_t<_i, FieldTypes...>;
return FieldType(std::move(rfl::get<_i>(values_)));
};
return rfl::make_tuple(wrap(Index<_is>{})...,
std::forward<AdditionalArgs>(_args)...);
return rfl::make_tuple(
std::remove_cvref_t<FieldTypes>(std::move(rfl::get<_is>(values_)))...,
std::forward<AdditionalArgs>(_args)...);
}

/// @brief Generates the fields for the NamedTuple (const lvalue overload).
Expand All @@ -594,11 +596,8 @@ class NamedTuple {
template <int... _is, class... AdditionalArgs>
auto make_fields(std::integer_sequence<int, _is...>,
AdditionalArgs... _args) const& {
const auto wrap = [this]<int _i>(Index<_i>) {
using FieldType = internal::nth_element_t<_i, FieldTypes...>;
return FieldType(rfl::get<_i>(values_));
};
return rfl::make_tuple(wrap(Index<_is>{})..., _args...);
return rfl::make_tuple(
std::remove_cvref_t<FieldTypes>(rfl::get<_is>(values_))..., _args...);
}

/// @brief Generates a new NamedTuple with one value replaced with a new
Expand Down Expand Up @@ -702,7 +701,12 @@ class NamedTuple {
Values values_;

/// @brief The position of rfl::ExtraFields, or -1 if there aren't any.
constexpr static int pos_extra_fields_ = find_extra_fields();
constexpr static int pos_extra_fields_ =
internal::find_extra_fields<FieldTypes...>();

static_assert(pos_extra_fields_ != -2,
"There can only be one rfl::ExtraFields in any struct or "
"named tuple.");
};

// ----------------------------------------------------------------------------
Expand Down
76 changes: 35 additions & 41 deletions include/rfl/internal/find_index.hpp
Original file line number Diff line number Diff line change
@@ -1,63 +1,57 @@
#ifndef RFL_FIND_INDEX_HPP_
#define RFL_FIND_INDEX_HPP_

#include <array>
#include <cstddef>
#include <string_view>
#include <tuple>
#include <type_traits>

#include "../Tuple.hpp"
#include "StringLiteral.hpp"

namespace rfl {
namespace internal {

template <class FieldType, StringLiteral _field_name, int _i>
struct FieldWrapper {
constexpr static int i_ = _i;
};
template <class Fields>
struct FindIndexImpl;

template <StringLiteral _field_name, class F1, int _i1, class F2, int _i2>
constexpr auto operator|(const FieldWrapper<F1, _field_name, _i1>& _f1,
const FieldWrapper<F2, _field_name, _i2>& _f2) {
if constexpr (F1::name_ == _field_name) {
return _f1;
} else {
return _f2;
template <class... FieldTypes>
struct FindIndexImpl<rfl::Tuple<FieldTypes...>> {
static consteval int find(const std::string_view _target) noexcept {
if constexpr (sizeof...(FieldTypes) == 0) {
return -1;
} else {
constexpr std::array<std::string_view, sizeof...(FieldTypes)> names{
std::remove_cvref_t<FieldTypes>::name_.string_view()...};
for (std::size_t i = 0; i < names.size(); ++i) {
if (names[i] == _target) {
return static_cast<int>(i);
}
}
return -1;
}
}
}

template <class Head, class... Tail>
constexpr auto find_matching_field(const Head& _head, const Tail&... _tail) {
return (_head | ... | _tail);
};

template <StringLiteral _field_name, class Fields, int... _is>
constexpr auto wrap_fields(std::integer_sequence<int, _is...>) {
return find_matching_field(FieldWrapper<rfl::tuple_element_t<_is, Fields>,
_field_name, _is>{}...)
.i_;
}
template <class... FieldTypes>
struct FindIndexImpl<std::tuple<FieldTypes...>>
: FindIndexImpl<rfl::Tuple<FieldTypes...>> {};

/// Finds the index of the field signified by _field_name
/// Finds the index of the field signified by _field_name or -1.
template <StringLiteral _field_name, class Fields>
constexpr int find_index() {
constexpr int ix = wrap_fields<_field_name, Fields>(
std::make_integer_sequence<int, rfl::tuple_size_v<Fields>>());
static_assert(rfl::tuple_element_t<ix, Fields>::name_ == _field_name,
"No matching field found.");
constexpr int find_index_or_minus_one() {
constexpr int ix = FindIndexImpl<std::remove_cvref_t<Fields>>::find(
_field_name.string_view());
return ix;
}

/// Finds the index of the field signified by _field_name or -1.
/// Finds the index of the field signified by _field_name
template <StringLiteral _field_name, class Fields>
constexpr int find_index_or_minus_one() {
if constexpr (rfl::tuple_size_v<Fields> == 0) {
return -1;
} else {
constexpr int ix = wrap_fields<_field_name, Fields>(
std::make_integer_sequence<int, rfl::tuple_size_v<Fields>>());
if constexpr (rfl::tuple_element_t<ix, Fields>::name_ == _field_name) {
return ix;
} else {
return -1;
}
}
constexpr int find_index() {
constexpr int ix = find_index_or_minus_one<_field_name, Fields>();
static_assert(ix != -1, "No matching field found.");
return ix;
}

} // namespace internal
Expand Down
26 changes: 15 additions & 11 deletions include/rfl/internal/has_arrays.hpp
Original file line number Diff line number Diff line change
@@ -1,26 +1,30 @@
#ifndef RFL_INTERNAL_HASARRAYS_HPP_
#define RFL_INTERNAL_HASARRAYS_HPP_

#include <tuple>
#include <type_traits>
#include <utility>

#include "../Tuple.hpp"
#include "is_array.hpp"

namespace rfl::internal {

template <class TupleType>
struct HasArrays;

template <class... Ts>
struct HasArrays<rfl::Tuple<Ts...>>
: std::bool_constant<(false || ... ||
is_array_v<std::remove_cvref_t<Ts>>)> {};

template <class... Ts>
struct HasArrays<std::tuple<Ts...>>
: std::bool_constant<(false || ... ||
is_array_v<std::remove_cvref_t<Ts>>)> {};

template <class TupleType>
constexpr bool has_arrays() {
const auto is_true_for_one =
[]<int _i>(std::integral_constant<int, _i>) -> bool {
using T = std::remove_cvref_t<tuple_element_t<_i, TupleType>>;
return is_array_v<T>;
};

return [&]<int... _is>(std::integer_sequence<int, _is...>) {
return (false || ... ||
is_true_for_one(std::integral_constant<int, _is>{}));
}(std::make_integer_sequence<int, rfl::tuple_size_v<TupleType>>());
return HasArrays<std::remove_cvref_t<TupleType>>::value;
}

} // namespace rfl::internal
Expand Down
Loading
Loading