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