Boost logo

Boost-Commit :

From: nielsdekker_at_[hidden]
Date: 2008-07-02 15:58:09


Author: niels_dekker
Date: 2008-07-02 15:58:08 EDT (Wed, 02 Jul 2008)
New Revision: 47003
URL: http://svn.boost.org/trac/boost/changeset/47003

Log:
Added array support to boost/sandbox/swap, according to ticket #2056. Was discussed at "[boost] [utility/swap] Okay to add array support to Boost.Swap(sandbox/swap)?" <http://lists.boost.org/Archives/boost/2008/06/138753.php>
Added:
   sandbox/swap/libs/utility/swap/test/swap_arrays.cpp (contents, props changed)
Text files modified:
   sandbox/swap/boost/utility/swap.hpp | 15 ++++++++++++++-
   sandbox/swap/libs/utility/swap.html | 13 ++++++++++++-
   sandbox/swap/libs/utility/swap/test/Jamfile.v2 | 3 ++-
   3 files changed, 28 insertions(+), 3 deletions(-)

Modified: sandbox/swap/boost/utility/swap.hpp
==============================================================================
--- sandbox/swap/boost/utility/swap.hpp (original)
+++ sandbox/swap/boost/utility/swap.hpp 2008-07-02 15:58:08 EDT (Wed, 02 Jul 2008)
@@ -1,15 +1,19 @@
-// Copyright (C) 2007 Steven Watanabe, Joseph Gauterin
+// Copyright (C) 2007, 2008 Steven Watanabe, Joseph Gauterin
 //
 // Distributed under the Boost Software License, Version 1.0. (See
 // accompanying file LICENSE_1_0.txt or copy at
 // http://www.boost.org/LICENSE_1_0.txt)
 // For more information, see http://www.boost.org
+//
+// Update:
+// 29 June 2008 (Added support for built-in arrays.) Niels Dekker
 
 
 #ifndef BOOST_UTILITY_SWAP_HPP
 #define BOOST_UTILITY_SWAP_HPP
 
 #include <algorithm> //for std::swap
+#include <cstddef> //for std::size_t
 
 namespace boost_swap_impl
 {
@@ -19,6 +23,15 @@
     using std::swap;//use std::swap if argument dependent lookup fails
     swap(left,right);
   }
+
+ template<class T, std::size_t N>
+ void swap_impl(T (& left)[N], T (& right)[N])
+ {
+ for (std::size_t i = 0; i < N; ++i)
+ {
+ ::boost_swap_impl::swap_impl(left[i], right[i]);
+ }
+ }
 }
 
 namespace boost

Modified: sandbox/swap/libs/utility/swap.html
==============================================================================
--- sandbox/swap/libs/utility/swap.html (original)
+++ sandbox/swap/libs/utility/swap.html 2008-07-02 15:58:08 EDT (Wed, 02 Jul 2008)
@@ -26,6 +26,10 @@
     <p>
       The alternative to using argument dependent lookup in this situation is to provide a template specialization of std::swap for every type that requires a specialized swap. Although this is legal C++, no boost libraries use this method, whereas many boost libraries provide specialized swap functions in their own namespaces.
     </p>
+ <p>
+ <tt>boost::swap</tt> also supports swapping built-in arrays. Note that <tt>std::swap</tt> doesn't yet do so, but a request to add an overload of <tt>std::swap</tt> for built-in arrays has been well received by the Library Working Group of the C++ Standards Committee: LWG issue 809. std::swap should be overloaded for array types
+ <tt>boost::swap</tt> is no-throw for arrays whose element type has a no-throw swap. However, if the swap function of the element type provides the strong guarantee, then <tt>boost::swap</tt> only provides the basic guarantee, for arrays of size > 1.
+ </p>
     
     <!-- Requirements -->
     <h2>Requirements</h2>
@@ -42,6 +46,10 @@
     <ul>
       <li>A template specialization of std::swap exists for T</li>
     </ul>
