| 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 | |
| 9 | #ifndef BOOST_CORE_INVOKE_SWAP_HPP |
| 10 | #define BOOST_CORE_INVOKE_SWAP_HPP |
| 11 | |
| 12 | // Note: the implementation of this utility contains various workarounds: |
| 13 | // - invoke_swap_impl is put outside the boost namespace, to avoid infinite |
| 14 | // recursion (causing stack overflow) when swapping objects of a primitive |
| 15 | // type. |
| 16 | // - std::swap is imported with a using-directive, rather than |
| 17 | // a using-declaration, because some compilers (including MSVC 7.1, |
| 18 | // Borland 5.9.3, and Intel 8.1) don't do argument-dependent lookup |
| 19 | // when it has a using-declaration instead. |
| 20 | // - The main entry function is called invoke_swap rather than swap |
| 21 | // to avoid forming an infinite recursion when the arguments are not |
| 22 | // swappable. |
| 23 | |
| 24 | #include <boost/core/enable_if.hpp> |
| 25 | #include <boost/config.hpp> |
| 26 | #if __cplusplus >= 201103L || defined(BOOST_DINKUMWARE_STDLIB) |
| 27 | #include <utility> // for std::swap (C++11) |
| 28 | #else |
| 29 | #include <algorithm> // for std::swap (C++98) |
| 30 | #endif |
| 31 | #include <cstddef> // for std::size_t |
| 32 | |
| 33 | #ifdef BOOST_HAS_PRAGMA_ONCE |
| 34 | #pragma once |
| 35 | #endif |
| 36 | |
| 37 | #if defined(BOOST_GCC) && (BOOST_GCC < 40700) |
| 38 | // gcc 4.6 ICEs on noexcept specifications below |
| 39 | #define BOOST_CORE_SWAP_NOEXCEPT_IF(x) |
| 40 | #else |
| 41 | #define BOOST_CORE_SWAP_NOEXCEPT_IF(x) BOOST_NOEXCEPT_IF(x) |
| 42 | #endif |
| 43 | |
| 44 | namespace boost_swap_impl { |
| 45 | |
| 46 | // we can't use type_traits here |
| 47 | |
| 48 | template<class T> struct is_const { enum _vt { value = 0 }; }; |
| 49 | template<class T> struct is_const<T const> { enum _vt { value = 1 }; }; |
| 50 | |
| 51 | // Use std::swap if argument dependent lookup fails. |
| 52 | // We need to have this at namespace scope to be able to use unqualified swap() call |
| 53 | // in noexcept specification. |
| 54 | using namespace std; |
| 55 | |
| 56 | template<class T> |
| 57 | BOOST_GPU_ENABLED |
| 58 | inline void invoke_swap_impl(T& left, T& right) BOOST_CORE_SWAP_NOEXCEPT_IF(BOOST_NOEXCEPT_EXPR(swap(left, right))) |
| 59 | { |
| 60 | swap(left, right); |
| 61 | } |
| 62 | |
| 63 | template<class T, std::size_t N> |
| 64 | BOOST_GPU_ENABLED |
| 65 | inline void invoke_swap_impl(T (& left)[N], T (& right)[N]) |
| 66 | BOOST_CORE_SWAP_NOEXCEPT_IF(BOOST_NOEXCEPT_EXPR(::boost_swap_impl::invoke_swap_impl(left[0], right[0]))) |
| 67 | { |
| 68 | for (std::size_t i = 0; i < N; ++i) |
| 69 | { |
| 70 | ::boost_swap_impl::invoke_swap_impl(left[i], right[i]); |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | } // namespace boost_swap_impl |
| 75 | |
| 76 | namespace boost { |
| 77 | namespace core { |
| 78 | |
| 79 | template<class T> |
| 80 | BOOST_GPU_ENABLED |
| 81 | inline typename enable_if_c< !::boost_swap_impl::is_const<T>::value >::type |
| 82 | invoke_swap(T& left, T& right) |
| 83 | BOOST_CORE_SWAP_NOEXCEPT_IF(BOOST_NOEXCEPT_EXPR(::boost_swap_impl::invoke_swap_impl(left, right))) |
| 84 | { |
| 85 | ::boost_swap_impl::invoke_swap_impl(left, right); |
| 86 | } |
| 87 | |
| 88 | } // namespace core |
| 89 | } // namespace boost |
| 90 | |
| 91 | #undef BOOST_CORE_SWAP_NOEXCEPT_IF |
| 92 | |
| 93 | #endif // BOOST_CORE_INVOKE_SWAP_HPP |
| 94 | |