mirror of
https://codeberg.org/DawnMagnet/reflect-cpp
synced 2026-12-03 00:26:19 +08:00
Use fold expressions inside the literals (#434)
This commit is contained in:
+15
-30
@@ -13,6 +13,7 @@
|
|||||||
#include "Result.hpp"
|
#include "Result.hpp"
|
||||||
#include "Tuple.hpp"
|
#include "Tuple.hpp"
|
||||||
#include "internal/StringLiteral.hpp"
|
#include "internal/StringLiteral.hpp"
|
||||||
|
#include "internal/find_index.hpp"
|
||||||
#include "internal/no_duplicate_field_names.hpp"
|
#include "internal/no_duplicate_field_names.hpp"
|
||||||
|
|
||||||
namespace rfl {
|
namespace rfl {
|
||||||
@@ -95,31 +96,24 @@ class Literal {
|
|||||||
|
|
||||||
/// Determines whether the literal contains any of the strings in the other
|
/// Determines whether the literal contains any of the strings in the other
|
||||||
/// literal at compile time.
|
/// literal at compile time.
|
||||||
template <class OtherLiteralType, int _i = 0>
|
template <class OtherLiteralType>
|
||||||
static constexpr bool contains_any() {
|
static constexpr bool contains_any() {
|
||||||
if constexpr (_i == num_fields_) {
|
return []<int... _is>(const std::integer_sequence<int, _is...>&) {
|
||||||
return false;
|
return (false || ... ||
|
||||||
} else {
|
OtherLiteralType::template contains<
|
||||||
constexpr auto name = find_name_within_own_fields<_i>();
|
find_name_within_own_fields<_is>()>());
|
||||||
return OtherLiteralType::template contains<name>() ||
|
}(std::make_integer_sequence<int, num_fields_>());
|
||||||
contains_any<OtherLiteralType, _i + 1>();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Determines whether the literal contains all of the strings in the other
|
/// Determines whether the literal contains all of the strings in the other
|
||||||
/// literal at compile time.
|
/// literal at compile time.
|
||||||
template <class OtherLiteralType, int _i = 0, int _n_found = 0>
|
template <class OtherLiteralType>
|
||||||
static constexpr bool contains_all() {
|
static constexpr bool contains_all() {
|
||||||
if constexpr (_i == num_fields_) {
|
return []<int... _is>(const std::integer_sequence<int, _is...>&) {
|
||||||
return _n_found == OtherLiteralType::num_fields_;
|
return (true && ... &&
|
||||||
} else {
|
OtherLiteralType::template contains<
|
||||||
constexpr auto name = find_name_within_own_fields<_i>();
|
find_name_within_own_fields<_is>()>());
|
||||||
if constexpr (OtherLiteralType::template contains<name>()) {
|
}(std::make_integer_sequence<int, num_fields_>());
|
||||||
return contains_all<OtherLiteralType, _i + 1, _n_found + 1>();
|
|
||||||
} else {
|
|
||||||
return contains_all<OtherLiteralType, _i + 1, _n_found>();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Determines whether the literal has duplicate strings at compile time.
|
/// Determines whether the literal has duplicate strings at compile time.
|
||||||
@@ -332,18 +326,9 @@ class Literal {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Finds the value of a string literal at compile time.
|
/// Finds the value of a string literal at compile time.
|
||||||
template <internal::StringLiteral _name, int _i = 0>
|
template <internal::StringLiteral _name>
|
||||||
static constexpr int find_value_of() {
|
static constexpr int find_value_of() {
|
||||||
if constexpr (_i == num_fields_) {
|
return internal::find_index_or_minus_one<_name, FieldsType>();
|
||||||
return -1;
|
|
||||||
} else {
|
|
||||||
using FieldType = tuple_element_t<_i, FieldsType>;
|
|
||||||
if constexpr (FieldType::name_ == _name) {
|
|
||||||
return _i;
|
|
||||||
} else {
|
|
||||||
return find_value_of<_name, _i + 1>();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Whether the literal contains this string.
|
/// Whether the literal contains this string.
|
||||||
|
|||||||
@@ -45,6 +45,22 @@ constexpr static int find_index() {
|
|||||||
return ix;
|
return ix;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Finds the index of the field signified by _field_name or -1.
|
||||||
|
template <StringLiteral _field_name, class Fields>
|
||||||
|
constexpr static 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
} // namespace internal
|
} // namespace internal
|
||||||
} // namespace rfl
|
} // namespace rfl
|
||||||
|
|
||||||
|
|||||||
@@ -19,6 +19,11 @@ struct Person {
|
|||||||
};
|
};
|
||||||
|
|
||||||
TEST(json, test_literal) {
|
TEST(json, test_literal) {
|
||||||
|
static_assert(!FirstName::template contains_any<LastName>(),
|
||||||
|
"Must be false.");
|
||||||
|
static_assert(FirstName::template contains_any<FirstName>(), "Must be true.");
|
||||||
|
static_assert(FirstName::template contains_all<FirstName>(), "Must be true.");
|
||||||
|
|
||||||
const auto bart = Person{.first_name = FirstName::make<"Bart">()};
|
const auto bart = Person{.first_name = FirstName::make<"Bart">()};
|
||||||
|
|
||||||
write_and_read(bart,
|
write_and_read(bart,
|
||||||
|
|||||||
Reference in New Issue
Block a user