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