stl_construct.h

Go to the documentation of this file.
00001 // nonstandard construct and destroy functions -*- C++ -*-
00002 
00003 // Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008,
00004 // 2009, 2010
00005 // Free Software Foundation, Inc.
00006 //
00007 // This file is part of the GNU ISO C++ Library.  This library is free
00008 // software; you can redistribute it and/or modify it under the
00009 // terms of the GNU General Public License as published by the
00010 // Free Software Foundation; either version 3, or (at your option)
00011 // any later version.
00012 
00013 // This library is distributed in the hope that it will be useful,
00014 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00015 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016 // GNU General Public License for more details.
00017 
00018 // Under Section 7 of GPL version 3, you are granted additional
00019 // permissions described in the GCC Runtime Library Exception, version
00020 // 3.1, as published by the Free Software Foundation.
00021 
00022 // You should have received a copy of the GNU General Public License and
00023 // a copy of the GCC Runtime Library Exception along with this program;
00024 // see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
00025 // <http://www.gnu.org/licenses/>.
00026 
00027 /*
00028  *
00029  * Copyright (c) 1994
00030  * Hewlett-Packard Company
00031  *
00032  * Permission to use, copy, modify, distribute and sell this software
00033  * and its documentation for any purpose is hereby granted without fee,
00034  * provided that the above copyright notice appear in all copies and
00035  * that both that copyright notice and this permission notice appear
00036  * in supporting documentation.  Hewlett-Packard Company makes no
00037  * representations about the suitability of this software for any
00038  * purpose.  It is provided "as is" without express or implied warranty.
00039  *
00040  *
00041  * Copyright (c) 1996,1997
00042  * Silicon Graphics Computer Systems, Inc.
00043  *
00044  * Permission to use, copy, modify, distribute and sell this software
00045  * and its documentation for any purpose is hereby granted without fee,
00046  * provided that the above copyright notice appear in all copies and
00047  * that both that copyright notice and this permission notice appear
00048  * in supporting documentation.  Silicon Graphics makes no
00049  * representations about the suitability of this software for any
00050  * purpose.  It is provided "as is" without express or implied warranty.
00051  */
00052 
00053 /** @file stl_construct.h
00054  *  This is an internal header file, included by other library headers.
00055  *  You should not attempt to use it directly.
00056  */
00057 
00058 #ifndef _STL_CONSTRUCT_H
00059 #define _STL_CONSTRUCT_H 1
00060 
00061 #include <new>
00062 #include <bits/move.h>
00063 
00064 _GLIBCXX_BEGIN_NAMESPACE(std)
00065 
00066   /**
00067    * Constructs an object in existing memory by invoking an allocated
00068    * object's constructor with an initializer.
00069    */
00070 #ifdef __GXX_EXPERIMENTAL_CXX0X__
00071   template<typename _T1, typename... _Args>
00072     inline void
00073     _Construct(_T1* __p, _Args&&... __args)
00074     { ::new(static_cast<void*>(__p)) _T1(std::forward<_Args>(__args)...); }
00075 #else
00076   template<typename _T1, typename _T2>
00077     inline void
00078     _Construct(_T1* __p, const _T2& __value)
00079     {
00080       // _GLIBCXX_RESOLVE_LIB_DEFECTS
00081       // 402. wrong new expression in [some_]allocator::construct
00082       ::new(static_cast<void*>(__p)) _T1(__value);
00083     }
00084 #endif
00085 
00086   /**
00087    * Destroy the object pointed to by a pointer type.
00088    */
00089   template<typename _Tp>
00090     inline void
00091     _Destroy(_Tp* __pointer)
00092     { __pointer->~_Tp(); }
00093 
00094   template<bool>
00095     struct _Destroy_aux
00096     {
00097       template<typename _ForwardIterator>
00098         static void
00099         __destroy(_ForwardIterator __first, _ForwardIterator __last)
00100     {
00101       for (; __first != __last; ++__first)
00102         std::_Destroy(std::__addressof(*__first));
00103     }
00104     };
00105 
00106   template<>
00107     struct _Destroy_aux<true>
00108     {
00109       template<typename _ForwardIterator>
00110         static void
00111         __destroy(_ForwardIterator, _ForwardIterator) { }
00112     };
00113 
00114   /**
00115    * Destroy a range of objects.  If the value_type of the object has
00116    * a trivial destructor, the compiler should optimize all of this
00117    * away, otherwise the objects' destructors must be invoked.
00118    */
00119   template<typename _ForwardIterator>
00120     inline void
00121     _Destroy(_ForwardIterator __first, _ForwardIterator __last)
00122     {
00123       typedef typename iterator_traits<_ForwardIterator>::value_type
00124                        _Value_type;
00125       std::_Destroy_aux<__has_trivial_destructor(_Value_type)>::
00126     __destroy(__first, __last);
00127     }
00128 
00129   /**
00130    * Destroy a range of objects using the supplied allocator.  For
00131    * nondefault allocators we do not optimize away invocation of 
00132    * destroy() even if _Tp has a trivial destructor.
00133    */
00134 
00135   template <typename _Tp> class allocator;
00136 
00137   template<typename _ForwardIterator, typename _Allocator>
00138     void
00139     _Destroy(_ForwardIterator __first, _ForwardIterator __last,
00140          _Allocator& __alloc)
00141     {
00142       for (; __first != __last; ++__first)
00143     __alloc.destroy(std::__addressof(*__first));
00144     }
00145 
00146   template<typename _ForwardIterator, typename _Tp>
00147     inline void
00148     _Destroy(_ForwardIterator __first, _ForwardIterator __last,
00149          allocator<_Tp>&)
00150     {
00151       _Destroy(__first, __last);
00152     }
00153 
00154 _GLIBCXX_END_NAMESPACE
00155 
00156 #endif /* _STL_CONSTRUCT_H */
00157