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