1 | // Copyright (C) 2007, 2008 Steven Watanabe, Joseph Gauterin, Niels Dekker |
2 | // |
3 | // Distributed under the Boost Software License, Version 1.0. (See |
4 | // accompanying file LICENSE_1_0.txt or copy at |
5 | // http://www.boost.org/LICENSE_1_0.txt) |
6 | // For more information, see http://www.boost.org |
7 | |
8 | |
9 | #ifndef BOOST_CORE_SWAP_HPP |
10 | #define BOOST_CORE_SWAP_HPP |
11 | |
12 | // Note: the implementation of this utility contains various workarounds: |
13 | // - 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 | // - swap_impl has a using-directive, rather than a using-declaration, |
17 | // because some compilers (including MSVC 7.1, Borland 5.9.3, and |
18 | // Intel 8.1) don't do argument-dependent lookup when it has a |
19 | // using-declaration instead. |
20 | // - boost::swap has two template arguments, instead of one, to |
21 | // avoid ambiguity when swapping objects of a Boost type that does |
22 | // not have its own boost::swap overload. |
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 | namespace boost_swap_impl |
34 | { |
35 | // we can't use type_traits here |
36 | |
37 | template<class T> struct is_const { enum _vt { value = 0 }; }; |
38 | template<class T> struct is_const<T const> { enum _vt { value = 1 }; }; |
39 | |
40 | template<class T> |
41 | BOOST_GPU_ENABLED |
42 | void swap_impl(T& left, T& right) |
43 | { |
44 | using namespace std;//use std::swap if argument dependent lookup fails |
45 | swap(left,right); |
46 | } |
47 | |
48 | template<class T, std::size_t N> |
49 | BOOST_GPU_ENABLED |
50 | void swap_impl(T (& left)[N], T (& right)[N]) |
51 | { |
52 | for (std::size_t i = 0; i < N; ++i) |
53 | { |
54 | ::boost_swap_impl::swap_impl(left[i], right[i]); |
55 | } |
56 | } |
57 | } |
58 | |
59 | namespace boost |
60 | { |
61 | template<class T1, class T2> |
62 | BOOST_GPU_ENABLED |
63 | typename enable_if_c< !boost_swap_impl::is_const<T1>::value && !boost_swap_impl::is_const<T2>::value >::type |
64 | swap(T1& left, T2& right) |
65 | { |
66 | ::boost_swap_impl::swap_impl(left, right); |
67 | } |
68 | } |
69 | |
70 | #endif |
71 | |