Boost logo

Boost-Commit :

From: joseph.gauterin_at_[hidden]
Date: 2007-10-03 07:32:23


Author: joseph.gauterin
Date: 2007-10-03 07:32:20 EDT (Wed, 03 Oct 2007)
New Revision: 39668
URL: http://svn.boost.org/trac/boost/changeset/39668

Log:
Initial version of utility/swap
Added:
   sandbox/swap/
   sandbox/swap/boost/
   sandbox/swap/boost/swap.hpp (contents, props changed)
   sandbox/swap/boost/utility/
   sandbox/swap/boost/utility/swap.hpp (contents, props changed)
   sandbox/swap/libs/
   sandbox/swap/libs/utility/
   sandbox/swap/libs/utility/swap/
   sandbox/swap/libs/utility/swap.html (contents, props changed)
   sandbox/swap/libs/utility/swap/test/
   sandbox/swap/libs/utility/swap/test/Jamfile.v2 (contents, props changed)
   sandbox/swap/libs/utility/swap/test/lib_header_1.cpp (contents, props changed)
   sandbox/swap/libs/utility/swap/test/lib_header_2.cpp (contents, props changed)
   sandbox/swap/libs/utility/swap/test/mixed_headers_1.cpp (contents, props changed)
   sandbox/swap/libs/utility/swap/test/mixed_headers_2.cpp (contents, props changed)
   sandbox/swap/libs/utility/swap/test/primitive.cpp (contents, props changed)
   sandbox/swap/libs/utility/swap/test/root_header_1.cpp (contents, props changed)
   sandbox/swap/libs/utility/swap/test/root_header_2.cpp (contents, props changed)
   sandbox/swap/libs/utility/swap/test/specialized_in_boost.cpp (contents, props changed)
   sandbox/swap/libs/utility/swap/test/specialized_in_global.cpp (contents, props changed)
   sandbox/swap/libs/utility/swap/test/specialized_in_other.cpp (contents, props changed)
   sandbox/swap/libs/utility/swap/test/specialized_in_std.cpp (contents, props changed)
   sandbox/swap/libs/utility/swap/test/swap_test_class.hpp (contents, props changed)

Added: sandbox/swap/boost/swap.hpp
==============================================================================
--- (empty file)
+++ sandbox/swap/boost/swap.hpp 2007-10-03 07:32:20 EDT (Wed, 03 Oct 2007)
@@ -0,0 +1,12 @@
+// Copyright (C) 2007 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)
+
+#ifndef BOOST_SWAP_HPP
+#define BOOST_SWAP_HPP
+
+#include "./utility/swap.hpp"
+
+#endif

Added: sandbox/swap/boost/utility/swap.hpp
==============================================================================
--- (empty file)
+++ sandbox/swap/boost/utility/swap.hpp 2007-10-03 07:32:20 EDT (Wed, 03 Oct 2007)
@@ -0,0 +1,38 @@
+// Copyright (C) 2007 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
+
+
+#ifndef BOOST_UTILITY_SWAP_HPP
+#define BOOST_UTILITY_SWAP_HPP
+
+#include <algorithm> //for std::swap
+
+namespace boost_swap_impl
+{
+ template<class T>
+ void swap_impl(T& left, T& right)
+ {
+ using std::swap;//use std::swap if argument dependent lookup fails
+ swap(left,right);
+ }
+}
+
+namespace boost
+{
+ namespace swap_adl_barrier
+ {
+ template<class T>
+ void swap(T& left, T& right)
+ {
+ ::boost_swap_impl::swap_impl(left, right);
+ }
+ }
+
+ using swap_adl_barrier::swap;
+}
+
+#endif

