1//===----------------------------------------------------------------------===// 2// 3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4// See https://llvm.org/LICENSE.txt for license information. 5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6// 7//===----------------------------------------------------------------------===// 8 9#ifndef _LIBCPP___TYPE_TRAITS_IS_SWAPPABLE_H 10#define _LIBCPP___TYPE_TRAITS_IS_SWAPPABLE_H 11 12#include <__config> 13#include <__type_traits/add_lvalue_reference.h> 14#include <__type_traits/conditional.h> 15#include <__type_traits/enable_if.h> 16#include <__type_traits/is_move_assignable.h> 17#include <__type_traits/is_move_constructible.h> 18#include <__type_traits/is_nothrow_move_assignable.h> 19#include <__type_traits/is_nothrow_move_constructible.h> 20#include <__type_traits/is_referenceable.h> 21#include <__type_traits/is_same.h> 22#include <__type_traits/is_void.h> 23#include <__type_traits/nat.h> 24#include <__utility/declval.h> 25#include <cstddef> 26 27#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) 28# pragma GCC system_header 29#endif 30 31_LIBCPP_BEGIN_NAMESPACE_STD 32 33template <class _Tp> 34struct __is_swappable; 35template <class _Tp> 36struct __is_nothrow_swappable; 37 38#ifndef _LIBCPP_CXX03_LANG 39template <class _Tp> 40using __swap_result_t = __enable_if_t<is_move_constructible<_Tp>::value && is_move_assignable<_Tp>::value>; 41#else 42template <class> 43using __swap_result_t = void; 44#endif 45 46template <class _Tp> 47inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 __swap_result_t<_Tp> swap(_Tp& __x, _Tp& __y) 48 _NOEXCEPT_(is_nothrow_move_constructible<_Tp>::value&& is_nothrow_move_assignable<_Tp>::value); 49 50template <class _Tp, size_t _Np, __enable_if_t<__is_swappable<_Tp>::value, int> = 0> 51inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void swap(_Tp (&__a)[_Np], _Tp (&__b)[_Np]) 52 _NOEXCEPT_(__is_nothrow_swappable<_Tp>::value); 53 54namespace __detail { 55// ALL generic swap overloads MUST already have a declaration available at this point. 56 57template <class _Tp, class _Up = _Tp, bool _NotVoid = !is_void<_Tp>::value && !is_void<_Up>::value> 58struct __swappable_with { 59 template <class _LHS, class _RHS> 60 static decltype(swap(std::declval<_LHS>(), std::declval<_RHS>())) __test_swap(int); 61 template <class, class> 62 static __nat __test_swap(long); 63 64 // Extra parens are needed for the C++03 definition of decltype. 65 typedef decltype((__test_swap<_Tp, _Up>(0))) __swap1; 66 typedef decltype((__test_swap<_Up, _Tp>(0))) __swap2; 67 68 static const bool value = _IsNotSame<__swap1, __nat>::value && _IsNotSame<__swap2, __nat>::value; 69}; 70 71template <class _Tp, class _Up> 72struct __swappable_with<_Tp, _Up, false> : false_type {}; 73 74template <class _Tp, class _Up = _Tp, bool _Swappable = __swappable_with<_Tp, _Up>::value> 75struct __nothrow_swappable_with { 76 static const bool value = 77#ifndef _LIBCPP_HAS_NO_NOEXCEPT 78 noexcept(swap(std::declval<_Tp>(), std::declval<_Up>()))&& noexcept( 79 swap(std::declval<_Up>(), std::declval<_Tp>())); 80#else 81 false; 82#endif 83}; 84 85template <class _Tp, class _Up> 86struct __nothrow_swappable_with<_Tp, _Up, false> : false_type {}; 87 88} // namespace __detail 89 90template <class _Tp> 91struct __is_swappable : public integral_constant<bool, __detail::__swappable_with<_Tp&>::value> {}; 92 93template <class _Tp> 94struct __is_nothrow_swappable : public integral_constant<bool, __detail::__nothrow_swappable_with<_Tp&>::value> {}; 95 96#if _LIBCPP_STD_VER >= 17 97 98template <class _Tp, class _Up> 99struct _LIBCPP_TEMPLATE_VIS is_swappable_with 100 : public integral_constant<bool, __detail::__swappable_with<_Tp, _Up>::value> {}; 101 102template <class _Tp> 103struct _LIBCPP_TEMPLATE_VIS is_swappable 104 : public __conditional_t<__libcpp_is_referenceable<_Tp>::value, 105 is_swappable_with<__add_lvalue_reference_t<_Tp>, __add_lvalue_reference_t<_Tp> >, 106 false_type> {}; 107 108template <class _Tp, class _Up> 109struct _LIBCPP_TEMPLATE_VIS is_nothrow_swappable_with 110 : public integral_constant<bool, __detail::__nothrow_swappable_with<_Tp, _Up>::value> {}; 111 112template <class _Tp> 113struct _LIBCPP_TEMPLATE_VIS is_nothrow_swappable 114 : public __conditional_t<__libcpp_is_referenceable<_Tp>::value, 115 is_nothrow_swappable_with<__add_lvalue_reference_t<_Tp>, __add_lvalue_reference_t<_Tp> >, 116 false_type> {}; 117 118template <class _Tp, class _Up> 119inline constexpr bool is_swappable_with_v = is_swappable_with<_Tp, _Up>::value; 120 121template <class _Tp> 122inline constexpr bool is_swappable_v = is_swappable<_Tp>::value; 123 124template <class _Tp, class _Up> 125inline constexpr bool is_nothrow_swappable_with_v = is_nothrow_swappable_with<_Tp, _Up>::value; 126 127template <class _Tp> 128inline constexpr bool is_nothrow_swappable_v = is_nothrow_swappable<_Tp>::value; 129 130#endif // _LIBCPP_STD_VER >= 17 131 132_LIBCPP_END_NAMESPACE_STD 133 134#endif // _LIBCPP___TYPE_TRAITS_IS_SWAPPABLE_H 135