1
0
forked from boostorg/core

Revise allocator access utilities

Now supports MSVC 2013, and no workarounds use allocator_traits.
This commit is contained in:
Glen Fernandes
2021-02-15 17:23:42 -05:00
parent 2e5ecbe6f6
commit 86bff4c2d3

View File

@ -11,11 +11,7 @@ Distributed under the Boost Software License, Version 1.0.
#include <boost/config.hpp> #include <boost/config.hpp>
#if !defined(BOOST_NO_CXX11_ALLOCATOR) #if !defined(BOOST_NO_CXX11_ALLOCATOR)
#include <boost/core/pointer_traits.hpp> #include <boost/core/pointer_traits.hpp>
#if !defined(BOOST_MSVC)
#include <limits> #include <limits>
#else
#include <memory>
#endif
#include <type_traits> #include <type_traits>
#endif #endif
#include <new> #include <new>
@ -23,21 +19,20 @@ Distributed under the Boost Software License, Version 1.0.
#include <utility> #include <utility>
#endif #endif
namespace boost { #if defined(_LIBCPP_SUPPRESS_DEPRECATED_PUSH)
namespace detail { _LIBCPP_SUPPRESS_DEPRECATED_PUSH
#if defined(BOOST_NO_CXX11_ALLOCATOR)
struct alloc_false {
BOOST_STATIC_CONSTEXPR bool value = false;
};
#else
template<class>
struct alloc_void {
typedef void type;
};
#endif #endif
} /* detail */ #if defined(_STL_DISABLE_DEPRECATED_WARNING)
_STL_DISABLE_DEPRECATED_WARNING
#endif
#if defined(_MSC_VER)
#pragma warning(push)
#pragma warning(disable:4996)
#endif
namespace boost {
template<class A> template<class A>
struct allocator_value_type { struct allocator_value_type {
@ -49,17 +44,21 @@ template<class A>
struct allocator_pointer { struct allocator_pointer {
typedef typename A::pointer type; typedef typename A::pointer type;
}; };
#elif defined(BOOST_MSVC)
template<class A>
struct allocator_pointer {
typedef typename std::allocator_traits<A>::pointer type;
};
#else #else
template<class A, class = void> template<class A, class = void>
struct allocator_pointer { struct allocator_pointer {
typedef typename A::value_type* type; typedef typename A::value_type* type;
}; };
namespace detail {
template<class>
struct alloc_void {
typedef void type;
};
} /* detail */
template<class A> template<class A>
struct allocator_pointer<A, struct allocator_pointer<A,
typename detail::alloc_void<typename A::pointer>::type> { typename detail::alloc_void<typename A::pointer>::type> {
@ -72,11 +71,6 @@ template<class A>
struct allocator_const_pointer { struct allocator_const_pointer {
typedef typename A::const_pointer type; typedef typename A::const_pointer type;
}; };
#elif defined(BOOST_MSVC)
template<class A>
struct allocator_const_pointer {
typedef typename std::allocator_traits<A>::const_pointer type;
};
#else #else
template<class A, class = void> template<class A, class = void>
struct allocator_const_pointer { struct allocator_const_pointer {
@ -137,11 +131,6 @@ template<class A>
struct allocator_difference_type { struct allocator_difference_type {
typedef typename A::difference_type type; typedef typename A::difference_type type;
}; };
#elif defined(BOOST_MSVC)
template<class A>
struct allocator_difference_type {
typedef typename std::allocator_traits<A>::difference_type type;
};
#else #else
template<class A, class = void> template<class A, class = void>
struct allocator_difference_type { struct allocator_difference_type {
@ -161,11 +150,6 @@ template<class A>
struct allocator_size_type { struct allocator_size_type {
typedef typename A::size_type type; typedef typename A::size_type type;
}; };
#elif defined(BOOST_MSVC)
template<class A>
struct allocator_size_type {
typedef typename std::allocator_traits<A>::size_type type;
};
#else #else
template<class A, class = void> template<class A, class = void>
struct allocator_size_type { struct allocator_size_type {
@ -181,6 +165,14 @@ struct allocator_size_type<A,
#endif #endif
#if defined(BOOST_NO_CXX11_ALLOCATOR) #if defined(BOOST_NO_CXX11_ALLOCATOR)
namespace detail {
struct alloc_false {
BOOST_STATIC_CONSTEXPR bool value = false;
};
} /* detail */
template<class A> template<class A>
struct allocator_propagate_on_container_copy_assignment { struct allocator_propagate_on_container_copy_assignment {
typedef detail::alloc_false type; typedef detail::alloc_false type;
@ -260,11 +252,6 @@ template<class A, class T>
struct allocator_rebind { struct allocator_rebind {
typedef typename A::template rebind<T>::other type; typedef typename A::template rebind<T>::other type;
}; };
#elif defined(BOOST_MSVC)
template<class A, class T>
struct allocator_rebind {
typedef typename std::allocator_traits<A>::template rebind_alloc<T> type;
};
#else #else
namespace detail { namespace detail {
@ -313,36 +300,31 @@ allocator_allocate(A& a, typename allocator_size_type<A>::type n,
{ {
return a.allocate(n, h); return a.allocate(n, h);
} }
#elif defined(BOOST_MSVC)
template<class A>
inline typename allocator_pointer<A>::type
allocator_allocate(A& a, typename allocator_size_type<A>::type n,
typename allocator_const_void_pointer<A>::type h)
{
return std::allocator_traits<A>::allocate(a, n, h);
}
#else #else
namespace detail { namespace detail {
template<class, class, class, class = void> struct alloc_none { };
struct alloc_has_allocate {
BOOST_STATIC_CONSTEXPR bool value = false;
};
template<class A, class N, class H> template<class A>
struct alloc_has_allocate<A, N, H, class alloc_has_allocate {
typename alloc_void<decltype(std::declval<A&>().allocate(std::declval<N>(), template<class O>
std::declval<H>()))>::type> { static auto check(int) -> decltype(std::declval<O&>().allocate(
BOOST_STATIC_CONSTEXPR bool value = true; std::declval<typename allocator_size_type<A>::type>(),
std::declval<typename allocator_const_void_pointer<A>::type>()));
template<class>
static alloc_none check(long);
public:
BOOST_STATIC_CONSTEXPR bool value =
!std::is_same<decltype(check<A>(0)), alloc_none>::value;
}; };
} /* detail */ } /* detail */
template<class A> template<class A>
inline typename std::enable_if<detail::alloc_has_allocate<A, inline typename std::enable_if<detail::alloc_has_allocate<A>::value,
typename allocator_size_type<A>::type, typename allocator_pointer<A>::type>::type
typename allocator_const_void_pointer<A>::type>::value,
typename allocator_pointer<A>::type>::type
allocator_allocate(A& a, typename allocator_size_type<A>::type n, allocator_allocate(A& a, typename allocator_size_type<A>::type n,
typename allocator_const_void_pointer<A>::type h) typename allocator_const_void_pointer<A>::type h)
{ {
@ -350,10 +332,8 @@ allocator_allocate(A& a, typename allocator_size_type<A>::type n,
} }
template<class A> template<class A>
inline typename std::enable_if<!detail::alloc_has_allocate<A, inline typename std::enable_if<!detail::alloc_has_allocate<A>::value,
typename allocator_size_type<A>::type, typename allocator_pointer<A>::type>::type
typename allocator_const_void_pointer<A>::type>::value,
typename allocator_pointer<A>::type>::type
allocator_allocate(A& a, typename allocator_size_type<A>::type n, allocator_allocate(A& a, typename allocator_size_type<A>::type n,
typename allocator_const_void_pointer<A>::type) typename allocator_const_void_pointer<A>::type)
{ {
@ -400,32 +380,28 @@ allocator_construct(A&, T* p, V& v)
::new((void*)p) T(v); ::new((void*)p) T(v);
} }
#endif #endif
#elif defined(BOOST_MSVC)
template<class A, class T, class... Args>
inline void
allocator_construct(A& a, T* p, Args&&... args)
{
std::allocator_traits<A>::construct(a, p, std::forward<Args>(args)...);
}
#else #else
namespace detail { namespace detail {
template<class, class, class, class...>
struct alloc_has_construct {
BOOST_STATIC_CONSTEXPR bool value = false;
};
template<class A, class T, class... Args> template<class A, class T, class... Args>
struct alloc_has_construct<typename alloc_void<decltype(std::declval<A class alloc_has_construct {
&>().construct(std::declval<T*>(), std::declval<Args&&>()...))>::type, template<class O>
A, T, Args...> { static auto check(int)
BOOST_STATIC_CONSTEXPR bool value = true; -> decltype(std::declval<O&>().construct(std::declval<T*>(),
std::declval<Args&&>()...));
template<class>
static alloc_none check(long);
public:
BOOST_STATIC_CONSTEXPR bool value =
!std::is_same<decltype(check<A>(0)), alloc_none>::value;
}; };
} /* detail */ } /* detail */
template<class A, class T, class... Args> template<class A, class T, class... Args>
inline typename std::enable_if<detail::alloc_has_construct<void, A, T, inline typename std::enable_if<detail::alloc_has_construct<A, T,
Args...>::value>::type Args...>::value>::type
allocator_construct(A& a, T* p, Args&&... args) allocator_construct(A& a, T* p, Args&&... args)
{ {
@ -433,7 +409,7 @@ allocator_construct(A& a, T* p, Args&&... args)
} }
template<class A, class T, class... Args> template<class A, class T, class... Args>
inline typename std::enable_if<!detail::alloc_has_construct<void, A, T, inline typename std::enable_if<!detail::alloc_has_construct<A, T,
Args...>::value>::type Args...>::value>::type
allocator_construct(A&, T* p, Args&&... args) allocator_construct(A&, T* p, Args&&... args)
{ {
@ -449,26 +425,21 @@ allocator_destroy(A&, T* p)
p->~T(); p->~T();
(void)p; (void)p;
} }
#elif defined(BOOST_MSVC)
template<class A, class T>
inline void
allocator_destroy(A& a, T* p)
{
std::allocator_traits<A>::destroy(a, p);
}
#else #else
namespace detail { namespace detail {
template<class, class, class = void>
struct alloc_has_destroy {
BOOST_STATIC_CONSTEXPR bool value = false;
};
template<class A, class T> template<class A, class T>
struct alloc_has_destroy<A, T, class alloc_has_destroy {
typename alloc_void<decltype(std::declval<A template<class O>
&>().destroy(std::declval<T*>()))>::type> { static auto check(int)
BOOST_STATIC_CONSTEXPR bool value = true; -> decltype(std::declval<O&>().destroy(std::declval<T*>()));
template<class>
static alloc_none check(long);
public:
BOOST_STATIC_CONSTEXPR bool value =
!std::is_same<decltype(check<A>(0)), alloc_none>::value;
}; };
} /* detail */ } /* detail */
@ -496,26 +467,20 @@ allocator_max_size(const A& a)
{ {
return a.max_size(); return a.max_size();
} }
#elif defined(BOOST_MSVC)
template<class A>
inline typename allocator_size_type<A>::type
allocator_max_size(const A& a)
{
return std::allocator_traits<A>::max_size(a);
}
#else #else
namespace detail { namespace detail {
template<class, class = void>
struct alloc_has_max_size {
BOOST_STATIC_CONSTEXPR bool value = false;
};
template<class A> template<class A>
struct alloc_has_max_size<A, class alloc_has_max_size {
typename alloc_void<decltype(std::declval<const template<class O>
A&>().max_size())>::type> { static auto check(int) -> decltype(std::declval<O&>().max_size());
BOOST_STATIC_CONSTEXPR bool value = true;
template<class>
static alloc_none check(long);
public:
BOOST_STATIC_CONSTEXPR bool value =
!std::is_same<decltype(check<A>(0)), alloc_none>::value;
}; };
} /* detail */ } /* detail */
@ -545,26 +510,21 @@ allocator_select_on_container_copy_construction(const A& a)
{ {
return a; return a;
} }
#elif defined(BOOST_MSVC)
template<class A>
inline A
allocator_select_on_container_copy_construction(const A& a)
{
return std::allocator_traits<A>::select_on_container_copy_construction(a);
}
#else #else
namespace detail { namespace detail {
template<class, class = void>
struct alloc_has_soccc {
BOOST_STATIC_CONSTEXPR bool value = false;
};
template<class A> template<class A>
struct alloc_has_soccc<A, class alloc_has_soccc {
typename alloc_void<decltype(std::declval<const template<class O>
A&>().select_on_container_copy_construction())>::type> { static auto check(int)
BOOST_STATIC_CONSTEXPR bool value = true; -> decltype(std::declval<O&>().select_on_container_copy_construction());
template<class>
static alloc_none check(long);
public:
BOOST_STATIC_CONSTEXPR bool value =
!std::is_same<decltype(check<A>(0)), alloc_none>::value;
}; };
} /* detail */ } /* detail */
@ -630,4 +590,16 @@ using allocator_rebind_t = typename allocator_rebind<A, T>::type;
} /* boost */ } /* boost */
#if defined(_LIBCPP_SUPPRESS_DEPRECATED_POP)
_LIBCPP_SUPPRESS_DEPRECATED_POP
#endif
#if defined(_STL_RESTORE_DEPRECATED_WARNING)
_STL_RESTORE_DEPRECATED_WARNING
#endif
#if defined(_MSC_VER)
#pragma warning(pop)
#endif
#endif #endif