swappable.h 4.0 KB

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