uses_allocator_args.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. // Utility functions for uses-allocator construction -*- C++ -*-
  2. // Copyright (C) 2019-2022 Free Software Foundation, Inc.
  3. //
  4. // This file is part of the GNU ISO C++ Library. This library is free
  5. // software; you can redistribute it and/or modify it under the
  6. // terms of the GNU General Public License as published by the
  7. // Free Software Foundation; either version 3, or (at your option)
  8. // any later version.
  9. // This library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. // Under Section 7 of GPL version 3, you are granted additional
  14. // permissions described in the GCC Runtime Library Exception, version
  15. // 3.1, as published by the Free Software Foundation.
  16. // You should have received a copy of the GNU General Public License and
  17. // a copy of the GCC Runtime Library Exception along with this program;
  18. // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  19. // <http://www.gnu.org/licenses/>.
  20. /** @file include/bits/uses_allocator_args.h
  21. * This is an internal header file, included by other library headers.
  22. * Do not attempt to use it directly. @headername{memory}
  23. */
  24. #ifndef _USES_ALLOCATOR_ARGS
  25. #define _USES_ALLOCATOR_ARGS 1
  26. #pragma GCC system_header
  27. #if __cplusplus > 201703L && __cpp_concepts
  28. #include <new> // for placement operator new
  29. #include <tuple> // for tuple, make_tuple, make_from_tuple
  30. #include <bits/stl_construct.h> // construct_at
  31. #include <bits/stl_pair.h> // pair
  32. namespace std _GLIBCXX_VISIBILITY(default)
  33. {
  34. _GLIBCXX_BEGIN_NAMESPACE_VERSION
  35. template<typename _Tp>
  36. concept _Std_pair = __is_pair<_Tp>;
  37. /** @addtogroup allocators
  38. * @{
  39. */
  40. // Not specified by C++20, used internally
  41. #define __cpp_lib_make_obj_using_allocator 201811L
  42. template<typename _Tp, typename _Alloc, typename... _Args>
  43. constexpr auto
  44. uses_allocator_construction_args(const _Alloc& __a,
  45. _Args&&... __args) noexcept
  46. requires (! _Std_pair<_Tp>)
  47. {
  48. if constexpr (uses_allocator_v<remove_cv_t<_Tp>, _Alloc>)
  49. {
  50. if constexpr (is_constructible_v<_Tp, allocator_arg_t,
  51. const _Alloc&, _Args...>)
  52. {
  53. return tuple<allocator_arg_t, const _Alloc&, _Args&&...>(
  54. allocator_arg, __a, std::forward<_Args>(__args)...);
  55. }
  56. else
  57. {
  58. static_assert(is_constructible_v<_Tp, _Args..., const _Alloc&>,
  59. "construction with an allocator must be possible"
  60. " if uses_allocator is true");
  61. return tuple<_Args&&..., const _Alloc&>(
  62. std::forward<_Args>(__args)..., __a);
  63. }
  64. }
  65. else
  66. {
  67. static_assert(is_constructible_v<_Tp, _Args...>);
  68. return tuple<_Args&&...>(std::forward<_Args>(__args)...);
  69. }
  70. }
  71. template<_Std_pair _Tp, typename _Alloc, typename _Tuple1, typename _Tuple2>
  72. constexpr auto
  73. uses_allocator_construction_args(const _Alloc& __a, piecewise_construct_t,
  74. _Tuple1&& __x, _Tuple2&& __y) noexcept;
  75. template<_Std_pair _Tp, typename _Alloc>
  76. constexpr auto
  77. uses_allocator_construction_args(const _Alloc&) noexcept;
  78. template<_Std_pair _Tp, typename _Alloc, typename _Up, typename _Vp>
  79. constexpr auto
  80. uses_allocator_construction_args(const _Alloc&, _Up&&, _Vp&&) noexcept;
  81. template<_Std_pair _Tp, typename _Alloc, typename _Up, typename _Vp>
  82. constexpr auto
  83. uses_allocator_construction_args(const _Alloc&,
  84. const pair<_Up, _Vp>&) noexcept;
  85. template<_Std_pair _Tp, typename _Alloc, typename _Up, typename _Vp>
  86. constexpr auto
  87. uses_allocator_construction_args(const _Alloc&, pair<_Up, _Vp>&&) noexcept;
  88. template<_Std_pair _Tp, typename _Alloc, typename _Tuple1, typename _Tuple2>
  89. constexpr auto
  90. uses_allocator_construction_args(const _Alloc& __a, piecewise_construct_t,
  91. _Tuple1&& __x, _Tuple2&& __y) noexcept
  92. {
  93. using _Tp1 = typename _Tp::first_type;
  94. using _Tp2 = typename _Tp::second_type;
  95. return std::make_tuple(piecewise_construct,
  96. std::apply([&__a](auto&&... __args1) {
  97. return std::uses_allocator_construction_args<_Tp1>(
  98. __a, std::forward<decltype(__args1)>(__args1)...);
  99. }, std::forward<_Tuple1>(__x)),
  100. std::apply([&__a](auto&&... __args2) {
  101. return std::uses_allocator_construction_args<_Tp2>(
  102. __a, std::forward<decltype(__args2)>(__args2)...);
  103. }, std::forward<_Tuple2>(__y)));
  104. }
  105. template<_Std_pair _Tp, typename _Alloc>
  106. constexpr auto
  107. uses_allocator_construction_args(const _Alloc& __a) noexcept
  108. {
  109. using _Tp1 = typename _Tp::first_type;
  110. using _Tp2 = typename _Tp::second_type;
  111. return std::make_tuple(piecewise_construct,
  112. std::uses_allocator_construction_args<_Tp1>(__a),
  113. std::uses_allocator_construction_args<_Tp2>(__a));
  114. }
  115. template<_Std_pair _Tp, typename _Alloc, typename _Up, typename _Vp>
  116. constexpr auto
  117. uses_allocator_construction_args(const _Alloc& __a, _Up&& __u, _Vp&& __v)
  118. noexcept
  119. {
  120. using _Tp1 = typename _Tp::first_type;
  121. using _Tp2 = typename _Tp::second_type;
  122. return std::make_tuple(piecewise_construct,
  123. std::uses_allocator_construction_args<_Tp1>(__a,
  124. std::forward<_Up>(__u)),
  125. std::uses_allocator_construction_args<_Tp2>(__a,
  126. std::forward<_Vp>(__v)));
  127. }
  128. template<_Std_pair _Tp, typename _Alloc, typename _Up, typename _Vp>
  129. constexpr auto
  130. uses_allocator_construction_args(const _Alloc& __a,
  131. const pair<_Up, _Vp>& __pr) noexcept
  132. {
  133. using _Tp1 = typename _Tp::first_type;
  134. using _Tp2 = typename _Tp::second_type;
  135. return std::make_tuple(piecewise_construct,
  136. std::uses_allocator_construction_args<_Tp1>(__a, __pr.first),
  137. std::uses_allocator_construction_args<_Tp2>(__a, __pr.second));
  138. }
  139. template<_Std_pair _Tp, typename _Alloc, typename _Up, typename _Vp>
  140. constexpr auto
  141. uses_allocator_construction_args(const _Alloc& __a,
  142. pair<_Up, _Vp>&& __pr) noexcept
  143. {
  144. using _Tp1 = typename _Tp::first_type;
  145. using _Tp2 = typename _Tp::second_type;
  146. return std::make_tuple(piecewise_construct,
  147. std::uses_allocator_construction_args<_Tp1>(__a,
  148. std::move(__pr).first),
  149. std::uses_allocator_construction_args<_Tp2>(__a,
  150. std::move(__pr).second));
  151. }
  152. template<typename _Tp, typename _Alloc, typename... _Args>
  153. constexpr _Tp
  154. make_obj_using_allocator(const _Alloc& __a, _Args&&... __args)
  155. {
  156. return std::make_from_tuple<_Tp>(
  157. std::uses_allocator_construction_args<_Tp>(__a,
  158. std::forward<_Args>(__args)...));
  159. }
  160. template<typename _Tp, typename _Alloc, typename... _Args>
  161. constexpr _Tp*
  162. uninitialized_construct_using_allocator(_Tp* __p, const _Alloc& __a,
  163. _Args&&... __args)
  164. {
  165. return std::apply([&](auto&&... __xs) {
  166. return std::construct_at(__p, std::forward<decltype(__xs)>(__xs)...);
  167. }, std::uses_allocator_construction_args<_Tp>(__a,
  168. std::forward<_Args>(__args)...));
  169. }
  170. /// @}
  171. _GLIBCXX_END_NAMESPACE_VERSION
  172. } // namespace std
  173. #endif // C++20
  174. #endif // _USES_ALLOCATOR_ARGS