]> granicus.if.org Git - llvm/commitdiff
Add llvm::apply to STLExtras.
authorZachary Turner <zturner@google.com>
Mon, 10 Oct 2016 16:44:09 +0000 (16:44 +0000)
committerZachary Turner <zturner@google.com>
Mon, 10 Oct 2016 16:44:09 +0000 (16:44 +0000)
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

include/llvm/ADT/STLExtras.h
unittests/ADT/STLExtrasTest.cpp

index a6e1d2166cb11aeceda764672bc979772ff22828..57134885cede3b43932ecdde1b02b140edc40568 100644 (file)
@@ -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
index db344bc6089edb6d5d5916400691d18c8b2efaf2..8675f6d5ce1e1595b57087ad7027b138d3981a89 100644 (file)
@@ -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));
+}
 }