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