libstdc++
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, 2011
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 bits/stl_construct.h
00054  *  This is an internal header file, included by other library headers.
00055  *  Do not attempt to use it directly. @headername{memory}
00056  */
00057 
00058 #ifndef _STL_CONSTRUCT_H
00059 #define _STL_CONSTRUCT_H 1
00060 
00061 #include <new>
00062 #include <bits/move.h>
00063 #include <ext/alloc_traits.h>
00064 
00065 namespace std _GLIBCXX_VISIBILITY(default)
00066 {
00067 _GLIBCXX_BEGIN_NAMESPACE_VERSION
00068 
00069   /**
00070    * Constructs an object in existing memory by invoking an allocated
00071    * object's constructor with an initializer.
00072    */
00073 #ifdef __GXX_EXPERIMENTAL_CXX0X__
00074   template<typename _T1, typename... _Args>
00075     inline void
00076     _Construct(_T1* __p, _Args&&... __args)
00077     { ::new(static_cast<void*>(__p)) _T1(std::forward<_Args>(__args)...); }
00078 #else
00079   template<typename _T1, typename _T2>
00080     inline void
00081     _Construct(_T1* __p, const _T2& __value)
00082     {
00083       // _GLIBCXX_RESOLVE_LIB_DEFECTS
00084       // 402. wrong new expression in [some_]allocator::construct
00085       ::new(static_cast<void*>(__p)) _T1(__value);
00086     }
00087 #endif
00088 
00089   /**
00090    * Destroy the object pointed to by a pointer type.
00091    */
00092   template<typename _Tp>
00093     inline void
00094     _Destroy(_Tp* __pointer)
00095     { __pointer->~_Tp(); }
00096 
00097   template<bool>
00098     struct _Destroy_aux
00099     {
00100       template<typename _ForwardIterator>
00101         static void
00102         __destroy(_ForwardIterator __first, _ForwardIterator __last)
00103     {
00104       for (; __first != __last; ++__first)
00105         std::_Destroy(std::__addressof(*__first));
00106     }
00107     };
00108 
00109   template<>
00110     struct _Destroy_aux<true>
00111     {
00112       template<typename _ForwardIterator>
00113         static void
00114         __destroy(_ForwardIterator, _ForwardIterator) { }
00115     };
00116 
00117   /**
00118    * Destroy a range of objects.  If the value_type of the object has
00119    * a trivial destructor, the compiler should optimize all of this
00120    * away, otherwise the objects' destructors must be invoked.
00121    */
00122   template<typename _ForwardIterator>
00123     inline void
00124     _Destroy(_ForwardIterator __first, _ForwardIterator __last)
00125     {
00126       typedef typename iterator_traits<_ForwardIterator>::value_type
00127                        _Value_type;
00128       std::_Destroy_aux<__has_trivial_destructor(_Value_type)>::
00129     __destroy(__first, __last);
00130     }
00131 
00132   /**
00133    * Destroy a range of objects using the supplied allocator.  For
00134    * nondefault allocators we do not optimize away invocation of 
00135    * destroy() even if _Tp has a trivial destructor.
00136    */
00137 
00138   template <typename _Tp> class allocator;
00139 
00140   template<typename _ForwardIterator, typename _Allocator>
00141     void
00142     _Destroy(_ForwardIterator __first, _ForwardIterator __last,
00143          _Allocator& __alloc)
00144     {
00145       typedef __gnu_cxx::__alloc_traits<_Allocator> __traits;
00146       for (; __first != __last; ++__first)
00147     __traits::destroy(__alloc, std::__addressof(*__first));
00148     }
00149 
00150   template<typename _ForwardIterator, typename _Tp>
00151     inline void
00152     _Destroy(_ForwardIterator __first, _ForwardIterator __last,
00153          allocator<_Tp>&)
00154     {
00155       _Destroy(__first, __last);
00156     }
00157 
00158 _GLIBCXX_END_NAMESPACE_VERSION
00159 } // namespace
00160 
00161 #endif /* _STL_CONSTRUCT_H */
00162