Added: sandbox/swap/libs/utility/swap.html
==============================================================================
--- (empty file)
+++ sandbox/swap/libs/utility/swap.html 2007-10-03 07:32:20 EDT (Wed, 03 Oct 2007)
@@ -0,0 +1,68 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
+<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US" lang="en-US">
+ <head>
+ <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
+ <title>Boost: Swap Documentation</title>
+ </head>
+ <body>
+ <!-- Page header -->
+ <img src="../../boost.png" alt="C++ Boost" align="middle" width="277" height="86"/>
+ <h1>Swap</h1>
+
+ <p>
+ <tt>template&lt;class T&gt; void swap(T&amp; <em>left</em>, T&amp; <em>right</em>);</tt>
+ </p>
+
+ <!-- Intoduction -->
+ <p>
+ The template function <tt>boost::swap</tt> allows the values of two variables to be swapped, using argument dependent lookup to select a specialized swap function if available. If no specialized swap function is available, <tt>std::swap is used</tt>.
+ </p>
+
+ <!-- Rationale -->
+ <h2>Rationale</h2>
+ <p>
+ The generic <tt>std::swap</tt> function requires that the elements to be swapped are assignable and copy constructable. It is ususally implemented using one copy constuction and two assignments. This is often both unneccessarily restrictive and unneccessarily slow. In addition, specialized swap functions are often able to provide the no-throw exception guarentee where the generic swap implementation provides only the weak guarantee.</p>
+ <p>
+ The alternative to using argument dependent lookup in this situation is to overload 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>
+
+ <!-- Requirements -->
+ <h2>Requirements</h2>
+ <p>Either:</p>
+ <ul>
+ <li>T must be assignable.</li>
+ <li>T must be copy constructable.</li>
+ </ul>
+ <p>Or:</p>
+ <ul>
+ <li>A function with the signature <tt>swap(T&,T&)</tt> is available via argument dependent lookup or such a function exists in namespace <tt>std</tt>.</li>
+ </ul>
+
+ <!-- Portability -->
+ <h2>Portability</h2>
+ <p>
+ Several older compilers do not support argument dependent lookup - on these compilers <tt>boost::swap</tt> will call <tt>std::swap</tt>, ignoring any specialized swap functions that could be found as a result of argument dependent lookup.
+ </p>
+
+ <!-- Credits -->
+ <h2>Credits</h2>
+ <ul>
+ <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><a href="mailto:Joseph.Gauterin_at_[hidden]">Joseph Gauterin</a></em> - for the initial idea, final implementation, tests, and documentation.
+ </li>
+ </ul>
+
+ <!-- Copyright info -->
+ <hr/>
+ <p>Revised: 26th September 2007</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;.)
+ </p>
+
+ </body>
+</html>

