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