aboutsummaryrefslogtreecommitdiffhomepage
path: root/absl/utility/utility.h
diff options
context:
space:
mode:
authorGravatar Abseil Team <absl-team@google.com>2018-01-17 09:53:47 -0800
committerGravatar vslashg <gfalcon@google.com>2018-01-17 18:56:51 -0500
commit5a8de8a37e3e282d22f69ca67b4aba8e4cca63ae (patch)
treef65088aad40e74c0959db9b313b30f59514f7467 /absl/utility/utility.h
parentbe40fdf1a86f4956d2f8125a7b6bd6d34e133c2d (diff)
Changes imported from Abseil "staging" branch:
- 5e874e644191fbf99f5636d6303de2b28b23392c Adds a absl::apply function, similar to c++17's std::apply. by Abseil Team <absl-team@google.com> - 16373c438d16a09725dace03ab7ba0f7c2337279 Add debugging_internal::StackTraceWorksForTest by Abseil Team <absl-team@google.com> - a623257aaaff8a5fba3377f34f92f319a104e444 Update absl::CondVar documentation in response to GitHub ... by Derek Mauro <dmauro@google.com> - 87d58a25bc4ecd46165dd1c417121c86cbc07be0 Add assert against uint128 negative bit shift undefined b... by Alex Strelnikov <strel@google.com> - af155c0d2a3556b56a9bcd6f9ee7416277185df8 Fix comment typos. by Abseil Team <absl-team@google.com> - 1824ae832eb75d447dea730b5968d952897e135a Rollback of: Add debugging_internal::StackTraceWorksForTest by Abseil Team <absl-team@google.com> - 97318f087ce63dd5acf1e0d3d697cd90a7d6ebfd Add debugging_internal::StackTraceWorksForTest by Abseil Team <absl-team@google.com> - 9dd1d17dca17f0ded3bda336b7521fd57d08a5cc Move log_severity.h out of internal. by Abseil Team <absl-team@google.com> - 2212bb56b1a8365d2303ff0983441298d08444e5 Internal change. by Alex Strelnikov <strel@google.com> GitOrigin-RevId: 5e874e644191fbf99f5636d6303de2b28b23392c Change-Id: Ic270393ac1f15866afb64617d28269cd829030f6
Diffstat (limited to 'absl/utility/utility.h')
-rw-r--r--absl/utility/utility.h79
1 files changed, 61 insertions, 18 deletions
diff --git a/absl/utility/utility.h b/absl/utility/utility.h
index 732cd4c..d37f4c3 100644
--- a/absl/utility/utility.h
+++ b/absl/utility/utility.h
@@ -23,6 +23,7 @@
// * make_integer_sequence<T, N> == std::make_integer_sequence<T, N>
// * make_index_sequence<N> == std::make_index_sequence<N>
// * index_sequence_for<Ts...> == std::index_sequence_for<Ts...>
+// * apply<Functor, Tuple> == std::apply<Functor, Tuple>
//
// This header file also provides the tag types `in_place_t`, `in_place_type_t`,
// and `in_place_index_t`, as well as the constant `in_place`, and
@@ -31,34 +32,20 @@
// References:
//
// http://en.cppreference.com/w/cpp/utility/integer_sequence
+// http://en.cppreference.com/w/cpp/utility/apply
// http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2013/n3658.html
//
-//
-// Example:
-// // Unpack a tuple for use as a function call's argument list.
-//
-// template <typename F, typename Tup, size_t... Is>
-// auto Impl(F f, const Tup& tup, absl::index_sequence<Is...>)
-// -> decltype(f(std::get<Is>(tup) ...)) {
-// return f(std::get<Is>(tup) ...);
-// }
-//
-// template <typename Tup>
-// using TupIdxSeq = absl::make_index_sequence<std::tuple_size<Tup>::value>;
-//
-// template <typename F, typename Tup>
-// auto ApplyFromTuple(F f, const Tup& tup)
-// -> decltype(Impl(f, tup, TupIdxSeq<Tup>{})) {
-// return Impl(f, tup, TupIdxSeq<Tup>{});
-// }
+
#ifndef ABSL_UTILITY_UTILITY_H_
#define ABSL_UTILITY_UTILITY_H_
#include <cstddef>
#include <cstdlib>
+#include <tuple>
#include <utility>
#include "absl/base/config.h"
+#include "absl/base/internal/invoke.h"
#include "absl/meta/type_traits.h"
namespace absl {
@@ -214,6 +201,62 @@ constexpr T&& forward(
return static_cast<T&&>(t);
}
+namespace utility_internal {
+// Helper method for expanding tuple into a called method.
+template <typename Functor, typename Tuple, std::size_t... Indexes>
+auto apply_helper(Functor&& functor, Tuple&& t, index_sequence<Indexes...>)
+ -> decltype(absl::base_internal::Invoke(
+ absl::forward<Functor>(functor),
+ std::get<Indexes>(absl::forward<Tuple>(t))...)) {
+ return absl::base_internal::Invoke(
+ absl::forward<Functor>(functor),
+ std::get<Indexes>(absl::forward<Tuple>(t))...);
+}
+
+} // namespace utility_internal
+
+// apply
+//
+// Invokes a Callable using elements of a tuple as its arguments.
+// Each element of the tuple corresponds to an argument of the call (in order).
+// Both the Callable argument and the tuple argument are perfect-forwarded.
+// For member-function Callables, the first tuple element acts as the `this`
+// pointer. `absl::apply` is designed to be a drop-in replacement for C++17's
+// `std::apply`. Unlike C++17's `std::apply`, this is not currently `constexpr`.
+//
+// Example:
+//
+// class Foo{void Bar(int);};
+// void user_function(int, std::string);
+// void user_function(std::unique_ptr<Foo>);
+//
+// int main()
+// {
+// std::tuple<int, std::string> tuple1(42, "bar");
+// // Invokes the user function overload on int, std::string.
+// absl::apply(&user_function, tuple1);
+//
+// auto foo = absl::make_unique<Foo>();
+// std::tuple<Foo*, int> tuple2(foo.get(), 42);
+// // Invokes the method Bar on foo with one argument 42.
+// absl::apply(&Foo::Bar, foo.get(), 42);
+//
+// std::tuple<std::unique_ptr<Foo>> tuple3(absl::make_unique<Foo>());
+// // Invokes the user function that takes ownership of the unique
+// // pointer.
+// absl::apply(&user_function, std::move(tuple));
+// }
+template <typename Functor, typename Tuple>
+auto apply(Functor&& functor, Tuple&& t)
+ -> decltype(utility_internal::apply_helper(
+ absl::forward<Functor>(functor), absl::forward<Tuple>(t),
+ absl::make_index_sequence<std::tuple_size<
+ typename std::remove_reference<Tuple>::type>::value>{})) {
+ return utility_internal::apply_helper(
+ absl::forward<Functor>(functor), absl::forward<Tuple>(t),
+ absl::make_index_sequence<std::tuple_size<
+ typename std::remove_reference<Tuple>::type>::value>{});
+}
} // namespace absl
#endif // ABSL_UTILITY_UTILITY_H_