From 9d64187c342eabbcf85a89520d81a224ceccca97 Mon Sep 17 00:00:00 2001 From: Steven Watanabe Date: Wed, 9 Jun 2010 19:30:35 +0000 Subject: [PATCH] Add an element-wise swap for Boost.Tuple. Fixes #1324 [SVN r62686] --- include/boost/tuple/detail/tuple_basic.hpp | 24 +++++++++++++++++++ .../detail/tuple_basic_no_partial_spec.hpp | 24 +++++++++++++++++++ test/tuple_test_bench.cpp | 21 ++++++++++++++++ 3 files changed, 69 insertions(+) diff --git a/include/boost/tuple/detail/tuple_basic.hpp b/include/boost/tuple/detail/tuple_basic.hpp index 42c4bb4..88f0d90 100644 --- a/include/boost/tuple/detail/tuple_basic.hpp +++ b/include/boost/tuple/detail/tuple_basic.hpp @@ -37,6 +37,7 @@ #include "boost/type_traits/cv_traits.hpp" #include "boost/type_traits/function_traits.hpp" +#include "boost/utility/swap.hpp" #include "boost/detail/workaround.hpp" // needed for BOOST_WORKAROUND @@ -947,6 +948,29 @@ tie(T0& t0, T1& t1, T2& t2, T3& t3, return t(t0, t1, t2, t3, t4, t5, t6, t7, t8, t9); } +template +void swap(tuple& lhs, + tuple& rhs); +inline void swap(null_type&, null_type&) {} +template +inline void swap(cons& lhs, cons& rhs) { + ::boost::swap(lhs.head, rhs.head); +} +template +inline void swap(cons& lhs, cons& rhs) { + ::boost::swap(lhs.head, rhs.head); + ::boost::tuples::swap(lhs.tail, rhs.tail); +} +template +inline void swap(tuple& lhs, + tuple& rhs) { + typedef tuple tuple_type; + typedef typename tuple_type::inherited base; + ::boost::tuples::swap(static_cast(lhs), static_cast(rhs)); +} + } // end of namespace tuples } // end of namespace boost diff --git a/include/boost/tuple/detail/tuple_basic_no_partial_spec.hpp b/include/boost/tuple/detail/tuple_basic_no_partial_spec.hpp index bb38662..7379bf8 100644 --- a/include/boost/tuple/detail/tuple_basic_no_partial_spec.hpp +++ b/include/boost/tuple/detail/tuple_basic_no_partial_spec.hpp @@ -27,6 +27,7 @@ #define BOOST_TUPLE_BASIC_NO_PARTIAL_SPEC_HPP #include "boost/type_traits.hpp" +#include "boost/utility/swap.hpp" #include #if defined BOOST_MSVC @@ -836,6 +837,29 @@ namespace tuples { detail::swallow_assign const ignore = detail::swallow_assign(); +template +void swap(tuple& lhs, + tuple& rhs); +inline void swap(null_type&, null_type&) {} +template +inline void swap(cons& lhs, cons& rhs) { + ::boost::swap(lhs.head, rhs.head); +} +template +inline void swap(cons& lhs, cons& rhs) { + ::boost::swap(lhs.head, rhs.head); + ::boost::tuples::swap(lhs.tail, rhs.tail); +} +template +inline void swap(tuple& lhs, + tuple& rhs) { + typedef tuple tuple_type; + typedef typename tuple_type::inherited base; + ::boost::tuples::swap(static_cast(lhs), static_cast(rhs)); +} + } // namespace tuples } // namespace boost #endif // BOOST_TUPLE_BASIC_NO_PARTIAL_SPEC_HPP diff --git a/test/tuple_test_bench.cpp b/test/tuple_test_bench.cpp index eec9e1c..8bf756f 100644 --- a/test/tuple_test_bench.cpp +++ b/test/tuple_test_bench.cpp @@ -445,6 +445,26 @@ void tuple_length_test() } +// ---------------------------------------------------------------------------- +// - testing swap ----------------------------------------------------------- +// ---------------------------------------------------------------------------- +void tuple_swap_test() +{ + tuple t1(1, 2.0f, 3.0), t2(4, 5.0f, 6.0); + swap(t1, t2); + BOOST_CHECK(get<0>(t1) == 4); + BOOST_CHECK(get<1>(t1) == 5.0f); + BOOST_CHECK(get<2>(t1) == 6.0); + BOOST_CHECK(get<0>(t2) == 1); + BOOST_CHECK(get<1>(t2) == 2.0f); + BOOST_CHECK(get<2>(t2) == 3.0); + + int i = 1,j = 2; + boost::tuple t3(i), t4(j); + swap(t3, t4); + BOOST_CHECK(i == 2); + BOOST_CHECK(j == 1); +} @@ -465,6 +485,7 @@ int test_main(int, char *[]) { cons_test(); const_tuple_test(); tuple_length_test(); + tuple_swap_test(); return 0; }