1111// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1212// See the License for the specific language governing permissions and
1313// limitations under the License.
14- //
15- // This header file contains C++14 versions of standard <utility> header
16- // abstractions available within C++17, and are designed to be drop-in
17- // replacement for code compliant with C++14 and C++17.
18- //
19- // The following abstractions are defined:
20- //
21- // * apply<Functor, Tuple> == std::apply<Functor, Tuple>
22- // * exchange<T> == std::exchange<T>
23- // * make_from_tuple<T> == std::make_from_tuple<T>
24- //
25- // References:
26- //
27- // https://en.cppreference.com/w/cpp/utility/apply
28- // http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2013/n3658.html
2914
3015#ifndef ABSL_UTILITY_UTILITY_H_
3116#define ABSL_UTILITY_UTILITY_H_
3621#include < utility>
3722
3823#include " absl/base/config.h"
24+
25+ // TODO(b/290784225): Include what you use cleanup required.
3926#include " absl/meta/type_traits.h"
4027
4128namespace absl {
@@ -45,6 +32,7 @@ ABSL_NAMESPACE_BEGIN
4532// abstractions for platforms that had not yet provided them. Those
4633// platforms are no longer supported. New code should simply use the
4734// the ones from std directly.
35+ using std::apply;
4836using std::exchange;
4937using std::forward;
5038using std::in_place;
@@ -56,99 +44,11 @@ using std::in_place_type_t;
5644using std::index_sequence;
5745using std::index_sequence_for;
5846using std::integer_sequence;
47+ using std::make_from_tuple;
5948using std::make_index_sequence;
6049using std::make_integer_sequence;
6150using std::move;
6251
63- namespace utility_internal {
64- // Helper method for expanding tuple into a called method.
65- template <typename Functor, typename Tuple, std::size_t ... Indexes>
66- auto apply_helper (Functor&& functor, Tuple&& t, index_sequence<Indexes...>)
67- -> decltype(std::invoke(absl::forward<Functor>(functor),
68- std::get<Indexes>(absl::forward<Tuple>(t))...)) {
69- return std::invoke (absl::forward<Functor>(functor),
70- std::get<Indexes>(absl::forward<Tuple>(t))...);
71- }
72- } // namespace utility_internal
73-
74- // apply
75- //
76- // Invokes a Callable using elements of a tuple as its arguments.
77- // Each element of the tuple corresponds to an argument of the call (in order).
78- // Both the Callable argument and the tuple argument are perfect-forwarded.
79- // For member-function Callables, the first tuple element acts as the `this`
80- // pointer. `absl::apply` is designed to be a drop-in replacement for C++17's
81- // `std::apply`. Unlike C++17's `std::apply`, this is not currently `constexpr`.
82- //
83- // Example:
84- //
85- // class Foo {
86- // public:
87- // void Bar(int);
88- // };
89- // void user_function1(int, std::string);
90- // void user_function2(std::unique_ptr<Foo>);
91- // auto user_lambda = [](int, int) {};
92- //
93- // int main()
94- // {
95- // std::tuple<int, std::string> tuple1(42, "bar");
96- // // Invokes the first user function on int, std::string.
97- // absl::apply(&user_function1, tuple1);
98- //
99- // std::tuple<std::unique_ptr<Foo>> tuple2(absl::make_unique<Foo>());
100- // // Invokes the user function that takes ownership of the unique
101- // // pointer.
102- // absl::apply(&user_function2, std::move(tuple2));
103- //
104- // auto foo = absl::make_unique<Foo>();
105- // std::tuple<Foo*, int> tuple3(foo.get(), 42);
106- // // Invokes the method Bar on foo with one argument, 42.
107- // absl::apply(&Foo::Bar, tuple3);
108- //
109- // std::tuple<int, int> tuple4(8, 9);
110- // // Invokes a lambda.
111- // absl::apply(user_lambda, tuple4);
112- // }
113- template <typename Functor, typename Tuple>
114- auto apply (Functor&& functor, Tuple&& t)
115- -> decltype(utility_internal::apply_helper(
116- absl::forward<Functor>(functor), absl::forward<Tuple>(t),
117- absl::make_index_sequence<std::tuple_size<
118- typename std::remove_reference<Tuple>::type>::value>{})) {
119- return utility_internal::apply_helper (
120- absl::forward<Functor>(functor), absl::forward<Tuple>(t),
121- absl::make_index_sequence<std::tuple_size<
122- typename std::remove_reference<Tuple>::type>::value>{});
123- }
124-
125- namespace utility_internal {
126- template <typename T, typename Tuple, size_t ... I>
127- T make_from_tuple_impl (Tuple&& tup, absl::index_sequence<I...>) {
128- return T (std::get<I>(std::forward<Tuple>(tup))...);
129- }
130- } // namespace utility_internal
131-
132- // make_from_tuple
133- //
134- // Given the template parameter type `T` and a tuple of arguments
135- // `std::tuple(arg0, arg1, ..., argN)` constructs an object of type `T` as if by
136- // calling `T(arg0, arg1, ..., argN)`.
137- //
138- // Example:
139- //
140- // std::tuple<const char*, size_t> args("hello world", 5);
141- // auto s = absl::make_from_tuple<std::string>(args);
142- // assert(s == "hello");
143- //
144- template <typename T, typename Tuple>
145- constexpr T make_from_tuple (Tuple&& tup) {
146- return utility_internal::make_from_tuple_impl<T>(
147- std::forward<Tuple>(tup),
148- absl::make_index_sequence<
149- std::tuple_size<absl::decay_t <Tuple>>::value>{});
150- }
151-
15252ABSL_NAMESPACE_END
15353} // namespace absl
15454
0 commit comments