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