1
0
forked from boostorg/core

Simplify pointer_traits detail traits

This commit is contained in:
Glen Fernandes
2018-01-27 00:56:45 -05:00
parent b5dcd204e5
commit e5281335e0

View File

@ -1,5 +1,5 @@
/* /*
Copyright 2017 Glen Joseph Fernandes Copyright 2017-2018 Glen Joseph Fernandes
(glenjofe@gmail.com) (glenjofe@gmail.com)
Distributed under the Boost Software License, Version 1.0. Distributed under the Boost Software License, Version 1.0.
@ -24,7 +24,7 @@ namespace detail {
template<class U> template<class U>
inline typename boost::pointer_traits<U>::element_type* inline typename boost::pointer_traits<U>::element_type*
ptr_traits_address(const U& v) BOOST_NOEXCEPT ptr_to_address(const U& v) BOOST_NOEXCEPT
{ {
return boost::pointer_traits<U>::to_address(v); return boost::pointer_traits<U>::to_address(v);
} }
@ -41,7 +41,7 @@ struct pointer_traits
}; };
static typename std::pointer_traits<T>::element_type* static typename std::pointer_traits<T>::element_type*
to_address(const T& v) BOOST_NOEXCEPT { to_address(const T& v) BOOST_NOEXCEPT {
return detail::ptr_traits_address(v.operator->()); return detail::ptr_to_address(v.operator->());
} }
}; };
@ -59,136 +59,103 @@ struct pointer_traits<T*>
#else #else
namespace detail { namespace detail {
struct ptr_traits_none { char first, second; }; template<class>
struct ptr_void {
template<class T> typedef void type;
struct ptr_traits_has_element {
private:
template<class U>
static ptr_traits_none call(...);
template<class U>
static char call(typename U::element_type* = 0);
public:
static const bool value = sizeof(call<T>(0)) == 1;
}; };
template<class T> template<class T>
struct ptr_traits_first; struct ptr_first;
#if !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES) #if !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES)
template<template<class, class...> class T, class U, class... Args> template<template<class, class...> class T, class U, class... Args>
struct ptr_traits_first<T<U, Args...> > { struct ptr_first<T<U, Args...> > {
typedef U type; typedef U type;
}; };
#else #else
template<template<class> class T, class U> template<template<class> class T, class U>
struct ptr_traits_first<T<U> > { struct ptr_first<T<U> > {
typedef U type; typedef U type;
}; };
template<template<class, class> class T, class U1, class U2> template<template<class, class> class T, class U1, class U2>
struct ptr_traits_first<T<U1, U2> > { struct ptr_first<T<U1, U2> > {
typedef U1 type; typedef U1 type;
}; };
template<template<class, class, class> class T, class U1, class U2, class U3> template<template<class, class, class> class T, class U1, class U2, class U3>
struct ptr_traits_first<T<U1, U2, U3> > { struct ptr_first<T<U1, U2, U3> > {
typedef U1 type; typedef U1 type;
}; };
#endif #endif
template<class T, bool = ptr_traits_has_element<T>::value> template<class T, class = void>
struct ptr_traits_element { struct ptr_element {
typedef typename ptr_first<T>::type type;
};
template<class T>
struct ptr_element<T, typename ptr_void<typename T::element_type>::type> {
typedef typename T::element_type type; typedef typename T::element_type type;
}; };
template<class T> template<class, class = void>
struct ptr_traits_element<T, false> { struct ptr_difference {
typedef typename ptr_traits_first<T>::type type;
};
template<class T>
struct ptr_traits_has_difference {
private:
template<class U>
static ptr_traits_none call(...);
template<class U>
static char call(typename U::difference_type* = 0);
public:
static const bool value = sizeof(call<T>(0)) == 1;
};
template<class T, bool = ptr_traits_has_difference<T>::value>
struct ptr_traits_difference {
typedef typename T::difference_type type;
};
template<class T>
struct ptr_traits_difference<T, false> {
typedef std::ptrdiff_t type; typedef std::ptrdiff_t type;
}; };
template<class T, class V> template<class T>
struct ptr_traits_has_rebind { struct ptr_difference<T,
private: typename ptr_void<typename T::difference_type>::type> {
template<class U> typedef typename T::difference_type type;
static ptr_traits_none call(...);
template<class U>
static char call(typename U::template rebind<V>* = 0);
public:
static const bool value = sizeof(call<T>(0)) == 1;
}; };
template<class T, class V> template<class T, class V>
struct ptr_traits_rebind_to; struct ptr_rebind_to;
#if !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES) #if !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES)
template<template<class, class...> class T, class U, class... Args, class V> template<template<class, class...> class T, class U, class... Args, class V>
struct ptr_traits_rebind_to<T<U, Args...>, V> { struct ptr_rebind_to<T<U, Args...>, V> {
typedef T<V, Args...> type; typedef T<V, Args...> type;
}; };
#else #else
template<template<class> class T, class U, class V> template<template<class> class T, class U, class V>
struct ptr_traits_rebind_to<T<U>, V> { struct ptr_rebind_to<T<U>, V> {
typedef T<V> type; typedef T<V> type;
}; };
template<template<class, class> class T, class U1, class U2, class V> template<template<class, class> class T, class U1, class U2, class V>
struct ptr_traits_rebind_to<T<U1, U2>, V> { struct ptr_rebind_to<T<U1, U2>, V> {
typedef T<V, U2> type; typedef T<V, U2> type;
}; };
template<template<class, class, class> class T, template<template<class, class, class> class T,
class U1, class U2, class U3, class V> class U1, class U2, class U3, class V>
struct ptr_traits_rebind_to<T<U1, U2, U3>, V> { struct ptr_rebind_to<T<U1, U2, U3>, V> {
typedef T<V, U2, U3> type; typedef T<V, U2, U3> type;
}; };
#endif #endif
#if !defined(BOOST_NO_CXX11_TEMPLATE_ALIASES) template<class T, class U, class = void>
template<class T, class U, bool = ptr_traits_has_rebind<T, U>::value> struct ptr_rebind {
struct ptr_traits_rebind { typedef typename ptr_rebind_to<T, U>::type type;
typedef typename T::template rebind<U> type;
}; };
#if !defined(BOOST_NO_CXX11_TEMPLATE_ALIASES)
template<class T, class U> template<class T, class U>
struct ptr_traits_rebind<T, U, false> { struct ptr_rebind<T, U,
typedef typename ptr_traits_rebind_to<T, U>::type type; typename ptr_void<typename T::template rebind<U> >::type> {
}; typedef typename T::template rebind<U> type;
#else
template<class T, class U>
struct ptr_traits_rebind {
typedef typename ptr_traits_rebind_to<T, U>::type type;
}; };
#endif #endif
template<class T> template<class T>
struct ptr_traits_value { struct ptr_value {
typedef T type; typedef T type;
}; };
template<> template<>
struct ptr_traits_value<void> { struct ptr_value<void> {
typedef struct { } type; typedef struct { } type;
}; };
@ -197,22 +164,22 @@ struct ptr_traits_value<void> {
template<class T> template<class T>
struct pointer_traits { struct pointer_traits {
typedef T pointer; typedef T pointer;
typedef typename detail::ptr_traits_element<T>::type element_type; typedef typename detail::ptr_element<T>::type element_type;
typedef typename detail::ptr_traits_difference<T>::type difference_type; typedef typename detail::ptr_difference<T>::type difference_type;
template<class U> template<class U>
struct rebind_to { struct rebind_to {
typedef typename detail::ptr_traits_rebind<T, U>::type type; typedef typename detail::ptr_rebind<T, U>::type type;
}; };
#if !defined(BOOST_NO_CXX11_TEMPLATE_ALIASES) #if !defined(BOOST_NO_CXX11_TEMPLATE_ALIASES)
template<class U> template<class U>
using rebind = typename detail::ptr_traits_rebind<T, U>::type; using rebind = typename detail::ptr_rebind<T, U>::type;
#endif #endif
static pointer static pointer
pointer_to(typename detail::ptr_traits_value<element_type>::type& v) { pointer_to(typename detail::ptr_value<element_type>::type& v) {
return pointer::pointer_to(v); return pointer::pointer_to(v);
} }
static element_type* to_address(const pointer& v) BOOST_NOEXCEPT { static element_type* to_address(const pointer& v) BOOST_NOEXCEPT {
return detail::ptr_traits_address(v.operator->()); return detail::ptr_to_address(v.operator->());
} }
}; };
@ -230,8 +197,8 @@ struct pointer_traits<T*> {
using rebind = U*; using rebind = U*;
#endif #endif
static T* static T*
pointer_to(typename detail::ptr_traits_value<T>::type& v) BOOST_NOEXCEPT { pointer_to(typename detail::ptr_value<T>::type& v) BOOST_NOEXCEPT {
return addressof(v); return boost::addressof(v);
} }
static T* to_address(T* v) BOOST_NOEXCEPT { static T* to_address(T* v) BOOST_NOEXCEPT {
return v; return v;