From: Zachary Turner <zturner@google.com> Date: Mon, 10 Oct 2016 16:44:09 +0000 (+0000) Subject: Add llvm::apply to STLExtras. X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=bd90bcccca2b8b6a27cc9a99f8dda013b3d6fae4;p=llvm Add llvm::apply to STLExtras. This is equivalent to the C++14 std::apply(). Since we are not using C++14 yet, this allows us to still make use of apply anyway. Differential revision: https://reviews.llvm.org/D25100 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@283779 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/include/llvm/ADT/STLExtras.h b/include/llvm/ADT/STLExtras.h index a6e1d2166cb..57134885ced 100644 --- a/include/llvm/ADT/STLExtras.h +++ b/include/llvm/ADT/STLExtras.h @@ -24,6 +24,7 @@ #include <functional> #include <iterator> #include <memory> +#include <tuple> #include <utility> // for std::pair #include "llvm/ADT/Optional.h" @@ -690,6 +691,28 @@ template <typename R> detail::enumerator_impl<R> enumerate(R &&Range) { return detail::enumerator_impl<R>(std::forward<R>(Range)); } +namespace detail { +template <typename F, typename Tuple, std::size_t... I> +auto apply_impl(F &&f, Tuple &&t, index_sequence<I...>) + -> decltype(std::forward<F>(f)(std::get<I>(std::forward<Tuple>(t))...)) { + return std::forward<F>(f)(std::get<I>(std::forward<Tuple>(t))...); +} +} + +/// Given an input tuple (a1, a2, ..., an), pass the arguments of the +/// tuple variadically to f as if by calling f(a1, a2, ..., an) and +/// return the result. +template <typename F, typename Tuple> +auto apply(F &&f, Tuple &&t) -> decltype(detail::apply_impl( + std::forward<F>(f), std::forward<Tuple>(t), + build_index_impl< + std::tuple_size<typename std::decay<Tuple>::type>::value>{})) { + using Indices = build_index_impl< + std::tuple_size<typename std::decay<Tuple>::type>::value>; + + return detail::apply_impl(std::forward<F>(f), std::forward<Tuple>(t), + Indices{}); +} } // End llvm namespace #endif diff --git a/unittests/ADT/STLExtrasTest.cpp b/unittests/ADT/STLExtrasTest.cpp index db344bc6089..8675f6d5ce1 100644 --- a/unittests/ADT/STLExtrasTest.cpp +++ b/unittests/ADT/STLExtrasTest.cpp @@ -192,4 +192,49 @@ TEST(STLExtrasTest, EnumerateLifetimeSemantics) { EXPECT_EQ(0, Moves); EXPECT_EQ(0, Destructors); } + +TEST(STLExtrasTest, ApplyTuple) { + auto T = std::make_tuple(1, 3, 7); + auto U = llvm::apply( + [](int A, int B, int C) { return std::make_tuple(A - B, B - C, C - A); }, + T); + + EXPECT_EQ(-2, std::get<0>(U)); + EXPECT_EQ(-4, std::get<1>(U)); + EXPECT_EQ(6, std::get<2>(U)); + + auto V = llvm::apply( + [](int A, int B, int C) { + return std::make_tuple(std::make_pair(A, char('A' + A)), + std::make_pair(B, char('A' + B)), + std::make_pair(C, char('A' + C))); + }, + T); + + EXPECT_EQ(std::make_pair(1, 'B'), std::get<0>(V)); + EXPECT_EQ(std::make_pair(3, 'D'), std::get<1>(V)); + EXPECT_EQ(std::make_pair(7, 'H'), std::get<2>(V)); +} + +class apply_variadic { + static int apply_one(int X) { return X + 1; } + static char apply_one(char C) { return C + 1; } + static StringRef apply_one(StringRef S) { return S.drop_back(); } + +public: + template <typename... Ts> + auto operator()(Ts &&... Items) + -> decltype(std::make_tuple(apply_one(Items)...)) { + return std::make_tuple(apply_one(Items)...); + } +}; + +TEST(STLExtrasTest, ApplyTupleVariadic) { + auto Items = std::make_tuple(1, llvm::StringRef("Test"), 'X'); + auto Values = apply(apply_variadic(), Items); + + EXPECT_EQ(2, std::get<0>(Values)); + EXPECT_EQ("Tes", std::get<1>(Values)); + EXPECT_EQ('Y', std::get<2>(Values)); +} }