utility.h 8.6 KB

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