1// nonstandard construct and destroy functions -*- C++ -*- 2 3// Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009 4// Free Software Foundation, Inc. 5// 6// This file is part of the GNU ISO C++ Library. This library is free 7// software; you can redistribute it and/or modify it under the 8// terms of the GNU General Public License as published by the 9// Free Software Foundation; either version 3, or (at your option) 10// any later version. 11 12// This library is distributed in the hope that it will be useful, 13// but WITHOUT ANY WARRANTY; without even the implied warranty of 14// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15// GNU General Public License for more details. 16 17// Under Section 7 of GPL version 3, you are granted additional 18// permissions described in the GCC Runtime Library Exception, version 19// 3.1, as published by the Free Software Foundation. 20 21// You should have received a copy of the GNU General Public License and 22// a copy of the GCC Runtime Library Exception along with this program; 23// see the files COPYING3 and COPYING.RUNTIME respectively. If not, see 24// <http://www.gnu.org/licenses/>. 25 26/* 27 * 28 * Copyright (c) 1994 29 * Hewlett-Packard Company 30 * 31 * Permission to use, copy, modify, distribute and sell this software 32 * and its documentation for any purpose is hereby granted without fee, 33 * provided that the above copyright notice appear in all copies and 34 * that both that copyright notice and this permission notice appear 35 * in supporting documentation. Hewlett-Packard Company makes no 36 * representations about the suitability of this software for any 37 * purpose. It is provided "as is" without express or implied warranty. 38 * 39 * 40 * Copyright (c) 1996,1997 41 * Silicon Graphics Computer Systems, Inc. 42 * 43 * Permission to use, copy, modify, distribute and sell this software 44 * and its documentation for any purpose is hereby granted without fee, 45 * provided that the above copyright notice appear in all copies and 46 * that both that copyright notice and this permission notice appear 47 * in supporting documentation. Silicon Graphics makes no 48 * representations about the suitability of this software for any 49 * purpose. It is provided "as is" without express or implied warranty. 50 */ 51 52/** @file stl_construct.h 53 * This is an internal header file, included by other library headers. 54 * You should not attempt to use it directly. 55 */ 56 57#ifndef _STL_CONSTRUCT_H 58#define _STL_CONSTRUCT_H 1 59 60#include <new> 61#include <bits/move.h> 62 63_GLIBCXX_BEGIN_NAMESPACE(std) 64 65 /** 66 * Constructs an object in existing memory by invoking an allocated 67 * object's constructor with an initializer. 68 */ 69 template<typename _T1, typename _T2> 70 inline void 71#ifdef __GXX_EXPERIMENTAL_CXX0X__ 72 // Allow perfect forwarding 73 _Construct(_T1* __p, _T2&& __value) 74#else 75 _Construct(_T1* __p, const _T2& __value) 76#endif 77 { 78 // _GLIBCXX_RESOLVE_LIB_DEFECTS 79 // 402. wrong new expression in [some_]allocator::construct 80 ::new(static_cast<void*>(__p)) _T1(_GLIBCXX_FORWARD(_T2, __value)); 81 } 82 83 /** 84 * Destroy the object pointed to by a pointer type. 85 */ 86 template<typename _Tp> 87 inline void 88 _Destroy(_Tp* __pointer) 89 { __pointer->~_Tp(); } 90 91 template<bool> 92 struct _Destroy_aux 93 { 94 template<typename _ForwardIterator> 95 static void 96 __destroy(_ForwardIterator __first, _ForwardIterator __last) 97 { 98 for (; __first != __last; ++__first) 99 std::_Destroy(&*__first); 100 } 101 }; 102 103 template<> 104 struct _Destroy_aux<true> 105 { 106 template<typename _ForwardIterator> 107 static void 108 __destroy(_ForwardIterator, _ForwardIterator) { } 109 }; 110 111 /** 112 * Destroy a range of objects. If the value_type of the object has 113 * a trivial destructor, the compiler should optimize all of this 114 * away, otherwise the objects' destructors must be invoked. 115 */ 116 template<typename _ForwardIterator> 117 inline void 118 _Destroy(_ForwardIterator __first, _ForwardIterator __last) 119 { 120 typedef typename iterator_traits<_ForwardIterator>::value_type 121 _Value_type; 122 std::_Destroy_aux<__has_trivial_destructor(_Value_type)>:: 123 __destroy(__first, __last); 124 } 125 126 /** 127 * Destroy a range of objects using the supplied allocator. For 128 * nondefault allocators we do not optimize away invocation of 129 * destroy() even if _Tp has a trivial destructor. 130 */ 131 132 template <typename _Tp> class allocator; 133 134 template<typename _ForwardIterator, typename _Allocator> 135 void 136 _Destroy(_ForwardIterator __first, _ForwardIterator __last, 137 _Allocator& __alloc) 138 { 139 for (; __first != __last; ++__first) 140 __alloc.destroy(&*__first); 141 } 142 143 template<typename _ForwardIterator, typename _Tp> 144 inline void 145 _Destroy(_ForwardIterator __first, _ForwardIterator __last, 146 allocator<_Tp>&) 147 { 148 _Destroy(__first, __last); 149 } 150 151_GLIBCXX_END_NAMESPACE 152 153#endif /* _STL_CONSTRUCT_H */ 154 155