Ticket #2056: single_patch_of_boost_array_swap_modifications.patch

File single_patch_of_boost_array_swap_modifications.patch, 6.0 KB (added by niels_dekker, 14 years ago)

Single patch that covers all the files added or modified, including Jamfile.v2

  • boost/utility/swap.hpp

     
    1 // Copyright (C) 2007 Steven Watanabe, Joseph Gauterin
     1// Copyright (C) 2007, 2008 Steven Watanabe, Joseph Gauterin
    22//
    33// Distributed under the Boost Software License, Version 1.0. (See
    44// accompanying file LICENSE_1_0.txt or copy at
    55// http://www.boost.org/LICENSE_1_0.txt)
    66// For more information, see http://www.boost.org
     7//
     8// Update:
     9// 29 June 2008 (Added support for built-in arrays.) Niels Dekker 
    710
    811
    912#ifndef BOOST_UTILITY_SWAP_HPP
    1013#define BOOST_UTILITY_SWAP_HPP
    1114
    1215#include <algorithm> //for std::swap
     16#include <cstddef> //for std::size_t
    1317
    1418namespace boost_swap_impl
    1519{
     
    1923    using std::swap;//use std::swap if argument dependent lookup fails
    2024    swap(left,right);
    2125  }
     26
     27  template<class T, std::size_t N>
     28  void swap_impl(T (& left)[N], T (& right)[N])
     29  {
     30    for (std::size_t i = 0; i < N; ++i)
     31    {
     32      ::boost_swap_impl::swap_impl(left[i], right[i]);
     33    }
     34  }
    2235}
    2336
    2437namespace boost
  • libs/utility/swap.html

     
    2626    <p>
    2727      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.
    2828    </p>
     29    <p>
     30      <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: <a href="http://www.open-std.org/jtc1/sc22/wg21/docs/lwg-active.html#809">LWG issue 809. std::swap should be overloaded for array types</a>
     31      <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.
     32    </p>
    2933   
    3034    <!-- Requirements -->
    3135    <h2>Requirements</h2>
     
    4246    <ul>
    4347      <li>A template specialization of std::swap exists for T</li>
    4448    </ul>
     49    <p>Or:</p>
     50    <ul>
     51      <li>T is a built-in array of swappable elements</li>
     52    </ul>
    4553
    4654   
    4755    <!-- Portability -->
     
    5664      <li>
    5765        <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.
    5866      </li>
     67      <li>
     68        <em>Niels Dekker</em> - for adding support for built-in arrays
     69      </li>
    5970      <li>       
    6071        <em><a href="mailto:Joseph.Gauterin@googlemail.com">Joseph Gauterin</a></em> - for the initial idea, final implementation, tests, and documentation.
    6172      </li>
     
    6374
    6475    <!-- Copyright info -->   
    6576    <hr/>
    66     <p>Revised: 3rd October 2007</p>
     77    <p>Revised: 29 June 2008</p>
    6778    <p>
    6879      Copyright 2007 Joseph Gauterin. Use, modification, and distribution are subject to the Boost Software License, Version 1.0.
    6980      (See accompanying file <a href="../../LICENSE_1_0.txt">LICENSE_1_0.txt</a> or a copy at &lt;<a href="http://www.boost.org/LICENSE_1_0.txt">http://www.boost.org/LICENSE_1_0.txt</a>&gt;.)
  • libs/utility/swap/test/Jamfile.v2

     
    1 # Copyright (c) 2007 Joseph Gauterin
     1# Copyright (c) 2007, 2008 Joseph Gauterin
    22#
    33# Distributed under the Boost Software License, Version 1.0.
    44# (See accompanying file LICENSE_1_0.txt or copy at
     
    2020    [ run specialized_in_global.cpp ../../../test/build//boost_test_exec_monitor/<link>static ]
    2121    [ run specialized_in_other.cpp ../../../test/build//boost_test_exec_monitor/<link>static  ]
    2222    [ run specialized_in_std.cpp ../../../test/build//boost_test_exec_monitor/<link>static    ]
     23    [ run swap_arrays.cpp ../../../test/build//boost_test_exec_monitor/<link>static    ]
    2324    ;
    2425   
    2526
  • libs/utility/swap/test/swap_arrays.cpp

     
     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#include <boost/utility/swap.hpp>
     8#define BOOST_INCLUDE_MAIN
     9#include <boost/test/test_tools.hpp>
     10
     11//Put test class in the global namespace
     12#include "./swap_test_class.hpp"
     13
     14
     15int test_main(int, char*[])
     16{
     17  const std::size_t dimension = 7;
     18
     19  swap_test_class array1[dimension];
     20  swap_test_class array2[dimension];
     21  boost::swap(array1, array2);
     22
     23  BOOST_CHECK_EQUAL(swap_test_class::swap_count(), dimension);
     24  BOOST_CHECK_EQUAL(swap_test_class::copy_count(), 0);
     25
     26  swap_test_class::reset();
     27
     28  const std::size_t firstDimension = 3;
     29  const std::size_t secondDimension = 4;
     30
     31  swap_test_class two_d_array1[firstDimension][secondDimension];
     32  swap_test_class two_d_array2[firstDimension][secondDimension];
     33  boost::swap(two_d_array1, two_d_array1);
     34
     35  BOOST_CHECK_EQUAL(swap_test_class::swap_count(), firstDimension*secondDimension);
     36  BOOST_CHECK_EQUAL(swap_test_class::copy_count(), 0);
     37
     38  return 0;
     39}