xref: /freebsd-src/contrib/llvm-project/libcxx/include/__memory/uninitialized_algorithms.h (revision 0eae32dcef82f6f06de6419a0d623d7def0cc8f6)
1 // -*- C++ -*-
2 //===----------------------------------------------------------------------===//
3 //
4 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5 // See https://llvm.org/LICENSE.txt for license information.
6 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #ifndef _LIBCPP___MEMORY_UNINITIALIZED_ALGORITHMS_H
11 #define _LIBCPP___MEMORY_UNINITIALIZED_ALGORITHMS_H
12 
13 #include <__config>
14 #include <__memory/addressof.h>
15 #include <__memory/construct_at.h>
16 #include <__memory/voidify.h>
17 #include <iterator>
18 #include <utility>
19 
20 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
21 #pragma GCC system_header
22 #endif
23 
24 _LIBCPP_BEGIN_NAMESPACE_STD
25 
26 template <class _InputIterator, class _ForwardIterator>
27 _ForwardIterator
28 uninitialized_copy(_InputIterator __f, _InputIterator __l, _ForwardIterator __r)
29 {
30     typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
31 #ifndef _LIBCPP_NO_EXCEPTIONS
32     _ForwardIterator __s = __r;
33     try
34     {
35 #endif
36         for (; __f != __l; ++__f, (void) ++__r)
37             ::new ((void*)_VSTD::addressof(*__r)) value_type(*__f);
38 #ifndef _LIBCPP_NO_EXCEPTIONS
39     }
40     catch (...)
41     {
42         for (; __s != __r; ++__s)
43             __s->~value_type();
44         throw;
45     }
46 #endif
47     return __r;
48 }
49 
50 template <class _InputIterator, class _Size, class _ForwardIterator>
51 _ForwardIterator
52 uninitialized_copy_n(_InputIterator __f, _Size __n, _ForwardIterator __r)
53 {
54     typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
55 #ifndef _LIBCPP_NO_EXCEPTIONS
56     _ForwardIterator __s = __r;
57     try
58     {
59 #endif
60         for (; __n > 0; ++__f, (void) ++__r, (void) --__n)
61             ::new ((void*)_VSTD::addressof(*__r)) value_type(*__f);
62 #ifndef _LIBCPP_NO_EXCEPTIONS
63     }
64     catch (...)
65     {
66         for (; __s != __r; ++__s)
67             __s->~value_type();
68         throw;
69     }
70 #endif
71     return __r;
72 }
73 
74 // uninitialized_fill
75 
76 template <class _ValueType, class _ForwardIterator, class _Sentinel, class _Tp>
77 inline _LIBCPP_HIDE_FROM_ABI
78 _ForwardIterator __uninitialized_fill(_ForwardIterator __first, _Sentinel __last, const _Tp& __x)
79 {
80     _ForwardIterator __idx = __first;
81 #ifndef _LIBCPP_NO_EXCEPTIONS
82     try
83     {
84 #endif
85         for (; __idx != __last; ++__idx)
86             ::new (_VSTD::__voidify(*__idx)) _ValueType(__x);
87 #ifndef _LIBCPP_NO_EXCEPTIONS
88     }
89     catch (...)
90     {
91         _VSTD::__destroy(__first, __idx);
92         throw;
93     }
94 #endif
95 
96     return __idx;
97 }
98 
99 template <class _ForwardIterator, class _Tp>
100 inline _LIBCPP_HIDE_FROM_ABI
101 void uninitialized_fill(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __x)
102 {
103     typedef typename iterator_traits<_ForwardIterator>::value_type _ValueType;
104     (void)_VSTD::__uninitialized_fill<_ValueType>(__first, __last, __x);
105 }
106 
107 // uninitialized_fill_n
108 
109 template <class _ValueType, class _ForwardIterator, class _Size, class _Tp>
110 inline _LIBCPP_HIDE_FROM_ABI
111 _ForwardIterator __uninitialized_fill_n(_ForwardIterator __first, _Size __n, const _Tp& __x)
112 {
113     _ForwardIterator __idx = __first;
114 #ifndef _LIBCPP_NO_EXCEPTIONS
115     try
116     {
117 #endif
118         for (; __n > 0; ++__idx, (void) --__n)
119             ::new (_VSTD::__voidify(*__idx)) _ValueType(__x);
120 #ifndef _LIBCPP_NO_EXCEPTIONS
121     }
122     catch (...)
123     {
124         _VSTD::__destroy(__first, __idx);
125         throw;
126     }
127 #endif
128 
129     return __idx;
130 }
131 
132 template <class _ForwardIterator, class _Size, class _Tp>
133 inline _LIBCPP_HIDE_FROM_ABI
134 _ForwardIterator uninitialized_fill_n(_ForwardIterator __first, _Size __n, const _Tp& __x)
135 {
136     typedef typename iterator_traits<_ForwardIterator>::value_type _ValueType;
137     return _VSTD::__uninitialized_fill_n<_ValueType>(__first, __n, __x);
138 }
139 
140 #if _LIBCPP_STD_VER > 14
141 
142 // uninitialized_default_construct
143 
144 template <class _ValueType, class _ForwardIterator, class _Sentinel>
145 inline _LIBCPP_HIDE_FROM_ABI
146 _ForwardIterator __uninitialized_default_construct(_ForwardIterator __first, _Sentinel __last) {
147     auto __idx = __first;
148 #ifndef _LIBCPP_NO_EXCEPTIONS
149     try {
150 #endif
151     for (; __idx != __last; ++__idx)
152         ::new (_VSTD::__voidify(*__idx)) _ValueType;
153 #ifndef _LIBCPP_NO_EXCEPTIONS
154     } catch (...) {
155         _VSTD::__destroy(__first, __idx);
156         throw;
157     }
158 #endif
159 
160     return __idx;
161 }
162 
163 template <class _ForwardIterator>
164 inline _LIBCPP_HIDE_FROM_ABI
165 void uninitialized_default_construct(_ForwardIterator __first, _ForwardIterator __last) {
166     using _ValueType = typename iterator_traits<_ForwardIterator>::value_type;
167     (void)_VSTD::__uninitialized_default_construct<_ValueType>(
168         _VSTD::move(__first), _VSTD::move(__last));
169 }
170 
171 // uninitialized_default_construct_n
172 
173 template <class _ValueType, class _ForwardIterator, class _Size>
174 inline _LIBCPP_HIDE_FROM_ABI
175 _ForwardIterator __uninitialized_default_construct_n(_ForwardIterator __first, _Size __n) {
176     auto __idx = __first;
177 #ifndef _LIBCPP_NO_EXCEPTIONS
178     try {
179 #endif
180     for (; __n > 0; ++__idx, (void) --__n)
181         ::new (_VSTD::__voidify(*__idx)) _ValueType;
182 #ifndef _LIBCPP_NO_EXCEPTIONS
183     } catch (...) {
184         _VSTD::__destroy(__first, __idx);
185         throw;
186     }
187 #endif
188 
189     return __idx;
190 }
191 
192 template <class _ForwardIterator, class _Size>
193 inline _LIBCPP_HIDE_FROM_ABI
194 _ForwardIterator uninitialized_default_construct_n(_ForwardIterator __first, _Size __n) {
195     using _ValueType = typename iterator_traits<_ForwardIterator>::value_type;
196     return _VSTD::__uninitialized_default_construct_n<_ValueType>(_VSTD::move(__first), __n);
197 }
198 
199 // uninitialized_value_construct
200 
201 template <class _ValueType, class _ForwardIterator, class _Sentinel>
202 inline _LIBCPP_HIDE_FROM_ABI
203 _ForwardIterator __uninitialized_value_construct(_ForwardIterator __first, _Sentinel __last) {
204     auto __idx = __first;
205 #ifndef _LIBCPP_NO_EXCEPTIONS
206     try {
207 #endif
208     for (; __idx != __last; ++__idx)
209         ::new (_VSTD::__voidify(*__idx)) _ValueType();
210 #ifndef _LIBCPP_NO_EXCEPTIONS
211     } catch (...) {
212         _VSTD::__destroy(__first, __idx);
213         throw;
214     }
215 #endif
216 
217     return __idx;
218 }
219 
220 template <class _ForwardIterator>
221 inline _LIBCPP_HIDE_FROM_ABI
222 void uninitialized_value_construct(_ForwardIterator __first, _ForwardIterator __last) {
223     using _ValueType = typename iterator_traits<_ForwardIterator>::value_type;
224     (void)_VSTD::__uninitialized_value_construct<_ValueType>(
225         _VSTD::move(__first), _VSTD::move(__last));
226 }
227 
228 // uninitialized_value_construct_n
229 
230 template <class _ValueType, class _ForwardIterator, class _Size>
231 inline _LIBCPP_HIDE_FROM_ABI
232 _ForwardIterator __uninitialized_value_construct_n(_ForwardIterator __first, _Size __n) {
233     auto __idx = __first;
234 #ifndef _LIBCPP_NO_EXCEPTIONS
235     try {
236 #endif
237     for (; __n > 0; ++__idx, (void) --__n)
238         ::new (_VSTD::__voidify(*__idx)) _ValueType();
239 #ifndef _LIBCPP_NO_EXCEPTIONS
240     } catch (...) {
241         _VSTD::__destroy(__first, __idx);
242         throw;
243     }
244 #endif
245 
246     return __idx;
247 }
248 
249 template <class _ForwardIterator, class _Size>
250 inline _LIBCPP_HIDE_FROM_ABI
251 _ForwardIterator uninitialized_value_construct_n(_ForwardIterator __first, _Size __n) {
252     using _ValueType = typename iterator_traits<_ForwardIterator>::value_type;
253     return __uninitialized_value_construct_n<_ValueType>(_VSTD::move(__first), __n);
254 }
255 
256 template <class _InputIt, class _ForwardIt>
257 inline _LIBCPP_INLINE_VISIBILITY
258 _ForwardIt uninitialized_move(_InputIt __first, _InputIt __last, _ForwardIt __first_res) {
259     using _Vt = typename iterator_traits<_ForwardIt>::value_type;
260     auto __idx = __first_res;
261 #ifndef _LIBCPP_NO_EXCEPTIONS
262     try {
263 #endif
264     for (; __first != __last; ++__idx, (void) ++__first)
265         ::new ((void*)_VSTD::addressof(*__idx)) _Vt(_VSTD::move(*__first));
266     return __idx;
267 #ifndef _LIBCPP_NO_EXCEPTIONS
268     } catch (...) {
269         _VSTD::destroy(__first_res, __idx);
270         throw;
271     }
272 #endif
273 }
274 
275 template <class _InputIt, class _Size, class _ForwardIt>
276 inline _LIBCPP_INLINE_VISIBILITY
277 pair<_InputIt, _ForwardIt>
278 uninitialized_move_n(_InputIt __first, _Size __n, _ForwardIt __first_res) {
279     using _Vt = typename iterator_traits<_ForwardIt>::value_type;
280     auto __idx = __first_res;
281 #ifndef _LIBCPP_NO_EXCEPTIONS
282     try {
283 #endif
284     for (; __n > 0; ++__idx, (void) ++__first, --__n)
285         ::new ((void*)_VSTD::addressof(*__idx)) _Vt(_VSTD::move(*__first));
286     return {__first, __idx};
287 #ifndef _LIBCPP_NO_EXCEPTIONS
288     } catch (...) {
289         _VSTD::destroy(__first_res, __idx);
290         throw;
291     }
292 #endif
293 }
294 
295 #endif // _LIBCPP_STD_VER > 14
296 
297 _LIBCPP_END_NAMESPACE_STD
298 
299 #endif // _LIBCPP___MEMORY_UNINITIALIZED_ALGORITHMS_H
300