Chris@101
|
1 /* Copyright 2003-2013 Joaquin M Lopez Munoz.
|
Chris@16
|
2 * Distributed under the Boost Software License, Version 1.0.
|
Chris@16
|
3 * (See accompanying file LICENSE_1_0.txt or copy at
|
Chris@16
|
4 * http://www.boost.org/LICENSE_1_0.txt)
|
Chris@16
|
5 *
|
Chris@16
|
6 * See Boost website at http://www.boost.org/
|
Chris@16
|
7 */
|
Chris@16
|
8
|
Chris@16
|
9 #ifndef BOOST_DETAIL_ALLOCATOR_UTILITIES_HPP
|
Chris@16
|
10 #define BOOST_DETAIL_ALLOCATOR_UTILITIES_HPP
|
Chris@16
|
11
|
Chris@16
|
12 #include <boost/config.hpp> /* keep it first to prevent nasty warns in MSVC */
|
Chris@16
|
13 #include <boost/detail/workaround.hpp>
|
Chris@16
|
14 #include <boost/mpl/eval_if.hpp>
|
Chris@16
|
15 #include <boost/type_traits/is_same.hpp>
|
Chris@16
|
16 #include <cstddef>
|
Chris@16
|
17 #include <memory>
|
Chris@16
|
18 #include <new>
|
Chris@16
|
19
|
Chris@16
|
20 namespace boost{
|
Chris@16
|
21
|
Chris@16
|
22 namespace detail{
|
Chris@16
|
23
|
Chris@16
|
24 /* Allocator adaption layer. Some stdlibs provide allocators without rebind
|
Chris@16
|
25 * and template ctors. These facilities are simulated with the external
|
Chris@16
|
26 * template class rebind_to and the aid of partial_std_allocator_wrapper.
|
Chris@16
|
27 */
|
Chris@16
|
28
|
Chris@16
|
29 namespace allocator{
|
Chris@16
|
30
|
Chris@16
|
31 /* partial_std_allocator_wrapper inherits the functionality of a std
|
Chris@16
|
32 * allocator while providing a templatized ctor and other bits missing
|
Chris@16
|
33 * in some stdlib implementation or another.
|
Chris@16
|
34 */
|
Chris@16
|
35
|
Chris@16
|
36 template<typename Type>
|
Chris@16
|
37 class partial_std_allocator_wrapper:public std::allocator<Type>
|
Chris@16
|
38 {
|
Chris@16
|
39 public:
|
Chris@16
|
40 /* Oddly enough, STLport does not define std::allocator<void>::value_type
|
Chris@16
|
41 * when configured to work without partial template specialization.
|
Chris@16
|
42 * No harm in supplying the definition here unconditionally.
|
Chris@16
|
43 */
|
Chris@16
|
44
|
Chris@16
|
45 typedef Type value_type;
|
Chris@16
|
46
|
Chris@16
|
47 partial_std_allocator_wrapper(){};
|
Chris@16
|
48
|
Chris@16
|
49 template<typename Other>
|
Chris@16
|
50 partial_std_allocator_wrapper(const partial_std_allocator_wrapper<Other>&){}
|
Chris@16
|
51
|
Chris@16
|
52 partial_std_allocator_wrapper(const std::allocator<Type>& x):
|
Chris@16
|
53 std::allocator<Type>(x)
|
Chris@16
|
54 {
|
Chris@16
|
55 };
|
Chris@16
|
56
|
Chris@16
|
57 #if defined(BOOST_DINKUMWARE_STDLIB)
|
Chris@16
|
58 /* Dinkumware guys didn't provide a means to call allocate() without
|
Chris@16
|
59 * supplying a hint, in disagreement with the standard.
|
Chris@16
|
60 */
|
Chris@16
|
61
|
Chris@16
|
62 Type* allocate(std::size_t n,const void* hint=0)
|
Chris@16
|
63 {
|
Chris@16
|
64 std::allocator<Type>& a=*this;
|
Chris@16
|
65 return a.allocate(n,hint);
|
Chris@16
|
66 }
|
Chris@16
|
67 #endif
|
Chris@16
|
68
|
Chris@16
|
69 };
|
Chris@16
|
70
|
Chris@16
|
71 /* Detects whether a given allocator belongs to a defective stdlib not
|
Chris@16
|
72 * having the required member templates.
|
Chris@16
|
73 * Note that it does not suffice to check the Boost.Config stdlib
|
Chris@16
|
74 * macros, as the user might have passed a custom, compliant allocator.
|
Chris@16
|
75 * The checks also considers partial_std_allocator_wrapper to be
|
Chris@16
|
76 * a standard defective allocator.
|
Chris@16
|
77 */
|
Chris@16
|
78
|
Chris@16
|
79 #if defined(BOOST_NO_STD_ALLOCATOR)&&\
|
Chris@16
|
80 (defined(BOOST_HAS_PARTIAL_STD_ALLOCATOR)||defined(BOOST_DINKUMWARE_STDLIB))
|
Chris@16
|
81
|
Chris@16
|
82 template<typename Allocator>
|
Chris@16
|
83 struct is_partial_std_allocator
|
Chris@16
|
84 {
|
Chris@16
|
85 BOOST_STATIC_CONSTANT(bool,
|
Chris@16
|
86 value=
|
Chris@16
|
87 (is_same<
|
Chris@16
|
88 std::allocator<BOOST_DEDUCED_TYPENAME Allocator::value_type>,
|
Chris@16
|
89 Allocator
|
Chris@16
|
90 >::value)||
|
Chris@16
|
91 (is_same<
|
Chris@16
|
92 partial_std_allocator_wrapper<
|
Chris@16
|
93 BOOST_DEDUCED_TYPENAME Allocator::value_type>,
|
Chris@16
|
94 Allocator
|
Chris@16
|
95 >::value));
|
Chris@16
|
96 };
|
Chris@16
|
97
|
Chris@16
|
98 #else
|
Chris@16
|
99
|
Chris@16
|
100 template<typename Allocator>
|
Chris@16
|
101 struct is_partial_std_allocator
|
Chris@16
|
102 {
|
Chris@16
|
103 BOOST_STATIC_CONSTANT(bool,value=false);
|
Chris@16
|
104 };
|
Chris@16
|
105
|
Chris@16
|
106 #endif
|
Chris@16
|
107
|
Chris@16
|
108 /* rebind operations for defective std allocators */
|
Chris@16
|
109
|
Chris@16
|
110 template<typename Allocator,typename Type>
|
Chris@16
|
111 struct partial_std_allocator_rebind_to
|
Chris@16
|
112 {
|
Chris@16
|
113 typedef partial_std_allocator_wrapper<Type> type;
|
Chris@16
|
114 };
|
Chris@16
|
115
|
Chris@16
|
116 /* rebind operation in all other cases */
|
Chris@16
|
117
|
Chris@16
|
118 template<typename Allocator>
|
Chris@16
|
119 struct rebinder
|
Chris@16
|
120 {
|
Chris@16
|
121 template<typename Type>
|
Chris@16
|
122 struct result
|
Chris@16
|
123 {
|
Chris@16
|
124 typedef typename Allocator::BOOST_NESTED_TEMPLATE
|
Chris@16
|
125 rebind<Type>::other other;
|
Chris@16
|
126 };
|
Chris@16
|
127 };
|
Chris@16
|
128
|
Chris@16
|
129 template<typename Allocator,typename Type>
|
Chris@16
|
130 struct compliant_allocator_rebind_to
|
Chris@16
|
131 {
|
Chris@16
|
132 typedef typename rebinder<Allocator>::
|
Chris@16
|
133 BOOST_NESTED_TEMPLATE result<Type>::other type;
|
Chris@16
|
134 };
|
Chris@16
|
135
|
Chris@16
|
136 /* rebind front-end */
|
Chris@16
|
137
|
Chris@16
|
138 template<typename Allocator,typename Type>
|
Chris@16
|
139 struct rebind_to:
|
Chris@16
|
140 mpl::eval_if_c<
|
Chris@16
|
141 is_partial_std_allocator<Allocator>::value,
|
Chris@16
|
142 partial_std_allocator_rebind_to<Allocator,Type>,
|
Chris@16
|
143 compliant_allocator_rebind_to<Allocator,Type>
|
Chris@16
|
144 >
|
Chris@16
|
145 {
|
Chris@16
|
146 };
|
Chris@16
|
147
|
Chris@16
|
148 /* allocator-independent versions of construct and destroy */
|
Chris@16
|
149
|
Chris@16
|
150 template<typename Type>
|
Chris@16
|
151 void construct(void* p,const Type& t)
|
Chris@16
|
152 {
|
Chris@16
|
153 new (p) Type(t);
|
Chris@16
|
154 }
|
Chris@16
|
155
|
Chris@16
|
156 #if BOOST_WORKAROUND(BOOST_MSVC,BOOST_TESTED_AT(1500))
|
Chris@16
|
157 /* MSVC++ issues spurious warnings about unreferencend formal parameters
|
Chris@16
|
158 * in destroy<Type> when Type is a class with trivial dtor.
|
Chris@16
|
159 */
|
Chris@16
|
160
|
Chris@16
|
161 #pragma warning(push)
|
Chris@16
|
162 #pragma warning(disable:4100)
|
Chris@16
|
163 #endif
|
Chris@16
|
164
|
Chris@16
|
165 template<typename Type>
|
Chris@16
|
166 void destroy(const Type* p)
|
Chris@16
|
167 {
|
Chris@16
|
168
|
Chris@16
|
169 #if BOOST_WORKAROUND(__SUNPRO_CC,BOOST_TESTED_AT(0x590))
|
Chris@16
|
170 const_cast<Type*>(p)->~Type();
|
Chris@16
|
171 #else
|
Chris@16
|
172 p->~Type();
|
Chris@16
|
173 #endif
|
Chris@16
|
174
|
Chris@16
|
175 }
|
Chris@16
|
176
|
Chris@16
|
177 #if BOOST_WORKAROUND(BOOST_MSVC,BOOST_TESTED_AT(1500))
|
Chris@16
|
178 #pragma warning(pop)
|
Chris@16
|
179 #endif
|
Chris@16
|
180
|
Chris@16
|
181 } /* namespace boost::detail::allocator */
|
Chris@16
|
182
|
Chris@16
|
183 } /* namespace boost::detail */
|
Chris@16
|
184
|
Chris@16
|
185 } /* namespace boost */
|
Chris@16
|
186
|
Chris@16
|
187 #endif
|