1fe6060f1SDimitry Andric //===----------------------------------------------------------------------===// 2fe6060f1SDimitry Andric // 3fe6060f1SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4fe6060f1SDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 5fe6060f1SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6fe6060f1SDimitry Andric // 7fe6060f1SDimitry Andric //===----------------------------------------------------------------------===// 8fe6060f1SDimitry Andric 9fe6060f1SDimitry Andric #ifndef _LIBCPP___UTILITY_SWAP_H 10fe6060f1SDimitry Andric #define _LIBCPP___UTILITY_SWAP_H 11fe6060f1SDimitry Andric 12fe6060f1SDimitry Andric #include <__config> 13*0fca6ea1SDimitry Andric #include <__type_traits/is_assignable.h> 14*0fca6ea1SDimitry Andric #include <__type_traits/is_constructible.h> 15*0fca6ea1SDimitry Andric #include <__type_traits/is_nothrow_assignable.h> 16*0fca6ea1SDimitry Andric #include <__type_traits/is_nothrow_constructible.h> 17bdd1243dSDimitry Andric #include <__type_traits/is_swappable.h> 18fe6060f1SDimitry Andric #include <__utility/declval.h> 19fe6060f1SDimitry Andric #include <__utility/move.h> 20fe6060f1SDimitry Andric #include <cstddef> 21fe6060f1SDimitry Andric 22fe6060f1SDimitry Andric #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) 23fe6060f1SDimitry Andric # pragma GCC system_header 24fe6060f1SDimitry Andric #endif 25fe6060f1SDimitry Andric 2606c3fb27SDimitry Andric _LIBCPP_PUSH_MACROS 2706c3fb27SDimitry Andric #include <__undef_macros> 2806c3fb27SDimitry Andric 29fe6060f1SDimitry Andric _LIBCPP_BEGIN_NAMESPACE_STD 30fe6060f1SDimitry Andric 31fe6060f1SDimitry Andric #ifndef _LIBCPP_CXX03_LANG 32fe6060f1SDimitry Andric template <class _Tp> 335f757f3fSDimitry Andric using __swap_result_t = __enable_if_t<is_move_constructible<_Tp>::value && is_move_assignable<_Tp>::value>; 34fe6060f1SDimitry Andric #else 35fe6060f1SDimitry Andric template <class> 36fe6060f1SDimitry Andric using __swap_result_t = void; 37fe6060f1SDimitry Andric #endif 38fe6060f1SDimitry Andric 39fe6060f1SDimitry Andric template <class _Tp> 405f757f3fSDimitry Andric inline _LIBCPP_HIDE_FROM_ABI __swap_result_t<_Tp> _LIBCPP_CONSTEXPR_SINCE_CXX20 swap(_Tp& __x, _Tp& __y) 41fe6060f1SDimitry Andric _NOEXCEPT_(is_nothrow_move_constructible<_Tp>::value&& is_nothrow_move_assignable<_Tp>::value) { 425f757f3fSDimitry Andric _Tp __t(std::move(__x)); 435f757f3fSDimitry Andric __x = std::move(__y); 445f757f3fSDimitry Andric __y = std::move(__t); 45fe6060f1SDimitry Andric } 46fe6060f1SDimitry Andric 47*0fca6ea1SDimitry Andric template <class _Tp, size_t _Np, __enable_if_t<__is_swappable_v<_Tp>, int> > 485f757f3fSDimitry Andric inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void swap(_Tp (&__a)[_Np], _Tp (&__b)[_Np]) 49*0fca6ea1SDimitry Andric _NOEXCEPT_(__is_nothrow_swappable_v<_Tp>) { 50fe6060f1SDimitry Andric for (size_t __i = 0; __i != _Np; ++__i) { 51fe6060f1SDimitry Andric swap(__a[__i], __b[__i]); 52fe6060f1SDimitry Andric } 53fe6060f1SDimitry Andric } 54fe6060f1SDimitry Andric 55fe6060f1SDimitry Andric _LIBCPP_END_NAMESPACE_STD 56fe6060f1SDimitry Andric 5706c3fb27SDimitry Andric _LIBCPP_POP_MACROS 5806c3fb27SDimitry Andric 59fe6060f1SDimitry Andric #endif // _LIBCPP___UTILITY_SWAP_H 60