Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/freebsd-src
Path: blob/main/contrib/llvm-project/libcxx/include/__concepts/swappable.h
35262 views
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_assignable.h>
19
#include <__type_traits/is_nothrow_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_PUSH_MACROS
32
#include <__undef_macros>
33
34
_LIBCPP_BEGIN_NAMESPACE_STD
35
36
#if _LIBCPP_STD_VER >= 20
37
38
// [concept.swappable]
39
40
namespace ranges {
41
namespace __swap {
42
43
template <class _Tp>
44
void swap(_Tp&, _Tp&) = delete;
45
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));
52
};
53
// clang-format on
54
55
struct __fn;
56
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]);
64
};
65
// clang-format on
66
67
template <class _Tp>
68
concept __exchangeable =
69
!__unqualified_swappable_with<_Tp&, _Tp&> && move_constructible<_Tp> && assignable_from<_Tp&, _Tp>;
70
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));
79
}
80
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]);
89
}
90
}
91
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));
97
}
98
};
99
} // namespace __swap
100
101
inline namespace __cpo {
102
inline constexpr auto swap = __swap::__fn{};
103
} // namespace __cpo
104
} // namespace ranges
105
106
template <class _Tp>
107
concept swappable = requires(_Tp& __a, _Tp& __b) { ranges::swap(__a, __b); };
108
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));
115
};
116
117
#endif // _LIBCPP_STD_VER >= 20
118
119
_LIBCPP_END_NAMESPACE_STD
120
121
_LIBCPP_POP_MACROS
122
123
#endif // _LIBCPP___CONCEPTS_SWAPPABLE_H
124
125