]> git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/include/bits/stl_uninitialized.h
PR c++/32158 (libstdc++ bits)
[thirdparty/gcc.git] / libstdc++-v3 / include / bits / stl_uninitialized.h
1 // Raw memory manipulators -*- C++ -*-
2
3 // Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007
4 // Free Software Foundation, Inc.
5 //
6 // This file is part of the GNU ISO C++ Library. This library is free
7 // software; you can redistribute it and/or modify it under the
8 // terms of the GNU General Public License as published by the
9 // Free Software Foundation; either version 2, or (at your option)
10 // any later version.
11
12 // This library is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License for more details.
16
17 // You should have received a copy of the GNU General Public License along
18 // with this library; see the file COPYING. If not, write to the Free
19 // Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
20 // USA.
21
22 // As a special exception, you may use this file as part of a free software
23 // library without restriction. Specifically, if other files instantiate
24 // templates or use macros or inline functions from this file, or you compile
25 // this file and link it with other files to produce an executable, this
26 // file does not by itself cause the resulting executable to be covered by
27 // the GNU General Public License. This exception does not however
28 // invalidate any other reasons why the executable file might be covered by
29 // the GNU General Public License.
30
31 /*
32 *
33 * Copyright (c) 1994
34 * Hewlett-Packard Company
35 *
36 * Permission to use, copy, modify, distribute and sell this software
37 * and its documentation for any purpose is hereby granted without fee,
38 * provided that the above copyright notice appear in all copies and
39 * that both that copyright notice and this permission notice appear
40 * in supporting documentation. Hewlett-Packard Company makes no
41 * representations about the suitability of this software for any
42 * purpose. It is provided "as is" without express or implied warranty.
43 *
44 *
45 * Copyright (c) 1996,1997
46 * Silicon Graphics Computer Systems, Inc.
47 *
48 * Permission to use, copy, modify, distribute and sell this software
49 * and its documentation for any purpose is hereby granted without fee,
50 * provided that the above copyright notice appear in all copies and
51 * that both that copyright notice and this permission notice appear
52 * in supporting documentation. Silicon Graphics makes no
53 * representations about the suitability of this software for any
54 * purpose. It is provided "as is" without express or implied warranty.
55 */
56
57 /** @file stl_uninitialized.h
58 * This is an internal header file, included by other library headers.
59 * You should not attempt to use it directly.
60 */
61
62 #ifndef _STL_UNINITIALIZED_H
63 #define _STL_UNINITIALIZED_H 1
64
65 _GLIBCXX_BEGIN_NAMESPACE(std)
66
67 template<bool>
68 struct __uninitialized_copy
69 {
70 template<typename _InputIterator, typename _ForwardIterator>
71 static _ForwardIterator
72 uninitialized_copy(_InputIterator __first, _InputIterator __last,
73 _ForwardIterator __result)
74 {
75 _ForwardIterator __cur = __result;
76 try
77 {
78 for (; __first != __last; ++__first, ++__cur)
79 std::_Construct(&*__cur, *__first);
80 return __cur;
81 }
82 catch(...)
83 {
84 std::_Destroy(__result, __cur);
85 __throw_exception_again;
86 }
87 }
88 };
89
90 template<>
91 struct __uninitialized_copy<true>
92 {
93 template<typename _InputIterator, typename _ForwardIterator>
94 static _ForwardIterator
95 uninitialized_copy(_InputIterator __first, _InputIterator __last,
96 _ForwardIterator __result)
97 { return std::copy(__first, __last, __result); }
98 };
99
100 /**
101 * @brief Copies the range [first,last) into result.
102 * @param first An input iterator.
103 * @param last An input iterator.
104 * @param result An output iterator.
105 * @return result + (first - last)
106 *
107 * Like copy(), but does not require an initialized output range.
108 */
109 template<typename _InputIterator, typename _ForwardIterator>
110 inline _ForwardIterator
111 uninitialized_copy(_InputIterator __first, _InputIterator __last,
112 _ForwardIterator __result)
113 {
114 typedef typename iterator_traits<_InputIterator>::value_type
115 _ValueType1;
116 typedef typename iterator_traits<_ForwardIterator>::value_type
117 _ValueType2;
118
119 return std::__uninitialized_copy<(__is_pod(_ValueType1)
120 && __is_pod(_ValueType2))>::
121 uninitialized_copy(__first, __last, __result);
122 }
123
124
125 template<bool>
126 struct __uninitialized_fill
127 {
128 template<typename _ForwardIterator, typename _Tp>
129 static void
130 uninitialized_fill(_ForwardIterator __first,
131 _ForwardIterator __last, const _Tp& __x)
132 {
133 _ForwardIterator __cur = __first;
134 try
135 {
136 for (; __cur != __last; ++__cur)
137 std::_Construct(&*__cur, __x);
138 }
139 catch(...)
140 {
141 std::_Destroy(__first, __cur);
142 __throw_exception_again;
143 }
144 }
145 };
146
147 template<>
148 struct __uninitialized_fill<true>
149 {
150 template<typename _ForwardIterator, typename _Tp>
151 static void
152 uninitialized_fill(_ForwardIterator __first,
153 _ForwardIterator __last, const _Tp& __x)
154 { std::fill(__first, __last, __x); }
155 };
156
157 /**
158 * @brief Copies the value x into the range [first,last).
159 * @param first An input iterator.
160 * @param last An input iterator.
161 * @param x The source value.
162 * @return Nothing.
163 *
164 * Like fill(), but does not require an initialized output range.
165 */
166 template<typename _ForwardIterator, typename _Tp>
167 inline void
168 uninitialized_fill(_ForwardIterator __first, _ForwardIterator __last,
169 const _Tp& __x)
170 {
171 typedef typename iterator_traits<_ForwardIterator>::value_type
172 _ValueType;
173
174 std::__uninitialized_fill<__is_pod(_ValueType)>::
175 uninitialized_fill(__first, __last, __x);
176 }
177
178
179 template<bool>
180 struct __uninitialized_fill_n
181 {
182 template<typename _ForwardIterator, typename _Size, typename _Tp>
183 static void
184 uninitialized_fill_n(_ForwardIterator __first, _Size __n,
185 const _Tp& __x)
186 {
187 _ForwardIterator __cur = __first;
188 try
189 {
190 for (; __n > 0; --__n, ++__cur)
191 std::_Construct(&*__cur, __x);
192 }
193 catch(...)
194 {
195 std::_Destroy(__first, __cur);
196 __throw_exception_again;
197 }
198 }
199 };
200
201 template<>
202 struct __uninitialized_fill_n<true>
203 {
204 template<typename _ForwardIterator, typename _Size, typename _Tp>
205 static void
206 uninitialized_fill_n(_ForwardIterator __first, _Size __n,
207 const _Tp& __x)
208 { std::fill_n(__first, __n, __x); }
209 };
210
211 /**
212 * @brief Copies the value x into the range [first,first+n).
213 * @param first An input iterator.
214 * @param n The number of copies to make.
215 * @param x The source value.
216 * @return Nothing.
217 *
218 * Like fill_n(), but does not require an initialized output range.
219 */
220 template<typename _ForwardIterator, typename _Size, typename _Tp>
221 inline void
222 uninitialized_fill_n(_ForwardIterator __first, _Size __n, const _Tp& __x)
223 {
224 typedef typename iterator_traits<_ForwardIterator>::value_type
225 _ValueType;
226
227 std::__uninitialized_fill_n<__is_pod(_ValueType)>::
228 uninitialized_fill_n(__first, __n, __x);
229 }
230
231 // Extensions: versions of uninitialized_copy, uninitialized_fill,
232 // and uninitialized_fill_n that take an allocator parameter.
233 // We dispatch back to the standard versions when we're given the
234 // default allocator. For nondefault allocators we do not use
235 // any of the POD optimizations.
236
237 template<typename _InputIterator, typename _ForwardIterator,
238 typename _Allocator>
239 _ForwardIterator
240 __uninitialized_copy_a(_InputIterator __first, _InputIterator __last,
241 _ForwardIterator __result, _Allocator& __alloc)
242 {
243 _ForwardIterator __cur = __result;
244 try
245 {
246 for (; __first != __last; ++__first, ++__cur)
247 __alloc.construct(&*__cur, *__first);
248 return __cur;
249 }
250 catch(...)
251 {
252 std::_Destroy(__result, __cur, __alloc);
253 __throw_exception_again;
254 }
255 }
256
257 template<typename _InputIterator, typename _ForwardIterator, typename _Tp>
258 inline _ForwardIterator
259 __uninitialized_copy_a(_InputIterator __first, _InputIterator __last,
260 _ForwardIterator __result, allocator<_Tp>&)
261 { return std::uninitialized_copy(__first, __last, __result); }
262
263 template<typename _ForwardIterator, typename _Tp, typename _Allocator>
264 void
265 __uninitialized_fill_a(_ForwardIterator __first, _ForwardIterator __last,
266 const _Tp& __x, _Allocator& __alloc)
267 {
268 _ForwardIterator __cur = __first;
269 try
270 {
271 for (; __cur != __last; ++__cur)
272 __alloc.construct(&*__cur, __x);
273 }
274 catch(...)
275 {
276 std::_Destroy(__first, __cur, __alloc);
277 __throw_exception_again;
278 }
279 }
280
281 template<typename _ForwardIterator, typename _Tp, typename _Tp2>
282 inline void
283 __uninitialized_fill_a(_ForwardIterator __first, _ForwardIterator __last,
284 const _Tp& __x, allocator<_Tp2>&)
285 { std::uninitialized_fill(__first, __last, __x); }
286
287 template<typename _ForwardIterator, typename _Size, typename _Tp,
288 typename _Allocator>
289 void
290 __uninitialized_fill_n_a(_ForwardIterator __first, _Size __n,
291 const _Tp& __x, _Allocator& __alloc)
292 {
293 _ForwardIterator __cur = __first;
294 try
295 {
296 for (; __n > 0; --__n, ++__cur)
297 __alloc.construct(&*__cur, __x);
298 }
299 catch(...)
300 {
301 std::_Destroy(__first, __cur, __alloc);
302 __throw_exception_again;
303 }
304 }
305
306 template<typename _ForwardIterator, typename _Size, typename _Tp,
307 typename _Tp2>
308 inline void
309 __uninitialized_fill_n_a(_ForwardIterator __first, _Size __n,
310 const _Tp& __x, allocator<_Tp2>&)
311 { std::uninitialized_fill_n(__first, __n, __x); }
312
313
314 // Extensions: __uninitialized_copy_copy, __uninitialized_copy_fill,
315 // __uninitialized_fill_copy. All of these algorithms take a user-
316 // supplied allocator, which is used for construction and destruction.
317
318 // __uninitialized_copy_copy
319 // Copies [first1, last1) into [result, result + (last1 - first1)), and
320 // copies [first2, last2) into
321 // [result, result + (last1 - first1) + (last2 - first2)).
322
323 template<typename _InputIterator1, typename _InputIterator2,
324 typename _ForwardIterator, typename _Allocator>
325 inline _ForwardIterator
326 __uninitialized_copy_copy(_InputIterator1 __first1,
327 _InputIterator1 __last1,
328 _InputIterator2 __first2,
329 _InputIterator2 __last2,
330 _ForwardIterator __result,
331 _Allocator& __alloc)
332 {
333 _ForwardIterator __mid = std::__uninitialized_copy_a(__first1, __last1,
334 __result,
335 __alloc);
336 try
337 {
338 return std::__uninitialized_copy_a(__first2, __last2, __mid, __alloc);
339 }
340 catch(...)
341 {
342 std::_Destroy(__result, __mid, __alloc);
343 __throw_exception_again;
344 }
345 }
346
347 // __uninitialized_fill_copy
348 // Fills [result, mid) with x, and copies [first, last) into
349 // [mid, mid + (last - first)).
350 template<typename _ForwardIterator, typename _Tp, typename _InputIterator,
351 typename _Allocator>
352 inline _ForwardIterator
353 __uninitialized_fill_copy(_ForwardIterator __result, _ForwardIterator __mid,
354 const _Tp& __x, _InputIterator __first,
355 _InputIterator __last, _Allocator& __alloc)
356 {
357 std::__uninitialized_fill_a(__result, __mid, __x, __alloc);
358 try
359 {
360 return std::__uninitialized_copy_a(__first, __last, __mid, __alloc);
361 }
362 catch(...)
363 {
364 std::_Destroy(__result, __mid, __alloc);
365 __throw_exception_again;
366 }
367 }
368
369 // __uninitialized_copy_fill
370 // Copies [first1, last1) into [first2, first2 + (last1 - first1)), and
371 // fills [first2 + (last1 - first1), last2) with x.
372 template<typename _InputIterator, typename _ForwardIterator, typename _Tp,
373 typename _Allocator>
374 inline void
375 __uninitialized_copy_fill(_InputIterator __first1, _InputIterator __last1,
376 _ForwardIterator __first2,
377 _ForwardIterator __last2, const _Tp& __x,
378 _Allocator& __alloc)
379 {
380 _ForwardIterator __mid2 = std::__uninitialized_copy_a(__first1, __last1,
381 __first2,
382 __alloc);
383 try
384 {
385 std::__uninitialized_fill_a(__mid2, __last2, __x, __alloc);
386 }
387 catch(...)
388 {
389 std::_Destroy(__first2, __mid2, __alloc);
390 __throw_exception_again;
391 }
392 }
393
394 _GLIBCXX_END_NAMESPACE
395
396 #endif /* _STL_UNINITIALIZED_H */