1 /* Copyright 2003-2009 Joaquin M Lopez Munoz.
\r
2 * Distributed under the Boost Software License, Version 1.0.
\r
3 * (See accompanying file LICENSE_1_0.txt or copy at
\r
4 * http://www.boost.org/LICENSE_1_0.txt)
\r
6 * See Boost website at http://www.boost.org/
\r
9 #ifndef BOOST_DETAIL_ALLOCATOR_UTILITIES_HPP
\r
10 #define BOOST_DETAIL_ALLOCATOR_UTILITIES_HPP
\r
12 #include <boost/config.hpp> /* keep it first to prevent nasty warns in MSVC */
\r
13 #include <boost/detail/workaround.hpp>
\r
14 #include <boost/mpl/aux_/msvc_never_true.hpp>
\r
15 #include <boost/mpl/eval_if.hpp>
\r
16 #include <boost/type_traits/is_same.hpp>
\r
25 /* Allocator adaption layer. Some stdlibs provide allocators without rebind
\r
26 * and template ctors. These facilities are simulated with the external
\r
27 * template class rebind_to and the aid of partial_std_allocator_wrapper.
\r
30 namespace allocator{
\r
32 /* partial_std_allocator_wrapper inherits the functionality of a std
\r
33 * allocator while providing a templatized ctor and other bits missing
\r
34 * in some stdlib implementation or another.
\r
37 template<typename Type>
\r
38 class partial_std_allocator_wrapper:public std::allocator<Type>
\r
41 /* Oddly enough, STLport does not define std::allocator<void>::value_type
\r
42 * when configured to work without partial template specialization.
\r
43 * No harm in supplying the definition here unconditionally.
\r
46 typedef Type value_type;
\r
48 partial_std_allocator_wrapper(){};
\r
50 template<typename Other>
\r
51 partial_std_allocator_wrapper(const partial_std_allocator_wrapper<Other>&){}
\r
53 partial_std_allocator_wrapper(const std::allocator<Type>& x):
\r
54 std::allocator<Type>(x)
\r
58 #if defined(BOOST_DINKUMWARE_STDLIB)
\r
59 /* Dinkumware guys didn't provide a means to call allocate() without
\r
60 * supplying a hint, in disagreement with the standard.
\r
63 Type* allocate(std::size_t n,const void* hint=0)
\r
65 std::allocator<Type>& a=*this;
\r
66 return a.allocate(n,hint);
\r
72 /* Detects whether a given allocator belongs to a defective stdlib not
\r
73 * having the required member templates.
\r
74 * Note that it does not suffice to check the Boost.Config stdlib
\r
75 * macros, as the user might have passed a custom, compliant allocator.
\r
76 * The checks also considers partial_std_allocator_wrapper to be
\r
77 * a standard defective allocator.
\r
80 #if defined(BOOST_NO_STD_ALLOCATOR)&&\
\r
81 (defined(BOOST_HAS_PARTIAL_STD_ALLOCATOR)||defined(BOOST_DINKUMWARE_STDLIB))
\r
83 template<typename Allocator>
\r
84 struct is_partial_std_allocator
\r
86 BOOST_STATIC_CONSTANT(bool,
\r
89 std::allocator<BOOST_DEDUCED_TYPENAME Allocator::value_type>,
\r
93 partial_std_allocator_wrapper<
\r
94 BOOST_DEDUCED_TYPENAME Allocator::value_type>,
\r
101 template<typename Allocator>
\r
102 struct is_partial_std_allocator
\r
104 BOOST_STATIC_CONSTANT(bool,value=false);
\r
109 /* rebind operations for defective std allocators */
\r
111 template<typename Allocator,typename Type>
\r
112 struct partial_std_allocator_rebind_to
\r
114 typedef partial_std_allocator_wrapper<Type> type;
\r
117 /* rebind operation in all other cases */
\r
119 #if BOOST_WORKAROUND(BOOST_MSVC,<1300)
\r
120 /* Workaround for a problem in MSVC with dependent template typedefs
\r
121 * when doing rebinding of allocators.
\r
122 * Modeled after <boost/mpl/aux_/msvc_dtw.hpp> (thanks, Aleksey!)
\r
125 template<typename Allocator>
\r
128 template<bool> struct fake_allocator:Allocator{};
\r
129 template<> struct fake_allocator<true>
\r
131 template<typename Type> struct rebind{};
\r
134 template<typename Type>
\r
136 fake_allocator<mpl::aux::msvc_never_true<Allocator>::value>::
\r
137 template rebind<Type>
\r
142 template<typename Allocator>
\r
145 template<typename Type>
\r
148 typedef typename Allocator::BOOST_NESTED_TEMPLATE
\r
149 rebind<Type>::other other;
\r
154 template<typename Allocator,typename Type>
\r
155 struct compliant_allocator_rebind_to
\r
157 typedef typename rebinder<Allocator>::
\r
158 BOOST_NESTED_TEMPLATE result<Type>::other type;
\r
161 /* rebind front-end */
\r
163 template<typename Allocator,typename Type>
\r
166 is_partial_std_allocator<Allocator>::value,
\r
167 partial_std_allocator_rebind_to<Allocator,Type>,
\r
168 compliant_allocator_rebind_to<Allocator,Type>
\r
173 /* allocator-independent versions of construct and destroy */
\r
175 template<typename Type>
\r
176 void construct(void* p,const Type& t)
\r
181 #if BOOST_WORKAROUND(BOOST_MSVC,BOOST_TESTED_AT(1500))
\r
182 /* MSVC++ issues spurious warnings about unreferencend formal parameters
\r
183 * in destroy<Type> when Type is a class with trivial dtor.
\r
186 #pragma warning(push)
\r
187 #pragma warning(disable:4100)
\r
190 template<typename Type>
\r
191 void destroy(const Type* p)
\r
194 #if BOOST_WORKAROUND(__SUNPRO_CC,BOOST_TESTED_AT(0x590))
\r
195 const_cast<Type*>(p)->~Type();
\r
202 #if BOOST_WORKAROUND(BOOST_MSVC,BOOST_TESTED_AT(1500))
\r
203 #pragma warning(pop)
\r
206 } /* namespace boost::detail::allocator */
\r
208 } /* namespace boost::detail */
\r
210 } /* namespace boost */
\r