blob: 31e0ceb949823bbcb46e3007c07cb8ea70c7a8be [file] [log] [blame]
Jeff Thompsona28eed82013-08-22 16:21:10 -07001//////////////////////////////////////////////////////////////////////////////
2//
3// (C) Copyright Pablo Halpern 2009. Distributed under the Boost
4// Software License, Version 1.0. (See accompanying file
5// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6//
7//////////////////////////////////////////////////////////////////////////////
8//
9// (C) Copyright Ion Gaztanaga 2011-2012. Distributed under the Boost
10// Software License, Version 1.0. (See accompanying file
11// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
12//
13// See http://www.boost.org/libs/container for documentation.
14//
15//////////////////////////////////////////////////////////////////////////////
16
17#ifndef BOOST_CONTAINER_ALLOCATOR_ALLOCATOR_TRAITS_HPP
18#define BOOST_CONTAINER_ALLOCATOR_ALLOCATOR_TRAITS_HPP
19
20#if (defined _MSC_VER) && (_MSC_VER >= 1200)
21# pragma once
22#endif
23
24#include <ndnboost/container/detail/config_begin.hpp>
25#include <ndnboost/container/detail/workaround.hpp>
26#include <ndnboost/intrusive/pointer_traits.hpp>
27#include <ndnboost/intrusive/detail/memory_util.hpp>
28#include <ndnboost/container/detail/memory_util.hpp>
29#include <ndnboost/type_traits/integral_constant.hpp>
30#include <ndnboost/container/detail/mpl.hpp>
31#include <ndnboost/move/utility.hpp>
32#include <limits> //numeric_limits<>::max()
33#include <new> //placement new
34#include <memory> //std::allocator
35
36#if defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES)
37#include <ndnboost/container/detail/preprocessor.hpp>
38#endif
39
40///@cond
41
42namespace ndnboost {
43namespace container {
44namespace container_detail {
45
46//workaround needed for C++03 compilers with no construct()
47//supporting rvalue references
48template<class A>
49struct is_std_allocator
50{ static const bool value = false; };
51
52template<class T>
53struct is_std_allocator< std::allocator<T> >
54{ static const bool value = true; };
55
56} //namespace container_detail {
57
58///@endcond
59
60//! The class template allocator_traits supplies a uniform interface to all allocator types.
61//! This class is a C++03-compatible implementation of std::allocator_traits
62template <typename Alloc>
63struct allocator_traits
64{
65 //allocator_type
66 typedef Alloc allocator_type;
67 //value_type
68 typedef typename Alloc::value_type value_type;
69
70 #if defined(BOOST_CONTAINER_DOXYGEN_INVOKED)
71 //! Alloc::pointer if such a type exists; otherwise, value_type*
72 //!
73 typedef unspecified pointer;
74 //! Alloc::const_pointer if such a type exists ; otherwise, pointer_traits<pointer>::rebind<const
75 //!
76 typedef see_documentation const_pointer;
77 //! Non-standard extension
78 //! Alloc::reference if such a type exists; otherwise, value_type&
79 typedef see_documentation reference;
80 //! Non-standard extension
81 //! Alloc::const_reference if such a type exists ; otherwise, const value_type&
82 typedef see_documentation const_reference;
83 //! Alloc::void_pointer if such a type exists ; otherwise, pointer_traits<pointer>::rebind<void>.
84 //!
85 typedef see_documentation void_pointer;
86 //! Alloc::const_void_pointer if such a type exists ; otherwis e, pointer_traits<pointer>::rebind<const
87 //!
88 typedef see_documentation const_void_pointer;
89 //! Alloc::difference_type if such a type exists ; otherwise, pointer_traits<pointer>::difference_type.
90 //!
91 typedef see_documentation difference_type;
92 //! Alloc::size_type if such a type exists ; otherwise, make_unsigned<difference_type>::type
93 //!
94 typedef see_documentation size_type;
95 //! Alloc::propagate_on_container_copy_assignment if such a type exists, otherwise an integral_constant
96 //! type with internal constant static member `value` == false.
97 typedef see_documentation propagate_on_container_copy_assignment;
98 //! Alloc::propagate_on_container_move_assignment if such a type exists, otherwise an integral_constant
99 //! type with internal constant static member `value` == false.
100 typedef see_documentation propagate_on_container_move_assignment;
101 //! Alloc::propagate_on_container_swap if such a type exists, otherwise an integral_constant
102 //! type with internal constant static member `value` == false.
103 typedef see_documentation propagate_on_container_swap;
104 //! Defines an allocator: Alloc::rebind<T>::other if such a type exists; otherwise, Alloc<T, Args>
105 //! if Alloc is a class template instantiation of the form Alloc<U, Args>, where Args is zero or
106 //! more type arguments ; otherwise, the instantiation of rebind_alloc is ill-formed.
107 //!
108 //! In C++03 compilers `rebind_alloc` is a struct derived from an allocator
109 //! deduced by previously detailed rules.
110 template <class T> using rebind_alloc = see_documentation;
111
112 //! In C++03 compilers `rebind_traits` is a struct derived from
113 //! `allocator_traits<OtherAlloc>`, where `OtherAlloc` is
114 //! the allocator deduced by rules explained in `rebind_alloc`.
115 template <class T> using rebind_traits = allocator_traits<rebind_alloc<T> >;
116
117 //! Non-standard extension: Portable allocator rebind for C++03 and C++11 compilers.
118 //! `type` is an allocator related to Alloc deduced deduced by rules explained in `rebind_alloc`.
119 template <class T>
120 struct portable_rebind_alloc
121 { typedef see_documentation type; };
122 #else
123 //pointer
124 typedef BOOST_INTRUSIVE_OBTAIN_TYPE_WITH_DEFAULT(ndnboost::container::container_detail::, Alloc,
125 pointer, value_type*)
126 pointer;
127 //const_pointer
128 typedef BOOST_INTRUSIVE_OBTAIN_TYPE_WITH_EVAL_DEFAULT(ndnboost::container::container_detail::, Alloc,
129 const_pointer, typename ndnboost::intrusive::pointer_traits<pointer>::template
130 rebind_pointer<const value_type>)
131 const_pointer;
132 //reference
133 typedef BOOST_INTRUSIVE_OBTAIN_TYPE_WITH_DEFAULT(ndnboost::container::container_detail::, Alloc,
134 reference, typename container_detail::unvoid<value_type>::type&)
135 reference;
136 //const_reference
137 typedef BOOST_INTRUSIVE_OBTAIN_TYPE_WITH_DEFAULT(ndnboost::container::container_detail::, Alloc,
138 const_reference, const typename container_detail::unvoid<value_type>::type&)
139 const_reference;
140 //void_pointer
141 typedef BOOST_INTRUSIVE_OBTAIN_TYPE_WITH_EVAL_DEFAULT(ndnboost::container::container_detail::, Alloc,
142 void_pointer, typename ndnboost::intrusive::pointer_traits<pointer>::template
143 rebind_pointer<void>)
144 void_pointer;
145 //const_void_pointer
146 typedef BOOST_INTRUSIVE_OBTAIN_TYPE_WITH_EVAL_DEFAULT(ndnboost::container::container_detail::, Alloc,
147 const_void_pointer, typename ndnboost::intrusive::pointer_traits<pointer>::template
148 rebind_pointer<const void>)
149 const_void_pointer;
150 //difference_type
151 typedef BOOST_INTRUSIVE_OBTAIN_TYPE_WITH_DEFAULT(ndnboost::container::container_detail::, Alloc,
152 difference_type, std::ptrdiff_t)
153 difference_type;
154 //size_type
155 typedef BOOST_INTRUSIVE_OBTAIN_TYPE_WITH_DEFAULT(ndnboost::container::container_detail::, Alloc,
156 size_type, std::size_t)
157 size_type;
158 //propagate_on_container_copy_assignment
159 typedef BOOST_INTRUSIVE_OBTAIN_TYPE_WITH_DEFAULT(ndnboost::container::container_detail::, Alloc,
160 propagate_on_container_copy_assignment, ndnboost::false_type)
161 propagate_on_container_copy_assignment;
162 //propagate_on_container_move_assignment
163 typedef BOOST_INTRUSIVE_OBTAIN_TYPE_WITH_DEFAULT(ndnboost::container::container_detail::, Alloc,
164 propagate_on_container_move_assignment, ndnboost::false_type)
165 propagate_on_container_move_assignment;
166 //propagate_on_container_swap
167 typedef BOOST_INTRUSIVE_OBTAIN_TYPE_WITH_DEFAULT(ndnboost::container::container_detail::, Alloc,
168 propagate_on_container_swap, ndnboost::false_type)
169 propagate_on_container_swap;
170
171 #if !defined(BOOST_NO_CXX11_TEMPLATE_ALIASES)
172 //C++11
173 template <typename T> using rebind_alloc = typename ndnboost::intrusive::detail::type_rebinder<Alloc, T>::type;
174 template <typename T> using rebind_traits = allocator_traits< rebind_alloc<T> >;
175 #else // #if !defined(BOOST_NO_CXX11_TEMPLATE_ALIASES)
176 //Some workaround for C++03 or C++11 compilers with no template aliases
177 template <typename T>
178 struct rebind_alloc : ndnboost::intrusive::detail::type_rebinder<Alloc,T>::type
179 {
180 typedef typename ndnboost::intrusive::detail::type_rebinder<Alloc,T>::type Base;
181 #if !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES)
182 template <typename... Args>
183 rebind_alloc(BOOST_FWD_REF(Args)... args)
184 : Base(ndnboost::forward<Args>(args)...)
185 {}
186 #else // #if !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES)
187 #define BOOST_PP_LOCAL_MACRO(n) \
188 BOOST_PP_EXPR_IF(n, template<) BOOST_PP_ENUM_PARAMS(n, class P) BOOST_PP_EXPR_IF(n, >) \
189 rebind_alloc(BOOST_PP_ENUM(n, BOOST_CONTAINER_PP_PARAM_LIST, _)) \
190 : Base(BOOST_PP_ENUM(n, BOOST_CONTAINER_PP_PARAM_FORWARD, _)) \
191 {} \
192 //
193 #define BOOST_PP_LOCAL_LIMITS (0, BOOST_CONTAINER_MAX_CONSTRUCTOR_PARAMETERS)
194 #include BOOST_PP_LOCAL_ITERATE()
195 #endif // #if !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES)
196 };
197
198 template <typename T>
199 struct rebind_traits
200 : allocator_traits<typename ndnboost::intrusive::detail::type_rebinder<Alloc, T>::type>
201 {};
202 #endif // #if !defined(BOOST_NO_CXX11_TEMPLATE_ALIASES)
203 template <class T>
204 struct portable_rebind_alloc
205 { typedef typename ndnboost::intrusive::detail::type_rebinder<Alloc, T>::type type; };
206 #endif //BOOST_CONTAINER_DOXYGEN_INVOKED
207
208 //! <b>Returns</b>: `a.allocate(n)`
209 //!
210 static pointer allocate(Alloc &a, size_type n)
211 { return a.allocate(n); }
212
213 //! <b>Returns</b>: `a.deallocate(p, n)`
214 //!
215 //! <b>Throws</b>: Nothing
216 static void deallocate(Alloc &a, pointer p, size_type n)
217 { a.deallocate(p, n); }
218
219 //! <b>Effects</b>: calls `a.allocate(n, p)` if that call is well-formed;
220 //! otherwise, invokes `a.allocate(n)`
221 static pointer allocate(Alloc &a, size_type n, const_void_pointer p)
222 {
223 const bool value = ndnboost::container::container_detail::
224 has_member_function_callable_with_allocate
225 <Alloc, const size_type, const const_void_pointer>::value;
226 ::ndnboost::integral_constant<bool, value> flag;
227 return allocator_traits::priv_allocate(flag, a, n, p);
228 }
229
230 //! <b>Effects</b>: calls `a.destroy(p)` if that call is well-formed;
231 //! otherwise, invokes `p->~T()`.
232 template<class T>
233 static void destroy(Alloc &a, T*p)
234 {
235 typedef T* destroy_pointer;
236 const bool value = ndnboost::container::container_detail::
237 has_member_function_callable_with_destroy
238 <Alloc, const destroy_pointer>::value;
239 ::ndnboost::integral_constant<bool, value> flag;
240 allocator_traits::priv_destroy(flag, a, p);
241 }
242
243 //! <b>Returns</b>: `a.max_size()` if that expression is well-formed; otherwise,
244 //! `numeric_limits<size_type>::max()`.
245 static size_type max_size(const Alloc &a)
246 {
247 const bool value = ndnboost::container::container_detail::
248 has_member_function_callable_with_max_size
249 <const Alloc>::value;
250 ::ndnboost::integral_constant<bool, value> flag;
251 return allocator_traits::priv_max_size(flag, a);
252 }
253
254 //! <b>Returns</b>: `a.select_on_container_copy_construction()` if that expression is well-formed;
255 //! otherwise, a.
256 static
257 #if !defined(BOOST_CONTAINER_DOXYGEN_INVOKED)
258 typename container_detail::if_c
259 < ndnboost::container::container_detail::
260 has_member_function_callable_with_select_on_container_copy_construction
261 <const Alloc>::value
262 , Alloc
263 , const Alloc &
264 >::type
265 #else
266 Alloc
267 #endif
268 select_on_container_copy_construction(const Alloc &a)
269 {
270 const bool value = ndnboost::container::container_detail::
271 has_member_function_callable_with_select_on_container_copy_construction
272 <const Alloc>::value;
273 ::ndnboost::integral_constant<bool, value> flag;
274 return allocator_traits::priv_select_on_container_copy_construction(flag, a);
275 }
276
277 #if !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES) || defined(BOOST_CONTAINER_DOXYGEN_INVOKED)
278 //! <b>Effects</b>: calls `a.construct(p, std::forward<Args>(args)...)` if that call is well-formed;
279 //! otherwise, invokes `::new (static_cast<void*>(p)) T(std::forward<Args>(args)...)`
280 template <class T, class ...Args>
281 static void construct(Alloc & a, T* p, BOOST_FWD_REF(Args)... args)
282 {
283 ::ndnboost::integral_constant<bool, container_detail::is_std_allocator<Alloc>::value> flag;
284 allocator_traits::priv_construct(flag, a, p, ::ndnboost::forward<Args>(args)...);
285 }
286 #endif
287 ///@cond
288 #if !defined(BOOST_CONTAINER_DOXYGEN_INVOKED)
289 private:
290 static pointer priv_allocate(ndnboost::true_type, Alloc &a, size_type n, const_void_pointer p)
291 { return a.allocate(n, p); }
292
293 static pointer priv_allocate(ndnboost::false_type, Alloc &a, size_type n, const_void_pointer)
294 { return allocator_traits::allocate(a, n); }
295
296 template<class T>
297 static void priv_destroy(ndnboost::true_type, Alloc &a, T* p)
298 { a.destroy(p); }
299
300 template<class T>
301 static void priv_destroy(ndnboost::false_type, Alloc &, T* p)
302 { p->~T(); (void)p; }
303
304 static size_type priv_max_size(ndnboost::true_type, const Alloc &a)
305 { return a.max_size(); }
306
307 static size_type priv_max_size(ndnboost::false_type, const Alloc &)
308 { return (std::numeric_limits<size_type>::max)(); }
309
310 static Alloc priv_select_on_container_copy_construction(ndnboost::true_type, const Alloc &a)
311 { return a.select_on_container_copy_construction(); }
312
313 static const Alloc &priv_select_on_container_copy_construction(ndnboost::false_type, const Alloc &a)
314 { return a; }
315
316 #if !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES)
317 template<class T, class ...Args>
318 static void priv_construct(ndnboost::false_type, Alloc &a, T *p, BOOST_FWD_REF(Args) ...args)
319 {
320 const bool value = ndnboost::container::container_detail::
321 has_member_function_callable_with_construct
322 < Alloc, T*, Args... >::value;
323 ::ndnboost::integral_constant<bool, value> flag;
324 priv_construct_dispatch2(flag, a, p, ::ndnboost::forward<Args>(args)...);
325 }
326
327 template<class T, class ...Args>
328 static void priv_construct(ndnboost::true_type, Alloc &a, T *p, BOOST_FWD_REF(Args) ...args)
329 {
330 priv_construct_dispatch2(ndnboost::false_type(), a, p, ::ndnboost::forward<Args>(args)...);
331 }
332
333 template<class T, class ...Args>
334 static void priv_construct_dispatch2(ndnboost::true_type, Alloc &a, T *p, BOOST_FWD_REF(Args) ...args)
335 { a.construct( p, ::ndnboost::forward<Args>(args)...); }
336
337 template<class T, class ...Args>
338 static void priv_construct_dispatch2(ndnboost::false_type, Alloc &, T *p, BOOST_FWD_REF(Args) ...args)
339 { ::new((void*)p) T(::ndnboost::forward<Args>(args)...); }
340 #else // #if !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES)
341 public:
342 #define BOOST_PP_LOCAL_MACRO(n) \
343 template<class T BOOST_PP_ENUM_TRAILING_PARAMS(n, class P) > \
344 static void construct(Alloc &a, T *p \
345 BOOST_PP_ENUM_TRAILING(n, BOOST_CONTAINER_PP_PARAM_LIST, _)) \
346 { \
347 ::ndnboost::integral_constant<bool, container_detail::is_std_allocator<Alloc>::value> flag; \
348 allocator_traits::priv_construct(flag, a, p \
349 BOOST_PP_ENUM_TRAILING(n, BOOST_CONTAINER_PP_PARAM_FORWARD, _)); \
350 } \
351 //
352 #define BOOST_PP_LOCAL_LIMITS (0, BOOST_CONTAINER_MAX_CONSTRUCTOR_PARAMETERS)
353 #include BOOST_PP_LOCAL_ITERATE()
354
355 private:
356 #define BOOST_PP_LOCAL_MACRO(n) \
357 template<class T BOOST_PP_ENUM_TRAILING_PARAMS(n, class P) > \
358 static void priv_construct(ndnboost::false_type, Alloc &a, T *p \
359 BOOST_PP_ENUM_TRAILING(n, BOOST_CONTAINER_PP_PARAM_LIST,_)) \
360 { \
361 const bool value = \
362 ndnboost::container::container_detail::has_member_function_callable_with_construct \
363 < Alloc, T* BOOST_PP_ENUM_TRAILING(n, BOOST_CONTAINER_PP_FWD_TYPE, _) >::value; \
364 ::ndnboost::integral_constant<bool, value> flag; \
365 priv_construct_dispatch2(flag, a, p \
366 BOOST_PP_ENUM_TRAILING(n, BOOST_CONTAINER_PP_PARAM_FORWARD, _) ); \
367 } \
368 \
369 template<class T BOOST_PP_ENUM_TRAILING_PARAMS(n, class P) > \
370 static void priv_construct(ndnboost::true_type, Alloc &a, T *p \
371 BOOST_PP_ENUM_TRAILING(n, BOOST_CONTAINER_PP_PARAM_LIST,_)) \
372 { \
373 priv_construct_dispatch2(ndnboost::false_type(), a, p \
374 BOOST_PP_ENUM_TRAILING(n, BOOST_CONTAINER_PP_PARAM_FORWARD, _) ); \
375 } \
376 \
377 template<class T BOOST_PP_ENUM_TRAILING_PARAMS(n, class P) > \
378 static void priv_construct_dispatch2(ndnboost::true_type, Alloc &a, T *p \
379 BOOST_PP_ENUM_TRAILING(n, BOOST_CONTAINER_PP_PARAM_LIST,_)) \
380 { a.construct( p BOOST_PP_ENUM_TRAILING(n, BOOST_CONTAINER_PP_PARAM_FORWARD, _) ); } \
381 \
382 template<class T BOOST_PP_ENUM_TRAILING_PARAMS(n, class P) > \
383 static void priv_construct_dispatch2(ndnboost::false_type, Alloc &, T *p \
384 BOOST_PP_ENUM_TRAILING(n, BOOST_CONTAINER_PP_PARAM_LIST, _) ) \
385 { ::new((void*)p) T(BOOST_PP_ENUM(n, BOOST_CONTAINER_PP_PARAM_FORWARD, _)); } \
386 //
387 #define BOOST_PP_LOCAL_LIMITS (0, BOOST_CONTAINER_MAX_CONSTRUCTOR_PARAMETERS)
388 #include BOOST_PP_LOCAL_ITERATE()
389 #endif // #if !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES)
390 #endif //#if defined(BOOST_CONTAINER_DOXYGEN_INVOKED)
391
392 ///@endcond
393};
394
395} //namespace container {
396} //namespace ndnboost {
397
398#include <ndnboost/container/detail/config_end.hpp>
399
400#endif // ! defined(BOOST_CONTAINER_ALLOCATOR_ALLOCATOR_TRAITS_HPP)