1
0
mirror of https://github.com/RPCS3/llvm-mirror.git synced 2024-10-19 02:52:53 +02:00

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

llvm-svn: 283779
This commit is contained in:
Zachary Turner 2016-10-10 16:44:09 +00:00
parent 408de1dad4
commit 4022c2b021
2 changed files with 68 additions and 0 deletions

View 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

View 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));
}
}