Skip to content

Commit

Permalink
Revise allocator access utilities
Browse files Browse the repository at this point in the history
Now supports MSVC 2013, and no workarounds use allocator_traits.
  • Loading branch information
glenfe committed Feb 16, 2021
1 parent 2e5ecbe commit 86bff4c
Showing 1 changed file with 104 additions and 132 deletions.
236 changes: 104 additions & 132 deletions include/boost/core/allocator_access.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,33 +11,28 @@ 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>
#if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES)
#include <utility>
#endif

namespace boost {
namespace detail {
#if defined(_LIBCPP_SUPPRESS_DEPRECATED_PUSH)
_LIBCPP_SUPPRESS_DEPRECATED_PUSH
#endif

#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(_STL_DISABLE_DEPRECATED_WARNING)
_STL_DISABLE_DEPRECATED_WARNING
#endif

} /* detail */
#if defined(_MSC_VER)
#pragma warning(push)
#pragma warning(disable:4996)
#endif

namespace boost {

template<class A>
struct allocator_value_type {
Expand All @@ -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> {
Expand All @@ -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 {
Expand Down Expand Up @@ -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 {
Expand All @@ -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 {
Expand All @@ -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;
Expand Down Expand Up @@ -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 {

Expand Down Expand Up @@ -313,47 +300,40 @@ 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 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);

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;
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)
{
return a.allocate(n, h);
}

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)
{
Expand Down Expand Up @@ -400,40 +380,36 @@ 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)
{
a.construct(p, std::forward<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)
{
Expand All @@ -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 */
Expand Down Expand Up @@ -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 */
Expand Down Expand Up @@ -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 */
Expand Down Expand Up @@ -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

0 comments on commit 86bff4c

Please sign in to comment.