# stl_uninitialized.h

Go to the documentation of this file.

    00001 // Raw memory manipulators -*- C++ -*-
    00002
    00003 // Copyright (C) 2001, 2004 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, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
    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
    00056 /** @file stl_uninitialized.h
    00057  *  This is an internal header file, included by other library headers.
    00058  *  You should not attempt to use it directly.
    00059  */
    00060
    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
    00096   /**
    00097    *  @brief Copies the range [first,last) into result.
    00098    *  @param  first  An input iterator.
    00099    *  @param  last   An input iterator.
    00100    *  @param  result An output iterator.
    00101    *  @return   result + (first - last)
    00102    *
    00103    *  Like copy(), but does not require an initialized output range.
    00104   */
    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 __type_traits<_ValueType>::is_POD_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
    00158   /**
    00159    *  @brief Copies the value x into the range [first,last).
    00160    *  @param  first  An input iterator.
    00161    *  @param  last   An input iterator.
    00162    *  @param  x      The source value.
    00163    *  @return   Nothing.
    00164    *
    00165    *  Like fill(), but does not require an initialized output range.
    00166   */
    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 __type_traits<_ValueType>::is_POD_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 _ForwardIterator
    00181     __uninitialized_fill_n_aux(_ForwardIterator __first, _Size __n,
    00182                    const _Tp& __x, __true_type)
    00183     { return std::fill_n(__first, __n, __x); }
    00184
    00185   template<typename _ForwardIterator, typename _Size, typename _Tp>
    00186     _ForwardIterator
    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       return __cur;
    00196     }
    00197       catch(...)
    00198     {
    00199       std::_Destroy(__first, __cur);
    00200       __throw_exception_again;
    00201     }
    00202     }
    00203
    00204   /**
    00205    *  @brief Copies the value x into the range [first,first+n).
    00206    *  @param  first  An input iterator.
    00207    *  @param  n      The number of copies to make.
    00208    *  @param  x      The source value.
    00209    *  @return   first+n
    00210    *
    00211    *  Like fill_n(), but does not require an initialized output range.
    00212   */
    00213   template<typename _ForwardIterator, typename _Size, typename _Tp>
    00214     inline _ForwardIterator
    00215     uninitialized_fill_n(_ForwardIterator __first, _Size __n, const _Tp& __x)
    00216     {
    00217       typedef typename iterator_traits<_ForwardIterator>::value_type _ValueType;
    00218       typedef typename __type_traits<_ValueType>::is_POD_type _Is_POD;
    00219       return std::__uninitialized_fill_n_aux(__first, __n, __x, _Is_POD());
    00220     }
    00221
    00222   // Extensions: __uninitialized_copy_copy, __uninitialized_copy_fill,
    00223   // __uninitialized_fill_copy.
    00224
    00225   // __uninitialized_copy_copy
    00226   // Copies [first1, last1) into [result, result + (last1 - first1)), and
    00227   //  copies [first2, last2) into
    00228   //  [result, result + (last1 - first1) + (last2 - first2)).
    00229
    00230   template<typename _InputIterator1, typename _InputIterator2,
    00231        typename _ForwardIterator>
    00232     inline _ForwardIterator
    00233     __uninitialized_copy_copy(_InputIterator1 __first1,
    00234                   _InputIterator1 __last1,
    00235                   _InputIterator2 __first2,
    00236                   _InputIterator2 __last2,
    00237                   _ForwardIterator __result)
    00238     {
    00239       _ForwardIterator __mid = std::uninitialized_copy(__first1, __last1,
    00240                                __result);
    00241       try
    00242     {
    00243       return std::uninitialized_copy(__first2, __last2, __mid);
    00244     }
    00245       catch(...)
    00246     {
    00247       std::_Destroy(__result, __mid);
    00248       __throw_exception_again;
    00249     }
    00250     }
    00251
    00252   // __uninitialized_fill_copy
    00253   // Fills [result, mid) with x, and copies [first, last) into
    00254   //  [mid, mid + (last - first)).
    00255   template<typename _ForwardIterator, typename _Tp, typename _InputIterator>
    00256     inline _ForwardIterator
    00257     __uninitialized_fill_copy(_ForwardIterator __result, _ForwardIterator __mid,
    00258                   const _Tp& __x, _InputIterator __first,
    00259                   _InputIterator __last)
    00260     {
    00261       std::uninitialized_fill(__result, __mid, __x);
    00262       try
    00263     {
    00264       return std::uninitialized_copy(__first, __last, __mid);
    00265     }
    00266       catch(...)
    00267     {
    00268       std::_Destroy(__result, __mid);
    00269       __throw_exception_again;
    00270     }
    00271     }
    00272
    00273   // __uninitialized_copy_fill
    00274   // Copies [first1, last1) into [first2, first2 + (last1 - first1)), and
    00275   //  fills [first2 + (last1 - first1), last2) with x.
    00276   template<typename _InputIterator, typename _ForwardIterator, typename _Tp>
    00277     inline void
    00278     __uninitialized_copy_fill(_InputIterator __first1, _InputIterator __last1,
    00279                   _ForwardIterator __first2,
    00280                   _ForwardIterator __last2, const _Tp& __x)
    00281     {
    00282       _ForwardIterator __mid2 = std::uninitialized_copy(__first1, __last1,
    00283                             __first2);
    00284       try
    00285     {
    00286       std::uninitialized_fill(__mid2, __last2, __x);
    00287     }
    00288       catch(...)
    00289     {
    00290       std::_Destroy(__first2, __mid2);
    00291       __throw_exception_again;
    00292     }
    00293     }
    00294
    00295 } // namespace std
    00296
    00297 #endif /* _STL_UNINITIALIZED_H */