Added: sandbox/swap/libs/utility/swap/test/Jamfile.v2
==============================================================================
--- (empty file)
+++ sandbox/swap/libs/utility/swap/test/Jamfile.v2 2007-10-03 07:32:20 EDT (Wed, 03 Oct 2007)
@@ -0,0 +1,25 @@
+# Copyright (c) 2007 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)
+
+# bring in rules for testing
+import testing ;
+
+test-suite swap
+ :
+ [ compile root_header_1.cpp ]
+ [ compile root_header_2.cpp ]
+ [ compile lib_header_1.cpp ]
+ [ compile lib_header_2.cpp ]
+ [ compile mixed_headers_1.cpp ]
+ [ compile mixed_headers_2.cpp ]
+ [ run primitive.cpp ../../../test/build//boost_test_exec_monitor/<link>static ]
+ [ run specialized_in_boost.cpp ../../../test/build//boost_test_exec_monitor/<link>static ]
+ [ 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 ]
+ ;
+
+

Added: sandbox/swap/libs/utility/swap/test/lib_header_1.cpp
==============================================================================
--- (empty file)
+++ sandbox/swap/libs/utility/swap/test/lib_header_1.cpp 2007-10-03 07:32:20 EDT (Wed, 03 Oct 2007)
@@ -0,0 +1,9 @@
+// Copyright (c) 2007 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)
+
+// Tests that the swap header compiles as a standalone translation unit
+
+#include <boost/utility/swap.hpp>

Added: sandbox/swap/libs/utility/swap/test/lib_header_2.cpp
==============================================================================
--- (empty file)
+++ sandbox/swap/libs/utility/swap/test/lib_header_2.cpp 2007-10-03 07:32:20 EDT (Wed, 03 Oct 2007)
@@ -0,0 +1,10 @@
+// Copyright (c) 2007 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)
+
+// Tests that the swap header include guards work correctly
+
+#include <boost/utility/swap.hpp>
+#include <boost/utility/swap.hpp>

Added: sandbox/swap/libs/utility/swap/test/mixed_headers_1.cpp
==============================================================================
--- (empty file)
+++ sandbox/swap/libs/utility/swap/test/mixed_headers_1.cpp 2007-10-03 07:32:20 EDT (Wed, 03 Oct 2007)
@@ -0,0 +1,10 @@
+// Copyright (c) 2007 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)
+
+// Tests that the swap headers work when both are included
+
+#include <boost/swap.hpp>
+#include <boost/utility/swap.hpp>

Added: sandbox/swap/libs/utility/swap/test/mixed_headers_2.cpp
==============================================================================
--- (empty file)
+++ sandbox/swap/libs/utility/swap/test/mixed_headers_2.cpp 2007-10-03 07:32:20 EDT (Wed, 03 Oct 2007)
@@ -0,0 +1,10 @@
+// Copyright (c) 2007 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)
+
+// Tests that the swap headers work when both are included
+
+#include <boost/utility/swap.hpp>
+#include <boost/swap.hpp>

Added: sandbox/swap/libs/utility/swap/test/primitive.cpp
==============================================================================
--- (empty file)
+++ sandbox/swap/libs/utility/swap/test/primitive.cpp 2007-10-03 07:32:20 EDT (Wed, 03 Oct 2007)
@@ -0,0 +1,22 @@
+// Copyright (c) 2007 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)
+
+#include <boost/utility/swap.hpp>
+#define BOOST_INCLUDE_MAIN
+#include <boost/test/test_tools.hpp>
+
+int test_main(int, char*[])
+{
+ int object1 = 1;
+ int object2 = 2;
+
+ boost::swap(object1,object2);
+
+ BOOST_CHECK_EQUAL(object1,2);
+ BOOST_CHECK_EQUAL(object2,1);
+
+ return 0;
+}
\ No newline at end of file

Added: sandbox/swap/libs/utility/swap/test/root_header_1.cpp
==============================================================================
--- (empty file)
+++ sandbox/swap/libs/utility/swap/test/root_header_1.cpp 2007-10-03 07:32:20 EDT (Wed, 03 Oct 2007)
@@ -0,0 +1,9 @@
+// Copyright (c) 2007 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)
+
+// Tests that the swap header compiles as a standalone translation unit
+
+#include <boost/swap.hpp>

Added: sandbox/swap/libs/utility/swap/test/root_header_2.cpp
==============================================================================
--- (empty file)
+++ sandbox/swap/libs/utility/swap/test/root_header_2.cpp 2007-10-03 07:32:20 EDT (Wed, 03 Oct 2007)
@@ -0,0 +1,10 @@
+// Copyright (c) 2007 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)
+
+// Tests that the swap header include guards work correctly
+
+#include <boost/swap.hpp>
+#include <boost/swap.hpp>

Added: sandbox/swap/libs/utility/swap/test/specialized_in_boost.cpp
==============================================================================
--- (empty file)
+++ sandbox/swap/libs/utility/swap/test/specialized_in_boost.cpp 2007-10-03 07:32:20 EDT (Wed, 03 Oct 2007)
@@ -0,0 +1,36 @@
+// Copyright (c) 2007 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)
+
+#include <boost/utility/swap.hpp>
+#define BOOST_INCLUDE_MAIN
+#include <boost/test/test_tools.hpp>
+
+//Put test class in namespace boost
+namespace boost
+{
+ #include "./swap_test_class.hpp"
+}
+
+//Provide swap function in namespace boost
+namespace boost
+{
+ void swap(swap_test_class& left, swap_test_class& right)
+ {
+ left.swap(right);
+ }
+}
+
+int test_main(int, char*[])
+{
+ boost::swap_test_class object1;
+ boost::swap_test_class object2;
+ boost::swap(object1,object2);
+
+ BOOST_CHECK_EQUAL(boost::swap_test_class::swap_count(),1);
+ BOOST_CHECK_EQUAL(boost::swap_test_class::copy_count(),0);
+
+ return 0;
+}
\ No newline at end of file

Added: sandbox/swap/libs/utility/swap/test/specialized_in_global.cpp
==============================================================================
--- (empty file)
+++ sandbox/swap/libs/utility/swap/test/specialized_in_global.cpp 2007-10-03 07:32:20 EDT (Wed, 03 Oct 2007)
@@ -0,0 +1,30 @@
+// Copyright (c) 2007 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)
+
+#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"
+
+//Provide swap function in gloabl namespace
+void swap(swap_test_class& left, swap_test_class& right)
+{
+ left.swap(right);
+}
+
+int test_main(int, char*[])
+{
+ swap_test_class object1;
+ swap_test_class object2;
+ boost::swap(object1,object2);
+
+ BOOST_CHECK_EQUAL(swap_test_class::swap_count(),1);
+ BOOST_CHECK_EQUAL(swap_test_class::copy_count(),0);
+
+ return 0;
+}
\ No newline at end of file

Added: sandbox/swap/libs/utility/swap/test/specialized_in_other.cpp
==============================================================================
--- (empty file)
+++ sandbox/swap/libs/utility/swap/test/specialized_in_other.cpp 2007-10-03 07:32:20 EDT (Wed, 03 Oct 2007)
@@ -0,0 +1,36 @@
+// Copyright (c) 2007 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)
+
+#include <boost/utility/swap.hpp>
+#define BOOST_INCLUDE_MAIN
+#include <boost/test/test_tools.hpp>
+
+//Put test class in namespace other
+namespace other
+{
+ #include "./swap_test_class.hpp"
+}
+
+//Provide swap function in namespace other
+namespace other
+{
+ void swap(swap_test_class& left, swap_test_class& right)
+ {
+ left.swap(right);
+ }
+}
+
+int test_main(int, char*[])
+{
+ other::swap_test_class object1;
+ other::swap_test_class object2;
+ boost::swap(object1,object2);
+
+ BOOST_CHECK_EQUAL(other::swap_test_class::swap_count(),1);
+ BOOST_CHECK_EQUAL(other::swap_test_class::copy_count(),0);
+
+ return 0;
+}
\ No newline at end of file

Added: sandbox/swap/libs/utility/swap/test/specialized_in_std.cpp
==============================================================================
--- (empty file)
+++ sandbox/swap/libs/utility/swap/test/specialized_in_std.cpp 2007-10-03 07:32:20 EDT (Wed, 03 Oct 2007)
@@ -0,0 +1,35 @@
+// Copyright (c) 2007 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)
+
+#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"
+
+
+//Provide swap function in namespace std
+namespace std
+{
+ template <>
+ void swap(swap_test_class& left, swap_test_class& right)
+ {
+ left.swap(right);
+ }
+}
+
+int test_main(int, char*[])
+{
+ swap_test_class object1;
+ swap_test_class object2;
+ boost::swap(object1,object2);
+
+ BOOST_CHECK_EQUAL(swap_test_class::swap_count(),1);
+ BOOST_CHECK_EQUAL(swap_test_class::copy_count(),0);
+
+ return 0;
+}
\ No newline at end of file

Added: sandbox/swap/libs/utility/swap/test/swap_test_class.hpp
==============================================================================
--- (empty file)
+++ sandbox/swap/libs/utility/swap/test/swap_test_class.hpp 2007-10-03 07:32:20 EDT (Wed, 03 Oct 2007)
@@ -0,0 +1,84 @@
+// Copyright (c) 2007 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)
+
+// Tests that the swap header compiles as a standalone translation unit
+
+#ifndef BOOST_UTILITY_SWAP_TEST_CLASS_HPP
+#define BOOST_UTILITY_SWAP_TEST_CLASS_HPP
+
+
+class swap_test_class
+{
+public:
+ swap_test_class()
+ {
+ ++constructCount();
+ }
+
+ ~swap_test_class()
+ {
+ ++destructCount();
+ }
+
+ swap_test_class(const swap_test_class&)
+ {
+ ++copyCount();
+ ++destructCount();
+ }
+
+ swap_test_class& operator=(const swap_test_class&)
+ {
+ ++copyCount();
+ }
+
+ void swap(swap_test_class& other)
+ {
+ ++swapCount();
+ }
+
+
+ static unsigned int swap_count(){ return swapCount(); }
+ static unsigned int copy_count(){ return copyCount(); }
+ static unsigned int construct_count(){ return constructCount(); }
+ static unsigned int destruct_count(){ return destructCount(); }
+
+ static void reset()
+ {
+ swapCount() = 0;
+ copyCount() = 0;
+ constructCount() = 0;
+ destructCount() = 0;
+ }
+
+private:
+ static unsigned int& swapCount()
+ {
+ static unsigned int value = 0;
+ return value;
+ }
+
+ static unsigned int& copyCount()
+ {
+ static unsigned int value = 0;
+ return value;
+ }
+
+ static unsigned int& constructCount()
+ {
+ static unsigned int value = 0;
+ return value;
+ }
+
+ static unsigned int& destructCount()
+ {
+ static unsigned int value = 0;
+ return value;
+ }
+
+};
+
+#endif
+


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