Chris@16: Chris@16: // Copyright 2005-2011 Daniel James. Chris@16: // Copyright 2009 Pablo Halpern. Chris@16: // Distributed under the Boost Software License, Version 1.0. (See accompanying Chris@16: // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) Chris@16: Chris@16: // See http://www.boost.org/libs/unordered for documentation Chris@16: Chris@16: #ifndef BOOST_UNORDERED_ALLOCATE_HPP Chris@16: #define BOOST_UNORDERED_ALLOCATE_HPP Chris@16: Chris@101: #include Chris@101: #if defined(BOOST_HAS_PRAGMA_ONCE) Chris@101: #pragma once Chris@16: #endif Chris@16: Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: Chris@16: #if !defined(BOOST_NO_CXX11_HDR_TUPLE) Chris@16: #include Chris@16: #endif Chris@16: Chris@16: #if defined(BOOST_MSVC) Chris@16: #pragma warning(push) Chris@16: #pragma warning(disable:4512) // assignment operator could not be generated. Chris@16: #pragma warning(disable:4345) // behavior change: an object of POD type Chris@16: // constructed with an initializer of the form () Chris@16: // will be default-initialized. Chris@16: #endif Chris@16: Chris@16: #define BOOST_UNORDERED_EMPLACE_LIMIT 10 Chris@16: Chris@16: namespace boost { namespace unordered { namespace detail { Chris@16: Chris@16: //////////////////////////////////////////////////////////////////////////// Chris@16: // Bits and pieces for implementing traits Chris@16: Chris@16: template typename boost::add_lvalue_reference::type make(); Chris@16: struct choice9 { typedef char (&type)[9]; }; Chris@16: struct choice8 : choice9 { typedef char (&type)[8]; }; Chris@16: struct choice7 : choice8 { typedef char (&type)[7]; }; Chris@16: struct choice6 : choice7 { typedef char (&type)[6]; }; Chris@16: struct choice5 : choice6 { typedef char (&type)[5]; }; Chris@16: struct choice4 : choice5 { typedef char (&type)[4]; }; Chris@16: struct choice3 : choice4 { typedef char (&type)[3]; }; Chris@16: struct choice2 : choice3 { typedef char (&type)[2]; }; Chris@16: struct choice1 : choice2 { typedef char (&type)[1]; }; Chris@16: choice1 choose(); Chris@16: Chris@16: typedef choice1::type yes_type; Chris@16: typedef choice2::type no_type; Chris@16: Chris@16: struct private_type Chris@16: { Chris@16: private_type const &operator,(int) const; Chris@16: }; Chris@16: Chris@16: template Chris@16: no_type is_private_type(T const&); Chris@16: yes_type is_private_type(private_type const&); Chris@16: Chris@16: struct convert_from_anything { Chris@16: template Chris@16: convert_from_anything(T const&); Chris@16: }; Chris@16: Chris@16: //////////////////////////////////////////////////////////////////////////// Chris@16: // emplace_args Chris@16: // Chris@16: // Either forwarding variadic arguments, or storing the arguments in Chris@16: // emplace_args##n Chris@16: Chris@16: #if !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES) Chris@16: Chris@16: #define BOOST_UNORDERED_EMPLACE_TEMPLATE typename... Args Chris@16: #define BOOST_UNORDERED_EMPLACE_ARGS BOOST_FWD_REF(Args)... args Chris@16: #define BOOST_UNORDERED_EMPLACE_FORWARD boost::forward(args)... Chris@16: Chris@16: #define BOOST_UNORDERED_EMPLACE_ARGS1(a0) a0 Chris@16: #define BOOST_UNORDERED_EMPLACE_ARGS2(a0, a1) a0, a1 Chris@16: #define BOOST_UNORDERED_EMPLACE_ARGS3(a0, a1, a2) a0, a1, a2 Chris@16: Chris@16: #else Chris@16: Chris@16: #define BOOST_UNORDERED_EMPLACE_TEMPLATE typename Args Chris@16: #define BOOST_UNORDERED_EMPLACE_ARGS Args const& args Chris@16: #define BOOST_UNORDERED_EMPLACE_FORWARD args Chris@16: Chris@16: #define BOOST_UNORDERED_FWD_PARAM(z, n, a) \ Chris@16: BOOST_FWD_REF(BOOST_PP_CAT(A, n)) BOOST_PP_CAT(a, n) Chris@16: Chris@16: #define BOOST_UNORDERED_CALL_FORWARD(z, i, a) \ Chris@16: boost::forward(BOOST_PP_CAT(a,i)) Chris@16: Chris@16: #define BOOST_UNORDERED_EARGS(z, n, _) \ Chris@16: template \ Chris@16: struct BOOST_PP_CAT(emplace_args, n) \ Chris@16: { \ Chris@16: BOOST_PP_REPEAT_##z(n, BOOST_UNORDERED_EARGS_MEMBER, _) \ Chris@16: BOOST_PP_CAT(emplace_args, n) ( \ Chris@16: BOOST_PP_ENUM_BINARY_PARAMS_Z(z, n, Arg, b) \ Chris@16: ) : BOOST_PP_ENUM_##z(n, BOOST_UNORDERED_EARGS_INIT, _) \ Chris@16: {} \ Chris@16: \ Chris@16: }; \ Chris@16: \ Chris@16: template \ Chris@16: inline BOOST_PP_CAT(emplace_args, n) < \ Chris@16: BOOST_PP_ENUM_PARAMS_Z(z, n, A) \ Chris@16: > create_emplace_args( \ Chris@16: BOOST_PP_ENUM_##z(n, BOOST_UNORDERED_FWD_PARAM, b) \ Chris@16: ) \ Chris@16: { \ Chris@16: BOOST_PP_CAT(emplace_args, n) < \ Chris@16: BOOST_PP_ENUM_PARAMS_Z(z, n, A) \ Chris@16: > e(BOOST_PP_ENUM_PARAMS_Z(z, n, b)); \ Chris@16: return e; \ Chris@16: } Chris@16: Chris@16: #define BOOST_UNORDERED_EMPLACE_ARGS1 create_emplace_args Chris@16: #define BOOST_UNORDERED_EMPLACE_ARGS2 create_emplace_args Chris@16: #define BOOST_UNORDERED_EMPLACE_ARGS3 create_emplace_args Chris@16: Chris@16: #if defined(BOOST_NO_CXX11_RVALUE_REFERENCES) Chris@16: Chris@16: #define BOOST_UNORDERED_EARGS_MEMBER(z, n, _) \ Chris@16: typedef BOOST_FWD_REF(BOOST_PP_CAT(A, n)) BOOST_PP_CAT(Arg, n); \ Chris@16: BOOST_PP_CAT(Arg, n) BOOST_PP_CAT(a, n); Chris@16: Chris@16: #define BOOST_UNORDERED_EARGS_INIT(z, n, _) \ Chris@16: BOOST_PP_CAT(a, n)( \ Chris@16: boost::forward(BOOST_PP_CAT(b, n))) Chris@16: Chris@16: #else Chris@16: Chris@16: #define BOOST_UNORDERED_EARGS_MEMBER(z, n, _) \ Chris@16: typedef typename boost::add_lvalue_reference::type \ Chris@16: BOOST_PP_CAT(Arg, n); \ Chris@16: BOOST_PP_CAT(Arg, n) BOOST_PP_CAT(a, n); Chris@16: Chris@16: #define BOOST_UNORDERED_EARGS_INIT(z, n, _) \ Chris@16: BOOST_PP_CAT(a, n)(BOOST_PP_CAT(b, n)) Chris@16: Chris@16: #endif Chris@16: Chris@16: BOOST_PP_REPEAT_FROM_TO(1, BOOST_UNORDERED_EMPLACE_LIMIT, BOOST_UNORDERED_EARGS, Chris@16: _) Chris@16: Chris@16: #undef BOOST_UNORDERED_DEFINE_EMPLACE_ARGS Chris@16: #undef BOOST_UNORDERED_EARGS_MEMBER Chris@16: #undef BOOST_UNORDERED_EARGS_INIT Chris@16: Chris@16: #endif Chris@16: Chris@16: }}} Chris@16: Chris@16: //////////////////////////////////////////////////////////////////////////////// Chris@16: // Chris@16: // Pick which version of allocator_traits to use Chris@16: // Chris@16: // 0 = Own partial implementation Chris@16: // 1 = std::allocator_traits Chris@16: // 2 = boost::container::allocator_traits Chris@16: Chris@16: #if !defined(BOOST_UNORDERED_USE_ALLOCATOR_TRAITS) Chris@16: # if defined(__GXX_EXPERIMENTAL_CXX0X__) && \ Chris@16: (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 7)) Chris@16: # define BOOST_UNORDERED_USE_ALLOCATOR_TRAITS 0 Chris@16: # elif defined(BOOST_MSVC) Chris@16: # if BOOST_MSVC < 1400 Chris@16: // Use container's allocator_traits for older versions of Visual Chris@16: // C++ as I don't test with them. Chris@16: # define BOOST_UNORDERED_USE_ALLOCATOR_TRAITS 2 Chris@16: # endif Chris@16: # endif Chris@16: #endif Chris@16: Chris@16: #if !defined(BOOST_UNORDERED_USE_ALLOCATOR_TRAITS) Chris@16: # define BOOST_UNORDERED_USE_ALLOCATOR_TRAITS 0 Chris@16: #endif Chris@16: Chris@16: //////////////////////////////////////////////////////////////////////////////// Chris@16: // Chris@16: // Some utilities for implementing allocator_traits, but useful elsewhere so Chris@16: // they're always defined. Chris@16: Chris@16: #if !defined(BOOST_NO_CXX11_HDR_TYPE_TRAITS) Chris@16: # include Chris@16: #endif Chris@16: Chris@16: namespace boost { namespace unordered { namespace detail { Chris@16: Chris@16: //////////////////////////////////////////////////////////////////////////// Chris@16: // Integral_constrant, true_type, false_type Chris@16: // Chris@16: // Uses the standard versions if available. Chris@16: Chris@16: #if !defined(BOOST_NO_CXX11_HDR_TYPE_TRAITS) Chris@16: Chris@16: using std::integral_constant; Chris@16: using std::true_type; Chris@16: using std::false_type; Chris@16: Chris@16: #else Chris@16: Chris@16: template Chris@16: struct integral_constant { enum { value = Value }; }; Chris@16: Chris@16: typedef boost::unordered::detail::integral_constant true_type; Chris@16: typedef boost::unordered::detail::integral_constant false_type; Chris@16: Chris@16: #endif Chris@16: Chris@16: //////////////////////////////////////////////////////////////////////////// Chris@16: // Explicitly call a destructor Chris@16: Chris@16: #if defined(BOOST_MSVC) Chris@16: #pragma warning(push) Chris@16: #pragma warning(disable:4100) // unreferenced formal parameter Chris@16: #endif Chris@16: Chris@16: namespace func { Chris@16: template Chris@16: inline void destroy(T* x) { Chris@16: x->~T(); Chris@16: } Chris@16: } Chris@16: Chris@16: #if defined(BOOST_MSVC) Chris@16: #pragma warning(pop) Chris@16: #endif Chris@16: Chris@16: //////////////////////////////////////////////////////////////////////////// Chris@16: // Expression test mechanism Chris@16: // Chris@16: // When SFINAE expressions are available, define Chris@16: // BOOST_UNORDERED_HAS_FUNCTION which can check if a function call is Chris@16: // supported by a class, otherwise define BOOST_UNORDERED_HAS_MEMBER which Chris@16: // can detect if a class has the specified member, but not that it has the Chris@16: // correct type, this is good enough for a passable impression of Chris@16: // allocator_traits. Chris@16: Chris@16: #if !defined(BOOST_NO_SFINAE_EXPR) Chris@16: Chris@16: template struct expr_test; Chris@16: template struct expr_test : T {}; Chris@16: Chris@16: # define BOOST_UNORDERED_CHECK_EXPRESSION(count, result, expression) \ Chris@16: template \ Chris@16: static typename boost::unordered::detail::expr_test< \ Chris@16: BOOST_PP_CAT(choice, result), \ Chris@16: sizeof(for_expr_test(( \ Chris@16: (expression), \ Chris@16: 0)))>::type test( \ Chris@16: BOOST_PP_CAT(choice, count)) Chris@16: Chris@16: # define BOOST_UNORDERED_DEFAULT_EXPRESSION(count, result) \ Chris@16: template \ Chris@16: static BOOST_PP_CAT(choice, result)::type test( \ Chris@16: BOOST_PP_CAT(choice, count)) Chris@16: Chris@16: # define BOOST_UNORDERED_HAS_FUNCTION(name, thing, args, _) \ Chris@16: struct BOOST_PP_CAT(has_, name) \ Chris@16: { \ Chris@16: template static char for_expr_test(U const&); \ Chris@16: BOOST_UNORDERED_CHECK_EXPRESSION(1, 1, \ Chris@16: boost::unordered::detail::make< thing >().name args); \ Chris@16: BOOST_UNORDERED_DEFAULT_EXPRESSION(2, 2); \ Chris@16: \ Chris@16: enum { value = sizeof(test(choose())) == sizeof(choice1::type) };\ Chris@16: } Chris@16: Chris@16: #else Chris@16: Chris@16: template struct identity { typedef T type; }; Chris@16: Chris@16: # define BOOST_UNORDERED_CHECK_MEMBER(count, result, name, member) \ Chris@16: \ Chris@16: typedef typename boost::unordered::detail::identity::type \ Chris@16: BOOST_PP_CAT(check, count); \ Chris@16: \ Chris@16: template \ Chris@16: struct BOOST_PP_CAT(test, count) { \ Chris@16: typedef BOOST_PP_CAT(choice, result) type; \ Chris@16: }; \ Chris@16: \ Chris@16: template static typename \ Chris@16: BOOST_PP_CAT(test, count)<&U::name>::type \ Chris@16: test(BOOST_PP_CAT(choice, count)) Chris@16: Chris@16: # define BOOST_UNORDERED_DEFAULT_MEMBER(count, result) \ Chris@16: template static BOOST_PP_CAT(choice, result)::type \ Chris@16: test(BOOST_PP_CAT(choice, count)) Chris@16: Chris@16: # define BOOST_UNORDERED_HAS_MEMBER(name) \ Chris@16: struct BOOST_PP_CAT(has_, name) \ Chris@16: { \ Chris@16: struct impl { \ Chris@16: struct base_mixin { int name; }; \ Chris@16: struct base : public T, public base_mixin {}; \ Chris@16: \ Chris@16: BOOST_UNORDERED_CHECK_MEMBER(1, 1, name, int base_mixin::*); \ Chris@16: BOOST_UNORDERED_DEFAULT_MEMBER(2, 2); \ Chris@16: \ Chris@16: enum { value = sizeof(choice2::type) == \ Chris@16: sizeof(test(choose())) \ Chris@16: }; \ Chris@16: }; \ Chris@16: \ Chris@16: enum { value = impl::value }; \ Chris@16: } Chris@16: Chris@16: #endif Chris@16: Chris@16: }}} Chris@16: Chris@16: //////////////////////////////////////////////////////////////////////////////// Chris@16: // Chris@16: // Allocator traits Chris@16: // Chris@16: // First our implementation, then later light wrappers around the alternatives Chris@16: Chris@16: #if BOOST_UNORDERED_USE_ALLOCATOR_TRAITS == 0 Chris@16: Chris@16: # include Chris@16: # include Chris@16: # include Chris@16: # if defined(BOOST_NO_SFINAE_EXPR) Chris@16: # include Chris@16: # endif Chris@16: Chris@16: # if !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES) && \ Chris@16: !defined(BOOST_NO_SFINAE_EXPR) Chris@16: # define BOOST_UNORDERED_DETAIL_FULL_CONSTRUCT 1 Chris@16: # else Chris@16: # define BOOST_UNORDERED_DETAIL_FULL_CONSTRUCT 0 Chris@16: # endif Chris@16: Chris@16: namespace boost { namespace unordered { namespace detail { Chris@16: Chris@16: // TODO: Does this match std::allocator_traits::rebind_alloc? Chris@16: template Chris@16: struct rebind_wrap Chris@16: { Chris@16: typedef typename Alloc::BOOST_NESTED_TEMPLATE rebind::other type; Chris@16: }; Chris@16: Chris@16: # if defined(BOOST_MSVC) && BOOST_MSVC <= 1400 Chris@16: Chris@16: # define BOOST_UNORDERED_DEFAULT_TYPE_TMPLT(tname) \ Chris@16: template \ Chris@16: struct default_type_ ## tname { \ Chris@16: \ Chris@16: template \ Chris@16: static choice1::type test(choice1, typename X::tname* = 0); \ Chris@16: \ Chris@16: template \ Chris@16: static choice2::type test(choice2, void* = 0); \ Chris@16: \ Chris@16: struct DefaultWrap { typedef Default tname; }; \ Chris@16: \ Chris@16: enum { value = (1 == sizeof(test(choose()))) }; \ Chris@16: \ Chris@16: typedef typename boost::detail::if_true:: \ Chris@16: BOOST_NESTED_TEMPLATE then \ Chris@16: ::type::tname type; \ Chris@16: } Chris@16: Chris@16: # else Chris@16: Chris@16: template Chris@16: struct sfinae : T2 {}; Chris@16: Chris@16: # define BOOST_UNORDERED_DEFAULT_TYPE_TMPLT(tname) \ Chris@16: template \ Chris@16: struct default_type_ ## tname { \ Chris@16: \ Chris@16: template \ Chris@16: static typename boost::unordered::detail::sfinae< \ Chris@16: typename X::tname, choice1>::type \ Chris@16: test(choice1); \ Chris@16: \ Chris@16: template \ Chris@16: static choice2::type test(choice2); \ Chris@16: \ Chris@16: struct DefaultWrap { typedef Default tname; }; \ Chris@16: \ Chris@16: enum { value = (1 == sizeof(test(choose()))) }; \ Chris@16: \ Chris@16: typedef typename boost::detail::if_true:: \ Chris@16: BOOST_NESTED_TEMPLATE then \ Chris@16: ::type::tname type; \ Chris@16: } Chris@16: Chris@16: # endif Chris@16: Chris@16: # define BOOST_UNORDERED_DEFAULT_TYPE(T,tname, arg) \ Chris@16: typename default_type_ ## tname::type Chris@16: Chris@16: BOOST_UNORDERED_DEFAULT_TYPE_TMPLT(pointer); Chris@16: BOOST_UNORDERED_DEFAULT_TYPE_TMPLT(const_pointer); Chris@16: BOOST_UNORDERED_DEFAULT_TYPE_TMPLT(void_pointer); Chris@16: BOOST_UNORDERED_DEFAULT_TYPE_TMPLT(const_void_pointer); Chris@16: BOOST_UNORDERED_DEFAULT_TYPE_TMPLT(difference_type); Chris@16: BOOST_UNORDERED_DEFAULT_TYPE_TMPLT(size_type); Chris@16: BOOST_UNORDERED_DEFAULT_TYPE_TMPLT(propagate_on_container_copy_assignment); Chris@16: BOOST_UNORDERED_DEFAULT_TYPE_TMPLT(propagate_on_container_move_assignment); Chris@16: BOOST_UNORDERED_DEFAULT_TYPE_TMPLT(propagate_on_container_swap); Chris@16: Chris@16: # if !defined(BOOST_NO_SFINAE_EXPR) Chris@16: Chris@16: template Chris@16: BOOST_UNORDERED_HAS_FUNCTION( Chris@16: select_on_container_copy_construction, U const, (), 0 Chris@16: ); Chris@16: Chris@16: template Chris@16: BOOST_UNORDERED_HAS_FUNCTION( Chris@16: max_size, U const, (), 0 Chris@16: ); Chris@16: Chris@16: # if !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES) Chris@16: Chris@16: template Chris@16: BOOST_UNORDERED_HAS_FUNCTION( Chris@16: construct, U, ( Chris@16: boost::unordered::detail::make(), Chris@16: boost::unordered::detail::make()...), 2 Chris@16: ); Chris@16: Chris@16: # else Chris@16: Chris@16: template Chris@16: BOOST_UNORDERED_HAS_FUNCTION( Chris@16: construct, U, ( Chris@16: boost::unordered::detail::make(), Chris@16: boost::unordered::detail::make()), 2 Chris@16: ); Chris@16: Chris@16: # endif Chris@16: Chris@16: template Chris@16: BOOST_UNORDERED_HAS_FUNCTION( Chris@16: destroy, U, (boost::unordered::detail::make()), 1 Chris@16: ); Chris@16: Chris@16: # else Chris@16: Chris@16: template Chris@16: BOOST_UNORDERED_HAS_MEMBER(select_on_container_copy_construction); Chris@16: Chris@16: template Chris@16: BOOST_UNORDERED_HAS_MEMBER(max_size); Chris@16: Chris@16: template Chris@16: BOOST_UNORDERED_HAS_MEMBER(construct); Chris@16: Chris@16: template Chris@16: BOOST_UNORDERED_HAS_MEMBER(destroy); Chris@16: Chris@16: # endif Chris@16: Chris@16: namespace func Chris@16: { Chris@16: Chris@16: template Chris@16: inline Alloc call_select_on_container_copy_construction(const Alloc& rhs, Chris@16: typename boost::enable_if_c< Chris@16: boost::unordered::detail:: Chris@16: has_select_on_container_copy_construction::value, void* Chris@16: >::type = 0) Chris@16: { Chris@16: return rhs.select_on_container_copy_construction(); Chris@16: } Chris@16: Chris@16: template Chris@16: inline Alloc call_select_on_container_copy_construction(const Alloc& rhs, Chris@16: typename boost::disable_if_c< Chris@16: boost::unordered::detail:: Chris@16: has_select_on_container_copy_construction::value, void* Chris@16: >::type = 0) Chris@16: { Chris@16: return rhs; Chris@16: } Chris@16: Chris@16: template Chris@16: inline SizeType call_max_size(const Alloc& a, Chris@16: typename boost::enable_if_c< Chris@16: boost::unordered::detail::has_max_size::value, void* Chris@16: >::type = 0) Chris@16: { Chris@16: return a.max_size(); Chris@16: } Chris@16: Chris@16: template Chris@16: inline SizeType call_max_size(const Alloc&, typename boost::disable_if_c< Chris@16: boost::unordered::detail::has_max_size::value, void* Chris@16: >::type = 0) Chris@16: { Chris@16: return (std::numeric_limits::max)(); Chris@16: } Chris@16: Chris@16: } // namespace func. Chris@16: Chris@16: template Chris@16: struct allocator_traits Chris@16: { Chris@16: typedef Alloc allocator_type; Chris@16: typedef typename Alloc::value_type value_type; Chris@16: Chris@16: typedef BOOST_UNORDERED_DEFAULT_TYPE(Alloc, pointer, value_type*) Chris@16: pointer; Chris@16: Chris@16: template Chris@16: struct pointer_to_other : boost::pointer_to_other {}; Chris@16: Chris@16: typedef BOOST_UNORDERED_DEFAULT_TYPE(Alloc, const_pointer, Chris@16: typename pointer_to_other::type) Chris@16: const_pointer; Chris@16: Chris@16: //typedef BOOST_UNORDERED_DEFAULT_TYPE(Alloc, void_pointer, Chris@16: // typename pointer_to_other::type) Chris@16: // void_pointer; Chris@16: // Chris@16: //typedef BOOST_UNORDERED_DEFAULT_TYPE(Alloc, const_void_pointer, Chris@16: // typename pointer_to_other::type) Chris@16: // const_void_pointer; Chris@16: Chris@16: typedef BOOST_UNORDERED_DEFAULT_TYPE(Alloc, difference_type, Chris@16: std::ptrdiff_t) difference_type; Chris@16: Chris@16: typedef BOOST_UNORDERED_DEFAULT_TYPE(Alloc, size_type, std::size_t) Chris@16: size_type; Chris@16: Chris@16: // TODO: rebind_alloc and rebind_traits Chris@16: Chris@16: static pointer allocate(Alloc& a, size_type n) Chris@16: { return a.allocate(n); } Chris@16: Chris@16: // I never use this, so I'll just comment it out for now. Chris@16: // Chris@16: //static pointer allocate(Alloc& a, size_type n, Chris@16: // const_void_pointer hint) Chris@16: // { return DEFAULT_FUNC(allocate, pointer)(a, n, hint); } Chris@16: Chris@16: static void deallocate(Alloc& a, pointer p, size_type n) Chris@16: { a.deallocate(p, n); } Chris@16: Chris@16: public: Chris@16: Chris@16: # if BOOST_UNORDERED_DETAIL_FULL_CONSTRUCT Chris@16: Chris@16: template Chris@16: static typename boost::enable_if_c< Chris@16: boost::unordered::detail::has_construct Chris@16: ::value>::type Chris@16: construct(Alloc& a, T* p, BOOST_FWD_REF(Args)... x) Chris@16: { Chris@16: a.construct(p, boost::forward(x)...); Chris@16: } Chris@16: Chris@16: template Chris@16: static typename boost::disable_if_c< Chris@16: boost::unordered::detail::has_construct Chris@16: ::value>::type Chris@16: construct(Alloc&, T* p, BOOST_FWD_REF(Args)... x) Chris@16: { Chris@16: new ((void*) p) T(boost::forward(x)...); Chris@16: } Chris@16: Chris@16: template Chris@16: static typename boost::enable_if_c< Chris@16: boost::unordered::detail::has_destroy::value>::type Chris@16: destroy(Alloc& a, T* p) Chris@16: { Chris@16: a.destroy(p); Chris@16: } Chris@16: Chris@16: template Chris@16: static typename boost::disable_if_c< Chris@16: boost::unordered::detail::has_destroy::value>::type Chris@16: destroy(Alloc&, T* p) Chris@16: { Chris@16: boost::unordered::detail::func::destroy(p); Chris@16: } Chris@16: Chris@16: # elif !defined(BOOST_NO_SFINAE_EXPR) Chris@16: Chris@16: template Chris@16: static typename boost::enable_if_c< Chris@16: boost::unordered::detail::has_construct::value>::type Chris@16: construct(Alloc& a, T* p, T const& x) Chris@16: { Chris@16: a.construct(p, x); Chris@16: } Chris@16: Chris@16: template Chris@16: static typename boost::disable_if_c< Chris@16: boost::unordered::detail::has_construct::value>::type Chris@16: construct(Alloc&, T* p, T const& x) Chris@16: { Chris@16: new ((void*) p) T(x); Chris@16: } Chris@16: Chris@16: template Chris@16: static typename boost::enable_if_c< Chris@16: boost::unordered::detail::has_destroy::value>::type Chris@16: destroy(Alloc& a, T* p) Chris@16: { Chris@16: a.destroy(p); Chris@16: } Chris@16: Chris@16: template Chris@16: static typename boost::disable_if_c< Chris@16: boost::unordered::detail::has_destroy::value>::type Chris@16: destroy(Alloc&, T* p) Chris@16: { Chris@16: boost::unordered::detail::func::destroy(p); Chris@16: } Chris@16: Chris@16: # else Chris@16: Chris@16: // If we don't have SFINAE expressions, only call construct for the Chris@16: // copy constructor for the allocator's value_type - as that's Chris@16: // the only construct method that old fashioned allocators support. Chris@16: Chris@16: template Chris@16: static void construct(Alloc& a, T* p, T const& x, Chris@16: typename boost::enable_if_c< Chris@16: boost::unordered::detail::has_construct::value && Chris@16: boost::is_same::value, Chris@16: void*>::type = 0) Chris@16: { Chris@16: a.construct(p, x); Chris@16: } Chris@16: Chris@16: template Chris@16: static void construct(Alloc&, T* p, T const& x, Chris@16: typename boost::disable_if_c< Chris@16: boost::unordered::detail::has_construct::value && Chris@16: boost::is_same::value, Chris@16: void*>::type = 0) Chris@16: { Chris@16: new ((void*) p) T(x); Chris@16: } Chris@16: Chris@16: template Chris@16: static void destroy(Alloc& a, T* p, Chris@16: typename boost::enable_if_c< Chris@16: boost::unordered::detail::has_destroy::value && Chris@16: boost::is_same::value, Chris@16: void*>::type = 0) Chris@16: { Chris@16: a.destroy(p); Chris@16: } Chris@16: Chris@16: template Chris@16: static void destroy(Alloc&, T* p, Chris@16: typename boost::disable_if_c< Chris@16: boost::unordered::detail::has_destroy::value && Chris@16: boost::is_same::value, Chris@16: void*>::type = 0) Chris@16: { Chris@16: boost::unordered::detail::func::destroy(p); Chris@16: } Chris@16: Chris@16: # endif Chris@16: Chris@16: static size_type max_size(const Alloc& a) Chris@16: { Chris@16: return boost::unordered::detail::func:: Chris@16: call_max_size(a); Chris@16: } Chris@16: Chris@16: // Allocator propagation on construction Chris@16: Chris@16: static Alloc select_on_container_copy_construction(Alloc const& rhs) Chris@16: { Chris@16: return boost::unordered::detail::func:: Chris@16: call_select_on_container_copy_construction(rhs); Chris@16: } Chris@16: Chris@16: // Allocator propagation on assignment and swap. Chris@16: // Return true if lhs is modified. Chris@16: typedef BOOST_UNORDERED_DEFAULT_TYPE( Chris@16: Alloc, propagate_on_container_copy_assignment, false_type) Chris@16: propagate_on_container_copy_assignment; Chris@16: typedef BOOST_UNORDERED_DEFAULT_TYPE( Chris@16: Alloc,propagate_on_container_move_assignment, false_type) Chris@16: propagate_on_container_move_assignment; Chris@16: typedef BOOST_UNORDERED_DEFAULT_TYPE( Chris@16: Alloc,propagate_on_container_swap,false_type) Chris@16: propagate_on_container_swap; Chris@16: }; Chris@16: }}} Chris@16: Chris@16: # undef BOOST_UNORDERED_DEFAULT_TYPE_TMPLT Chris@16: # undef BOOST_UNORDERED_DEFAULT_TYPE Chris@16: Chris@16: //////////////////////////////////////////////////////////////////////////////// Chris@16: // Chris@16: // std::allocator_traits Chris@16: Chris@16: #elif BOOST_UNORDERED_USE_ALLOCATOR_TRAITS == 1 Chris@16: Chris@16: # include Chris@16: Chris@16: # define BOOST_UNORDERED_DETAIL_FULL_CONSTRUCT 1 Chris@16: Chris@16: namespace boost { namespace unordered { namespace detail { Chris@16: Chris@16: template Chris@16: struct allocator_traits : std::allocator_traits {}; Chris@16: Chris@16: template Chris@16: struct rebind_wrap Chris@16: { Chris@16: typedef typename std::allocator_traits:: Chris@16: template rebind_alloc type; Chris@16: }; Chris@16: }}} Chris@16: Chris@16: //////////////////////////////////////////////////////////////////////////////// Chris@16: // Chris@16: // boost::container::allocator_traits Chris@16: Chris@16: #elif BOOST_UNORDERED_USE_ALLOCATOR_TRAITS == 2 Chris@16: Chris@16: # include Chris@16: Chris@16: # define BOOST_UNORDERED_DETAIL_FULL_CONSTRUCT 0 Chris@16: Chris@16: namespace boost { namespace unordered { namespace detail { Chris@16: Chris@16: template Chris@16: struct allocator_traits : Chris@16: boost::container::allocator_traits {}; Chris@16: Chris@16: template Chris@16: struct rebind_wrap : Chris@16: boost::container::allocator_traits:: Chris@16: template portable_rebind_alloc Chris@16: {}; Chris@16: Chris@16: }}} Chris@16: Chris@16: #else Chris@16: Chris@16: #error "Invalid BOOST_UNORDERED_USE_ALLOCATOR_TRAITS value." Chris@16: Chris@16: #endif Chris@16: Chris@16: Chris@16: namespace boost { namespace unordered { namespace detail { namespace func { Chris@16: Chris@16: //////////////////////////////////////////////////////////////////////////// Chris@16: // call_construct Chris@16: Chris@16: #if !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES) Chris@16: Chris@16: # if BOOST_UNORDERED_DETAIL_FULL_CONSTRUCT Chris@16: Chris@16: template Chris@16: inline void call_construct(Alloc& alloc, T* address, Chris@16: BOOST_FWD_REF(Args)... args) Chris@16: { Chris@16: boost::unordered::detail::allocator_traits::construct(alloc, Chris@16: address, boost::forward(args)...); Chris@16: } Chris@16: Chris@16: template Chris@16: inline void destroy_value_impl(Alloc& alloc, T* x) { Chris@16: boost::unordered::detail::allocator_traits::destroy(alloc, x); Chris@16: } Chris@16: Chris@16: Chris@16: # else Chris@16: Chris@16: template Chris@16: inline void call_construct(Alloc&, T* address, Chris@16: BOOST_FWD_REF(Args)... args) Chris@16: { Chris@16: new((void*) address) T(boost::forward(args)...); Chris@16: } Chris@16: Chris@16: template Chris@16: inline void destroy_value_impl(Alloc&, T* x) { Chris@16: boost::unordered::detail::func::destroy(x); Chris@16: } Chris@16: Chris@16: Chris@16: # endif Chris@16: Chris@16: #else Chris@16: Chris@16: template Chris@16: inline void destroy_value_impl(Alloc&, T* x) { Chris@16: boost::unordered::detail::func::destroy(x); Chris@16: } Chris@16: Chris@16: #endif Chris@16: Chris@16: //////////////////////////////////////////////////////////////////////////// Chris@16: // Construct from tuple Chris@16: // Chris@16: // Used for piecewise construction. Chris@16: Chris@16: #if !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES) Chris@16: Chris@16: # define BOOST_UNORDERED_CONSTRUCT_FROM_TUPLE(n, namespace_) \ Chris@16: template \ Chris@16: void construct_from_tuple(Alloc& alloc, T* ptr, namespace_ tuple<>) \ Chris@16: { \ Chris@16: boost::unordered::detail::func::call_construct(alloc, ptr); \ Chris@16: } \ Chris@16: \ Chris@16: BOOST_PP_REPEAT_FROM_TO(1, n, \ Chris@16: BOOST_UNORDERED_CONSTRUCT_FROM_TUPLE_IMPL, namespace_) Chris@16: Chris@16: # define BOOST_UNORDERED_CONSTRUCT_FROM_TUPLE_IMPL(z, n, namespace_) \ Chris@16: template \ Chris@16: void construct_from_tuple(Alloc& alloc, T* ptr, \ Chris@16: namespace_ tuple const& x) \ Chris@16: { \ Chris@16: boost::unordered::detail::func::call_construct(alloc, ptr, \ Chris@16: BOOST_PP_ENUM_##z(n, BOOST_UNORDERED_GET_TUPLE_ARG, namespace_) \ Chris@16: ); \ Chris@16: } Chris@16: Chris@16: # define BOOST_UNORDERED_GET_TUPLE_ARG(z, n, namespace_) \ Chris@16: namespace_ get(x) Chris@16: Chris@16: #elif !defined(__SUNPRO_CC) Chris@16: Chris@16: # define BOOST_UNORDERED_CONSTRUCT_FROM_TUPLE(n, namespace_) \ Chris@16: template \ Chris@16: void construct_from_tuple(Alloc&, T* ptr, namespace_ tuple<>) \ Chris@16: { \ Chris@16: new ((void*) ptr) T(); \ Chris@16: } \ Chris@16: \ Chris@16: BOOST_PP_REPEAT_FROM_TO(1, n, \ Chris@16: BOOST_UNORDERED_CONSTRUCT_FROM_TUPLE_IMPL, namespace_) Chris@16: Chris@16: # define BOOST_UNORDERED_CONSTRUCT_FROM_TUPLE_IMPL(z, n, namespace_) \ Chris@16: template \ Chris@16: void construct_from_tuple(Alloc&, T* ptr, \ Chris@16: namespace_ tuple const& x) \ Chris@16: { \ Chris@16: new ((void*) ptr) T( \ Chris@16: BOOST_PP_ENUM_##z(n, BOOST_UNORDERED_GET_TUPLE_ARG, namespace_) \ Chris@16: ); \ Chris@16: } Chris@16: Chris@16: # define BOOST_UNORDERED_GET_TUPLE_ARG(z, n, namespace_) \ Chris@16: namespace_ get(x) Chris@16: Chris@16: #else Chris@16: Chris@16: template struct length {}; Chris@16: Chris@16: # define BOOST_UNORDERED_CONSTRUCT_FROM_TUPLE(n, namespace_) \ Chris@16: template \ Chris@16: void construct_from_tuple_impl( \ Chris@101: boost::unordered::detail::func::length<0>, Alloc&, T* ptr, \ Chris@16: namespace_ tuple<>) \ Chris@16: { \ Chris@16: new ((void*) ptr) T(); \ Chris@16: } \ Chris@16: \ Chris@16: BOOST_PP_REPEAT_FROM_TO(1, n, \ Chris@16: BOOST_UNORDERED_CONSTRUCT_FROM_TUPLE_IMPL, namespace_) Chris@16: Chris@16: # define BOOST_UNORDERED_CONSTRUCT_FROM_TUPLE_IMPL(z, n, namespace_) \ Chris@16: template \ Chris@16: void construct_from_tuple_impl( \ Chris@101: boost::unordered::detail::func::length, Alloc&, T* ptr, \ Chris@16: namespace_ tuple const& x) \ Chris@16: { \ Chris@16: new ((void*) ptr) T( \ Chris@16: BOOST_PP_ENUM_##z(n, BOOST_UNORDERED_GET_TUPLE_ARG, namespace_) \ Chris@16: ); \ Chris@16: } Chris@16: Chris@16: # define BOOST_UNORDERED_GET_TUPLE_ARG(z, n, namespace_) \ Chris@16: namespace_ get(x) Chris@16: Chris@16: #endif Chris@16: Chris@16: BOOST_UNORDERED_CONSTRUCT_FROM_TUPLE(10, boost::) Chris@16: Chris@16: #if !defined(__SUNPRO_CC) && !defined(BOOST_NO_CXX11_HDR_TUPLE) Chris@16: BOOST_UNORDERED_CONSTRUCT_FROM_TUPLE(10, std::) Chris@16: #endif Chris@16: Chris@16: #undef BOOST_UNORDERED_CONSTRUCT_FROM_TUPLE Chris@16: #undef BOOST_UNORDERED_CONSTRUCT_FROM_TUPLE_IMPL Chris@16: #undef BOOST_UNORDERED_GET_TUPLE_ARG Chris@16: Chris@16: #if defined(__SUNPRO_CC) Chris@16: Chris@16: template Chris@16: void construct_from_tuple(Alloc& alloc, T* ptr, Tuple const& x) Chris@16: { Chris@16: construct_from_tuple_impl( Chris@101: boost::unordered::detail::func::length< Chris@16: boost::tuples::length::value>(), Chris@16: alloc, ptr, x); Chris@16: } Chris@16: Chris@16: #endif Chris@16: Chris@16: //////////////////////////////////////////////////////////////////////////// Chris@16: // Trait to check for piecewise construction. Chris@16: Chris@16: template Chris@16: struct use_piecewise { Chris@16: static choice1::type test(choice1, Chris@16: boost::unordered::piecewise_construct_t); Chris@16: Chris@16: static choice2::type test(choice2, ...); Chris@16: Chris@16: enum { value = sizeof(choice1::type) == Chris@16: sizeof(test(choose(), boost::unordered::detail::make())) }; Chris@16: }; Chris@16: Chris@16: #if !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES) Chris@16: Chris@16: //////////////////////////////////////////////////////////////////////////// Chris@16: // Construct from variadic parameters Chris@16: Chris@16: // For the standard pair constructor. Chris@16: Chris@16: template Chris@16: inline void construct_value_impl(Alloc& alloc, T* address, Chris@16: BOOST_FWD_REF(Args)... args) Chris@16: { Chris@16: boost::unordered::detail::func::call_construct(alloc, Chris@16: address, boost::forward(args)...); Chris@16: } Chris@16: Chris@16: // Special case for piece_construct Chris@16: // Chris@16: // TODO: When possible, it might be better to use std::pair's Chris@16: // constructor for std::piece_construct with std::tuple. Chris@16: Chris@16: template Chris@16: inline typename enable_if, void>::type Chris@16: construct_value_impl(Alloc& alloc, std::pair* address, Chris@16: BOOST_FWD_REF(A0), BOOST_FWD_REF(A1) a1, BOOST_FWD_REF(A2) a2) Chris@16: { Chris@16: boost::unordered::detail::func::construct_from_tuple(alloc, Chris@16: boost::addressof(address->first), boost::forward(a1)); Chris@16: boost::unordered::detail::func::construct_from_tuple(alloc, Chris@16: boost::addressof(address->second), boost::forward(a2)); Chris@16: } Chris@16: Chris@16: #else // BOOST_NO_CXX11_VARIADIC_TEMPLATES Chris@16: Chris@16: //////////////////////////////////////////////////////////////////////////////// Chris@16: // Construct from emplace_args Chris@16: Chris@16: // Explicitly write out first three overloads for the sake of sane Chris@16: // error messages. Chris@16: Chris@16: template Chris@16: inline void construct_value_impl(Alloc&, T* address, Chris@16: emplace_args1 const& args) Chris@16: { Chris@16: new((void*) address) T(boost::forward(args.a0)); Chris@16: } Chris@16: Chris@16: template Chris@16: inline void construct_value_impl(Alloc&, T* address, Chris@16: emplace_args2 const& args) Chris@16: { Chris@16: new((void*) address) T( Chris@16: boost::forward(args.a0), Chris@16: boost::forward(args.a1) Chris@16: ); Chris@16: } Chris@16: Chris@16: template Chris@16: inline void construct_value_impl(Alloc&, T* address, Chris@16: emplace_args3 const& args) Chris@16: { Chris@16: new((void*) address) T( Chris@16: boost::forward(args.a0), Chris@16: boost::forward(args.a1), Chris@16: boost::forward(args.a2) Chris@16: ); Chris@16: } Chris@16: Chris@16: // Use a macro for the rest. Chris@16: Chris@16: #define BOOST_UNORDERED_CONSTRUCT_IMPL(z, num_params, _) \ Chris@16: template < \ Chris@16: typename Alloc, typename T, \ Chris@16: BOOST_PP_ENUM_PARAMS_Z(z, num_params, typename A) \ Chris@16: > \ Chris@16: inline void construct_value_impl(Alloc&, T* address, \ Chris@16: boost::unordered::detail::BOOST_PP_CAT(emplace_args,num_params) < \ Chris@16: BOOST_PP_ENUM_PARAMS_Z(z, num_params, A) \ Chris@16: > const& args) \ Chris@16: { \ Chris@16: new((void*) address) T( \ Chris@16: BOOST_PP_ENUM_##z(num_params, BOOST_UNORDERED_CALL_FORWARD, \ Chris@16: args.a)); \ Chris@16: } Chris@16: Chris@16: BOOST_PP_REPEAT_FROM_TO(4, BOOST_UNORDERED_EMPLACE_LIMIT, Chris@16: BOOST_UNORDERED_CONSTRUCT_IMPL, _) Chris@16: Chris@16: #undef BOOST_UNORDERED_CONSTRUCT_IMPL Chris@16: Chris@16: // Construct with piece_construct Chris@16: Chris@16: template Chris@16: inline void construct_value_impl(Alloc& alloc, std::pair* address, Chris@16: boost::unordered::detail::emplace_args3 const& args, Chris@16: typename enable_if, void*>::type = 0) Chris@16: { Chris@16: boost::unordered::detail::func::construct_from_tuple(alloc, Chris@16: boost::addressof(address->first), args.a1); Chris@16: boost::unordered::detail::func::construct_from_tuple(alloc, Chris@16: boost::addressof(address->second), args.a2); Chris@16: } Chris@16: Chris@16: #endif // BOOST_NO_CXX11_VARIADIC_TEMPLATES Chris@16: Chris@16: }}}} Chris@16: Chris@16: namespace boost { namespace unordered { namespace detail { Chris@16: Chris@16: //////////////////////////////////////////////////////////////////////////// Chris@16: // Chris@16: // array_constructor Chris@16: // Chris@16: // Allocate and construct an array in an exception safe manner, and Chris@16: // clean up if an exception is thrown before the container takes charge Chris@16: // of it. Chris@16: Chris@16: template Chris@16: struct array_constructor Chris@16: { Chris@16: typedef boost::unordered::detail::allocator_traits traits; Chris@16: typedef typename traits::pointer pointer; Chris@16: Chris@16: Allocator& alloc_; Chris@16: pointer ptr_; Chris@16: pointer constructed_; Chris@16: std::size_t length_; Chris@16: Chris@16: array_constructor(Allocator& a) Chris@16: : alloc_(a), ptr_(), constructed_(), length_(0) Chris@16: { Chris@16: constructed_ = pointer(); Chris@16: ptr_ = pointer(); Chris@16: } Chris@16: Chris@16: ~array_constructor() { Chris@16: if (ptr_) { Chris@101: for(pointer p = ptr_; p != constructed_; ++p) { Chris@101: boost::unordered::detail::func::destroy( Chris@101: boost::addressof(*p)); Chris@101: } Chris@16: Chris@16: traits::deallocate(alloc_, ptr_, length_); Chris@16: } Chris@16: } Chris@16: Chris@16: template Chris@16: void construct(V const& v, std::size_t l) Chris@16: { Chris@16: BOOST_ASSERT(!ptr_); Chris@16: length_ = l; Chris@16: ptr_ = traits::allocate(alloc_, length_); Chris@16: pointer end = ptr_ + static_cast(length_); Chris@101: for(constructed_ = ptr_; constructed_ != end; ++constructed_) { Chris@101: new ((void*) boost::addressof(*constructed_)) V(v); Chris@101: } Chris@16: } Chris@16: Chris@16: pointer get() const Chris@16: { Chris@16: return ptr_; Chris@16: } Chris@16: Chris@16: pointer release() Chris@16: { Chris@16: pointer p(ptr_); Chris@16: ptr_ = pointer(); Chris@16: return p; Chris@16: } Chris@16: Chris@16: private: Chris@16: Chris@16: array_constructor(array_constructor const&); Chris@16: array_constructor& operator=(array_constructor const&); Chris@16: }; Chris@16: }}} Chris@16: Chris@16: #if defined(BOOST_MSVC) Chris@16: #pragma warning(pop) Chris@16: #endif Chris@16: Chris@16: #endif