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___CONCEPTS_SWAPPABLE_H 10 #define _LIBCPP___CONCEPTS_SWAPPABLE_H 11 12 #include <__concepts/assignable.h> 13 #include <__concepts/class_or_enum.h> 14 #include <__concepts/common_reference_with.h> 15 #include <__concepts/constructible.h> 16 #include <__config> 17 #include <__type_traits/extent.h> 18 #include <__type_traits/is_nothrow_move_assignable.h> 19 #include <__type_traits/is_nothrow_move_constructible.h> 20 #include <__type_traits/remove_cvref.h> 21 #include <__utility/exchange.h> 22 #include <__utility/forward.h> 23 #include <__utility/move.h> 24 #include <__utility/swap.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 33 #if _LIBCPP_STD_VER > 17 34 35 // [concept.swappable] 36 37 namespace ranges { 38 namespace __swap { 39 40 template<class _Tp> 41 void swap(_Tp&, _Tp&) = delete; 42 43 template<class _Tp, class _Up> 44 concept __unqualified_swappable_with = 45 (__class_or_enum<remove_cvref_t<_Tp>> || __class_or_enum<remove_cvref_t<_Up>>) && 46 requires(_Tp&& __t, _Up&& __u) { 47 swap(_VSTD::forward<_Tp>(__t), _VSTD::forward<_Up>(__u)); 48 }; 49 50 struct __fn; 51 52 template<class _Tp, class _Up, size_t _Size> 53 concept __swappable_arrays = 54 !__unqualified_swappable_with<_Tp(&)[_Size], _Up(&)[_Size]> && 55 extent_v<_Tp> == extent_v<_Up> && 56 requires(_Tp(& __t)[_Size], _Up(& __u)[_Size], const __fn& __swap) { 57 __swap(__t[0], __u[0]); 58 }; 59 60 template<class _Tp> 61 concept __exchangeable = 62 !__unqualified_swappable_with<_Tp&, _Tp&> && 63 move_constructible<_Tp> && 64 assignable_from<_Tp&, _Tp>; 65 66 struct __fn { 67 // 2.1 `S` is `(void)swap(E1, E2)`* if `E1` or `E2` has class or enumeration type and... 68 // *The name `swap` is used here unqualified. 69 template<class _Tp, class _Up> 70 requires __unqualified_swappable_with<_Tp, _Up> 71 constexpr void operator()(_Tp&& __t, _Up&& __u) const 72 noexcept(noexcept(swap(_VSTD::forward<_Tp>(__t), _VSTD::forward<_Up>(__u)))) 73 { 74 swap(_VSTD::forward<_Tp>(__t), _VSTD::forward<_Up>(__u)); 75 } 76 77 // 2.2 Otherwise, if `E1` and `E2` are lvalues of array types with equal extent and... 78 template<class _Tp, class _Up, size_t _Size> 79 requires __swappable_arrays<_Tp, _Up, _Size> 80 constexpr void operator()(_Tp(& __t)[_Size], _Up(& __u)[_Size]) const 81 noexcept(noexcept((*this)(*__t, *__u))) 82 { 83 // TODO(cjdb): replace with `ranges::swap_ranges`. 84 for (size_t __i = 0; __i < _Size; ++__i) { 85 (*this)(__t[__i], __u[__i]); 86 } 87 } 88 89 // 2.3 Otherwise, if `E1` and `E2` are lvalues of the same type `T` that models... 90 template<__exchangeable _Tp> 91 constexpr void operator()(_Tp& __x, _Tp& __y) const 92 noexcept(is_nothrow_move_constructible_v<_Tp> && is_nothrow_move_assignable_v<_Tp>) 93 { 94 __y = _VSTD::exchange(__x, _VSTD::move(__y)); 95 } 96 }; 97 } // namespace __swap 98 99 inline namespace __cpo { 100 inline constexpr auto swap = __swap::__fn{}; 101 } // namespace __cpo 102 } // namespace ranges 103 104 template<class _Tp> 105 concept swappable = requires(_Tp& __a, _Tp& __b) { ranges::swap(__a, __b); }; 106 107 template<class _Tp, class _Up> 108 concept swappable_with = 109 common_reference_with<_Tp, _Up> && 110 requires(_Tp&& __t, _Up&& __u) { 111 ranges::swap(_VSTD::forward<_Tp>(__t), _VSTD::forward<_Tp>(__t)); 112 ranges::swap(_VSTD::forward<_Up>(__u), _VSTD::forward<_Up>(__u)); 113 ranges::swap(_VSTD::forward<_Tp>(__t), _VSTD::forward<_Up>(__u)); 114 ranges::swap(_VSTD::forward<_Up>(__u), _VSTD::forward<_Tp>(__t)); 115 }; 116 117 #endif // _LIBCPP_STD_VER > 17 118 119 _LIBCPP_END_NAMESPACE_STD 120 121 #endif // _LIBCPP___CONCEPTS_SWAPPABLE_H 122