+ <p>Or:</p>
+ <ul>
+ <li>T is a built-in array of swappable elements</li>
+ </ul>
 
     
     <!-- Portability -->
@@ -56,6 +64,9 @@
       <li>
         <em>Steven Wanatabe</em> - for the idea to use a barrier namespaces, enabling the function to have the name '<tt>swap</tt>' without introducing ambiguity or infinite recursion.
       </li>
+ <li>
+ <em>Niels Dekker</em> - for adding support for built-in arrays
+ </li>
       <li>
         <em><a href="mailto:Joseph.Gauterin_at_[hidden]">Joseph Gauterin</a></em> - for the initial idea, final implementation, tests, and documentation.
       </li>
@@ -63,7 +74,7 @@
 
     <!-- Copyright info -->
     <hr/>
- <p>Revised: 3rd October 2007</p>
+ <p>Revised: 29 June 2008</p>
     <p>
       Copyright 2007 Joseph Gauterin. Use, modification, and distribution are subject to the Boost Software License, Version 1.0.
       (See accompanying file LICENSE_1_0.txt or a copy at &lt;http://www.boost.org/LICENSE_1_0.txt&gt;.)

Modified: sandbox/swap/libs/utility/swap/test/Jamfile.v2
==============================================================================
--- sandbox/swap/libs/utility/swap/test/Jamfile.v2 (original)
+++ sandbox/swap/libs/utility/swap/test/Jamfile.v2 2008-07-02 15:58:08 EDT (Wed, 02 Jul 2008)
@@ -1,4 +1,4 @@
-# Copyright (c) 2007 Joseph Gauterin
+# Copyright (c) 2007, 2008 Joseph Gauterin
 #
 # Distributed under the Boost Software License, Version 1.0.
 # (See accompanying file LICENSE_1_0.txt or copy at
@@ -20,6 +20,7 @@
     [ run specialized_in_global.cpp ../../../test/build//boost_test_exec_monitor/<link>static ]
     [ run specialized_in_other.cpp ../../../test/build//boost_test_exec_monitor/<link>static ]
     [ run specialized_in_std.cpp ../../../test/build//boost_test_exec_monitor/<link>static ]
+ [ run swap_arrays.cpp ../../../test/build//boost_test_exec_monitor/<link>static ]
     ;
     
 

Added: sandbox/swap/libs/utility/swap/test/swap_arrays.cpp
==============================================================================
--- (empty file)
+++ sandbox/swap/libs/utility/swap/test/swap_arrays.cpp 2008-07-02 15:58:08 EDT (Wed, 02 Jul 2008)
@@ -0,0 +1,39 @@
+// Copyright (c) 2008 Joseph Gauterin, Niels Dekker
+//
+// Distributed under the Boost Software License, Version 1.0.
+// (See accompanying file LICENSE_1_0.txt or copy at
+// http://www.boost.org/LICENSE_1_0.txt)
+
+#include <boost/utility/swap.hpp>
+#define BOOST_INCLUDE_MAIN
+#include <boost/test/test_tools.hpp>
+
+//Put test class in the global namespace
+#include "./swap_test_class.hpp"
+
+
+int test_main(int, char*[])
+{
+ const std::size_t dimension = 7;
+
+ swap_test_class array1[dimension];
+ swap_test_class array2[dimension];
+ boost::swap(array1, array2);
+
+ BOOST_CHECK_EQUAL(swap_test_class::swap_count(), dimension);
+ BOOST_CHECK_EQUAL(swap_test_class::copy_count(), 0);
+
+ swap_test_class::reset();
+
+ const std::size_t firstDimension = 3;
+ const std::size_t secondDimension = 4;
+
+ swap_test_class two_d_array1[firstDimension][secondDimension];
+ swap_test_class two_d_array2[firstDimension][secondDimension];
+ boost::swap(two_d_array1, two_d_array1);
+
+ BOOST_CHECK_EQUAL(swap_test_class::swap_count(), firstDimension*secondDimension);
+ BOOST_CHECK_EQUAL(swap_test_class::copy_count(), 0);
+
+ 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