libstdc++
ext/memory
Go to the documentation of this file.
00001 // Memory extensions -*- C++ -*-
00002 
00003 // Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009
00004 // Free Software Foundation, Inc.
00005 //
00006 // This file is part of the GNU ISO C++ Library.  This library is free
00007 // software; you can redistribute it and/or modify it under the
00008 // terms of the GNU General Public License as published by the
00009 // Free Software Foundation; either version 3, or (at your option)
00010 // any later version.
00011 
00012 // This library is distributed in the hope that it will be useful,
00013 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00014 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015 // GNU General Public License for more details.
00016 
00017 // Under Section 7 of GPL version 3, you are granted additional
00018 // permissions described in the GCC Runtime Library Exception, version
00019 // 3.1, as published by the Free Software Foundation.
00020 
00021 // You should have received a copy of the GNU General Public License and
00022 // a copy of the GCC Runtime Library Exception along with this program;
00023 // see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
00024 // <http://www.gnu.org/licenses/>.
00025 
00026 /*
00027  *
00028  * Copyright (c) 1994
00029  * Hewlett-Packard Company
00030  *
00031  * Permission to use, copy, modify, distribute and sell this software
00032  * and its documentation for any purpose is hereby granted without fee,
00033  * provided that the above copyright notice appear in all copies and
00034  * that both that copyright notice and this permission notice appear
00035  * in supporting documentation.  Hewlett-Packard Company makes no
00036  * representations about the suitability of this software for any
00037  * purpose.  It is provided "as is" without express or implied warranty.
00038  *
00039  *
00040  * Copyright (c) 1996
00041  * Silicon Graphics Computer Systems, Inc.
00042  *
00043  * Permission to use, copy, modify, distribute and sell this software
00044  * and its documentation for any purpose is hereby granted without fee,
00045  * provided that the above copyright notice appear in all copies and
00046  * that both that copyright notice and this permission notice appear
00047  * in supporting documentation.  Silicon Graphics makes no
00048  * representations about the suitability of this software for any
00049  * purpose.  It is provided "as is" without express or implied warranty.
00050  */
00051 
00052 /** @file ext/memory
00053  *  This file is a GNU extension to the Standard C++ Library (possibly
00054  *  containing extensions from the HP/SGI STL subset).
00055  */
00056 
00057 #ifndef _EXT_MEMORY
00058 #define _EXT_MEMORY 1
00059 
00060 #pragma GCC system_header
00061 
00062 #include <memory>
00063 #include <bits/stl_tempbuf.h>
00064 
00065 namespace __gnu_cxx _GLIBCXX_VISIBILITY(default)
00066 {
00067 _GLIBCXX_BEGIN_NAMESPACE_VERSION
00068 
00069   using std::ptrdiff_t;
00070   using std::pair;
00071   using std::__iterator_category;
00072   using std::_Temporary_buffer;
00073 
00074   template<typename _InputIter, typename _Size, typename _ForwardIter>
00075     pair<_InputIter, _ForwardIter>
00076     __uninitialized_copy_n(_InputIter __first, _Size __count,
00077                _ForwardIter __result, std::input_iterator_tag)
00078     {
00079       _ForwardIter __cur = __result;
00080       __try
00081     {
00082       for (; __count > 0 ; --__count, ++__first, ++__cur)
00083         std::_Construct(&*__cur, *__first);
00084       return pair<_InputIter, _ForwardIter>(__first, __cur);
00085     }
00086       __catch(...)
00087     {
00088       std::_Destroy(__result, __cur);
00089       __throw_exception_again;
00090     }
00091     }
00092 
00093   template<typename _RandomAccessIter, typename _Size, typename _ForwardIter>
00094     inline pair<_RandomAccessIter, _ForwardIter>
00095     __uninitialized_copy_n(_RandomAccessIter __first, _Size __count,
00096                _ForwardIter __result,
00097                std::random_access_iterator_tag)
00098     {
00099       _RandomAccessIter __last = __first + __count;
00100       return (pair<_RandomAccessIter, _ForwardIter>
00101           (__last, std::uninitialized_copy(__first, __last, __result)));
00102     }
00103 
00104   template<typename _InputIter, typename _Size, typename _ForwardIter>
00105     inline pair<_InputIter, _ForwardIter>
00106     __uninitialized_copy_n(_InputIter __first, _Size __count,
00107                _ForwardIter __result)
00108     { return __gnu_cxx::__uninitialized_copy_n(__first, __count, __result,
00109                            __iterator_category(__first)); }
00110 
00111   /**
00112    *  @brief Copies the range [first,last) into result.
00113    *  @param  __first  An input iterator.
00114    *  @param  __count  Length
00115    *  @param  __result An output iterator.
00116    *  @return   __result + (__first + __count)
00117    *  @ingroup SGIextensions
00118    *
00119    *  Like copy(), but does not require an initialized output range.
00120   */
00121   template<typename _InputIter, typename _Size, typename _ForwardIter>
00122     inline pair<_InputIter, _ForwardIter>
00123     uninitialized_copy_n(_InputIter __first, _Size __count,
00124              _ForwardIter __result)
00125     { return __gnu_cxx::__uninitialized_copy_n(__first, __count, __result,
00126                            __iterator_category(__first)); }
00127 
00128 
00129   // An alternative version of uninitialized_copy_n that constructs
00130   // and destroys objects with a user-provided allocator.
00131   template<typename _InputIter, typename _Size, typename _ForwardIter,
00132            typename _Allocator>
00133     pair<_InputIter, _ForwardIter>
00134     __uninitialized_copy_n_a(_InputIter __first, _Size __count,
00135                  _ForwardIter __result,
00136                  _Allocator __alloc)
00137     {
00138       _ForwardIter __cur = __result;
00139       __try
00140     {
00141       for (; __count > 0 ; --__count, ++__first, ++__cur)
00142         __alloc.construct(&*__cur, *__first);
00143       return pair<_InputIter, _ForwardIter>(__first, __cur);
00144     }
00145       __catch(...)
00146     {
00147       std::_Destroy(__result, __cur, __alloc);
00148       __throw_exception_again;
00149     }
00150     }
00151 
00152   template<typename _InputIter, typename _Size, typename _ForwardIter,
00153            typename _Tp>
00154     inline pair<_InputIter, _ForwardIter>
00155     __uninitialized_copy_n_a(_InputIter __first, _Size __count,
00156                  _ForwardIter __result,
00157                  std::allocator<_Tp>)
00158     {
00159       return __gnu_cxx::uninitialized_copy_n(__first, __count, __result);
00160     }
00161 
00162   /**
00163    *  This class provides similar behavior and semantics of the standard
00164    *  functions get_temporary_buffer() and return_temporary_buffer(), but
00165    *  encapsulated in a type vaguely resembling a standard container.
00166    *
00167    *  By default, a temporary_buffer<Iter> stores space for objects of
00168    *  whatever type the Iter iterator points to.  It is constructed from a
00169    *  typical [first,last) range, and provides the begin(), end(), size()
00170    *  functions, as well as requested_size().  For non-trivial types, copies
00171    *  of *first will be used to initialize the storage.
00172    *
00173    *  @c malloc is used to obtain underlying storage.
00174    *
00175    *  Like get_temporary_buffer(), not all the requested memory may be
00176    *  available.  Ideally, the created buffer will be large enough to hold a
00177    *  copy of [first,last), but if size() is less than requested_size(),
00178    *  then this didn't happen.
00179    *
00180    *  @ingroup SGIextensions
00181   */
00182   template <class _ForwardIterator, class _Tp
00183         = typename std::iterator_traits<_ForwardIterator>::value_type >
00184     struct temporary_buffer : public _Temporary_buffer<_ForwardIterator, _Tp>
00185     {
00186       /// Requests storage large enough to hold a copy of [first,last).
00187       temporary_buffer(_ForwardIterator __first, _ForwardIterator __last)
00188       : _Temporary_buffer<_ForwardIterator, _Tp>(__first, __last) { }
00189       
00190       /// Destroys objects and frees storage.
00191       ~temporary_buffer() { }
00192     };
00193 
00194 _GLIBCXX_END_NAMESPACE_VERSION
00195 } // namespace
00196 
00197 #endif
00198