swappable.h 3.7 KB

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