2 * Copyright (c) 2012-2014 Glen Joseph Fernandes
3 * glenfe at live dot com
5 * Distributed under the Boost Software License,
6 * Version 1.0. (See accompanying file LICENSE_1_0.txt
7 * or copy at http://boost.org/LICENSE_1_0.txt)
9 #ifndef BOOST_SMART_PTR_DETAIL_ARRAY_UTILITY_HPP
10 #define BOOST_SMART_PTR_DETAIL_ARRAY_UTILITY_HPP
12 #include <boost/config.hpp>
13 #include <boost/type_traits/has_trivial_constructor.hpp>
14 #include <boost/type_traits/has_trivial_destructor.hpp>
15 #if !defined(BOOST_NO_CXX11_ALLOCATOR)
21 typedef boost::true_type ms_is_trivial;
22 typedef boost::false_type ms_no_trivial;
25 inline void ms_destroy(T*, std::size_t, ms_is_trivial) {
29 inline void ms_destroy(T* memory, std::size_t size, ms_no_trivial) {
30 for (std::size_t i = size; i > 0;) {
36 inline void ms_destroy(T* memory, std::size_t size) {
37 boost::has_trivial_destructor<T> trivial;
38 ms_destroy(memory, size, trivial);
42 inline void ms_init(T* memory, std::size_t size, ms_is_trivial) {
43 for (std::size_t i = 0; i < size; i++) {
44 void* p1 = memory + i;
50 inline void ms_init(T* memory, std::size_t size, ms_no_trivial) {
51 #if !defined(BOOST_NO_EXCEPTIONS)
54 for (; i < size; i++) {
55 void* p1 = memory + i;
59 ms_destroy(memory, i);
63 for (std::size_t i = 0; i < size; i++) {
64 void* p1 = memory + i;
71 inline void ms_init(T* memory, std::size_t size) {
72 boost::has_trivial_default_constructor<T> trivial;
73 ms_init(memory, size, trivial);
76 template<class T, std::size_t N>
77 inline void ms_init(T* memory, std::size_t size, const T* list) {
78 #if !defined(BOOST_NO_EXCEPTIONS)
81 for (; i < size; i++) {
82 void* p1 = memory + i;
83 ::new(p1) T(list[i % N]);
86 ms_destroy(memory, i);
90 for (std::size_t i = 0; i < size; i++) {
91 void* p1 = memory + i;
92 ::new(p1) T(list[i % N]);
97 #if !defined(BOOST_NO_CXX11_ALLOCATOR)
98 template<class T, class A>
99 inline void as_destroy(const A& allocator, T* memory,
101 typedef typename std::allocator_traits<A>::
102 template rebind_alloc<T> TA;
103 typedef typename std::allocator_traits<A>::
104 template rebind_traits<T> TT;
106 for (std::size_t i = size; i > 0;) {
107 TT::destroy(a2, &memory[--i]);
111 template<class T, class A>
112 inline void as_init(const A& allocator, T* memory, std::size_t size,
114 typedef typename std::allocator_traits<A>::
115 template rebind_alloc<T> TA;
116 typedef typename std::allocator_traits<A>::
117 template rebind_traits<T> TT;
119 for (std::size_t i = 0; i < size; i++) {
120 TT::construct(a2, memory + i);
124 template<class T, class A>
125 inline void as_init(const A& allocator, T* memory, std::size_t size,
127 typedef typename std::allocator_traits<A>::
128 template rebind_alloc<T> TA;
129 typedef typename std::allocator_traits<A>::
130 template rebind_traits<T> TT;
132 #if !defined(BOOST_NO_EXCEPTIONS)
135 for (; i < size; i++) {
136 TT::construct(a2, memory + i);
139 as_destroy(a2, memory, i);
143 for (std::size_t i = 0; i < size; i++) {
144 TT::construct(a2, memory + i);
149 template<class T, class A>
150 inline void as_init(const A& allocator, T* memory, std::size_t size) {
151 boost::has_trivial_default_constructor<T> trivial;
152 as_init(allocator, memory, size, trivial);
155 template<class T, class A, std::size_t N>
156 inline void as_init(const A& allocator, T* memory, std::size_t size,
158 typedef typename std::allocator_traits<A>::
159 template rebind_alloc<T> TA;
160 typedef typename std::allocator_traits<A>::
161 template rebind_traits<T> TT;
163 #if !defined(BOOST_NO_EXCEPTIONS)
166 for (; i < size; i++) {
167 TT::construct(a2, memory + i, list[i % N]);
170 as_destroy(a2, memory, i);
174 for (std::size_t i = 0; i < size; i++) {
175 TT::construct(a2, memory + i, list[i % N]);
182 inline void ms_noinit(T*, std::size_t, ms_is_trivial) {
186 inline void ms_noinit(T* memory, std::size_t size, ms_no_trivial) {
187 #if !defined(BOOST_NO_EXCEPTIONS)
190 for (; i < size; i++) {
191 void* p1 = memory + i;
195 ms_destroy(memory, i);
199 for (std::size_t i = 0; i < size; i++) {
200 void* p1 = memory + i;
207 inline void ms_noinit(T* memory, std::size_t size) {
208 boost::has_trivial_default_constructor<T> trivial;
209 ms_noinit(memory, size, trivial);