Unordered: allocator_helpers cleanup.

[SVN r78370]
This commit is contained in:
Daniel James
2012-05-07 18:10:27 +00:00
parent 39aed02e32
commit 6604abe600

View File

@ -19,8 +19,15 @@
#include <boost/assert.hpp>
#include <boost/utility/addressof.hpp>
#if !defined(BOOST_UNORDERED_USE_ALLOCATOR_TRAITS)
////////////////////////////////////////////////////////////////////////////////
//
// Pick which version of allocator_traits to use
//
// 0 = Own partial implementation
// 1 = std::allocator_traits
// 2 = boost::container::allocator_traits
#if !defined(BOOST_UNORDERED_USE_ALLOCATOR_TRAITS)
# if defined(__GXX_EXPERIMENTAL_CXX0X__) && \
(__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 7))
# define BOOST_UNORDERED_USE_ALLOCATOR_TRAITS 1
@ -39,24 +46,15 @@
# define BOOST_UNORDERED_USE_ALLOCATOR_TRAITS 0
#endif
#if BOOST_UNORDERED_USE_ALLOCATOR_TRAITS == 0
# include <boost/limits.hpp>
# include <boost/utility/enable_if.hpp>
# include <boost/pointer_to_other.hpp>
# if defined(BOOST_NO_SFINAE_EXPR)
# include <boost/type_traits/is_same.hpp>
# endif
#elif BOOST_UNORDERED_USE_ALLOCATOR_TRAITS == 1
# include <memory>
#elif BOOST_UNORDERED_USE_ALLOCATOR_TRAITS == 2
# include <boost/container/allocator_traits.hpp>
#endif
////////////////////////////////////////////////////////////////////////////////
//
// Some utilities for implementing allocator_traits, but useful elsewhere so
// they're always defined.
#if !defined(BOOST_NO_0X_HDR_TYPE_TRAITS)
# include <type_traits>
#endif
namespace boost { namespace unordered { namespace detail {
////////////////////////////////////////////////////////////////////////////
@ -99,6 +97,13 @@ namespace boost { namespace unordered { namespace detail {
////////////////////////////////////////////////////////////////////////////
// Expression test mechanism
//
// When SFINAE expressions are available, define
// BOOST_UNORDERED_HAS_FUNCTION which can check if a function call is
// supported by a class, otherwise define BOOST_UNORDERED_HAS_MEMBER which
// can detect if a class has the specified member, but not that it has the
// correct type, this is good enough for a passable impression of
// allocator_traits.
#if !defined(BOOST_NO_SFINAE_EXPR)
@ -172,38 +177,22 @@ namespace boost { namespace unordered { namespace detail {
#endif
////////////////////////////////////////////////////////////////////////////
}}}
////////////////////////////////////////////////////////////////////////////////
//
// Allocator traits
//
// First our implementation, then later light wrappers around the alternatives
#if BOOST_UNORDERED_USE_ALLOCATOR_TRAITS == 1
#if BOOST_UNORDERED_USE_ALLOCATOR_TRAITS == 0
#define BOOST_UNORDERED_DETAIL_FULL_CONSTRUCT 1
template <typename Alloc>
struct allocator_traits : std::allocator_traits<Alloc> {};
template <typename Alloc, typename T>
struct rebind_wrap
{
typedef typename std::allocator_traits<Alloc>::
template rebind_alloc<T> type;
};
#elif BOOST_UNORDERED_USE_ALLOCATOR_TRAITS == 2
#define BOOST_UNORDERED_DETAIL_FULL_CONSTRUCT 0
template <typename Alloc>
struct allocator_traits :
boost::container::allocator_traits<Alloc> {};
template <typename Alloc, typename T>
struct rebind_wrap :
boost::container::allocator_traits<Alloc>::
template portable_rebind_alloc<T>
{};
#else
# include <boost/limits.hpp>
# include <boost/utility/enable_if.hpp>
# include <boost/pointer_to_other.hpp>
# if defined(BOOST_NO_SFINAE_EXPR)
# include <boost/type_traits/is_same.hpp>
# endif
# if defined(BOOST_UNORDERED_VARIADIC_MOVE) && \
!defined(BOOST_NO_SFINAE_EXPR)
@ -212,12 +201,13 @@ namespace boost { namespace unordered { namespace detail {
# define BOOST_UNORDERED_DETAIL_FULL_CONSTRUCT 0
# endif
namespace boost { namespace unordered { namespace detail {
// TODO: Does this match std::allocator_traits<Alloc>::rebind_alloc<T>?
template <typename Alloc, typename T>
struct rebind_wrap
{
typedef typename Alloc::BOOST_NESTED_TEMPLATE rebind<T>::other
type;
typedef typename Alloc::BOOST_NESTED_TEMPLATE rebind<T>::other type;
};
# if defined(BOOST_MSVC) && BOOST_MSVC <= 1400
@ -283,6 +273,7 @@ namespace boost { namespace unordered { namespace detail {
BOOST_UNORDERED_DEFAULT_TYPE_TMPLT(propagate_on_container_swap);
# if !defined(BOOST_NO_SFINAE_EXPR)
template <typename T>
BOOST_UNORDERED_HAS_FUNCTION(
select_on_container_copy_construction, U const, (), 0
@ -294,26 +285,32 @@ namespace boost { namespace unordered { namespace detail {
);
# if defined(BOOST_UNORDERED_VARIADIC_MOVE)
template <typename T, typename ValueType, typename... Args>
BOOST_UNORDERED_HAS_FUNCTION(
construct, U, (
boost::unordered::detail::make<ValueType*>(),
boost::unordered::detail::make<Args const>()...), 2
);
# else
template <typename T, typename ValueType>
BOOST_UNORDERED_HAS_FUNCTION(
construct, U, (
boost::unordered::detail::make<ValueType*>(),
boost::unordered::detail::make<ValueType const>()), 2
);
# endif
template <typename T, typename ValueType>
BOOST_UNORDERED_HAS_FUNCTION(
destroy, U, (boost::unordered::detail::make<ValueType*>()), 1
);
# else
template <typename T>
BOOST_UNORDERED_HAS_MEMBER(select_on_container_copy_construction);
@ -325,6 +322,7 @@ namespace boost { namespace unordered { namespace detail {
template <typename T, typename ValueType>
BOOST_UNORDERED_HAS_MEMBER(destroy);
# endif
template <typename Alloc>
@ -427,6 +425,22 @@ namespace boost { namespace unordered { namespace detail {
new ((void*) p) T(boost::forward<Args>(x)...);
}
template <typename T>
static typename boost::enable_if_c<
boost::unordered::detail::has_destroy<Alloc, T>::value>::type
destroy(Alloc& a, T* p)
{
a.destroy(p);
}
template <typename T>
static typename boost::disable_if_c<
boost::unordered::detail::has_destroy<Alloc, T>::value>::type
destroy(Alloc&, T* p)
{
boost::unordered::detail::destroy(p);
}
# elif !defined(BOOST_NO_SFINAE_EXPR)
template <typename T>
@ -445,31 +459,6 @@ namespace boost { namespace unordered { namespace detail {
new ((void*) p) T(x);
}
#else
// If we don't have SFINAE expressions, only construct the type
// that matches the allocator.
template <typename T>
static typename boost::enable_if_c<
boost::unordered::detail::has_construct<Alloc, T>::value &&
boost::is_same<T, value_type>::value
>::type
construct(Alloc& a, T* p, T const& x)
{
a.construct(p, x);
}
template <typename T>
static typename boost::disable_if_c<
boost::unordered::detail::has_construct<Alloc, T>::value &&
boost::is_same<T, value_type>::value
>::type
construct(Alloc&, T* p, T const& x)
{
new ((void*) p) T(x);
}
# endif
# if BOOST_UNORDERED_DETAIL_FULL_CONSTRUCT
@ -490,26 +479,31 @@ namespace boost { namespace unordered { namespace detail {
boost::unordered::detail::destroy(p);
}
#elif !defined(BOOST_NO_SFINAE_EXPR)
# else
// If we don't have SFINAE expressions, only call construct the type
// that matches the allocator.
template <typename T>
static typename boost::enable_if_c<
boost::unordered::detail::has_destroy<Alloc, T>::value>::type
destroy(Alloc& a, T* p)
boost::unordered::detail::has_construct<Alloc, T>::value &&
boost::is_same<T, value_type>::value
>::type
construct(Alloc& a, T* p, T const& x)
{
a.destroy(p);
a.construct(p, x);
}
template <typename T>
static typename boost::disable_if_c<
boost::unordered::detail::has_destroy<Alloc, T>::value>::type
destroy(Alloc&, T* p)
boost::unordered::detail::has_construct<Alloc, T>::value &&
boost::is_same<T, value_type>::value
>::type
construct(Alloc&, T* p, T const& x)
{
boost::unordered::detail::destroy(p);
new ((void*) p) T(x);
}
#else
template <typename T>
static typename boost::enable_if_c<
boost::unordered::detail::has_destroy<Alloc, T>::value &&
@ -557,13 +551,78 @@ namespace boost { namespace unordered { namespace detail {
Alloc,propagate_on_container_swap,false_type)
propagate_on_container_swap;
};
}}}
# undef BOOST_UNORDERED_DEFAULT_TYPE_TMPLT
# undef BOOST_UNORDERED_DEFAULT_TYPE
////////////////////////////////////////////////////////////////////////////////
//
// std::allocator_traits
#elif BOOST_UNORDERED_USE_ALLOCATOR_TRAITS == 1
# include <memory>
# define BOOST_UNORDERED_DETAIL_FULL_CONSTRUCT 1
namespace boost { namespace unordered { namespace detail {
template <typename Alloc>
struct allocator_traits : std::allocator_traits<Alloc> {};
template <typename Alloc, typename T>
struct rebind_wrap
{
typedef typename std::allocator_traits<Alloc>::
template rebind_alloc<T> type;
};
}}}
////////////////////////////////////////////////////////////////////////////////
//
// boost::container::allocator_traits
#elif BOOST_UNORDERED_USE_ALLOCATOR_TRAITS == 2
# include <boost/container/allocator_traits.hpp>
# define BOOST_UNORDERED_DETAIL_FULL_CONSTRUCT 0
namespace boost { namespace unordered { namespace detail {
template <typename Alloc>
struct allocator_traits :
boost::container::allocator_traits<Alloc> {};
template <typename Alloc, typename T>
struct rebind_wrap :
boost::container::allocator_traits<Alloc>::
template portable_rebind_alloc<T>
{};
}}}
#else
#error "Invalid BOOST_UNORDERED_USE_ALLOCATOR_TRAITS value."
#endif
////////////////////////////////////////////////////////////////////////////////
//
// Some helper functions for allocating & constructing
namespace boost { namespace unordered { namespace detail {
////////////////////////////////////////////////////////////////////////////
//
// construct_node/destroy_node
//
// Construct a node using the best available method.
#if BOOST_UNORDERED_DETAIL_FULL_CONSTRUCT
template <typename Alloc, typename T, BOOST_UNORDERED_EMPLACE_TEMPLATE>
inline void construct_node(Alloc& a, T* p, BOOST_UNORDERED_EMPLACE_ARGS)
{
@ -576,7 +635,9 @@ namespace boost { namespace unordered { namespace detail {
{
boost::unordered::detail::allocator_traits<Alloc>::destroy(a, p);
}
#else
template <typename Alloc, typename T, BOOST_UNORDERED_EMPLACE_TEMPLATE>
inline void construct_node(Alloc& a, T* p, BOOST_UNORDERED_EMPLACE_ARGS)
{
@ -596,8 +657,11 @@ namespace boost { namespace unordered { namespace detail {
boost::unordered::detail::destroy(p->value_ptr());
boost::unordered::detail::allocator_traits<Alloc>::destroy(a, p);
}
#endif
////////////////////////////////////////////////////////////////////////////
//
// array_constructor
//
// Allocate and construct an array in an exception safe manner, and
@ -653,7 +717,9 @@ namespace boost { namespace unordered { namespace detail {
ptr_ = pointer();
return p;
}
private:
array_constructor(array_constructor const&);
array_constructor& operator=(array_constructor const&);
};