mirror of
https://codeberg.org/DawnMagnet/reflect-cpp
synced 2026-12-06 18:56:19 +08:00
@@ -12,6 +12,7 @@
|
||||
#include "get.hpp"
|
||||
#include "internal/StringLiteral.hpp"
|
||||
#include "internal/find_index.hpp"
|
||||
#include "internal/is_extra_fields.hpp"
|
||||
#include "internal/no_duplicate_field_names.hpp"
|
||||
|
||||
namespace rfl {
|
||||
@@ -282,6 +283,22 @@ class NamedTuple {
|
||||
inline auto operator!=(const rfl::NamedTuple<FieldTypes...>& _other) const {
|
||||
return !(*this == _other);
|
||||
}
|
||||
|
||||
/// Returns the number of fields. Note that this is not necessary the same
|
||||
/// thing as .size(), because there might be rfl::ExtraFields, which are
|
||||
/// simply counted as one entry by .size(), but are counted by individually by
|
||||
/// .num_fields().
|
||||
size_t num_fields() const {
|
||||
if constexpr (pos_extra_fields() == -1) {
|
||||
return size();
|
||||
} else {
|
||||
return calc_num_fields<pos_extra_fields()>();
|
||||
}
|
||||
}
|
||||
|
||||
/// The position of the extra fields, or -1 if there aren't any.
|
||||
constexpr static int pos_extra_fields() { return pos_extra_fields_; }
|
||||
|
||||
/// Replaces one or several fields, returning a new version
|
||||
/// with the non-replaced fields left unchanged.
|
||||
template <internal::StringLiteral _name, class FType, class... OtherRFields>
|
||||
@@ -409,6 +426,38 @@ class NamedTuple {
|
||||
return std::apply(a, std::forward<std::tuple<TupContent...>>(_tuple));
|
||||
}
|
||||
|
||||
/// Unfortunately, MSVC forces us to do this...
|
||||
template <int _pos>
|
||||
size_t calc_num_fields() const {
|
||||
const auto& extra_fields = get<_pos>();
|
||||
if constexpr (std::is_pointer_v<
|
||||
std::remove_cvref_t<decltype(extra_fields)>>) {
|
||||
return size() + extra_fields->size() - 1;
|
||||
} else {
|
||||
return size() + extra_fields.size() - 1;
|
||||
}
|
||||
}
|
||||
|
||||
/// Finds the position of the extra fields, or -1 if there aren't any.
|
||||
template <int _i = 0, int _idx = -1>
|
||||
constexpr static int find_extra_fields() {
|
||||
if constexpr (_i == size()) {
|
||||
return _idx;
|
||||
} else {
|
||||
using FieldType = typename std::tuple_element_t<_i, Fields>;
|
||||
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>();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Generates the fields.
|
||||
template <int num_additional_fields = 0, class... Args>
|
||||
auto make_fields(Args&&... _args) {
|
||||
@@ -423,7 +472,7 @@ class NamedTuple {
|
||||
} else {
|
||||
// When we add additional fields, it is more intuitive to add
|
||||
// them to the end, that is why we do it like this.
|
||||
using FieldType = typename std::tuple_element<i, Fields>::type;
|
||||
using FieldType = typename std::tuple_element_t<i, Fields>;
|
||||
using T = std::remove_cvref_t<typename FieldType::Type>;
|
||||
return make_fields<num_additional_fields>(
|
||||
FieldType(std::forward<T>(std::get<i>(values_))),
|
||||
@@ -554,6 +603,9 @@ class NamedTuple {
|
||||
/// everything else is resolved at compile time. It should have no
|
||||
/// runtime overhead over a normal std::tuple.
|
||||
Values values_;
|
||||
|
||||
/// The position of rfl::ExtraFields, or -1 if there aren't any.
|
||||
constexpr static int pos_extra_fields_ = find_extra_fields();
|
||||
};
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
@@ -611,6 +663,12 @@ class NamedTuple<> {
|
||||
/// Returns an empty tuple.
|
||||
auto fields() const { return std::tuple(); }
|
||||
|
||||
/// Must always be 0.
|
||||
size_t num_fields() const { return 0; }
|
||||
|
||||
/// Must always be -1.
|
||||
constexpr static int pos_extra_fields() { return -1; }
|
||||
|
||||
/// Must always be 0.
|
||||
static constexpr size_t size() { return 0; }
|
||||
|
||||
|
||||
Reference in New Issue
Block a user