1 /* Copyright 2003-2013 Joaquin M Lopez Munoz.
2 * Distributed under the Boost Software License, Version 1.0.
3 * (See accompanying file LICENSE_1_0.txt or copy at
4 * http://www.boost.org/LICENSE_1_0.txt)
6 * See Boost website at http://www.boost.org/
9 #ifndef BOOST_DETAIL_ALLOCATOR_UTILITIES_HPP
10 #define BOOST_DETAIL_ALLOCATOR_UTILITIES_HPP
12 #include <boost/config.hpp> /* keep it first to prevent nasty warns in MSVC */
13 #include <boost/detail/workaround.hpp>
14 #include <boost/mpl/eval_if.hpp>
15 #include <boost/type_traits/is_same.hpp>
24 /* Allocator adaption layer. Some stdlibs provide allocators without rebind
25 * and template ctors. These facilities are simulated with the external
26 * template class rebind_to and the aid of partial_std_allocator_wrapper.
31 /* partial_std_allocator_wrapper inherits the functionality of a std
32 * allocator while providing a templatized ctor and other bits missing
33 * in some stdlib implementation or another.
36 template<typename Type>
37 class partial_std_allocator_wrapper:public std::allocator<Type>
40 /* Oddly enough, STLport does not define std::allocator<void>::value_type
41 * when configured to work without partial template specialization.
42 * No harm in supplying the definition here unconditionally.
45 typedef Type value_type;
47 partial_std_allocator_wrapper(){};
49 template<typename Other>
50 partial_std_allocator_wrapper(const partial_std_allocator_wrapper<Other>&){}
52 partial_std_allocator_wrapper(const std::allocator<Type>& x):
53 std::allocator<Type>(x)
57 #if defined(BOOST_DINKUMWARE_STDLIB)
58 /* Dinkumware guys didn't provide a means to call allocate() without
59 * supplying a hint, in disagreement with the standard.
62 Type* allocate(std::size_t n,const void* hint=0)
64 std::allocator<Type>& a=*this;
65 return a.allocate(n,hint);
71 /* Detects whether a given allocator belongs to a defective stdlib not
72 * having the required member templates.
73 * Note that it does not suffice to check the Boost.Config stdlib
74 * macros, as the user might have passed a custom, compliant allocator.
75 * The checks also considers partial_std_allocator_wrapper to be
76 * a standard defective allocator.
79 #if defined(BOOST_NO_STD_ALLOCATOR)&&\
80 (defined(BOOST_HAS_PARTIAL_STD_ALLOCATOR)||defined(BOOST_DINKUMWARE_STDLIB))
82 template<typename Allocator>
83 struct is_partial_std_allocator
85 BOOST_STATIC_CONSTANT(bool,
88 std::allocator<BOOST_DEDUCED_TYPENAME Allocator::value_type>,
92 partial_std_allocator_wrapper<
93 BOOST_DEDUCED_TYPENAME Allocator::value_type>,
100 template<typename Allocator>
101 struct is_partial_std_allocator
103 BOOST_STATIC_CONSTANT(bool,value=false);
108 /* rebind operations for defective std allocators */
110 template<typename Allocator,typename Type>
111 struct partial_std_allocator_rebind_to
113 typedef partial_std_allocator_wrapper<Type> type;
116 /* rebind operation in all other cases */
118 template<typename Allocator>
121 template<typename Type>
124 typedef typename Allocator::BOOST_NESTED_TEMPLATE
125 rebind<Type>::other other;
129 template<typename Allocator,typename Type>
130 struct compliant_allocator_rebind_to
132 typedef typename rebinder<Allocator>::
133 BOOST_NESTED_TEMPLATE result<Type>::other type;
136 /* rebind front-end */
138 template<typename Allocator,typename Type>
141 is_partial_std_allocator<Allocator>::value,
142 partial_std_allocator_rebind_to<Allocator,Type>,
143 compliant_allocator_rebind_to<Allocator,Type>
148 /* allocator-independent versions of construct and destroy */
150 template<typename Type>
151 void construct(void* p,const Type& t)
156 #if BOOST_WORKAROUND(BOOST_MSVC,BOOST_TESTED_AT(1500))
157 /* MSVC++ issues spurious warnings about unreferencend formal parameters
158 * in destroy<Type> when Type is a class with trivial dtor.
161 #pragma warning(push)
162 #pragma warning(disable:4100)
165 template<typename Type>
166 void destroy(const Type* p)
169 #if BOOST_WORKAROUND(__SUNPRO_CC,BOOST_TESTED_AT(0x590))
170 const_cast<Type*>(p)->~Type();
177 #if BOOST_WORKAROUND(BOOST_MSVC,BOOST_TESTED_AT(1500))
181 } /* namespace boost::detail::allocator */
183 } /* namespace boost::detail */
185 } /* namespace boost */