swap.h 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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___UTILITY_SWAP_H
  9. #define _LIBCPP___UTILITY_SWAP_H
  10. #include <__config>
  11. #include <__type_traits/is_assignable.h>
  12. #include <__type_traits/is_constructible.h>
  13. #include <__type_traits/is_nothrow_assignable.h>
  14. #include <__type_traits/is_nothrow_constructible.h>
  15. #include <__type_traits/is_swappable.h>
  16. #include <__utility/declval.h>
  17. #include <__utility/move.h>
  18. #include <cstddef>
  19. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  20. # pragma GCC system_header
  21. #endif
  22. _LIBCPP_PUSH_MACROS
  23. #include <__undef_macros>
  24. _LIBCPP_BEGIN_NAMESPACE_STD
  25. #ifndef _LIBCPP_CXX03_LANG
  26. template <class _Tp>
  27. using __swap_result_t = __enable_if_t<is_move_constructible<_Tp>::value && is_move_assignable<_Tp>::value>;
  28. #else
  29. template <class>
  30. using __swap_result_t = void;
  31. #endif
  32. template <class _Tp>
  33. inline _LIBCPP_HIDE_FROM_ABI __swap_result_t<_Tp> _LIBCPP_CONSTEXPR_SINCE_CXX20 swap(_Tp& __x, _Tp& __y)
  34. _NOEXCEPT_(is_nothrow_move_constructible<_Tp>::value&& is_nothrow_move_assignable<_Tp>::value) {
  35. _Tp __t(std::move(__x));
  36. __x = std::move(__y);
  37. __y = std::move(__t);
  38. }
  39. template <class _Tp, size_t _Np, __enable_if_t<__is_swappable<_Tp>::value, int> >
  40. inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void swap(_Tp (&__a)[_Np], _Tp (&__b)[_Np])
  41. _NOEXCEPT_(__is_nothrow_swappable<_Tp>::value) {
  42. for (size_t __i = 0; __i != _Np; ++__i) {
  43. swap(__a[__i], __b[__i]);
  44. }
  45. }
  46. _LIBCPP_END_NAMESPACE_STD
  47. _LIBCPP_POP_MACROS
  48. #endif // _LIBCPP___UTILITY_SWAP_H