xref: /freebsd/contrib/llvm-project/libcxx/include/__iterator/iter_swap.h (revision 7ef62cebc2f965b0f640263e179276928885e33d)
1 // -*- C++ -*-
2 //===----------------------------------------------------------------------===//
3 //
4 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5 // See https://llvm.org/LICENSE.txt for license information.
6 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #ifndef _LIBCPP___ITERATOR_ITER_SWAP_H
11 #define _LIBCPP___ITERATOR_ITER_SWAP_H
12 
13 #include <__concepts/class_or_enum.h>
14 #include <__concepts/swappable.h>
15 #include <__config>
16 #include <__iterator/concepts.h>
17 #include <__iterator/iter_move.h>
18 #include <__iterator/iterator_traits.h>
19 #include <__iterator/readable_traits.h>
20 #include <__type_traits/remove_cvref.h>
21 #include <__utility/declval.h>
22 #include <__utility/forward.h>
23 #include <__utility/move.h>
24 
25 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
26 #  pragma GCC system_header
27 #endif
28 
29 _LIBCPP_BEGIN_NAMESPACE_STD
30 
31 #if _LIBCPP_STD_VER > 17
32 
33 // [iter.cust.swap]
34 
35 namespace ranges {
36 namespace __iter_swap {
37   template<class _I1, class _I2>
38   void iter_swap(_I1, _I2) = delete;
39 
40   template<class _T1, class _T2>
41   concept __unqualified_iter_swap =
42     (__class_or_enum<remove_cvref_t<_T1>> || __class_or_enum<remove_cvref_t<_T2>>) &&
43     requires (_T1&& __x, _T2&& __y) {
44       // NOLINTNEXTLINE(libcpp-robust-against-adl) iter_swap ADL calls should only be made through ranges::iter_swap
45       iter_swap(_VSTD::forward<_T1>(__x), _VSTD::forward<_T2>(__y));
46     };
47 
48   template<class _T1, class _T2>
49   concept __readable_swappable =
50     indirectly_readable<_T1> && indirectly_readable<_T2> &&
51     swappable_with<iter_reference_t<_T1>, iter_reference_t<_T2>>;
52 
53 
54   struct __fn {
55     // NOLINTBEGIN(libcpp-robust-against-adl) iter_swap ADL calls should only be made through ranges::iter_swap
56     template <class _T1, class _T2>
57       requires __unqualified_iter_swap<_T1, _T2>
58     _LIBCPP_HIDE_FROM_ABI
59     constexpr void operator()(_T1&& __x, _T2&& __y) const
60       noexcept(noexcept(iter_swap(_VSTD::forward<_T1>(__x), _VSTD::forward<_T2>(__y))))
61     {
62       (void)iter_swap(_VSTD::forward<_T1>(__x), _VSTD::forward<_T2>(__y));
63     }
64     // NOLINTEND(libcpp-robust-against-adl)
65 
66     template <class _T1, class _T2>
67       requires (!__unqualified_iter_swap<_T1, _T2>) &&
68                __readable_swappable<_T1, _T2>
69     _LIBCPP_HIDE_FROM_ABI
70     constexpr void operator()(_T1&& __x, _T2&& __y) const
71       noexcept(noexcept(ranges::swap(*_VSTD::forward<_T1>(__x), *_VSTD::forward<_T2>(__y))))
72     {
73       ranges::swap(*_VSTD::forward<_T1>(__x), *_VSTD::forward<_T2>(__y));
74     }
75 
76     template <class _T1, class _T2>
77       requires (!__unqualified_iter_swap<_T1, _T2> &&
78                 !__readable_swappable<_T1, _T2>) &&
79                indirectly_movable_storable<_T1, _T2> &&
80                indirectly_movable_storable<_T2, _T1>
81     _LIBCPP_HIDE_FROM_ABI
82     constexpr void operator()(_T1&& __x, _T2&& __y) const
83       noexcept(noexcept(iter_value_t<_T2>(ranges::iter_move(__y))) &&
84                noexcept(*__y = ranges::iter_move(__x)) &&
85                noexcept(*_VSTD::forward<_T1>(__x) = std::declval<iter_value_t<_T2>>()))
86     {
87       iter_value_t<_T2> __old(ranges::iter_move(__y));
88       *__y = ranges::iter_move(__x);
89       *_VSTD::forward<_T1>(__x) = _VSTD::move(__old);
90     }
91   };
92 } // namespace __iter_swap
93 
94 inline namespace __cpo {
95   inline constexpr auto iter_swap = __iter_swap::__fn{};
96 } // namespace __cpo
97 } // namespace ranges
98 
99 template<class _I1, class _I2 = _I1>
100 concept indirectly_swappable =
101   indirectly_readable<_I1> && indirectly_readable<_I2> &&
102   requires(const _I1 __i1, const _I2 __i2) {
103     ranges::iter_swap(__i1, __i1);
104     ranges::iter_swap(__i2, __i2);
105     ranges::iter_swap(__i1, __i2);
106     ranges::iter_swap(__i2, __i1);
107   };
108 
109 #endif // _LIBCPP_STD_VER > 17
110 
111 _LIBCPP_END_NAMESPACE_STD
112 
113 #endif // _LIBCPP___ITERATOR_ITER_SWAP_H
114