stl_uninitialized.h

Go to the documentation of this file.
00001 // Raw memory manipulators -*- C++ -*-
00002 
00003 // Copyright (C) 2001, 2004, 2005 Free Software Foundation, Inc.
00004 //
00005 // This file is part of the GNU ISO C++ Library.  This library is free
00006 // software; you can redistribute it and/or modify it under the
00007 // terms of the GNU General Public License as published by the
00008 // Free Software Foundation; either version 2, or (at your option)
00009 // any later version.
00010 
00011 // This library is distributed in the hope that it will be useful,
00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014 // GNU General Public License for more details.
00015 
00016 // You should have received a copy of the GNU General Public License along
00017 // with this library; see the file COPYING.  If not, write to the Free
00018 // Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
00019 // USA.
00020 
00021 // As a special exception, you may use this file as part of a free software
00022 // library without restriction.  Specifically, if other files instantiate
00023 // templates or use macros or inline functions from this file, or you compile
00024 // this file and link it with other files to produce an executable, this
00025 // file does not by itself cause the resulting executable to be covered by
00026 // the GNU General Public License.  This exception does not however
00027 // invalidate any other reasons why the executable file might be covered by
00028 // the GNU General Public License.
00029 
00030 /*
00031  *
00032  * Copyright (c) 1994
00033  * Hewlett-Packard Company
00034  *
00035  * Permission to use, copy, modify, distribute and sell this software
00036  * and its documentation for any purpose is hereby granted without fee,
00037  * provided that the above copyright notice appear in all copies and
00038  * that both that copyright notice and this permission notice appear
00039  * in supporting documentation.  Hewlett-Packard Company makes no
00040  * representations about the suitability of this software for any
00041  * purpose.  It is provided "as is" without express or implied warranty.
00042  *
00043  *
00044  * Copyright (c) 1996,1997
00045  * Silicon Graphics Computer Systems, Inc.
00046  *
00047  * Permission to use, copy, modify, distribute and sell this software
00048  * and its documentation for any purpose is hereby granted without fee,
00049  * provided that the above copyright notice appear in all copies and
00050  * that both that copyright notice and this permission notice appear
00051  * in supporting documentation.  Silicon Graphics makes no
00052  * representations about the suitability of this software for any
00053  * purpose.  It is provided "as is" without express or implied warranty.
00054  */
00055 
00061 #ifndef _STL_UNINITIALIZED_H
00062 #define _STL_UNINITIALIZED_H 1
00063 
00064 #include <cstring>
00065 
00066 namespace std
00067 {
00068   // uninitialized_copy
00069   template<typename _InputIterator, typename _ForwardIterator>
00070     inline _ForwardIterator
00071     __uninitialized_copy_aux(_InputIterator __first, _InputIterator __last,
00072                  _ForwardIterator __result,
00073                  __true_type)
00074     { return std::copy(__first, __last, __result); }
00075 
00076   template<typename _InputIterator, typename _ForwardIterator>
00077     inline _ForwardIterator
00078     __uninitialized_copy_aux(_InputIterator __first, _InputIterator __last,
00079                  _ForwardIterator __result,
00080                  __false_type)
00081     {
00082       _ForwardIterator __cur = __result;
00083       try
00084     {
00085       for (; __first != __last; ++__first, ++__cur)
00086         std::_Construct(&*__cur, *__first);
00087       return __cur;
00088     }
00089       catch(...)
00090     {
00091       std::_Destroy(__result, __cur);
00092       __throw_exception_again;
00093     }
00094     }
00095 
00105   template<typename _InputIterator, typename _ForwardIterator>
00106     inline _ForwardIterator
00107     uninitialized_copy(_InputIterator __first, _InputIterator __last,
00108                _ForwardIterator __result)
00109     {
00110       typedef typename iterator_traits<_ForwardIterator>::value_type _ValueType;
00111       typedef typename std::__is_scalar<_ValueType>::__type _Is_POD;
00112       return std::__uninitialized_copy_aux(__first, __last, __result,
00113                        _Is_POD());
00114     }
00115 
00116   inline char*
00117   uninitialized_copy(const char* __first, const char* __last, char* __result)
00118   {
00119     std::memmove(__result, __first, __last - __first);
00120     return __result + (__last - __first);
00121   }
00122 
00123   inline wchar_t*
00124   uninitialized_copy(const wchar_t* __first, const wchar_t* __last,
00125              wchar_t* __result)
00126   {
00127     std::memmove(__result, __first, sizeof(wchar_t) * (__last - __first));
00128     return __result + (__last - __first);
00129   }
00130 
00131   // Valid if copy construction is equivalent to assignment, and if the
00132   // destructor is trivial.
00133   template<typename _ForwardIterator, typename _Tp>
00134     inline void
00135     __uninitialized_fill_aux(_ForwardIterator __first,
00136                  _ForwardIterator __last,
00137                  const _Tp& __x, __true_type)
00138     { std::fill(__first, __last, __x); }
00139 
00140   template<typename _ForwardIterator, typename _Tp>
00141     void
00142     __uninitialized_fill_aux(_ForwardIterator __first, _ForwardIterator __last,
00143                  const _Tp& __x, __false_type)
00144     {
00145       _ForwardIterator __cur = __first;
00146       try
00147     {
00148       for (; __cur != __last; ++__cur)
00149         std::_Construct(&*__cur, __x);
00150     }
00151       catch(...)
00152     {
00153       std::_Destroy(__first, __cur);
00154       __throw_exception_again;
00155     }
00156     }
00157 
00167   template<typename _ForwardIterator, typename _Tp>
00168     inline void
00169     uninitialized_fill(_ForwardIterator __first, _ForwardIterator __last,
00170                const _Tp& __x)
00171     {
00172       typedef typename iterator_traits<_ForwardIterator>::value_type _ValueType;
00173       typedef typename std::__is_scalar<_ValueType>::__type _Is_POD;
00174       std::__uninitialized_fill_aux(__first, __last, __x, _Is_POD());
00175     }
00176 
00177   // Valid if copy construction is equivalent to assignment, and if the
00178   //  destructor is trivial.
00179   template<typename _ForwardIterator, typename _Size, typename _Tp>
00180     inline void
00181     __uninitialized_fill_n_aux(_ForwardIterator __first, _Size __n,
00182                    const _Tp& __x, __true_type)
00183     { std::fill_n(__first, __n, __x); }
00184 
00185   template<typename _ForwardIterator, typename _Size, typename _Tp>
00186     void
00187     __uninitialized_fill_n_aux(_ForwardIterator __first, _Size __n,
00188                    const _Tp& __x, __false_type)
00189     {
00190       _ForwardIterator __cur = __first;
00191       try
00192     {
00193       for (; __n > 0; --__n, ++__cur)
00194         std::_Construct(&*__cur, __x);
00195     }
00196       catch(...)
00197     {
00198       std::_Destroy(__first, __cur);
00199       __throw_exception_again;
00200     }
00201     }
00202 
00212   template<typename _ForwardIterator, typename _Size, typename _Tp>
00213     inline void
00214     uninitialized_fill_n(_ForwardIterator __first, _Size __n, const _Tp& __x)
00215     {
00216       typedef typename iterator_traits<_ForwardIterator>::value_type _ValueType;
00217       typedef typename std::__is_scalar<_ValueType>::__type _Is_POD;
00218       std::__uninitialized_fill_n_aux(__first, __n, __x, _Is_POD());
00219     }
00220 
00221   // Extensions: versions of uninitialized_copy, uninitialized_fill,
00222   //  and uninitialized_fill_n that take an allocator parameter.
00223   //  We dispatch back to the standard versions when we're given the
00224   //  default allocator.  For nondefault allocators we do not use 
00225   //  any of the POD optimizations.
00226 
00227   template<typename _InputIterator, typename _ForwardIterator,
00228        typename _Allocator>
00229     _ForwardIterator
00230     __uninitialized_copy_a(_InputIterator __first, _InputIterator __last,
00231                _ForwardIterator __result,
00232                _Allocator __alloc)
00233     {
00234       _ForwardIterator __cur = __result;
00235       try
00236     {
00237       for (; __first != __last; ++__first, ++__cur)
00238         __alloc.construct(&*__cur, *__first);
00239       return __cur;
00240     }
00241       catch(...)
00242     {
00243       std::_Destroy(__result, __cur, __alloc);
00244       __throw_exception_again;
00245     }
00246     }
00247 
00248   template<typename _InputIterator, typename _ForwardIterator, typename _Tp>
00249     inline _ForwardIterator
00250     __uninitialized_copy_a(_InputIterator __first, _InputIterator __last,
00251                _ForwardIterator __result,
00252                allocator<_Tp>)
00253     {
00254       return std::uninitialized_copy(__first, __last, __result);
00255     }
00256 
00257   template<typename _ForwardIterator, typename _Tp, typename _Allocator>
00258     void
00259     __uninitialized_fill_a(_ForwardIterator __first, _ForwardIterator __last,
00260                const _Tp& __x, _Allocator __alloc)
00261     {
00262       _ForwardIterator __cur = __first;
00263       try
00264     {
00265       for (; __cur != __last; ++__cur)
00266         __alloc.construct(&*__cur, __x);
00267     }
00268       catch(...)
00269     {
00270       std::_Destroy(__first, __cur, __alloc);
00271       __throw_exception_again;
00272     }
00273     }
00274 
00275   template<typename _ForwardIterator, typename _Tp, typename _Tp2>
00276     inline void
00277     __uninitialized_fill_a(_ForwardIterator __first, _ForwardIterator __last,
00278                const _Tp& __x, allocator<_Tp2>)
00279     {
00280       std::uninitialized_fill(__first, __last, __x);
00281     }
00282 
00283   template<typename _ForwardIterator, typename _Size, typename _Tp,
00284        typename _Allocator>
00285     void
00286     __uninitialized_fill_n_a(_ForwardIterator __first, _Size __n, 
00287                  const _Tp& __x,
00288                  _Allocator __alloc)
00289     {
00290       _ForwardIterator __cur = __first;
00291       try
00292     {
00293       for (; __n > 0; --__n, ++__cur)
00294         __alloc.construct(&*__cur, __x);
00295     }
00296       catch(...)
00297     {
00298       std::_Destroy(__first, __cur, __alloc);
00299       __throw_exception_again;
00300     }
00301     }
00302 
00303   template<typename _ForwardIterator, typename _Size, typename _Tp,
00304        typename _Tp2>
00305     void
00306     __uninitialized_fill_n_a(_ForwardIterator __first, _Size __n, 
00307                  const _Tp& __x,
00308                  allocator<_Tp2>)
00309     {
00310       std::uninitialized_fill_n(__first, __n, __x);
00311     }
00312 
00313 
00314   // Extensions: __uninitialized_copy_copy, __uninitialized_copy_fill,
00315   // __uninitialized_fill_copy.  All of these algorithms take a user-
00316   // supplied allocator, which is used for construction and destruction.
00317 
00318   // __uninitialized_copy_copy
00319   // Copies [first1, last1) into [result, result + (last1 - first1)), and
00320   //  copies [first2, last2) into
00321   //  [result, result + (last1 - first1) + (last2 - first2)).
00322 
00323   template<typename _InputIterator1, typename _InputIterator2,
00324        typename _ForwardIterator, typename _Allocator>
00325     inline _ForwardIterator
00326     __uninitialized_copy_copy(_InputIterator1 __first1,
00327                   _InputIterator1 __last1,
00328                   _InputIterator2 __first2,
00329                   _InputIterator2 __last2,
00330                   _ForwardIterator __result,
00331                   _Allocator __alloc)
00332     {
00333       _ForwardIterator __mid = std::__uninitialized_copy_a(__first1, __last1,
00334                                __result,
00335                                __alloc);
00336       try
00337     {
00338       return std::__uninitialized_copy_a(__first2, __last2, __mid, __alloc);
00339     }
00340       catch(...)
00341     {
00342       std::_Destroy(__result, __mid, __alloc);
00343       __throw_exception_again;
00344     }
00345     }
00346 
00347   // __uninitialized_fill_copy
00348   // Fills [result, mid) with x, and copies [first, last) into
00349   //  [mid, mid + (last - first)).
00350   template<typename _ForwardIterator, typename _Tp, typename _InputIterator,
00351        typename _Allocator>
00352     inline _ForwardIterator
00353     __uninitialized_fill_copy(_ForwardIterator __result, _ForwardIterator __mid,
00354                   const _Tp& __x, _InputIterator __first,
00355                   _InputIterator __last,
00356                   _Allocator __alloc)
00357     {
00358       std::__uninitialized_fill_a(__result, __mid, __x, __alloc);
00359       try
00360     {
00361       return std::__uninitialized_copy_a(__first, __last, __mid, __alloc);
00362     }
00363       catch(...)
00364     {
00365       std::_Destroy(__result, __mid, __alloc);
00366       __throw_exception_again;
00367     }
00368     }
00369 
00370   // __uninitialized_copy_fill
00371   // Copies [first1, last1) into [first2, first2 + (last1 - first1)), and
00372   //  fills [first2 + (last1 - first1), last2) with x.
00373   template<typename _InputIterator, typename _ForwardIterator, typename _Tp,
00374        typename _Allocator>
00375     inline void
00376     __uninitialized_copy_fill(_InputIterator __first1, _InputIterator __last1,
00377                   _ForwardIterator __first2,
00378                   _ForwardIterator __last2, const _Tp& __x,
00379                   _Allocator __alloc)
00380     {
00381       _ForwardIterator __mid2 = std::__uninitialized_copy_a(__first1, __last1,
00382                                 __first2,
00383                                 __alloc);
00384       try
00385     {
00386       std::__uninitialized_fill_a(__mid2, __last2, __x, __alloc);
00387     }
00388       catch(...)
00389     {
00390       std::_Destroy(__first2, __mid2, __alloc);
00391       __throw_exception_again;
00392     }
00393     }
00394 
00395 } // namespace std
00396 
00397 #endif /* _STL_UNINITIALIZED_H */

Generated on Tue Feb 2 16:56:41 2010 for GNU C++ STL by  doxygen 1.4.7