OSDN Git Service

Daily bump.
[pf3gnuchains/gcc-fork.git] / libstdc++-v3 / include / bits / stl_uninitialized.h
1 // Raw memory manipulators -*- C++ -*-
2
3 // Copyright (C) 2001 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 2, 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 // You should have received a copy of the GNU General Public License along
17 // with this library; see the file COPYING.  If not, write to the Free
18 // Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
19 // USA.
20
21 // As a special exception, you may use this file as part of a free software
22 // library without restriction.  Specifically, if other files instantiate
23 // templates or use macros or inline functions from this file, or you compile
24 // this file and link it with other files to produce an executable, this
25 // file does not by itself cause the resulting executable to be covered by
26 // the GNU General Public License.  This exception does not however
27 // invalidate any other reasons why the executable file might be covered by
28 // the GNU General Public License.
29
30 /*
31  *
32  * Copyright (c) 1994
33  * Hewlett-Packard Company
34  *
35  * Permission to use, copy, modify, distribute and sell this software
36  * and its documentation for any purpose is hereby granted without fee,
37  * provided that the above copyright notice appear in all copies and
38  * that both that copyright notice and this permission notice appear
39  * in supporting documentation.  Hewlett-Packard Company makes no
40  * representations about the suitability of this software for any
41  * purpose.  It is provided "as is" without express or implied warranty.
42  *
43  *
44  * Copyright (c) 1996,1997
45  * Silicon Graphics Computer Systems, Inc.
46  *
47  * Permission to use, copy, modify, distribute and sell this software
48  * and its documentation for any purpose is hereby granted without fee,
49  * provided that the above copyright notice appear in all copies and
50  * that both that copyright notice and this permission notice appear
51  * in supporting documentation.  Silicon Graphics makes no
52  * representations about the suitability of this software for any
53  * purpose.  It is provided "as is" without express or implied warranty.
54  */
55
56 /** @file stl_uninitialized.h
57  *  This is an internal header file, included by other library headers.
58  *  You should not attempt to use it directly.
59  */
60
61 #ifndef _CPP_BITS_STL_UNINITIALIZED_H
62 #define _CPP_BITS_STL_UNINITIALIZED_H 1
63
64 #include <cstring>
65
66 namespace std
67 {
68
69   // uninitialized_copy
70
71   template<typename _InputIterator, typename _ForwardIterator>
72     inline _ForwardIterator 
73     __uninitialized_copy_aux(_InputIterator __first, _InputIterator __last,
74                              _ForwardIterator __result,
75                              __true_type)
76     { return copy(__first, __last, __result); }
77
78   template<typename _InputIterator, typename _ForwardIterator>
79     _ForwardIterator 
80     __uninitialized_copy_aux(_InputIterator __first, _InputIterator __last,
81                              _ForwardIterator __result,
82                              __false_type)
83     {
84       _ForwardIterator __cur = __result;
85       try {
86         for ( ; __first != __last; ++__first, ++__cur)
87           _Construct(&*__cur, *__first);
88         return __cur;
89       }
90       catch(...)
91         {
92           _Destroy(__result, __cur);
93           __throw_exception_again; 
94         }
95     }
96
97   /**
98    *  @brief Copies the range [first,last) into result.
99    *  @param  first  An input iterator.
100    *  @param  last   An input iterator.
101    *  @param  result An output iterator.
102    *  @return   result + (first - last)
103    *
104    *  Like copy(), but does not require an initialized output range.
105   */
106   template<typename _InputIterator, typename _ForwardIterator>
107     inline _ForwardIterator
108     uninitialized_copy(_InputIterator __first, _InputIterator __last, _ForwardIterator __result)
109     {
110       typedef typename iterator_traits<_ForwardIterator>::value_type _ValueType;
111       typedef typename __type_traits<_ValueType>::is_POD_type _Is_POD;
112       return __uninitialized_copy_aux(__first, __last, __result, _Is_POD());
113     }
114
115   inline char*
116   uninitialized_copy(const char* __first, const char* __last, char* __result)
117   {
118     memmove(__result, __first, __last - __first);
119     return __result + (__last - __first);
120   }
121
122   inline wchar_t* 
123   uninitialized_copy(const wchar_t* __first, const wchar_t* __last,
124                      wchar_t* __result)
125   {
126     memmove(__result, __first, sizeof(wchar_t) * (__last - __first));
127     return __result + (__last - __first);
128   }
129
130   // Valid if copy construction is equivalent to assignment, and if the
131   // destructor is trivial.
132   template<typename _ForwardIterator, typename _Tp>
133     inline void
134     __uninitialized_fill_aux(_ForwardIterator __first, _ForwardIterator __last, 
135                              const _Tp& __x, __true_type)
136     { fill(__first, __last, __x); }
137
138   template<typename _ForwardIterator, typename _Tp>
139     void
140     __uninitialized_fill_aux(_ForwardIterator __first, _ForwardIterator __last, 
141                              const _Tp& __x, __false_type)
142     {
143       _ForwardIterator __cur = __first;
144       try {
145         for ( ; __cur != __last; ++__cur)
146           _Construct(&*__cur, __x);
147       }
148       catch(...)
149         {
150           _Destroy(__first, __cur);
151           __throw_exception_again; 
152         }
153     }
154
155   /**
156    *  @brief Copies the value x into the range [first,last).
157    *  @param  first  An input iterator.
158    *  @param  last   An input iterator.
159    *  @param  x      The source value.
160    *  @return   Nothing.
161    *
162    *  Like fill(), but does not require an initialized output range.
163   */
164   template<typename _ForwardIterator, typename _Tp>
165     inline void
166     uninitialized_fill(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __x)
167     {
168       typedef typename iterator_traits<_ForwardIterator>::value_type _ValueType;
169       typedef typename __type_traits<_ValueType>::is_POD_type _Is_POD;
170       __uninitialized_fill_aux(__first, __last, __x, _Is_POD());
171     }
172
173   // Valid if copy construction is equivalent to assignment, and if the
174   //  destructor is trivial.
175   template<typename _ForwardIterator, typename _Size, typename _Tp>
176     inline _ForwardIterator
177     __uninitialized_fill_n_aux(_ForwardIterator __first, _Size __n,
178                                const _Tp& __x, __true_type)
179     {
180       return fill_n(__first, __n, __x);
181     }
182
183   template<typename _ForwardIterator, typename _Size, typename _Tp>
184     _ForwardIterator
185     __uninitialized_fill_n_aux(_ForwardIterator __first, _Size __n,
186                                const _Tp& __x, __false_type)
187     {
188       _ForwardIterator __cur = __first;
189       try {
190         for ( ; __n > 0; --__n, ++__cur)
191           _Construct(&*__cur, __x);
192         return __cur;
193       }
194       catch(...)
195         { 
196           _Destroy(__first, __cur);
197           __throw_exception_again; 
198         }
199     }
200
201   /**
202    *  @brief Copies the value x into the range [first,first+n).
203    *  @param  first  An input iterator.
204    *  @param  n      The number of copies to make.
205    *  @param  x      The source value.
206    *  @return   first+n
207    *
208    *  Like fill_n(), but does not require an initialized output range.
209   */
210   template<typename _ForwardIterator, typename _Size, typename _Tp>
211     inline _ForwardIterator 
212     uninitialized_fill_n(_ForwardIterator __first, _Size __n, const _Tp& __x)
213     {
214       typedef typename iterator_traits<_ForwardIterator>::value_type _ValueType;
215       typedef typename __type_traits<_ValueType>::is_POD_type _Is_POD;
216       return __uninitialized_fill_n_aux(__first, __n, __x, _Is_POD());
217     }
218
219   // Extensions: __uninitialized_copy_copy, __uninitialized_copy_fill, 
220   // __uninitialized_fill_copy.
221
222   // __uninitialized_copy_copy
223   // Copies [first1, last1) into [result, result + (last1 - first1)), and
224   //  copies [first2, last2) into
225   //  [result, result + (last1 - first1) + (last2 - first2)).
226
227   template<typename _InputIterator1, typename _InputIterator2, typename _ForwardIterator>
228     inline _ForwardIterator
229     __uninitialized_copy_copy(_InputIterator1 __first1, _InputIterator1 __last1,
230                               _InputIterator2 __first2, _InputIterator2 __last2,
231                               _ForwardIterator __result)
232     {
233       _ForwardIterator __mid = uninitialized_copy(__first1, __last1, __result);
234       try {
235         return uninitialized_copy(__first2, __last2, __mid);
236       }
237       catch(...)
238         { 
239           _Destroy(__result, __mid);
240           __throw_exception_again; 
241         }
242     }
243
244   // __uninitialized_fill_copy
245   // Fills [result, mid) with x, and copies [first, last) into
246   //  [mid, mid + (last - first)).
247   template<typename _ForwardIterator, typename _Tp, typename _InputIterator>
248     inline _ForwardIterator 
249     __uninitialized_fill_copy(_ForwardIterator __result, _ForwardIterator __mid,
250                               const _Tp& __x,
251                               _InputIterator __first, _InputIterator __last)
252     {
253       uninitialized_fill(__result, __mid, __x);
254       try {
255         return uninitialized_copy(__first, __last, __mid);
256       }
257       catch(...)
258         {
259           _Destroy(__result, __mid);
260           __throw_exception_again; 
261         }
262     }
263
264   // __uninitialized_copy_fill
265   // Copies [first1, last1) into [first2, first2 + (last1 - first1)), and
266   //  fills [first2 + (last1 - first1), last2) with x.
267   template<typename _InputIterator, typename _ForwardIterator, typename _Tp>
268     inline void
269     __uninitialized_copy_fill(_InputIterator __first1, _InputIterator __last1,
270                               _ForwardIterator __first2, _ForwardIterator __last2,
271                               const _Tp& __x)
272     {
273       _ForwardIterator __mid2 = uninitialized_copy(__first1, __last1, __first2);
274       try {
275         uninitialized_fill(__mid2, __last2, __x);
276       }
277       catch(...)
278         {
279           _Destroy(__first2, __mid2);
280           __throw_exception_again; 
281         }
282     }
283
284 } // namespace std
285
286 #endif /* _CPP_BITS_STL_UNINITIALIZED_H */