Skip to content

Commit 616fc07

Browse files
Only check for duplicate field names when absolutely necessary
1 parent 94633b5 commit 616fc07

4 files changed

Lines changed: 41 additions & 32 deletions

File tree

include/rfl/Literal.hpp

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -413,9 +413,6 @@ class Literal {
413413
static_assert(sizeof...(fields_) <= std::numeric_limits<ValueType>::max(),
414414
"Too many fields.");
415415

416-
static_assert(sizeof...(fields_) <= 1 || !has_duplicates(),
417-
"Duplicate strings are not allowed in a Literal.");
418-
419416
private:
420417
/// The underlying value.
421418
ValueType value_;

include/rfl/internal/to_ptr_field_tuple.hpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,22 @@
1212
#include "to_ptr_field.hpp"
1313
#include "wrap_in_fields.hpp"
1414

15-
namespace rfl {
16-
namespace internal {
15+
namespace rfl::internal {
1716

1817
template <class T>
1918
auto to_ptr_field_tuple(T& _t) {
2019
if constexpr (std::is_pointer_v<std::remove_cvref_t<T>>) {
2120
return to_ptr_field_tuple(*_t);
21+
2222
} else if constexpr (is_named_tuple_v<T>) {
2323
return nt_to_ptr_named_tuple(_t).fields();
24+
2425
} else if constexpr (has_fields<T>()) {
2526
return bind_to_tuple(_t, [](auto* _ptr) { return to_ptr_field(*_ptr); });
27+
2628
} else if constexpr (is_empty<T>()) {
2729
return rfl::Tuple();
30+
2831
} else {
2932
using FieldNames = field_names_t<T>;
3033
auto tup =
@@ -33,7 +36,6 @@ auto to_ptr_field_tuple(T& _t) {
3336
}
3437
}
3538

36-
} // namespace internal
37-
} // namespace rfl
39+
} // namespace rfl::internal
3840

3941
#endif

include/rfl/internal/to_ptr_named_tuple.hpp

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
#ifndef RFL_INTERNAL_TO_PTR_NAMED_TUPLE_HPP_
22
#define RFL_INTERNAL_TO_PTR_NAMED_TUPLE_HPP_
33

4-
54
#include "../Tuple.hpp"
65
#include "../always_false.hpp"
76
#include "../field_names_t.hpp"
@@ -12,11 +11,12 @@
1211
#include "is_empty.hpp"
1312
#include "is_field.hpp"
1413
#include "is_named_tuple.hpp"
14+
#include "no_duplicate_field_names.hpp"
1515
#include "to_flattened_ptr_tuple.hpp"
1616
#include "to_ptr_field_tuple.hpp"
17+
#include "to_ptr_tuple.hpp"
1718

18-
namespace rfl {
19-
namespace internal {
19+
namespace rfl::internal {
2020

2121
template <class PtrFieldTuple>
2222
auto flatten_ptr_field_tuple(PtrFieldTuple& _t) {
@@ -34,8 +34,7 @@ auto flatten_ptr_field_tuple(PtrFieldTuple& _t) {
3434

3535
return [&]<int... _is>(std::integer_sequence<int, _is...>) {
3636
return rfl::tuple_cat(get_one(std::integral_constant<int, _is>{})...);
37-
}
38-
(std::make_integer_sequence<int, size>());
37+
}(std::make_integer_sequence<int, size>());
3938
}
4039

4140
template <class PtrFieldTuple>
@@ -48,7 +47,12 @@ auto field_tuple_to_named_tuple(PtrFieldTuple& _ptr_field_tuple) {
4847
return rfl::apply(ft_to_nt, std::move(_ptr_field_tuple));
4948
} else {
5049
const auto flattened_tuple = flatten_ptr_field_tuple(_ptr_field_tuple);
51-
return rfl::apply(ft_to_nt, flattened_tuple);
50+
auto named_tuple = rfl::apply(ft_to_nt, flattened_tuple);
51+
static_assert(
52+
no_duplicate_field_names<
53+
typename std::remove_cvref_t<decltype(named_tuple)>::Fields>(),
54+
"Flattening created duplicate field names.");
55+
return named_tuple;
5256
}
5357
}
5458

@@ -59,22 +63,40 @@ auto to_ptr_named_tuple(T&& _t) {
5963
if constexpr (has_fields<std::remove_cvref_t<T>>()) {
6064
if constexpr (std::is_pointer_v<std::remove_cvref_t<T>>) {
6165
return to_ptr_named_tuple(*_t);
66+
6267
} else if constexpr (is_named_tuple_v<std::remove_cvref_t<T>>) {
6368
return nt_to_ptr_named_tuple(_t);
69+
6470
} else {
6571
auto ptr_field_tuple = to_ptr_field_tuple(_t);
6672
return field_tuple_to_named_tuple(ptr_field_tuple);
6773
}
74+
6875
} else if constexpr (is_empty<T>()) {
6976
return rfl::NamedTuple<>();
77+
7078
} else {
71-
using FieldNames = rfl::field_names_t<T>;
72-
auto flattened_ptr_tuple = to_flattened_ptr_tuple(_t);
73-
return copy_flattened_tuple_to_named_tuple<FieldNames>(flattened_ptr_tuple);
79+
auto ptr_tuple = to_ptr_tuple(_t);
80+
81+
using PtrTuple = std::remove_cvref_t<decltype(ptr_tuple)>;
82+
83+
if constexpr (!has_flatten_fields<PtrTuple>()) {
84+
return copy_flattened_tuple_to_named_tuple<field_names_t<T>>(ptr_tuple);
85+
86+
} else {
87+
using FieldNames = rfl::field_names_t<T>;
88+
auto flattened_ptr_tuple = to_flattened_ptr_tuple(_t);
89+
auto named_tuple =
90+
copy_flattened_tuple_to_named_tuple<FieldNames>(flattened_ptr_tuple);
91+
static_assert(
92+
no_duplicate_field_names<
93+
typename std::remove_cvref_t<decltype(named_tuple)>::Fields>(),
94+
"Flattening created duplicate field names.");
95+
return named_tuple;
96+
}
7497
}
7598
}
7699

77-
} // namespace internal
78-
} // namespace rfl
100+
} // namespace rfl::internal
79101

80102
#endif

include/rfl/parsing/NamedTupleParser.hpp

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,12 @@
99

1010
#include "../NamedTuple.hpp"
1111
#include "../Result.hpp"
12-
#include "../always_false.hpp"
1312
#include "../internal/default_if_missing_v.hpp"
1413
#include "../internal/has_default_val_v.hpp"
15-
#include "../internal/is_array.hpp"
1614
#include "../internal/is_attribute.hpp"
17-
#include "../internal/is_basic_type.hpp"
1815
#include "../internal/is_default_val_v.hpp"
1916
#include "../internal/is_extra_fields.hpp"
2017
#include "../internal/is_skip.hpp"
21-
#include "../internal/no_duplicate_field_names.hpp"
2218
#include "../internal/nth_element_t.hpp"
2319
#include "../internal/ptr_cast.hpp"
2420
#include "../to_view.hpp"
@@ -35,8 +31,7 @@
3531
#include "schema/Type.hpp"
3632
#include "to_single_error_message.hpp"
3733

38-
namespace rfl {
39-
namespace parsing {
34+
namespace rfl::parsing {
4035

4136
template <class R, class W, bool _ignore_empty_containers, bool _all_required,
4237
bool _no_field_names, class ProcessorsType, class... FieldTypes>
@@ -85,8 +80,6 @@ struct NamedTupleParser {
8580
*/
8681
static Result<NamedTuple<FieldTypes...>> read(
8782
const R& _r, const InputVarType& _var) noexcept {
88-
static_assert(
89-
internal::no_duplicate_field_names<typename NamedTupleType::Fields>());
9083
alignas(NamedTuple<FieldTypes...>) unsigned char
9184
buf[sizeof(NamedTuple<FieldTypes...>)];
9285
auto ptr = internal::ptr_cast<NamedTuple<FieldTypes...>*>(&buf);
@@ -116,8 +109,6 @@ struct NamedTupleParser {
116109
std::optional<Error>>
117110
read_view(const R& _r, const InputVarType& _var,
118111
NamedTuple<FieldTypes...>* _view) noexcept {
119-
static_assert(
120-
internal::no_duplicate_field_names<typename NamedTupleType::Fields>());
121112
if constexpr (_no_field_names) {
122113
auto arr = _r.to_array(_var);
123114
if (!arr) [[unlikely]] {
@@ -146,8 +137,6 @@ struct NamedTupleParser {
146137
static std::optional<Error> read_view_with_default(
147138
const R& _r, const InputVarType& _var,
148139
NamedTuple<FieldTypes...>* _view) noexcept {
149-
static_assert(
150-
internal::no_duplicate_field_names<typename NamedTupleType::Fields>());
151140
if constexpr (_no_field_names) {
152141
auto arr = _r.to_array(_var);
153142
if (!arr) [[unlikely]] {
@@ -390,7 +379,6 @@ struct NamedTupleParser {
390379
}
391380
};
392381

393-
} // namespace parsing
394-
} // namespace rfl
382+
} // namespace rfl::parsing
395383

396384
#endif

0 commit comments

Comments
 (0)