]>
Commit | Line | Data |
---|---|---|
92f5a8d4 TL |
1 | /* |
2 | Copyright 2019 Glen Joseph Fernandes | |
3 | (glenjofe@gmail.com) | |
4 | ||
5 | Distributed under the Boost Software License, Version 1.0. | |
6 | (http://www.boost.org/LICENSE_1_0.txt) | |
7 | */ | |
8 | #ifndef BOOST_CORE_ALLOC_CONSTRUCT_HPP | |
9 | #define BOOST_CORE_ALLOC_CONSTRUCT_HPP | |
10 | ||
11 | #include <boost/core/noinit_adaptor.hpp> | |
12 | ||
13 | namespace boost { | |
14 | ||
15 | #if !defined(BOOST_NO_CXX11_ALLOCATOR) | |
16 | template<class A, class T> | |
17 | inline void | |
18 | alloc_destroy(A& a, T* p) | |
19 | { | |
20 | std::allocator_traits<A>::destroy(a, p); | |
21 | } | |
22 | ||
23 | template<class A, class T> | |
24 | inline void | |
25 | alloc_destroy_n(A& a, T* p, std::size_t n) | |
26 | { | |
27 | while (n > 0) { | |
28 | std::allocator_traits<A>::destroy(a, p + --n); | |
29 | } | |
30 | } | |
31 | #else | |
32 | template<class A, class T> | |
33 | inline void | |
34 | alloc_destroy(A&, T* p) | |
35 | { | |
36 | p->~T(); | |
37 | } | |
38 | ||
39 | template<class A, class T> | |
40 | inline void | |
41 | alloc_destroy_n(A&, T* p, std::size_t n) | |
42 | { | |
43 | while (n > 0) { | |
44 | p[--n].~T(); | |
45 | } | |
46 | } | |
47 | #endif | |
48 | ||
49 | namespace detail { | |
50 | ||
51 | template<class A, class T> | |
52 | class alloc_destroyer { | |
53 | public: | |
54 | alloc_destroyer(A& a, T* p) BOOST_NOEXCEPT | |
55 | : a_(a), | |
56 | p_(p), | |
57 | n_(0) { } | |
58 | ||
59 | ~alloc_destroyer() { | |
60 | boost::alloc_destroy_n(a_, p_, n_); | |
61 | } | |
62 | ||
63 | std::size_t& size() BOOST_NOEXCEPT { | |
64 | return n_; | |
65 | } | |
66 | ||
67 | private: | |
68 | alloc_destroyer(const alloc_destroyer&); | |
69 | alloc_destroyer& operator=(const alloc_destroyer&); | |
70 | ||
71 | A& a_; | |
72 | T* p_; | |
73 | std::size_t n_; | |
74 | }; | |
75 | ||
76 | } /* detail */ | |
77 | ||
78 | #if !defined(BOOST_NO_CXX11_ALLOCATOR) | |
79 | template<class A, class T> | |
80 | inline void | |
81 | alloc_construct(A& a, T* p) | |
82 | { | |
83 | std::allocator_traits<A>::construct(a, p); | |
84 | } | |
85 | ||
86 | #if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES) | |
87 | #if !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES) | |
88 | template<class A, class T, class U, class... V> | |
89 | inline void | |
90 | alloc_construct(A& a, T* p, U&& u, V&&... v) | |
91 | { | |
92 | std::allocator_traits<A>::construct(a, p, std::forward<U>(u), | |
93 | std::forward<V>(v)...); | |
94 | } | |
95 | #else | |
96 | template<class A, class T, class U> | |
97 | inline void | |
98 | alloc_construct(A& a, T* p, U&& u) | |
99 | { | |
100 | std::allocator_traits<A>::construct(a, p, std::forward<U>(u)); | |
101 | } | |
102 | #endif | |
103 | #else | |
104 | template<class A, class T, class U> | |
105 | inline void | |
106 | alloc_construct(A& a, T* p, const U& u) | |
107 | { | |
108 | std::allocator_traits<A>::construct(a, p, u); | |
109 | } | |
110 | ||
111 | template<class A, class T, class U> | |
112 | inline void | |
113 | alloc_construct(A& a, T* p, U& u) | |
114 | { | |
115 | std::allocator_traits<A>::construct(a, p, u); | |
116 | } | |
117 | #endif | |
118 | ||
119 | template<class A, class T> | |
120 | inline void | |
121 | alloc_construct_n(A& a, T* p, std::size_t n) | |
122 | { | |
123 | detail::alloc_destroyer<A, T> hold(a, p); | |
124 | for (std::size_t& i = hold.size(); i < n; ++i) { | |
125 | std::allocator_traits<A>::construct(a, p + i); | |
126 | } | |
127 | hold.size() = 0; | |
128 | } | |
129 | ||
130 | template<class A, class T> | |
131 | inline void | |
132 | alloc_construct_n(A& a, T* p, std::size_t n, const T* l, std::size_t m) | |
133 | { | |
134 | detail::alloc_destroyer<A, T> hold(a, p); | |
135 | for (std::size_t& i = hold.size(); i < n; ++i) { | |
136 | std::allocator_traits<A>::construct(a, p + i, l[i % m]); | |
137 | } | |
138 | hold.size() = 0; | |
139 | } | |
140 | ||
141 | template<class A, class T, class I> | |
142 | inline void | |
143 | alloc_construct_n(A& a, T* p, std::size_t n, I b) | |
144 | { | |
145 | detail::alloc_destroyer<A, T> hold(a, p); | |
146 | for (std::size_t& i = hold.size(); i < n; void(++i), void(++b)) { | |
147 | std::allocator_traits<A>::construct(a, p + i, *b); | |
148 | } | |
149 | hold.size() = 0; | |
150 | } | |
151 | #else | |
152 | template<class A, class T> | |
153 | inline void | |
154 | alloc_construct(A&, T* p) | |
155 | { | |
156 | ::new(static_cast<void*>(p)) T(); | |
157 | } | |
158 | ||
159 | template<class A, class T> | |
160 | inline void | |
161 | alloc_construct(noinit_adaptor<A>&, T* p) | |
162 | { | |
163 | ::new(static_cast<void*>(p)) T; | |
164 | } | |
165 | ||
166 | #if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES) | |
167 | #if !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES) | |
168 | template<class A, class T, class U, class... V> | |
169 | inline void | |
170 | alloc_construct(A&, T* p, U&& u, V&&... v) | |
171 | { | |
172 | ::new(static_cast<void*>(p)) T(std::forward<U>(u), std::forward<V>(v)...); | |
173 | } | |
174 | #else | |
175 | template<class A, class T, class U> | |
176 | inline void | |
177 | alloc_construct(A& a, T* p, U&& u) | |
178 | { | |
179 | ::new(static_cast<void*>(p)) T(std::forward<U>(u)); | |
180 | } | |
181 | #endif | |
182 | #else | |
183 | template<class A, class T, class U> | |
184 | inline void | |
185 | alloc_construct(A&, T* p, const U& u) | |
186 | { | |
187 | ::new(static_cast<void*>(p)) T(u); | |
188 | } | |
189 | ||
190 | template<class A, class T, class U> | |
191 | inline void | |
192 | alloc_construct(A&, T* p, U& u) | |
193 | { | |
194 | ::new(static_cast<void*>(p)) T(u); | |
195 | } | |
196 | #endif | |
197 | ||
198 | template<class A, class T> | |
199 | inline void | |
200 | alloc_construct_n(A& a, T* p, std::size_t n) | |
201 | { | |
202 | detail::alloc_destroyer<A, T> hold(a, p); | |
203 | for (std::size_t& i = hold.size(); i < n; ++i) { | |
204 | ::new(static_cast<void*>(p + i)) T(); | |
205 | } | |
206 | hold.size() = 0; | |
207 | } | |
208 | ||
209 | template<class A, class T> | |
210 | inline void | |
211 | alloc_construct_n(noinit_adaptor<A>& a, T* p, std::size_t n) | |
212 | { | |
213 | detail::alloc_destroyer<noinit_adaptor<A>, T> hold(a, p); | |
214 | for (std::size_t& i = hold.size(); i < n; ++i) { | |
215 | ::new(static_cast<void*>(p + i)) T; | |
216 | } | |
217 | hold.size() = 0; | |
218 | } | |
219 | ||
220 | template<class A, class T> | |
221 | inline void | |
222 | alloc_construct_n(A& a, T* p, std::size_t n, const T* l, std::size_t m) | |
223 | { | |
224 | detail::alloc_destroyer<A, T> hold(a, p); | |
225 | for (std::size_t& i = hold.size(); i < n; ++i) { | |
226 | ::new(static_cast<void*>(p + i)) T(l[i % m]); | |
227 | } | |
228 | hold.size() = 0; | |
229 | } | |
230 | ||
231 | template<class A, class T, class I> | |
232 | inline void | |
233 | alloc_construct_n(A& a, T* p, std::size_t n, I b) | |
234 | { | |
235 | detail::alloc_destroyer<A, T> hold(a, p); | |
236 | for (std::size_t& i = hold.size(); i < n; void(++i), void(++b)) { | |
237 | ::new(static_cast<void*>(p + i)) T(*b); | |
238 | } | |
239 | hold.size() = 0; | |
240 | } | |
241 | #endif | |
242 | ||
243 | } /* boost */ | |
244 | ||
245 | #endif |