xref: /aosp_15_r20/external/abseil-cpp/absl/utility/utility.h (revision 9356374a3709195abf420251b3e825997ff56c0f)
1*9356374aSAndroid Build Coastguard Worker // Copyright 2017 The Abseil Authors.
2*9356374aSAndroid Build Coastguard Worker //
3*9356374aSAndroid Build Coastguard Worker // Licensed under the Apache License, Version 2.0 (the "License");
4*9356374aSAndroid Build Coastguard Worker // you may not use this file except in compliance with the License.
5*9356374aSAndroid Build Coastguard Worker // You may obtain a copy of the License at
6*9356374aSAndroid Build Coastguard Worker //
7*9356374aSAndroid Build Coastguard Worker //      https://www.apache.org/licenses/LICENSE-2.0
8*9356374aSAndroid Build Coastguard Worker //
9*9356374aSAndroid Build Coastguard Worker // Unless required by applicable law or agreed to in writing, software
10*9356374aSAndroid Build Coastguard Worker // distributed under the License is distributed on an "AS IS" BASIS,
11*9356374aSAndroid Build Coastguard Worker // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12*9356374aSAndroid Build Coastguard Worker // See the License for the specific language governing permissions and
13*9356374aSAndroid Build Coastguard Worker // limitations under the License.
14*9356374aSAndroid Build Coastguard Worker //
15*9356374aSAndroid Build Coastguard Worker // This header file contains C++14 versions of standard <utility> header
16*9356374aSAndroid Build Coastguard Worker // abstractions available within C++17, and are designed to be drop-in
17*9356374aSAndroid Build Coastguard Worker // replacement for code compliant with C++14 and C++17.
18*9356374aSAndroid Build Coastguard Worker //
19*9356374aSAndroid Build Coastguard Worker // The following abstractions are defined:
20*9356374aSAndroid Build Coastguard Worker //
21*9356374aSAndroid Build Coastguard Worker //   * apply<Functor, Tuple>         == std::apply<Functor, Tuple>
22*9356374aSAndroid Build Coastguard Worker //   * exchange<T>                   == std::exchange<T>
23*9356374aSAndroid Build Coastguard Worker //   * make_from_tuple<T>            == std::make_from_tuple<T>
24*9356374aSAndroid Build Coastguard Worker //
25*9356374aSAndroid Build Coastguard Worker // This header file also provides the tag types `in_place_t`, `in_place_type_t`,
26*9356374aSAndroid Build Coastguard Worker // and `in_place_index_t`, as well as the constant `in_place`, and
27*9356374aSAndroid Build Coastguard Worker // `constexpr` `std::move()` and `std::forward()` implementations in C++11.
28*9356374aSAndroid Build Coastguard Worker //
29*9356374aSAndroid Build Coastguard Worker // References:
30*9356374aSAndroid Build Coastguard Worker //
31*9356374aSAndroid Build Coastguard Worker //  https://en.cppreference.com/w/cpp/utility/apply
32*9356374aSAndroid Build Coastguard Worker //  http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2013/n3658.html
33*9356374aSAndroid Build Coastguard Worker 
34*9356374aSAndroid Build Coastguard Worker #ifndef ABSL_UTILITY_UTILITY_H_
35*9356374aSAndroid Build Coastguard Worker #define ABSL_UTILITY_UTILITY_H_
36*9356374aSAndroid Build Coastguard Worker 
37*9356374aSAndroid Build Coastguard Worker #include <cstddef>
38*9356374aSAndroid Build Coastguard Worker #include <cstdlib>
39*9356374aSAndroid Build Coastguard Worker #include <tuple>
40*9356374aSAndroid Build Coastguard Worker #include <utility>
41*9356374aSAndroid Build Coastguard Worker 
42*9356374aSAndroid Build Coastguard Worker #include "absl/base/config.h"
43*9356374aSAndroid Build Coastguard Worker #include "absl/base/internal/inline_variable.h"
44*9356374aSAndroid Build Coastguard Worker #include "absl/base/internal/invoke.h"
45*9356374aSAndroid Build Coastguard Worker #include "absl/meta/type_traits.h"
46*9356374aSAndroid Build Coastguard Worker 
47*9356374aSAndroid Build Coastguard Worker namespace absl {
48*9356374aSAndroid Build Coastguard Worker ABSL_NAMESPACE_BEGIN
49*9356374aSAndroid Build Coastguard Worker 
50*9356374aSAndroid Build Coastguard Worker // Historical note: Abseil once provided implementations of these
51*9356374aSAndroid Build Coastguard Worker // abstractions for platforms that had not yet provided them. Those
52*9356374aSAndroid Build Coastguard Worker // platforms are no longer supported. New code should simply use the
53*9356374aSAndroid Build Coastguard Worker // the ones from std directly.
54*9356374aSAndroid Build Coastguard Worker using std::exchange;
55*9356374aSAndroid Build Coastguard Worker using std::forward;
56*9356374aSAndroid Build Coastguard Worker using std::index_sequence;
57*9356374aSAndroid Build Coastguard Worker using std::index_sequence_for;
58*9356374aSAndroid Build Coastguard Worker using std::integer_sequence;
59*9356374aSAndroid Build Coastguard Worker using std::make_index_sequence;
60*9356374aSAndroid Build Coastguard Worker using std::make_integer_sequence;
61*9356374aSAndroid Build Coastguard Worker using std::move;
62*9356374aSAndroid Build Coastguard Worker 
63*9356374aSAndroid Build Coastguard Worker namespace utility_internal {
64*9356374aSAndroid Build Coastguard Worker 
65*9356374aSAndroid Build Coastguard Worker template <typename T>
66*9356374aSAndroid Build Coastguard Worker struct InPlaceTypeTag {
67*9356374aSAndroid Build Coastguard Worker   explicit InPlaceTypeTag() = delete;
68*9356374aSAndroid Build Coastguard Worker   InPlaceTypeTag(const InPlaceTypeTag&) = delete;
69*9356374aSAndroid Build Coastguard Worker   InPlaceTypeTag& operator=(const InPlaceTypeTag&) = delete;
70*9356374aSAndroid Build Coastguard Worker };
71*9356374aSAndroid Build Coastguard Worker 
72*9356374aSAndroid Build Coastguard Worker template <size_t I>
73*9356374aSAndroid Build Coastguard Worker struct InPlaceIndexTag {
74*9356374aSAndroid Build Coastguard Worker   explicit InPlaceIndexTag() = delete;
75*9356374aSAndroid Build Coastguard Worker   InPlaceIndexTag(const InPlaceIndexTag&) = delete;
76*9356374aSAndroid Build Coastguard Worker   InPlaceIndexTag& operator=(const InPlaceIndexTag&) = delete;
77*9356374aSAndroid Build Coastguard Worker };
78*9356374aSAndroid Build Coastguard Worker 
79*9356374aSAndroid Build Coastguard Worker }  // namespace utility_internal
80*9356374aSAndroid Build Coastguard Worker 
81*9356374aSAndroid Build Coastguard Worker // Tag types
82*9356374aSAndroid Build Coastguard Worker 
83*9356374aSAndroid Build Coastguard Worker #ifdef ABSL_USES_STD_OPTIONAL
84*9356374aSAndroid Build Coastguard Worker 
85*9356374aSAndroid Build Coastguard Worker using std::in_place_t;
86*9356374aSAndroid Build Coastguard Worker using std::in_place;
87*9356374aSAndroid Build Coastguard Worker 
88*9356374aSAndroid Build Coastguard Worker #else  // ABSL_USES_STD_OPTIONAL
89*9356374aSAndroid Build Coastguard Worker 
90*9356374aSAndroid Build Coastguard Worker // in_place_t
91*9356374aSAndroid Build Coastguard Worker //
92*9356374aSAndroid Build Coastguard Worker // Tag type used to specify in-place construction, such as with
93*9356374aSAndroid Build Coastguard Worker // `absl::optional`, designed to be a drop-in replacement for C++17's
94*9356374aSAndroid Build Coastguard Worker // `std::in_place_t`.
95*9356374aSAndroid Build Coastguard Worker struct in_place_t {};
96*9356374aSAndroid Build Coastguard Worker 
97*9356374aSAndroid Build Coastguard Worker ABSL_INTERNAL_INLINE_CONSTEXPR(in_place_t, in_place, {});
98*9356374aSAndroid Build Coastguard Worker 
99*9356374aSAndroid Build Coastguard Worker #endif  // ABSL_USES_STD_OPTIONAL
100*9356374aSAndroid Build Coastguard Worker 
101*9356374aSAndroid Build Coastguard Worker #if defined(ABSL_USES_STD_ANY) || defined(ABSL_USES_STD_VARIANT)
102*9356374aSAndroid Build Coastguard Worker using std::in_place_type;
103*9356374aSAndroid Build Coastguard Worker using std::in_place_type_t;
104*9356374aSAndroid Build Coastguard Worker #else
105*9356374aSAndroid Build Coastguard Worker 
106*9356374aSAndroid Build Coastguard Worker // in_place_type_t
107*9356374aSAndroid Build Coastguard Worker //
108*9356374aSAndroid Build Coastguard Worker // Tag type used for in-place construction when the type to construct needs to
109*9356374aSAndroid Build Coastguard Worker // be specified, such as with `absl::any`, designed to be a drop-in replacement
110*9356374aSAndroid Build Coastguard Worker // for C++17's `std::in_place_type_t`.
111*9356374aSAndroid Build Coastguard Worker template <typename T>
112*9356374aSAndroid Build Coastguard Worker using in_place_type_t = void (*)(utility_internal::InPlaceTypeTag<T>);
113*9356374aSAndroid Build Coastguard Worker 
114*9356374aSAndroid Build Coastguard Worker template <typename T>
in_place_type(utility_internal::InPlaceTypeTag<T>)115*9356374aSAndroid Build Coastguard Worker void in_place_type(utility_internal::InPlaceTypeTag<T>) {}
116*9356374aSAndroid Build Coastguard Worker #endif  // ABSL_USES_STD_ANY || ABSL_USES_STD_VARIANT
117*9356374aSAndroid Build Coastguard Worker 
118*9356374aSAndroid Build Coastguard Worker #ifdef ABSL_USES_STD_VARIANT
119*9356374aSAndroid Build Coastguard Worker using std::in_place_index;
120*9356374aSAndroid Build Coastguard Worker using std::in_place_index_t;
121*9356374aSAndroid Build Coastguard Worker #else
122*9356374aSAndroid Build Coastguard Worker 
123*9356374aSAndroid Build Coastguard Worker // in_place_index_t
124*9356374aSAndroid Build Coastguard Worker //
125*9356374aSAndroid Build Coastguard Worker // Tag type used for in-place construction when the type to construct needs to
126*9356374aSAndroid Build Coastguard Worker // be specified, such as with `absl::any`, designed to be a drop-in replacement
127*9356374aSAndroid Build Coastguard Worker // for C++17's `std::in_place_index_t`.
128*9356374aSAndroid Build Coastguard Worker template <size_t I>
129*9356374aSAndroid Build Coastguard Worker using in_place_index_t = void (*)(utility_internal::InPlaceIndexTag<I>);
130*9356374aSAndroid Build Coastguard Worker 
131*9356374aSAndroid Build Coastguard Worker template <size_t I>
in_place_index(utility_internal::InPlaceIndexTag<I>)132*9356374aSAndroid Build Coastguard Worker void in_place_index(utility_internal::InPlaceIndexTag<I>) {}
133*9356374aSAndroid Build Coastguard Worker #endif  // ABSL_USES_STD_VARIANT
134*9356374aSAndroid Build Coastguard Worker 
135*9356374aSAndroid Build Coastguard Worker namespace utility_internal {
136*9356374aSAndroid Build Coastguard Worker // Helper method for expanding tuple into a called method.
137*9356374aSAndroid Build Coastguard Worker template <typename Functor, typename Tuple, std::size_t... Indexes>
138*9356374aSAndroid Build Coastguard Worker auto apply_helper(Functor&& functor, Tuple&& t, index_sequence<Indexes...>)
139*9356374aSAndroid Build Coastguard Worker     -> decltype(absl::base_internal::invoke(
140*9356374aSAndroid Build Coastguard Worker         absl::forward<Functor>(functor),
141*9356374aSAndroid Build Coastguard Worker         std::get<Indexes>(absl::forward<Tuple>(t))...)) {
142*9356374aSAndroid Build Coastguard Worker   return absl::base_internal::invoke(
143*9356374aSAndroid Build Coastguard Worker       absl::forward<Functor>(functor),
144*9356374aSAndroid Build Coastguard Worker       std::get<Indexes>(absl::forward<Tuple>(t))...);
145*9356374aSAndroid Build Coastguard Worker }
146*9356374aSAndroid Build Coastguard Worker 
147*9356374aSAndroid Build Coastguard Worker }  // namespace utility_internal
148*9356374aSAndroid Build Coastguard Worker 
149*9356374aSAndroid Build Coastguard Worker // apply
150*9356374aSAndroid Build Coastguard Worker //
151*9356374aSAndroid Build Coastguard Worker // Invokes a Callable using elements of a tuple as its arguments.
152*9356374aSAndroid Build Coastguard Worker // Each element of the tuple corresponds to an argument of the call (in order).
153*9356374aSAndroid Build Coastguard Worker // Both the Callable argument and the tuple argument are perfect-forwarded.
154*9356374aSAndroid Build Coastguard Worker // For member-function Callables, the first tuple element acts as the `this`
155*9356374aSAndroid Build Coastguard Worker // pointer. `absl::apply` is designed to be a drop-in replacement for C++17's
156*9356374aSAndroid Build Coastguard Worker // `std::apply`. Unlike C++17's `std::apply`, this is not currently `constexpr`.
157*9356374aSAndroid Build Coastguard Worker //
158*9356374aSAndroid Build Coastguard Worker // Example:
159*9356374aSAndroid Build Coastguard Worker //
160*9356374aSAndroid Build Coastguard Worker //   class Foo {
161*9356374aSAndroid Build Coastguard Worker //    public:
162*9356374aSAndroid Build Coastguard Worker //     void Bar(int);
163*9356374aSAndroid Build Coastguard Worker //   };
164*9356374aSAndroid Build Coastguard Worker //   void user_function1(int, std::string);
165*9356374aSAndroid Build Coastguard Worker //   void user_function2(std::unique_ptr<Foo>);
166*9356374aSAndroid Build Coastguard Worker //   auto user_lambda = [](int, int) {};
167*9356374aSAndroid Build Coastguard Worker //
168*9356374aSAndroid Build Coastguard Worker //   int main()
169*9356374aSAndroid Build Coastguard Worker //   {
170*9356374aSAndroid Build Coastguard Worker //       std::tuple<int, std::string> tuple1(42, "bar");
171*9356374aSAndroid Build Coastguard Worker //       // Invokes the first user function on int, std::string.
172*9356374aSAndroid Build Coastguard Worker //       absl::apply(&user_function1, tuple1);
173*9356374aSAndroid Build Coastguard Worker //
174*9356374aSAndroid Build Coastguard Worker //       std::tuple<std::unique_ptr<Foo>> tuple2(absl::make_unique<Foo>());
175*9356374aSAndroid Build Coastguard Worker //       // Invokes the user function that takes ownership of the unique
176*9356374aSAndroid Build Coastguard Worker //       // pointer.
177*9356374aSAndroid Build Coastguard Worker //       absl::apply(&user_function2, std::move(tuple2));
178*9356374aSAndroid Build Coastguard Worker //
179*9356374aSAndroid Build Coastguard Worker //       auto foo = absl::make_unique<Foo>();
180*9356374aSAndroid Build Coastguard Worker //       std::tuple<Foo*, int> tuple3(foo.get(), 42);
181*9356374aSAndroid Build Coastguard Worker //       // Invokes the method Bar on foo with one argument, 42.
182*9356374aSAndroid Build Coastguard Worker //       absl::apply(&Foo::Bar, tuple3);
183*9356374aSAndroid Build Coastguard Worker //
184*9356374aSAndroid Build Coastguard Worker //       std::tuple<int, int> tuple4(8, 9);
185*9356374aSAndroid Build Coastguard Worker //       // Invokes a lambda.
186*9356374aSAndroid Build Coastguard Worker //       absl::apply(user_lambda, tuple4);
187*9356374aSAndroid Build Coastguard Worker //   }
188*9356374aSAndroid Build Coastguard Worker template <typename Functor, typename Tuple>
189*9356374aSAndroid Build Coastguard Worker auto apply(Functor&& functor, Tuple&& t)
190*9356374aSAndroid Build Coastguard Worker     -> decltype(utility_internal::apply_helper(
191*9356374aSAndroid Build Coastguard Worker         absl::forward<Functor>(functor), absl::forward<Tuple>(t),
192*9356374aSAndroid Build Coastguard Worker         absl::make_index_sequence<std::tuple_size<
193*9356374aSAndroid Build Coastguard Worker             typename std::remove_reference<Tuple>::type>::value>{})) {
194*9356374aSAndroid Build Coastguard Worker   return utility_internal::apply_helper(
195*9356374aSAndroid Build Coastguard Worker       absl::forward<Functor>(functor), absl::forward<Tuple>(t),
196*9356374aSAndroid Build Coastguard Worker       absl::make_index_sequence<std::tuple_size<
197*9356374aSAndroid Build Coastguard Worker           typename std::remove_reference<Tuple>::type>::value>{});
198*9356374aSAndroid Build Coastguard Worker }
199*9356374aSAndroid Build Coastguard Worker 
200*9356374aSAndroid Build Coastguard Worker namespace utility_internal {
201*9356374aSAndroid Build Coastguard Worker template <typename T, typename Tuple, size_t... I>
make_from_tuple_impl(Tuple && tup,absl::index_sequence<I...>)202*9356374aSAndroid Build Coastguard Worker T make_from_tuple_impl(Tuple&& tup, absl::index_sequence<I...>) {
203*9356374aSAndroid Build Coastguard Worker   return T(std::get<I>(std::forward<Tuple>(tup))...);
204*9356374aSAndroid Build Coastguard Worker }
205*9356374aSAndroid Build Coastguard Worker }  // namespace utility_internal
206*9356374aSAndroid Build Coastguard Worker 
207*9356374aSAndroid Build Coastguard Worker // make_from_tuple
208*9356374aSAndroid Build Coastguard Worker //
209*9356374aSAndroid Build Coastguard Worker // Given the template parameter type `T` and a tuple of arguments
210*9356374aSAndroid Build Coastguard Worker // `std::tuple(arg0, arg1, ..., argN)` constructs an object of type `T` as if by
211*9356374aSAndroid Build Coastguard Worker // calling `T(arg0, arg1, ..., argN)`.
212*9356374aSAndroid Build Coastguard Worker //
213*9356374aSAndroid Build Coastguard Worker // Example:
214*9356374aSAndroid Build Coastguard Worker //
215*9356374aSAndroid Build Coastguard Worker //   std::tuple<const char*, size_t> args("hello world", 5);
216*9356374aSAndroid Build Coastguard Worker //   auto s = absl::make_from_tuple<std::string>(args);
217*9356374aSAndroid Build Coastguard Worker //   assert(s == "hello");
218*9356374aSAndroid Build Coastguard Worker //
219*9356374aSAndroid Build Coastguard Worker template <typename T, typename Tuple>
make_from_tuple(Tuple && tup)220*9356374aSAndroid Build Coastguard Worker constexpr T make_from_tuple(Tuple&& tup) {
221*9356374aSAndroid Build Coastguard Worker   return utility_internal::make_from_tuple_impl<T>(
222*9356374aSAndroid Build Coastguard Worker       std::forward<Tuple>(tup),
223*9356374aSAndroid Build Coastguard Worker       absl::make_index_sequence<
224*9356374aSAndroid Build Coastguard Worker           std::tuple_size<absl::decay_t<Tuple>>::value>{});
225*9356374aSAndroid Build Coastguard Worker }
226*9356374aSAndroid Build Coastguard Worker 
227*9356374aSAndroid Build Coastguard Worker ABSL_NAMESPACE_END
228*9356374aSAndroid Build Coastguard Worker }  // namespace absl
229*9356374aSAndroid Build Coastguard Worker 
230*9356374aSAndroid Build Coastguard Worker #endif  // ABSL_UTILITY_UTILITY_H_
231