From 2cb4a96a99e8acbf57a31d4d06ed5e21799d878e Mon Sep 17 00:00:00 2001 From: Louis Dionne Date: Tue, 12 Jan 2021 11:53:24 -0500 Subject: [PATCH] [libc++] NFCI: Refactor allocator_traits The implementation had a lot of boilerplate and was more complicated than necessary. This NFC refactoring introduces a few macros to reduce code duplication, and uses a consistent style and formatting for the whole file. Differential Revision: https://reviews.llvm.org/D94544 --- libcxx/include/__memory/allocator_traits.h | 722 ++++++++------------- libcxx/include/memory | 4 +- 2 files changed, 269 insertions(+), 457 deletions(-) diff --git a/libcxx/include/__memory/allocator_traits.h b/libcxx/include/__memory/allocator_traits.h index cdbdb9ef8e37..9443f61b71bc 100644 --- a/libcxx/include/__memory/allocator_traits.h +++ b/libcxx/include/__memory/allocator_traits.h @@ -24,564 +24,376 @@ _LIBCPP_PUSH_MACROS _LIBCPP_BEGIN_NAMESPACE_STD -template -struct __has_pointer_type : false_type {}; +#define _LIBCPP_ALLOCATOR_TRAITS_HAS_XXX(NAME, PROPERTY) \ + template struct NAME : false_type { }; \ + template struct NAME<_Tp, typename __void_t::type> : true_type { } -template -struct __has_pointer_type<_Tp, - typename __void_t::type> : true_type {}; - -namespace __pointer_type_imp -{ - -template ::value> -struct __pointer_type -{ - typedef _LIBCPP_NODEBUG_TYPE typename _Dp::pointer type; +// __pointer +_LIBCPP_ALLOCATOR_TRAITS_HAS_XXX(__has_pointer, pointer); +template ::type, + bool = __has_pointer<_RawAlloc>::value> +struct __pointer { + using type _LIBCPP_NODEBUG_TYPE = typename _RawAlloc::pointer; +}; +template +struct __pointer<_Tp, _Alloc, _RawAlloc, false> { + using type _LIBCPP_NODEBUG_TYPE = _Tp*; }; -template -struct __pointer_type<_Tp, _Dp, false> -{ - typedef _LIBCPP_NODEBUG_TYPE _Tp* type; +// __const_pointer +_LIBCPP_ALLOCATOR_TRAITS_HAS_XXX(__has_const_pointer, const_pointer); +template ::value> +struct __const_pointer { + using type _LIBCPP_NODEBUG_TYPE = typename _Alloc::const_pointer; }; - -} // __pointer_type_imp - -template -struct __pointer_type -{ - typedef _LIBCPP_NODEBUG_TYPE typename __pointer_type_imp::__pointer_type<_Tp, typename remove_reference<_Dp>::type>::type type; -}; - -template -struct __has_const_pointer : false_type {}; - -template -struct __has_const_pointer<_Tp, - typename __void_t::type> : true_type {}; - -template ::value> -struct __const_pointer -{ - typedef _LIBCPP_NODEBUG_TYPE typename _Alloc::const_pointer type; -}; - template -struct __const_pointer<_Tp, _Ptr, _Alloc, false> -{ -#ifndef _LIBCPP_CXX03_LANG - typedef _LIBCPP_NODEBUG_TYPE typename pointer_traits<_Ptr>::template rebind type; +struct __const_pointer<_Tp, _Ptr, _Alloc, false> { +#ifdef _LIBCPP_CXX03_LANG + using type = typename pointer_traits<_Ptr>::template rebind::other; #else - typedef typename pointer_traits<_Ptr>::template rebind::other type; + using type _LIBCPP_NODEBUG_TYPE = typename pointer_traits<_Ptr>::template rebind; #endif }; -template -struct __has_void_pointer : false_type {}; - -template -struct __has_void_pointer<_Tp, - typename __void_t::type> : true_type {}; - -template ::value> -struct __void_pointer -{ - typedef _LIBCPP_NODEBUG_TYPE typename _Alloc::void_pointer type; +// __void_pointer +_LIBCPP_ALLOCATOR_TRAITS_HAS_XXX(__has_void_pointer, void_pointer); +template ::value> +struct __void_pointer { + using type _LIBCPP_NODEBUG_TYPE = typename _Alloc::void_pointer; }; - template -struct __void_pointer<_Ptr, _Alloc, false> -{ -#ifndef _LIBCPP_CXX03_LANG - typedef _LIBCPP_NODEBUG_TYPE typename pointer_traits<_Ptr>::template rebind type; +struct __void_pointer<_Ptr, _Alloc, false> { +#ifdef _LIBCPP_CXX03_LANG + using type _LIBCPP_NODEBUG_TYPE = typename pointer_traits<_Ptr>::template rebind::other; #else - typedef _LIBCPP_NODEBUG_TYPE typename pointer_traits<_Ptr>::template rebind::other type; + using type _LIBCPP_NODEBUG_TYPE = typename pointer_traits<_Ptr>::template rebind; #endif }; -template -struct __has_const_void_pointer : false_type {}; - -template -struct __has_const_void_pointer<_Tp, - typename __void_t::type> : true_type {}; - -template ::value> -struct __const_void_pointer -{ - typedef _LIBCPP_NODEBUG_TYPE typename _Alloc::const_void_pointer type; +// __const_void_pointer +_LIBCPP_ALLOCATOR_TRAITS_HAS_XXX(__has_const_void_pointer, const_void_pointer); +template ::value> +struct __const_void_pointer { + using type _LIBCPP_NODEBUG_TYPE = typename _Alloc::const_void_pointer; }; - template -struct __const_void_pointer<_Ptr, _Alloc, false> -{ -#ifndef _LIBCPP_CXX03_LANG - typedef _LIBCPP_NODEBUG_TYPE typename pointer_traits<_Ptr>::template rebind type; +struct __const_void_pointer<_Ptr, _Alloc, false> { +#ifdef _LIBCPP_CXX03_LANG + using type _LIBCPP_NODEBUG_TYPE = typename pointer_traits<_Ptr>::template rebind::other; #else - typedef _LIBCPP_NODEBUG_TYPE typename pointer_traits<_Ptr>::template rebind::other type; + using type _LIBCPP_NODEBUG_TYPE = typename pointer_traits<_Ptr>::template rebind; #endif }; -template -struct __has_size_type : false_type {}; - -template -struct __has_size_type<_Tp, - typename __void_t::type> : true_type {}; - +// __size_type +_LIBCPP_ALLOCATOR_TRAITS_HAS_XXX(__has_size_type, size_type); template ::value> -struct __size_type -{ - typedef _LIBCPP_NODEBUG_TYPE typename make_unsigned<_DiffType>::type type; -}; - +struct __size_type : make_unsigned<_DiffType> { }; template -struct __size_type<_Alloc, _DiffType, true> -{ - typedef _LIBCPP_NODEBUG_TYPE typename _Alloc::size_type type; +struct __size_type<_Alloc, _DiffType, true> { + using type _LIBCPP_NODEBUG_TYPE = typename _Alloc::size_type; }; -template -struct __has_propagate_on_container_copy_assignment : false_type {}; - -template -struct __has_propagate_on_container_copy_assignment<_Tp, - typename __void_t::type> - : true_type {}; +// __alloc_traits_difference_type +_LIBCPP_ALLOCATOR_TRAITS_HAS_XXX(__has_alloc_traits_difference_type, difference_type); +template ::value> +struct __alloc_traits_difference_type { + using type _LIBCPP_NODEBUG_TYPE = typename pointer_traits<_Ptr>::difference_type; +}; +template +struct __alloc_traits_difference_type<_Alloc, _Ptr, true> { + using type _LIBCPP_NODEBUG_TYPE = typename _Alloc::difference_type; +}; +// __propagate_on_container_copy_assignment +_LIBCPP_ALLOCATOR_TRAITS_HAS_XXX(__has_propagate_on_container_copy_assignment, propagate_on_container_copy_assignment); template ::value> -struct __propagate_on_container_copy_assignment -{ - typedef _LIBCPP_NODEBUG_TYPE false_type type; -}; - +struct __propagate_on_container_copy_assignment : false_type { }; template -struct __propagate_on_container_copy_assignment<_Alloc, true> -{ - typedef _LIBCPP_NODEBUG_TYPE typename _Alloc::propagate_on_container_copy_assignment type; +struct __propagate_on_container_copy_assignment<_Alloc, true> { + using type _LIBCPP_NODEBUG_TYPE = typename _Alloc::propagate_on_container_copy_assignment; }; -template -struct __has_propagate_on_container_move_assignment : false_type {}; - -template -struct __has_propagate_on_container_move_assignment<_Tp, - typename __void_t::type> - : true_type {}; - +// __propagate_on_container_move_assignment +_LIBCPP_ALLOCATOR_TRAITS_HAS_XXX(__has_propagate_on_container_move_assignment, propagate_on_container_move_assignment); template ::value> -struct __propagate_on_container_move_assignment -{ - typedef false_type type; -}; - +struct __propagate_on_container_move_assignment : false_type { }; template -struct __propagate_on_container_move_assignment<_Alloc, true> -{ - typedef _LIBCPP_NODEBUG_TYPE typename _Alloc::propagate_on_container_move_assignment type; +struct __propagate_on_container_move_assignment<_Alloc, true> { + using type _LIBCPP_NODEBUG_TYPE = typename _Alloc::propagate_on_container_move_assignment; }; -template -struct __has_propagate_on_container_swap : false_type {}; - -template -struct __has_propagate_on_container_swap<_Tp, - typename __void_t::type> - : true_type {}; - +// __propagate_on_container_swap +_LIBCPP_ALLOCATOR_TRAITS_HAS_XXX(__has_propagate_on_container_swap, propagate_on_container_swap); template ::value> -struct __propagate_on_container_swap -{ - typedef false_type type; -}; - +struct __propagate_on_container_swap : false_type { }; template -struct __propagate_on_container_swap<_Alloc, true> -{ - typedef _LIBCPP_NODEBUG_TYPE typename _Alloc::propagate_on_container_swap type; +struct __propagate_on_container_swap<_Alloc, true> { + using type _LIBCPP_NODEBUG_TYPE = typename _Alloc::propagate_on_container_swap; }; -template -struct __has_is_always_equal : false_type {}; - -template -struct __has_is_always_equal<_Tp, - typename __void_t::type> - : true_type {}; - +// __is_always_equal +_LIBCPP_ALLOCATOR_TRAITS_HAS_XXX(__has_is_always_equal, is_always_equal); template ::value> -struct __is_always_equal -{ - typedef _LIBCPP_NODEBUG_TYPE typename _VSTD::is_empty<_Alloc>::type type; -}; - +struct __is_always_equal : is_empty<_Alloc> { }; template -struct __is_always_equal<_Alloc, true> -{ - typedef _LIBCPP_NODEBUG_TYPE typename _Alloc::is_always_equal type; -}; - -template ::value> -struct __has_rebind_other -{ -private: - struct __two {char __lx; char __lxx;}; - template static __two __test(...); - _LIBCPP_SUPPRESS_DEPRECATED_PUSH - template static char __test(typename _Xp::template rebind<_Up>::other* = 0); - _LIBCPP_SUPPRESS_DEPRECATED_POP -public: - static const bool value = sizeof(__test<_Tp>(0)) == 1; +struct __is_always_equal<_Alloc, true> { + using type _LIBCPP_NODEBUG_TYPE = typename _Alloc::is_always_equal; }; +// __allocator_traits_rebind +_LIBCPP_SUPPRESS_DEPRECATED_PUSH +template +struct __has_rebind_other : false_type { }; template -struct __has_rebind_other<_Tp, _Up, false> -{ - static const bool value = false; -}; +struct __has_rebind_other<_Tp, _Up, typename __void_t< + typename _Tp::template rebind<_Up>::other +>::type> : true_type { }; template ::value> -struct __allocator_traits_rebind -{ - _LIBCPP_SUPPRESS_DEPRECATED_PUSH - typedef _LIBCPP_NODEBUG_TYPE typename _Tp::template rebind<_Up>::other type; - _LIBCPP_SUPPRESS_DEPRECATED_POP +struct __allocator_traits_rebind { + using type _LIBCPP_NODEBUG_TYPE = typename _Tp::template rebind<_Up>::other; }; - template