From 530232ba16f07cd4160c9f3af0d7780d994a6450 Mon Sep 17 00:00:00 2001 From: Persson-dev Date: Sat, 8 Feb 2025 19:39:37 +0100 Subject: [PATCH] add back tupleforeach --- include/sp/common/Templates.h | 41 +++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/include/sp/common/Templates.h b/include/sp/common/Templates.h index da12115..c7b2875 100644 --- a/include/sp/common/Templates.h +++ b/include/sp/common/Templates.h @@ -9,4 +9,45 @@ namespace sp { template using tuple_cat_t = decltype(std::tuple_cat(std::declval()...)); +namespace details { + +template +class TupleForEachHelper { + public: + template + static void Exec(TTuple&& tuple, TFunc&& func) { + using Tuple = typename std::decay::type; + static const std::size_t TupleSize = std::tuple_size::value; + static_assert(TRem <= TupleSize, "Incorrect parameters"); + + // Invoke function with current element + static const std::size_t Idx = TupleSize - TRem; + func(std::get(std::forward(tuple))); + + // Compile time recursion - invoke function with the remaining elements + TupleForEachHelper::Exec(std::forward(tuple), std::forward(func)); + } +}; + +template <> +class TupleForEachHelper<0> { + public: + // Stop compile time recursion + template + static void Exec(TTuple&& tuple, TFunc&& func) { + static_cast(tuple); + static_cast(func); + } +}; + +} // namespace details + +template +void TupleForEach(TFunc&& func, TTuple&& tuple) { + using Tuple = typename std::decay::type; + static const std::size_t TupleSize = std::tuple_size::value; + + details::TupleForEachHelper::Exec(std::forward(tuple), std::forward(func)); +} + } // namespace sp