[rtsan] Remove mkfifoat interceptor (#116997)
[llvm-project.git] / libcxx / include / __concepts / swappable.h
blob985c733021a0d78c0b0bb347c665e6858290a748
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 //===----------------------------------------------------------------------===//
9 #ifndef _LIBCPP___CONCEPTS_SWAPPABLE_H
10 #define _LIBCPP___CONCEPTS_SWAPPABLE_H
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 #include <__utility/swap.h>
27 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
28 # pragma GCC system_header
29 #endif
31 _LIBCPP_PUSH_MACROS
32 #include <__undef_macros>
34 _LIBCPP_BEGIN_NAMESPACE_STD
36 #if _LIBCPP_STD_VER >= 20
38 // [concept.swappable]
40 namespace ranges {
41 namespace __swap {
43 template <class _Tp>
44 void swap(_Tp&, _Tp&) = delete;
46 // clang-format off
47 template <class _Tp, class _Up>
48 concept __unqualified_swappable_with =
49 (__class_or_enum<remove_cvref_t<_Tp>> || __class_or_enum<remove_cvref_t<_Up>>) &&
50 requires(_Tp&& __t, _Up&& __u) {
51 swap(std::forward<_Tp>(__t), std::forward<_Up>(__u));
53 // clang-format on
55 struct __fn;
57 // clang-format off
58 template <class _Tp, class _Up, size_t _Size>
59 concept __swappable_arrays =
60 !__unqualified_swappable_with<_Tp (&)[_Size], _Up (&)[_Size]> &&
61 extent_v<_Tp> == extent_v<_Up> &&
62 requires(_Tp (&__t)[_Size], _Up (&__u)[_Size], const __fn& __swap) {
63 __swap(__t[0], __u[0]);
65 // clang-format on
67 template <class _Tp>
68 concept __exchangeable =
69 !__unqualified_swappable_with<_Tp&, _Tp&> && move_constructible<_Tp> && assignable_from<_Tp&, _Tp>;
71 struct __fn {
72 // 2.1 `S` is `(void)swap(E1, E2)`* if `E1` or `E2` has class or enumeration type and...
73 // *The name `swap` is used here unqualified.
74 template <class _Tp, class _Up>
75 requires __unqualified_swappable_with<_Tp, _Up>
76 _LIBCPP_HIDE_FROM_ABI constexpr void operator()(_Tp&& __t, _Up&& __u) const
77 noexcept(noexcept(swap(std::forward<_Tp>(__t), std::forward<_Up>(__u)))) {
78 swap(std::forward<_Tp>(__t), std::forward<_Up>(__u));
81 // 2.2 Otherwise, if `E1` and `E2` are lvalues of array types with equal extent and...
82 template <class _Tp, class _Up, size_t _Size>
83 requires __swappable_arrays<_Tp, _Up, _Size>
84 _LIBCPP_HIDE_FROM_ABI constexpr void operator()(_Tp (&__t)[_Size], _Up (&__u)[_Size]) const
85 noexcept(noexcept((*this)(*__t, *__u))) {
86 // TODO(cjdb): replace with `ranges::swap_ranges`.
87 for (size_t __i = 0; __i < _Size; ++__i) {
88 (*this)(__t[__i], __u[__i]);
92 // 2.3 Otherwise, if `E1` and `E2` are lvalues of the same type `T` that models...
93 template <__exchangeable _Tp>
94 _LIBCPP_HIDE_FROM_ABI constexpr void operator()(_Tp& __x, _Tp& __y) const
95 noexcept(is_nothrow_move_constructible_v<_Tp> && is_nothrow_move_assignable_v<_Tp>) {
96 __y = std::exchange(__x, std::move(__y));
99 } // namespace __swap
101 inline namespace __cpo {
102 inline constexpr auto swap = __swap::__fn{};
103 } // namespace __cpo
104 } // namespace ranges
106 template <class _Tp>
107 concept swappable = requires(_Tp& __a, _Tp& __b) { ranges::swap(__a, __b); };
109 template <class _Tp, class _Up>
110 concept swappable_with = common_reference_with<_Tp, _Up> && requires(_Tp&& __t, _Up&& __u) {
111 ranges::swap(std::forward<_Tp>(__t), std::forward<_Tp>(__t));
112 ranges::swap(std::forward<_Up>(__u), std::forward<_Up>(__u));
113 ranges::swap(std::forward<_Tp>(__t), std::forward<_Up>(__u));
114 ranges::swap(std::forward<_Up>(__u), std::forward<_Tp>(__t));
117 #endif // _LIBCPP_STD_VER >= 20
119 _LIBCPP_END_NAMESPACE_STD
121 _LIBCPP_POP_MACROS
123 #endif // _LIBCPP___CONCEPTS_SWAPPABLE_H