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

Generated on 19 Jun 2018 for libstdc++ by  doxygen 1.6.1