From 79ee48e0f68a3c2db2e75615cf26d4c7a239744a Mon Sep 17 00:00:00 2001 From: Stanislaw Halik Date: Tue, 15 Nov 2022 09:19:04 +0100 Subject: entity: add visit_tuple --- src/entity.hpp | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) (limited to 'src/entity.hpp') diff --git a/src/entity.hpp b/src/entity.hpp index 32ab759f..31a13632 100644 --- a/src/entity.hpp +++ b/src/entity.hpp @@ -131,11 +131,12 @@ struct Entity final { }; namespace detail { + template requires std::invocable(std::declval()))> constexpr CORRADE_ALWAYS_INLINE void visit_tuple(F&& fun, Tuple&& tuple) { - using Size = std::tuple_size>; + using Size = std::tuple_size>; static_assert(N < Size()); fun(std::get(tuple)); @@ -143,6 +144,20 @@ constexpr CORRADE_ALWAYS_INLINE void visit_tuple(F&& fun, Tuple&& tuple) visit_tuple(std::forward(fun), std::forward(tuple)); } +template +requires std::is_invocable_r_v(std::declval()))> +constexpr CORRADE_ALWAYS_INLINE bool find_in_tuple(F&& fun, Tuple&& tuple) +{ + using Size = std::tuple_size>; + static_assert(N < Size()); + + if (fun(std::get(tuple))) + return true; + if constexpr(N+1 < Size()) + return find_in_tuple(std::forward(fun), std::forward(tuple)); + return false; +} + } // namespace detail template @@ -153,6 +168,16 @@ constexpr void visit_tuple(F&& fun, Tuple&& tuple) detail::visit_tuple(std::forward(fun), std::forward(tuple)); } +template +constexpr bool find_in_tuple(F&& fun, Tuple&& tuple) +{ + using Size = std::tuple_size>; + if constexpr(Size() > 0) + return detail::find_in_tuple(std::forward(fun), std::forward(tuple)); + else + return false; +} + enum class erased_field_type : std::uint32_t { none, string, -- cgit v1.2.3