allocator_traits.hpp 20 KB

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