| 1 | // Copyright (c) 2008 Joseph Gauterin, Niels Dekker |
| 2 | // |
| 3 | // Distributed under the Boost Software License, Version 1.0. |
| 4 | // (See accompanying file LICENSE_1_0.txt or copy at |
| 5 | // http://www.boost.org/LICENSE_1_0.txt) |
| 6 | |
| 7 | // Tests whether instances of a class from a namespace other than boost are |
| 8 | // properly swapped, when both boost and the other namespace have a custom |
| 9 | // swap function for that class. Note that it shouldn't be necessary for a class |
| 10 | // in an other namespace to have a custom swap function in boost, because the |
| 11 | // boost::core::invoke_swap utility should find the swap function in the other |
| 12 | // namespace, by argument dependent lookup (ADL). Unfortunately ADL isn't fully |
| 13 | // implemented by some specific compiler versions, including Intel C++ 8.1, MSVC 7.1, |
| 14 | // and Borland 5.9.3. Users of those compilers might consider adding a swap overload |
| 15 | // to the boost namespace. |
| 16 | |
| 17 | #include <boost/core/invoke_swap.hpp> |
| 18 | #include <boost/core/lightweight_test.hpp> |
| 19 | #define BOOST_CHECK BOOST_TEST |
| 20 | #define BOOST_CHECK_EQUAL BOOST_TEST_EQ |
| 21 | |
| 22 | //Put test class in namespace other |
| 23 | namespace other |
| 24 | { |
| 25 | #include "./swap_test_class.hpp" |
| 26 | } |
| 27 | |
| 28 | //Provide swap function in namespace boost |
| 29 | namespace boost |
| 30 | { |
| 31 | void swap(::other::swap_test_class& left, ::other::swap_test_class& right) |
| 32 | { |
| 33 | left.swap(other&: right); |
| 34 | } |
| 35 | } |
| 36 | |
| 37 | //Provide swap function in namespace other |
| 38 | namespace other |
| 39 | { |
| 40 | void swap(swap_test_class& left, swap_test_class& right) |
| 41 | { |
| 42 | left.swap(other&: right); |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | int main() |
| 47 | { |
| 48 | const other::swap_test_class initial_value1(1); |
| 49 | const other::swap_test_class initial_value2(2); |
| 50 | |
| 51 | other::swap_test_class object1 = initial_value1; |
| 52 | other::swap_test_class object2 = initial_value2; |
| 53 | |
| 54 | other::swap_test_class::reset(); |
| 55 | boost::core::invoke_swap(left&: object1,right&: object2); |
| 56 | |
| 57 | BOOST_CHECK(object1 == initial_value2); |
| 58 | BOOST_CHECK(object2 == initial_value1); |
| 59 | |
| 60 | BOOST_CHECK_EQUAL(other::swap_test_class::swap_count(),1); |
| 61 | BOOST_CHECK_EQUAL(other::swap_test_class::copy_count(),0); |
| 62 | |
| 63 | return boost::report_errors(); |
| 64 | } |
| 65 | |
| 66 | |