brintos

brintos / llvm-project-archived public Read only

0
0
Text · 4.0 KiB · c691d3a Raw
123 lines · c
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-exception6//7//===----------------------------------------------------------------------===//8 9#ifndef _LIBCPP___CONCEPTS_SWAPPABLE_H10#define _LIBCPP___CONCEPTS_SWAPPABLE_H11 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 26#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)27#  pragma GCC system_header28#endif29 30_LIBCPP_PUSH_MACROS31#include <__undef_macros>32 33_LIBCPP_BEGIN_NAMESPACE_STD34 35#if _LIBCPP_STD_VER >= 2036 37// [concept.swappable]38 39namespace ranges {40namespace __swap {41 42template <class _Tp>43void swap(_Tp&, _Tp&) = delete;44 45// clang-format off46template <class _Tp, class _Up>47concept __unqualified_swappable_with =48    (__class_or_enum<remove_cvref_t<_Tp>> || __class_or_enum<remove_cvref_t<_Up>>) &&49    requires(_Tp&& __t, _Up&& __u) {50        swap(std::forward<_Tp>(__t), std::forward<_Up>(__u));51    };52// clang-format on53 54struct __fn;55 56// clang-format off57template <class _Tp, class _Up, size_t _Size>58concept __swappable_arrays =59    !__unqualified_swappable_with<_Tp (&)[_Size], _Up (&)[_Size]> &&60    extent_v<_Tp> == extent_v<_Up> &&61    requires(_Tp (&__t)[_Size], _Up (&__u)[_Size], const __fn& __swap) {62        __swap(__t[0], __u[0]);63    };64// clang-format on65 66template <class _Tp>67concept __exchangeable =68    !__unqualified_swappable_with<_Tp&, _Tp&> && move_constructible<_Tp> && assignable_from<_Tp&, _Tp>;69 70struct __fn {71  // 2.1   `S` is `(void)swap(E1, E2)`* if `E1` or `E2` has class or enumeration type and...72  // *The name `swap` is used here unqualified.73  template <class _Tp, class _Up>74    requires __unqualified_swappable_with<_Tp, _Up>75  _LIBCPP_HIDE_FROM_ABI constexpr void operator()(_Tp&& __t, _Up&& __u) const76      noexcept(noexcept(swap(std::forward<_Tp>(__t), std::forward<_Up>(__u)))) {77    swap(std::forward<_Tp>(__t), std::forward<_Up>(__u));78  }79 80  // 2.2   Otherwise, if `E1` and `E2` are lvalues of array types with equal extent and...81  template <class _Tp, class _Up, size_t _Size>82    requires __swappable_arrays<_Tp, _Up, _Size>83  _LIBCPP_HIDE_FROM_ABI constexpr void operator()(_Tp (&__t)[_Size], _Up (&__u)[_Size]) const84      noexcept(noexcept((*this)(*__t, *__u))) {85    // TODO(cjdb): replace with `ranges::swap_ranges`.86    for (size_t __i = 0; __i < _Size; ++__i) {87      (*this)(__t[__i], __u[__i]);88    }89  }90 91  // 2.3   Otherwise, if `E1` and `E2` are lvalues of the same type `T` that models...92  template <__exchangeable _Tp>93  _LIBCPP_HIDE_FROM_ABI constexpr void operator()(_Tp& __x, _Tp& __y) const94      noexcept(is_nothrow_move_constructible_v<_Tp> && is_nothrow_move_assignable_v<_Tp>) {95    __y = std::exchange(__x, std::move(__y));96  }97};98} // namespace __swap99 100inline namespace __cpo {101inline constexpr auto swap = __swap::__fn{};102} // namespace __cpo103} // namespace ranges104 105template <class _Tp>106concept swappable = requires(_Tp& __a, _Tp& __b) { ranges::swap(__a, __b); };107 108template <class _Tp, class _Up>109concept swappable_with = common_reference_with<_Tp, _Up> && requires(_Tp&& __t, _Up&& __u) {110  ranges::swap(std::forward<_Tp>(__t), std::forward<_Tp>(__t));111  ranges::swap(std::forward<_Up>(__u), std::forward<_Up>(__u));112  ranges::swap(std::forward<_Tp>(__t), std::forward<_Up>(__u));113  ranges::swap(std::forward<_Up>(__u), std::forward<_Tp>(__t));114};115 116#endif // _LIBCPP_STD_VER >= 20117 118_LIBCPP_END_NAMESPACE_STD119 120_LIBCPP_POP_MACROS121 122#endif // _LIBCPP___CONCEPTS_SWAPPABLE_H123