]> git.ipfire.org Git - thirdparty/gcc.git/blame - libstdc++-v3/include/bits/stl_uninitialized.h
stl_construct.h (_Destroy(_ForwardIterator, _ForwardIterator __last, _Allocator)...
[thirdparty/gcc.git] / libstdc++-v3 / include / bits / stl_uninitialized.h
CommitLineData
42526146
PE
1// Raw memory manipulators -*- C++ -*-
2
263e3c33 3// Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007
8072ddb0 4// Free Software Foundation, Inc.
42526146
PE
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
83f51799 19// Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
42526146
PE
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
725dc051
BK
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
729e3d3f
PE
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.
725dc051
BK
60 */
61
3d7c150e
BK
62#ifndef _STL_UNINITIALIZED_H
63#define _STL_UNINITIALIZED_H 1
725dc051 64
3cbc7af0
BK
65_GLIBCXX_BEGIN_NAMESPACE(std)
66
08addde6 67 template<typename _InputIterator, typename _ForwardIterator>
ff2ea587
PC
68 _ForwardIterator
69 __uninitialized_copy_aux(_InputIterator __first,
70 _InputIterator __last,
71 _ForwardIterator __result)
02d92e3b 72 {
08addde6 73 _ForwardIterator __cur = __result;
ed6814f7 74 try
917a9fd4 75 {
368b7a30 76 for (; __first != __last; ++__first, ++__cur)
917a9fd4
SW
77 std::_Construct(&*__cur, *__first);
78 return __cur;
79 }
322821b9
BK
80 catch(...)
81 {
9dd90ac6 82 std::_Destroy(__result, __cur);
ed6814f7 83 __throw_exception_again;
322821b9 84 }
02d92e3b
SW
85 }
86
82b61df5
PE
87 /**
88 * @brief Copies the range [first,last) into result.
89 * @param first An input iterator.
90 * @param last An input iterator.
91 * @param result An output iterator.
92 * @return result + (first - last)
93 *
94 * Like copy(), but does not require an initialized output range.
95 */
08addde6
PE
96 template<typename _InputIterator, typename _ForwardIterator>
97 inline _ForwardIterator
ed6814f7 98 uninitialized_copy(_InputIterator __first, _InputIterator __last,
917a9fd4 99 _ForwardIterator __result)
02d92e3b 100 {
ff2ea587
PC
101 typedef typename iterator_traits<_ForwardIterator>::value_type
102 _ValueType;
103 if (__is_pod(_ValueType))
104 return std::copy(__first, __last, __result);
105 else
106 return std::__uninitialized_copy_aux(__first, __last, __result);
02d92e3b
SW
107 }
108
02d92e3b 109
08addde6 110 template<typename _ForwardIterator, typename _Tp>
02d92e3b 111 void
ff2ea587
PC
112 __uninitialized_fill_aux(_ForwardIterator __first,
113 _ForwardIterator __last,
114 const _Tp& __x)
02d92e3b 115 {
08addde6 116 _ForwardIterator __cur = __first;
15d72060
PC
117 try
118 {
368b7a30 119 for (; __cur != __last; ++__cur)
15d72060
PC
120 std::_Construct(&*__cur, __x);
121 }
322821b9
BK
122 catch(...)
123 {
9dd90ac6 124 std::_Destroy(__first, __cur);
ed6814f7 125 __throw_exception_again;
322821b9 126 }
02d92e3b
SW
127 }
128
82b61df5
PE
129 /**
130 * @brief Copies the value x into the range [first,last).
131 * @param first An input iterator.
132 * @param last An input iterator.
133 * @param x The source value.
134 * @return Nothing.
135 *
136 * Like fill(), but does not require an initialized output range.
137 */
08addde6 138 template<typename _ForwardIterator, typename _Tp>
02d92e3b 139 inline void
ed6814f7 140 uninitialized_fill(_ForwardIterator __first, _ForwardIterator __last,
917a9fd4 141 const _Tp& __x)
02d92e3b 142 {
ff2ea587
PC
143 typedef typename iterator_traits<_ForwardIterator>::value_type
144 _ValueType;
145 if (__is_pod(_ValueType))
146 std::fill(__first, __last, __x);
147 else
148 std::__uninitialized_fill_aux(__first, __last, __x);
02d92e3b
SW
149 }
150
02d92e3b 151
08addde6 152 template<typename _ForwardIterator, typename _Size, typename _Tp>
1985f1cd 153 void
08addde6 154 __uninitialized_fill_n_aux(_ForwardIterator __first, _Size __n,
ff2ea587 155 const _Tp& __x)
02d92e3b 156 {
08addde6 157 _ForwardIterator __cur = __first;
ed6814f7 158 try
917a9fd4 159 {
368b7a30 160 for (; __n > 0; --__n, ++__cur)
917a9fd4 161 std::_Construct(&*__cur, __x);
917a9fd4 162 }
322821b9 163 catch(...)
ed6814f7 164 {
9dd90ac6 165 std::_Destroy(__first, __cur);
ed6814f7 166 __throw_exception_again;
322821b9 167 }
02d92e3b
SW
168 }
169
82b61df5
PE
170 /**
171 * @brief Copies the value x into the range [first,first+n).
172 * @param first An input iterator.
173 * @param n The number of copies to make.
174 * @param x The source value.
368b7a30 175 * @return Nothing.
82b61df5
PE
176 *
177 * Like fill_n(), but does not require an initialized output range.
178 */
08addde6 179 template<typename _ForwardIterator, typename _Size, typename _Tp>
368b7a30 180 inline void
08addde6 181 uninitialized_fill_n(_ForwardIterator __first, _Size __n, const _Tp& __x)
02d92e3b 182 {
ff2ea587
PC
183 typedef typename iterator_traits<_ForwardIterator>::value_type
184 _ValueType;
185 if (__is_pod(_ValueType))
186 std::fill_n(__first, __n, __x);
187 else
188 std::__uninitialized_fill_n_aux(__first, __n, __x);
02d92e3b
SW
189 }
190
1985f1cd
MA
191 // Extensions: versions of uninitialized_copy, uninitialized_fill,
192 // and uninitialized_fill_n that take an allocator parameter.
193 // We dispatch back to the standard versions when we're given the
194 // default allocator. For nondefault allocators we do not use
195 // any of the POD optimizations.
196
197 template<typename _InputIterator, typename _ForwardIterator,
198 typename _Allocator>
199 _ForwardIterator
200 __uninitialized_copy_a(_InputIterator __first, _InputIterator __last,
c531371e 201 _ForwardIterator __result, _Allocator& __alloc)
1985f1cd
MA
202 {
203 _ForwardIterator __cur = __result;
204 try
205 {
206 for (; __first != __last; ++__first, ++__cur)
207 __alloc.construct(&*__cur, *__first);
208 return __cur;
209 }
210 catch(...)
211 {
212 std::_Destroy(__result, __cur, __alloc);
213 __throw_exception_again;
214 }
215 }
216
217 template<typename _InputIterator, typename _ForwardIterator, typename _Tp>
218 inline _ForwardIterator
219 __uninitialized_copy_a(_InputIterator __first, _InputIterator __last,
c531371e 220 _ForwardIterator __result, allocator<_Tp>&)
8072ddb0 221 { return std::uninitialized_copy(__first, __last, __result); }
1985f1cd
MA
222
223 template<typename _ForwardIterator, typename _Tp, typename _Allocator>
224 void
225 __uninitialized_fill_a(_ForwardIterator __first, _ForwardIterator __last,
c531371e 226 const _Tp& __x, _Allocator& __alloc)
1985f1cd
MA
227 {
228 _ForwardIterator __cur = __first;
229 try
230 {
231 for (; __cur != __last; ++__cur)
232 __alloc.construct(&*__cur, __x);
233 }
234 catch(...)
235 {
236 std::_Destroy(__first, __cur, __alloc);
237 __throw_exception_again;
238 }
239 }
240
241 template<typename _ForwardIterator, typename _Tp, typename _Tp2>
242 inline void
243 __uninitialized_fill_a(_ForwardIterator __first, _ForwardIterator __last,
c531371e 244 const _Tp& __x, allocator<_Tp2>&)
8072ddb0 245 { std::uninitialized_fill(__first, __last, __x); }
1985f1cd
MA
246
247 template<typename _ForwardIterator, typename _Size, typename _Tp,
248 typename _Allocator>
249 void
250 __uninitialized_fill_n_a(_ForwardIterator __first, _Size __n,
c531371e 251 const _Tp& __x, _Allocator& __alloc)
1985f1cd
MA
252 {
253 _ForwardIterator __cur = __first;
254 try
255 {
256 for (; __n > 0; --__n, ++__cur)
257 __alloc.construct(&*__cur, __x);
258 }
259 catch(...)
260 {
261 std::_Destroy(__first, __cur, __alloc);
262 __throw_exception_again;
263 }
264 }
265
266 template<typename _ForwardIterator, typename _Size, typename _Tp,
267 typename _Tp2>
8072ddb0 268 inline void
1985f1cd 269 __uninitialized_fill_n_a(_ForwardIterator __first, _Size __n,
c531371e 270 const _Tp& __x, allocator<_Tp2>&)
8072ddb0 271 { std::uninitialized_fill_n(__first, __n, __x); }
1985f1cd
MA
272
273
ed6814f7 274 // Extensions: __uninitialized_copy_copy, __uninitialized_copy_fill,
1985f1cd
MA
275 // __uninitialized_fill_copy. All of these algorithms take a user-
276 // supplied allocator, which is used for construction and destruction.
02d92e3b
SW
277
278 // __uninitialized_copy_copy
279 // Copies [first1, last1) into [result, result + (last1 - first1)), and
280 // copies [first2, last2) into
281 // [result, result + (last1 - first1) + (last2 - first2)).
282
ed6814f7 283 template<typename _InputIterator1, typename _InputIterator2,
1985f1cd 284 typename _ForwardIterator, typename _Allocator>
08addde6 285 inline _ForwardIterator
ed6814f7 286 __uninitialized_copy_copy(_InputIterator1 __first1,
917a9fd4 287 _InputIterator1 __last1,
ed6814f7 288 _InputIterator2 __first2,
917a9fd4 289 _InputIterator2 __last2,
1985f1cd 290 _ForwardIterator __result,
c531371e 291 _Allocator& __alloc)
02d92e3b 292 {
1985f1cd
MA
293 _ForwardIterator __mid = std::__uninitialized_copy_a(__first1, __last1,
294 __result,
295 __alloc);
15d72060
PC
296 try
297 {
1985f1cd 298 return std::__uninitialized_copy_a(__first2, __last2, __mid, __alloc);
15d72060 299 }
322821b9 300 catch(...)
ed6814f7 301 {
1985f1cd 302 std::_Destroy(__result, __mid, __alloc);
ed6814f7 303 __throw_exception_again;
322821b9 304 }
02d92e3b
SW
305 }
306
307 // __uninitialized_fill_copy
308 // Fills [result, mid) with x, and copies [first, last) into
309 // [mid, mid + (last - first)).
1985f1cd
MA
310 template<typename _ForwardIterator, typename _Tp, typename _InputIterator,
311 typename _Allocator>
ed6814f7 312 inline _ForwardIterator
08addde6 313 __uninitialized_fill_copy(_ForwardIterator __result, _ForwardIterator __mid,
15d72060 314 const _Tp& __x, _InputIterator __first,
c531371e 315 _InputIterator __last, _Allocator& __alloc)
02d92e3b 316 {
1985f1cd 317 std::__uninitialized_fill_a(__result, __mid, __x, __alloc);
15d72060
PC
318 try
319 {
1985f1cd 320 return std::__uninitialized_copy_a(__first, __last, __mid, __alloc);
15d72060 321 }
322821b9
BK
322 catch(...)
323 {
1985f1cd 324 std::_Destroy(__result, __mid, __alloc);
ed6814f7 325 __throw_exception_again;
322821b9 326 }
02d92e3b
SW
327 }
328
329 // __uninitialized_copy_fill
330 // Copies [first1, last1) into [first2, first2 + (last1 - first1)), and
331 // fills [first2 + (last1 - first1), last2) with x.
1985f1cd
MA
332 template<typename _InputIterator, typename _ForwardIterator, typename _Tp,
333 typename _Allocator>
02d92e3b 334 inline void
08addde6 335 __uninitialized_copy_fill(_InputIterator __first1, _InputIterator __last1,
15d72060 336 _ForwardIterator __first2,
1985f1cd 337 _ForwardIterator __last2, const _Tp& __x,
c531371e 338 _Allocator& __alloc)
02d92e3b 339 {
1985f1cd
MA
340 _ForwardIterator __mid2 = std::__uninitialized_copy_a(__first1, __last1,
341 __first2,
342 __alloc);
ed6814f7 343 try
917a9fd4 344 {
1985f1cd 345 std::__uninitialized_fill_a(__mid2, __last2, __x, __alloc);
917a9fd4 346 }
322821b9
BK
347 catch(...)
348 {
1985f1cd 349 std::_Destroy(__first2, __mid2, __alloc);
ed6814f7 350 __throw_exception_again;
322821b9 351 }
02d92e3b 352 }
725dc051 353
3cbc7af0 354_GLIBCXX_END_NAMESPACE
725dc051 355
3d7c150e 356#endif /* _STL_UNINITIALIZED_H */