Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r62686 - in trunk: boost/tuple/detail libs/tuple/test
From: steven_at_[hidden]
Date: 2010-06-09 15:30:36


Author: steven_watanabe
Date: 2010-06-09 15:30:35 EDT (Wed, 09 Jun 2010)
New Revision: 62686
URL: http://svn.boost.org/trac/boost/changeset/62686

Log:
Add an element-wise swap for Boost.Tuple. Fixes #1324
Text files modified:
   trunk/boost/tuple/detail/tuple_basic.hpp | 24 ++++++++++++++++++++++++
   trunk/boost/tuple/detail/tuple_basic_no_partial_spec.hpp | 24 ++++++++++++++++++++++++
   trunk/libs/tuple/test/tuple_test_bench.cpp | 21 +++++++++++++++++++++
   3 files changed, 69 insertions(+), 0 deletions(-)

Modified: trunk/boost/tuple/detail/tuple_basic.hpp
==============================================================================
--- trunk/boost/tuple/detail/tuple_basic.hpp (original)
+++ trunk/boost/tuple/detail/tuple_basic.hpp 2010-06-09 15:30:35 EDT (Wed, 09 Jun 2010)
@@ -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 @@
   return t(t0, t1, t2, t3, t4, t5, t6, t7, t8, t9);
 }
 
+template <class T0, class T1, class T2, class T3, class T4,
+ class T5, class T6, class T7, class T8, class T9>
+void swap(tuple<T0, T1, T2, T3, T4, T5, T6, T7, T8, T9>& lhs,
+ tuple<T0, T1, T2, T3, T4, T5, T6, T7, T8, T9>& rhs);
+inline void swap(null_type&, null_type&) {}
+template<class HH>
+inline void swap(cons<HH, null_type>& lhs, cons<HH, null_type>& rhs) {
+ ::boost::swap(lhs.head, rhs.head);
+}
+template<class HH, class TT>
+inline void swap(cons<HH, TT>& lhs, cons<HH, TT>& rhs) {
+ ::boost::swap(lhs.head, rhs.head);
+ ::boost::tuples::swap(lhs.tail, rhs.tail);
+}
+template <class T0, class T1, class T2, class T3, class T4,
+ class T5, class T6, class T7, class T8, class T9>
+inline void swap(tuple<T0, T1, T2, T3, T4, T5, T6, T7, T8, T9>& lhs,
+ tuple<T0, T1, T2, T3, T4, T5, T6, T7, T8, T9>& rhs) {
+ typedef tuple<T0, T1, T2, T3, T4, T5, T6, T7, T8, T9> tuple_type;
+ typedef typename tuple_type::inherited base;
+ ::boost::tuples::swap(static_cast<base&>(lhs), static_cast<base&>(rhs));
+}
+
 } // end of namespace tuples
 } // end of namespace boost
 

Modified: trunk/boost/tuple/detail/tuple_basic_no_partial_spec.hpp
==============================================================================
--- trunk/boost/tuple/detail/tuple_basic_no_partial_spec.hpp (original)
+++ trunk/boost/tuple/detail/tuple_basic_no_partial_spec.hpp 2010-06-09 15:30:35 EDT (Wed, 09 Jun 2010)
@@ -27,6 +27,7 @@
 #define BOOST_TUPLE_BASIC_NO_PARTIAL_SPEC_HPP
 
 #include "boost/type_traits.hpp"
+#include "boost/utility/swap.hpp"
 #include <utility>
 
 #if defined BOOST_MSVC
@@ -836,6 +837,29 @@
 
 detail::swallow_assign const ignore = detail::swallow_assign();
 
+template <class T0, class T1, class T2, class T3, class T4,
+ class T5, class T6, class T7, class T8, class T9>
+void swap(tuple<T0, T1, T2, T3, T4, T5, T6, T7, T8, T9>& lhs,
+ tuple<T0, T1, T2, T3, T4, T5, T6, T7, T8, T9>& rhs);
+inline void swap(null_type&, null_type&) {}
+template<class HH>
+inline void swap(cons<HH, null_type>& lhs, cons<HH, null_type>& rhs) {
+ ::boost::swap(lhs.head, rhs.head);
+}
+template<class HH, class TT>
+inline void swap(cons<HH, TT>& lhs, cons<HH, TT>& rhs) {
+ ::boost::swap(lhs.head, rhs.head);
+ ::boost::tuples::swap(lhs.tail, rhs.tail);
+}
+template <class T0, class T1, class T2, class T3, class T4,
+ class T5, class T6, class T7, class T8, class T9>
+inline void swap(tuple<T0, T1, T2, T3, T4, T5, T6, T7, T8, T9>& lhs,
+ tuple<T0, T1, T2, T3, T4, T5, T6, T7, T8, T9>& rhs) {
+ typedef tuple<T0, T1, T2, T3, T4, T5, T6, T7, T8, T9> tuple_type;
+ typedef typename tuple_type::inherited base;
+ ::boost::tuples::swap(static_cast<base&>(lhs), static_cast<base&>(rhs));
+}
+
 } // namespace tuples
 } // namespace boost
 #endif // BOOST_TUPLE_BASIC_NO_PARTIAL_SPEC_HPP

Modified: trunk/libs/tuple/test/tuple_test_bench.cpp
==============================================================================
--- trunk/libs/tuple/test/tuple_test_bench.cpp (original)
+++ trunk/libs/tuple/test/tuple_test_bench.cpp 2010-06-09 15:30:35 EDT (Wed, 09 Jun 2010)
@@ -445,6 +445,26 @@
 
 }
 
+// ----------------------------------------------------------------------------
+// - testing swap -----------------------------------------------------------
+// ----------------------------------------------------------------------------
+void tuple_swap_test()
+{
+ tuple<int, float, double> 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<int&> t3(i), t4(j);
+ swap(t3, t4);
+ BOOST_CHECK(i == 2);
+ BOOST_CHECK(j == 1);
+}
 
 
 
@@ -465,6 +485,7 @@
   cons_test();
   const_tuple_test();
   tuple_length_test();
+ tuple_swap_test();
   return 0;
 }
 


Boost-Commit list run by bdawes at acm.org, david.abrahams at rcn.com, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk