1/*
2Copyright 2019 Glen Joseph Fernandes
3(glenjofe@gmail.com)
4
5Distributed 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
13namespace boost {
14
15template<class A, class T>
16inline void
17alloc_destroy(A& a, T* p)
18{
19 boost::allocator_destroy(a, p);
20}
21
22template<class A, class T>
23inline void
24alloc_destroy_n(A& a, T* p, std::size_t n)
25{
26 while (n > 0) {
27 boost::allocator_destroy(a, p + --n);
28 }
29}
30
31template<class A, class T>
32inline void
33alloc_destroy(noinit_adaptor<A>&, T* p)
34{
35 p->~T();
36}
37
38template<class A, class T>
39inline void
40alloc_destroy_n(noinit_adaptor<A>&, T* p, std::size_t n)
41{
42 while (n > 0) {
43 p[--n].~T();
44 }
45}
46
47namespace detail {
48
49template<class A, class T>
50class alloc_destroyer {
51public:
52 alloc_destroyer(A& a, T* p) BOOST_NOEXCEPT
53 : a_(a),
54 p_(p),
55 n_(0) { }
56
57 ~alloc_destroyer() {
58 boost::alloc_destroy_n(a_, p_, n_);
59 }
60
61 std::size_t& size() BOOST_NOEXCEPT {
62 return n_;
63 }
64
65private:
66 alloc_destroyer(const alloc_destroyer&);
67 alloc_destroyer& operator=(const alloc_destroyer&);
68
69 A& a_;
70 T* p_;
71 std::size_t n_;
72};
73
74} /* detail */
75
76template<class A, class T>
77inline void
78alloc_construct(A& a, T* p)
79{
80 boost::allocator_construct(a, p);
81}
82
83#if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES)
84#if !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES)
85template<class A, class T, class U, class... V>
86inline void
87alloc_construct(A& a, T* p, U&& u, V&&... v)
88{
89 boost::allocator_construct(a, p, std::forward<U>(u),
90 std::forward<V>(v)...);
91}
92#else
93template<class A, class T, class U>
94inline void
95alloc_construct(A& a, T* p, U&& u)
96{
97 boost::allocator_construct(a, p, std::forward<U>(u));
98}
99#endif
100#else
101template<class A, class T, class U>
102inline void
103alloc_construct(A& a, T* p, const U& u)
104{
105 boost::allocator_construct(a, p, u);
106}
107
108template<class A, class T, class U>
109inline void
110alloc_construct(A& a, T* p, U& u)
111{
112 boost::allocator_construct(a, p, u);
113}
114#endif
115
116template<class A, class T>
117inline void
118alloc_construct_n(A& a, T* p, std::size_t n)
119{
120 detail::alloc_destroyer<A, T> hold(a, p);
121 for (std::size_t& i = hold.size(); i < n; ++i) {
122 boost::allocator_construct(a, p + i);
123 }
124 hold.size() = 0;
125}
126
127template<class A, class T>
128inline void
129alloc_construct_n(A& a, T* p, std::size_t n, const T* l, std::size_t m)
130{
131 detail::alloc_destroyer<A, T> hold(a, p);
132 for (std::size_t& i = hold.size(); i < n; ++i) {
133 boost::allocator_construct(a, p + i, l[i % m]);
134 }
135 hold.size() = 0;
136}
137
138template<class A, class T, class I>
139inline void
140alloc_construct_n(A& a, T* p, std::size_t n, I b)
141{
142 detail::alloc_destroyer<A, T> hold(a, p);
143 for (std::size_t& i = hold.size(); i < n; void(++i), void(++b)) {
144 boost::allocator_construct(a, p + i, *b);
145 }
146 hold.size() = 0;
147}
148
149template<class A, class T>
150inline void
151alloc_construct(noinit_adaptor<A>&, T* p)
152{
153 ::new(static_cast<void*>(p)) T;
154}
155
156template<class A, class T>
157inline void
158alloc_construct_n(noinit_adaptor<A>& a, T* p, std::size_t n)
159{
160 detail::alloc_destroyer<noinit_adaptor<A>, T> hold(a, p);
161 for (std::size_t& i = hold.size(); i < n; ++i) {
162 ::new(static_cast<void*>(p + i)) T;
163 }
164 hold.size() = 0;
165}
166
167} /* boost */
168
169#endif
170

source code of include/boost/core/alloc_construct.hpp