xref: /freebsd/contrib/llvm-project/libcxx/include/__utility/swap.h (revision c8e7f78a3d28ff6e6223ed136ada8e1e2f34965e)
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___UTILITY_SWAP_H
10 #define _LIBCPP___UTILITY_SWAP_H
11 
12 #include <__config>
13 #include <__type_traits/is_move_assignable.h>
14 #include <__type_traits/is_move_constructible.h>
15 #include <__type_traits/is_nothrow_move_assignable.h>
16 #include <__type_traits/is_nothrow_move_constructible.h>
17 #include <__type_traits/is_swappable.h>
18 #include <__utility/declval.h>
19 #include <__utility/move.h>
20 #include <cstddef>
21 
22 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
23 #  pragma GCC system_header
24 #endif
25 
26 _LIBCPP_PUSH_MACROS
27 #include <__undef_macros>
28 
29 _LIBCPP_BEGIN_NAMESPACE_STD
30 
31 #ifndef _LIBCPP_CXX03_LANG
32 template <class _Tp>
33 using __swap_result_t = typename enable_if<is_move_constructible<_Tp>::value && is_move_assignable<_Tp>::value>::type;
34 #else
35 template <class>
36 using __swap_result_t = void;
37 #endif
38 
39 template <class _Tp>
40 inline _LIBCPP_INLINE_VISIBILITY __swap_result_t<_Tp> _LIBCPP_CONSTEXPR_SINCE_CXX20 swap(_Tp& __x, _Tp& __y)
41     _NOEXCEPT_(is_nothrow_move_constructible<_Tp>::value&& is_nothrow_move_assignable<_Tp>::value) {
42   _Tp __t(_VSTD::move(__x));
43   __x = _VSTD::move(__y);
44   __y = _VSTD::move(__t);
45 }
46 
47 template <class _Tp, size_t _Np>
48 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20 typename enable_if<__is_swappable<_Tp>::value>::type
49 swap(_Tp (&__a)[_Np], _Tp (&__b)[_Np]) _NOEXCEPT_(__is_nothrow_swappable<_Tp>::value) {
50   for (size_t __i = 0; __i != _Np; ++__i) {
51     swap(__a[__i], __b[__i]);
52   }
53 }
54 
55 _LIBCPP_END_NAMESPACE_STD
56 
57 _LIBCPP_POP_MACROS
58 
59 #endif // _LIBCPP___UTILITY_SWAP_H
60