# stl_uninitialized.h

Go to the documentation of this file.

    00001 // Raw memory manipulators -*- C++ -*-
    00002
    00003 // Copyright (C) 2001 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
    00061 #ifndef _CPP_BITS_STL_UNINITIALIZED_H
    00062 #define _CPP_BITS_STL_UNINITIALIZED_H 1
    00063
    00064 #include <cstring>
    00065
    00066 namespace std
    00067 {
    00068
    00069   // uninitialized_copy
    00070
    00071   template<typename _InputIter, typename _ForwardIter>
    00072     inline _ForwardIter
    00073     __uninitialized_copy_aux(_InputIter __first, _InputIter __last,
    00074                  _ForwardIter __result,
    00075                  __true_type)
    00076     { return copy(__first, __last, __result); }
    00077
    00078   template<typename _InputIter, typename _ForwardIter>
    00079     _ForwardIter
    00080     __uninitialized_copy_aux(_InputIter __first, _InputIter __last,
    00081                  _ForwardIter __result,
    00082                  __false_type)
    00083     {
    00084       _ForwardIter __cur = __result;
    00085       try {
    00086     for ( ; __first != __last; ++__first, ++__cur)
    00087       _Construct(&*__cur, *__first);
    00088     return __cur;
    00089       }
    00090       catch(...)
    00091     {
    00092       _Destroy(__result, __cur);
    00093       __throw_exception_again;
    00094     }
    00095     }
    00096
    00106   template<typename _InputIter, typename _ForwardIter>
    00107     inline _ForwardIter
    00108     uninitialized_copy(_InputIter __first, _InputIter __last, _ForwardIter __result)
    00109     {
    00110       typedef typename iterator_traits<_ForwardIter>::value_type _ValueType;
    00111       typedef typename __type_traits<_ValueType>::is_POD_type _Is_POD;
    00112       return __uninitialized_copy_aux(__first, __last, __result, _Is_POD());
    00113     }
    00114
    00115   inline char*
    00116   uninitialized_copy(const char* __first, const char* __last, char* __result)
    00117   {
    00118     memmove(__result, __first, __last - __first);
    00119     return __result + (__last - __first);
    00120   }
    00121
    00122   inline wchar_t*
    00123   uninitialized_copy(const wchar_t* __first, const wchar_t* __last,
    00124              wchar_t* __result)
    00125   {
    00126     memmove(__result, __first, sizeof(wchar_t) * (__last - __first));
    00127     return __result + (__last - __first);
    00128   }
    00129
    00130   // Valid if copy construction is equivalent to assignment, and if the
    00131   // destructor is trivial.
    00132   template<typename _ForwardIter, typename _Tp>
    00133     inline void
    00134     __uninitialized_fill_aux(_ForwardIter __first, _ForwardIter __last,
    00135                  const _Tp& __x, __true_type)
    00136     { fill(__first, __last, __x); }
    00137
    00138   template<typename _ForwardIter, typename _Tp>
    00139     void
    00140     __uninitialized_fill_aux(_ForwardIter __first, _ForwardIter __last,
    00141                  const _Tp& __x, __false_type)
    00142     {
    00143       _ForwardIter __cur = __first;
    00144       try {
    00145     for ( ; __cur != __last; ++__cur)
    00146       _Construct(&*__cur, __x);
    00147       }
    00148       catch(...)
    00149     {
    00150       _Destroy(__first, __cur);
    00151       __throw_exception_again;
    00152     }
    00153     }
    00154
    00164   template<typename _ForwardIter, typename _Tp>
    00165     inline void
    00166     uninitialized_fill(_ForwardIter __first, _ForwardIter __last, const _Tp& __x)
    00167     {
    00168       typedef typename iterator_traits<_ForwardIter>::value_type _ValueType;
    00169       typedef typename __type_traits<_ValueType>::is_POD_type _Is_POD;
    00170       __uninitialized_fill_aux(__first, __last, __x, _Is_POD());
    00171     }
    00172
    00173   // Valid if copy construction is equivalent to assignment, and if the
    00174   //  destructor is trivial.
    00175   template<typename _ForwardIter, typename _Size, typename _Tp>
    00176     inline _ForwardIter
    00177     __uninitialized_fill_n_aux(_ForwardIter __first, _Size __n,
    00178                    const _Tp& __x, __true_type)
    00179     {
    00180       return fill_n(__first, __n, __x);
    00181     }
    00182
    00183   template<typename _ForwardIter, typename _Size, typename _Tp>
    00184     _ForwardIter
    00185     __uninitialized_fill_n_aux(_ForwardIter __first, _Size __n,
    00186                    const _Tp& __x, __false_type)
    00187     {
    00188       _ForwardIter __cur = __first;
    00189       try {
    00190     for ( ; __n > 0; --__n, ++__cur)
    00191       _Construct(&*__cur, __x);
    00192     return __cur;
    00193       }
    00194       catch(...)
    00195     {
    00196       _Destroy(__first, __cur);
    00197       __throw_exception_again;
    00198     }
    00199     }
    00200
    00210   template<typename _ForwardIter, typename _Size, typename _Tp>
    00211     inline _ForwardIter
    00212     uninitialized_fill_n(_ForwardIter __first, _Size __n, const _Tp& __x)
    00213     {
    00214       typedef typename iterator_traits<_ForwardIter>::value_type _ValueType;
    00215       typedef typename __type_traits<_ValueType>::is_POD_type _Is_POD;
    00216       return __uninitialized_fill_n_aux(__first, __n, __x, _Is_POD());
    00217     }
    00218
    00219   // Extensions: __uninitialized_copy_copy, __uninitialized_copy_fill,
    00220   // __uninitialized_fill_copy.
    00221
    00222   // __uninitialized_copy_copy
    00223   // Copies [first1, last1) into [result, result + (last1 - first1)), and
    00224   //  copies [first2, last2) into
    00225   //  [result, result + (last1 - first1) + (last2 - first2)).
    00226
    00227   template<typename _InputIter1, typename _InputIter2, typename _ForwardIter>
    00228     inline _ForwardIter
    00229     __uninitialized_copy_copy(_InputIter1 __first1, _InputIter1 __last1,
    00230                   _InputIter2 __first2, _InputIter2 __last2,
    00231                   _ForwardIter __result)
    00232     {
    00233       _ForwardIter __mid = uninitialized_copy(__first1, __last1, __result);
    00234       try {
    00235     return uninitialized_copy(__first2, __last2, __mid);
    00236       }
    00237       catch(...)
    00238     {
    00239       _Destroy(__result, __mid);
    00240       __throw_exception_again;
    00241     }
    00242     }
    00243
    00244   // __uninitialized_fill_copy
    00245   // Fills [result, mid) with x, and copies [first, last) into
    00246   //  [mid, mid + (last - first)).
    00247   template<typename _ForwardIter, typename _Tp, typename _InputIter>
    00248     inline _ForwardIter
    00249     __uninitialized_fill_copy(_ForwardIter __result, _ForwardIter __mid,
    00250                   const _Tp& __x,
    00251                   _InputIter __first, _InputIter __last)
    00252     {
    00253       uninitialized_fill(__result, __mid, __x);
    00254       try {
    00255     return uninitialized_copy(__first, __last, __mid);
    00256       }
    00257       catch(...)
    00258     {
    00259       _Destroy(__result, __mid);
    00260       __throw_exception_again;
    00261     }
    00262     }
    00263
    00264   // __uninitialized_copy_fill
    00265   // Copies [first1, last1) into [first2, first2 + (last1 - first1)), and
    00266   //  fills [first2 + (last1 - first1), last2) with x.
    00267   template<typename _InputIter, typename _ForwardIter, typename _Tp>
    00268     inline void
    00269     __uninitialized_copy_fill(_InputIter __first1, _InputIter __last1,
    00270                   _ForwardIter __first2, _ForwardIter __last2,
    00271                   const _Tp& __x)
    00272     {
    00273       _ForwardIter __mid2 = uninitialized_copy(__first1, __last1, __first2);
    00274       try {
    00275     uninitialized_fill(__mid2, __last2, __x);
    00276       }
    00277       catch(...)
    00278     {
    00279       _Destroy(__first2, __mid2);
    00280       __throw_exception_again;
    00281     }
    00282     }
    00283
    00284 } // namespace std
    00285
    00286 #endif /* _CPP_BITS_STL_UNINITIALIZED_H */
    00287
    00288 // Local Variables:
    00289 // mode:C++
    00290 // End: