invoke_swap.hpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. // Copyright (C) 2007, 2008 Steven Watanabe, Joseph Gauterin, Niels Dekker
  2. // Copyright (C) 2023 Andrey Semashev
  3. //
  4. // Distributed under the Boost Software License, Version 1.0. (See
  5. // accompanying file LICENSE_1_0.txt or copy at
  6. // http://www.boost.org/LICENSE_1_0.txt)
  7. // For more information, see http://www.boost.org
  8. #ifndef BOOST_CORE_INVOKE_SWAP_HPP
  9. #define BOOST_CORE_INVOKE_SWAP_HPP
  10. // Note: the implementation of this utility contains various workarounds:
  11. // - invoke_swap_impl is put outside the boost namespace, to avoid infinite
  12. // recursion (causing stack overflow) when swapping objects of a primitive
  13. // type.
  14. // - std::swap is imported with a using-directive, rather than
  15. // a using-declaration, because some compilers (including MSVC 7.1,
  16. // Borland 5.9.3, and Intel 8.1) don't do argument-dependent lookup
  17. // when it has a using-declaration instead.
  18. // - The main entry function is called invoke_swap rather than swap
  19. // to avoid forming an infinite recursion when the arguments are not
  20. // swappable.
  21. #include <boost/core/enable_if.hpp>
  22. #include <boost/config.hpp>
  23. #if __cplusplus >= 201103L || defined(BOOST_DINKUMWARE_STDLIB)
  24. #include <utility> // for std::swap (C++11)
  25. #else
  26. #include <algorithm> // for std::swap (C++98)
  27. #endif
  28. #include <cstddef> // for std::size_t
  29. #ifdef BOOST_HAS_PRAGMA_ONCE
  30. #pragma once
  31. #endif
  32. #if defined(BOOST_GCC) && (BOOST_GCC < 40700)
  33. // gcc 4.6 ICEs on noexcept specifications below
  34. #define BOOST_CORE_SWAP_NOEXCEPT_IF(x)
  35. #else
  36. #define BOOST_CORE_SWAP_NOEXCEPT_IF(x) BOOST_NOEXCEPT_IF(x)
  37. #endif
  38. namespace boost_swap_impl {
  39. // we can't use type_traits here
  40. template<class T> struct is_const { enum _vt { value = 0 }; };
  41. template<class T> struct is_const<T const> { enum _vt { value = 1 }; };
  42. // Use std::swap if argument dependent lookup fails.
  43. // We need to have this at namespace scope to be able to use unqualified swap() call
  44. // in noexcept specification.
  45. using namespace std;
  46. template<class T>
  47. BOOST_GPU_ENABLED
  48. inline void invoke_swap_impl(T& left, T& right) BOOST_CORE_SWAP_NOEXCEPT_IF(BOOST_NOEXCEPT_EXPR(swap(left, right)))
  49. {
  50. swap(left, right);
  51. }
  52. template<class T, std::size_t N>
  53. BOOST_GPU_ENABLED
  54. inline void invoke_swap_impl(T (& left)[N], T (& right)[N])
  55. BOOST_CORE_SWAP_NOEXCEPT_IF(BOOST_NOEXCEPT_EXPR(::boost_swap_impl::invoke_swap_impl(left[0], right[0])))
  56. {
  57. for (std::size_t i = 0; i < N; ++i)
  58. {
  59. ::boost_swap_impl::invoke_swap_impl(left[i], right[i]);
  60. }
  61. }
  62. } // namespace boost_swap_impl
  63. namespace boost {
  64. namespace core {
  65. template<class T>
  66. BOOST_GPU_ENABLED
  67. inline typename enable_if_c< !::boost_swap_impl::is_const<T>::value >::type
  68. invoke_swap(T& left, T& right)
  69. BOOST_CORE_SWAP_NOEXCEPT_IF(BOOST_NOEXCEPT_EXPR(::boost_swap_impl::invoke_swap_impl(left, right)))
  70. {
  71. ::boost_swap_impl::invoke_swap_impl(left, right);
  72. }
  73. } // namespace core
  74. } // namespace boost
  75. #undef BOOST_CORE_SWAP_NOEXCEPT_IF
  76. #endif // BOOST_CORE_INVOKE_SWAP_HPP