mirror of
https://github.com/boostorg/utility.git
synced 2025-10-14 17:45:20 +02:00
Compare commits
10 Commits
svn-branch
...
boost-1.38
Author | SHA1 | Date | |
---|---|---|---|
|
570b84cdc7 | ||
|
b2e6a82adb | ||
|
390372294a | ||
|
ffbbf38e12 | ||
|
9e73b2c6ae | ||
|
633832e872 | ||
|
862cb2a4e0 | ||
|
b012f16ee5 | ||
|
3d96ab26d4 | ||
|
8652bf51ec |
31
CMakeLists.txt
Normal file
31
CMakeLists.txt
Normal file
@@ -0,0 +1,31 @@
|
|||||||
|
#----------------------------------------------------------------------------
|
||||||
|
# This file was automatically generated from the original CMakeLists.txt file
|
||||||
|
# Add a variable to hold the headers for the library
|
||||||
|
set (lib_headers
|
||||||
|
assert.hpp
|
||||||
|
call_traits.hpp
|
||||||
|
checked_delete.hpp
|
||||||
|
compressed_pair.hpp
|
||||||
|
current_function.hpp
|
||||||
|
operators.hpp
|
||||||
|
throw_exception.hpp
|
||||||
|
utility.hpp
|
||||||
|
utility
|
||||||
|
)
|
||||||
|
|
||||||
|
# Add a library target to the build system
|
||||||
|
boost_library_project(
|
||||||
|
utility
|
||||||
|
# SRCDIRS
|
||||||
|
TESTDIRS test
|
||||||
|
HEADERS ${lib_headers}
|
||||||
|
# DOCDIRS
|
||||||
|
DESCRIPTION "Various small utilities for C++ programming."
|
||||||
|
MODULARIZED
|
||||||
|
AUTHORS "David Abrahams <dave -at- boostpro.com>"
|
||||||
|
"Brad King"
|
||||||
|
"Douglas Gregor <doug.gregor -at- gmail.com>"
|
||||||
|
# MAINTAINERS
|
||||||
|
)
|
||||||
|
|
||||||
|
|
@@ -99,7 +99,7 @@ directly to the container:</p>
|
|||||||
<H2><A NAME="framework"></A>Framework</H2>
|
<H2><A NAME="framework"></A>Framework</H2>
|
||||||
<p>
|
<p>
|
||||||
This library proposes a framework to allow some containers to directly contruct contained objects in-place without requiring
|
This library proposes a framework to allow some containers to directly contruct contained objects in-place without requiring
|
||||||
the entire set of constructor overloads ftom the contained type. It also allows the container to remove the CopyConstuctible
|
the entire set of constructor overloads from the contained type. It also allows the container to remove the CopyConstuctible
|
||||||
requirement from the contained type since objects can be directly constructed in-place without need of a copy.<br>
|
requirement from the contained type since objects can be directly constructed in-place without need of a copy.<br>
|
||||||
The only requirement on the container is that it must provide proper storage (that is, correctly aligned and sized).
|
The only requirement on the container is that it must provide proper storage (that is, correctly aligned and sized).
|
||||||
Naturally, the container will typically support uninitialized storage to avoid the in-place construction to override
|
Naturally, the container will typically support uninitialized storage to avoid the in-place construction to override
|
||||||
@@ -293,4 +293,4 @@ the latest version of this file can be found at <A
|
|||||||
HREF="http://www.boost.org">www.boost.org</A>, and the boost
|
HREF="http://www.boost.org">www.boost.org</A>, and the boost
|
||||||
<A HREF="http://www.boost.org/more/mailing_lists.htm#main">discussion lists</A></P>
|
<A HREF="http://www.boost.org/more/mailing_lists.htm#main">discussion lists</A></P>
|
||||||
</BODY>
|
</BODY>
|
||||||
</HTML>
|
</HTML>
|
||||||
|
@@ -7,6 +7,6 @@
|
|||||||
#ifndef BOOST_SWAP_HPP
|
#ifndef BOOST_SWAP_HPP
|
||||||
#define BOOST_SWAP_HPP
|
#define BOOST_SWAP_HPP
|
||||||
|
|
||||||
#include "./utility/swap.hpp"
|
#include "boost/utility/swap.hpp"
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
@@ -7,6 +7,7 @@
|
|||||||
// 21 Ago 2002 (Created) Fernando Cacciola
|
// 21 Ago 2002 (Created) Fernando Cacciola
|
||||||
// 24 Dec 2007 (Refactored and worked around various compiler bugs) Fernando Cacciola, Niels Dekker
|
// 24 Dec 2007 (Refactored and worked around various compiler bugs) Fernando Cacciola, Niels Dekker
|
||||||
// 23 May 2008 (Fixed operator= const issue, added initialized_value) Niels Dekker, Fernando Cacciola
|
// 23 May 2008 (Fixed operator= const issue, added initialized_value) Niels Dekker, Fernando Cacciola
|
||||||
|
// 21 Ago 2008 (Added swap) Niels Dekker, Fernando Cacciola
|
||||||
//
|
//
|
||||||
#ifndef BOOST_UTILITY_VALUE_INIT_21AGO2002_HPP
|
#ifndef BOOST_UTILITY_VALUE_INIT_21AGO2002_HPP
|
||||||
#define BOOST_UTILITY_VALUE_INIT_21AGO2002_HPP
|
#define BOOST_UTILITY_VALUE_INIT_21AGO2002_HPP
|
||||||
@@ -22,6 +23,7 @@
|
|||||||
#include <boost/static_assert.hpp>
|
#include <boost/static_assert.hpp>
|
||||||
#include <boost/type_traits/cv_traits.hpp>
|
#include <boost/type_traits/cv_traits.hpp>
|
||||||
#include <boost/type_traits/alignment_of.hpp>
|
#include <boost/type_traits/alignment_of.hpp>
|
||||||
|
#include <boost/swap.hpp>
|
||||||
#include <cstring>
|
#include <cstring>
|
||||||
#include <new>
|
#include <new>
|
||||||
|
|
||||||
@@ -93,6 +95,11 @@ class value_initialized
|
|||||||
return wrapper_address()->data;
|
return wrapper_address()->data;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void swap(value_initialized & arg)
|
||||||
|
{
|
||||||
|
::boost::swap( this->data(), arg.data() );
|
||||||
|
}
|
||||||
|
|
||||||
operator T&() const { return this->data(); }
|
operator T&() const { return this->data(); }
|
||||||
|
|
||||||
} ;
|
} ;
|
||||||
@@ -110,6 +117,12 @@ T& get ( value_initialized<T>& x )
|
|||||||
return x.data() ;
|
return x.data() ;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
template<class T>
|
||||||
|
void swap ( value_initialized<T> & lhs, value_initialized<T> & rhs )
|
||||||
|
{
|
||||||
|
lhs.swap(rhs) ;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
class initialized_value_t
|
class initialized_value_t
|
||||||
{
|
{
|
||||||
|
1
module.cmake
Normal file
1
module.cmake
Normal file
@@ -0,0 +1 @@
|
|||||||
|
boost_module(utility DEPENDS iterator exception detail )
|
16
swap.html
16
swap.html
@@ -14,7 +14,7 @@
|
|||||||
<tt>template<class T> void swap(T& <em>left</em>, T& <em>right</em>);</tt>
|
<tt>template<class T> void swap(T& <em>left</em>, T& <em>right</em>);</tt>
|
||||||
</p>
|
</p>
|
||||||
|
|
||||||
<!-- Intoduction -->
|
<!-- Introduction -->
|
||||||
<p>
|
<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</tt> is used.
|
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</tt> is used.
|
||||||
</p>
|
</p>
|
||||||
@@ -24,10 +24,10 @@
|
|||||||
<p>
|
<p>
|
||||||
The generic <tt>std::swap</tt> function requires that the elements to be swapped are assignable and copy constructible. It is usually implemented using one copy construction and two assignments - this is often both unnecessarily restrictive and unnecessarily slow. In addition, where the generic swap implementation provides only the basic guarantee, specialized swap functions are often able to provide the no-throw exception guarantee (and it is considered best practice to do so where possible<sup><a href="#ref1">1</a></sup>).</p>
|
The generic <tt>std::swap</tt> function requires that the elements to be swapped are assignable and copy constructible. It is usually implemented using one copy construction and two assignments - this is often both unnecessarily restrictive and unnecessarily slow. In addition, where the generic swap implementation provides only the basic guarantee, specialized swap functions are often able to provide the no-throw exception guarantee (and it is considered best practice to do so where possible<sup><a href="#ref1">1</a></sup>).</p>
|
||||||
<p>
|
<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.
|
The alternative to using argument dependent lookup in this situation is to provide a template specialization of <tt>std::swap</tt> 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>
|
||||||
<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<sup><a href="#ref2">2</a></sup>.
|
<tt>boost::swap</tt> also supports swapping built-in arrays. Note that <tt>std::swap</tt> originally did not do so, but a request to add an overload of <tt>std::swap</tt> for built-in arrays has been accepted by the C++ Standards Committee<sup><a href="#ref2">2</a></sup>.
|
||||||
</p>
|
</p>
|
||||||
|
|
||||||
<!-- Exception Safety -->
|
<!-- Exception Safety -->
|
||||||
@@ -45,11 +45,11 @@
|
|||||||
</ul>
|
</ul>
|
||||||
<p>Or:</p>
|
<p>Or:</p>
|
||||||
<ul>
|
<ul>
|
||||||
<li>A function with the signature <tt>swap(T&,T&)</tt> is available via argument dependent lookup</li>
|
<li>A function with the signature <tt>swap(T&,T&)</tt> is available via argument dependent lookup</li>
|
||||||
</ul>
|
</ul>
|
||||||
<p>Or:</p>
|
<p>Or:</p>
|
||||||
<ul>
|
<ul>
|
||||||
<li>A template specialization of std::swap exists for T</li>
|
<li>A template specialization of <tt>std::swap</tt> exists for T</li>
|
||||||
</ul>
|
</ul>
|
||||||
<p>Or:</p>
|
<p>Or:</p>
|
||||||
<ul>
|
<ul>
|
||||||
@@ -73,18 +73,18 @@
|
|||||||
<em><a href="mailto:Joseph.Gauterin@googlemail.com">Joseph Gauterin</a></em> - for the initial idea, implementation, tests, and documentation
|
<em><a href="mailto:Joseph.Gauterin@googlemail.com">Joseph Gauterin</a></em> - for the initial idea, implementation, tests, and documentation
|
||||||
</li>
|
</li>
|
||||||
<li>
|
<li>
|
||||||
<em>Steven Wanatabe</em> - for the idea to make boost::swap less specialized than std::swap, thereby allowing the function to have the name 'swap' without introducing ambiguity
|
<em>Steven Watanabe</em> - for the idea to make <tt>boost::swap</tt> less specialized than <tt>std::swap</tt>, thereby allowing the function to have the name 'swap' without introducing ambiguity
|
||||||
</li>
|
</li>
|
||||||
</ul>
|
</ul>
|
||||||
|
|
||||||
<!-- References -->
|
<!-- References -->
|
||||||
<hr/>
|
<hr/>
|
||||||
<p><sup><a id="ref1"/>[1]</sup>Scott Meyers, Effective C++ Third Edition, Item 25: "Consider support for a non-throwing swap"</p>
|
<p><sup><a id="ref1"/>[1]</sup>Scott Meyers, Effective C++ Third Edition, Item 25: "Consider support for a non-throwing swap"</p>
|
||||||
<p><sup><a id="ref2"/>[2]</sup><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></p>
|
<p><sup><a id="ref2"/>[2]</sup><a href="http://www.open-std.org/jtc1/sc22/wg21/docs/lwg-defects.html#809">LWG Defect Report 809 (std::swap should be overloaded for array types)</a></p>
|
||||||
|
|
||||||
<!-- Copyright info -->
|
<!-- Copyright info -->
|
||||||
<hr/>
|
<hr/>
|
||||||
<p>Revised: 4 August 2008</p>
|
<p>Revised: 15 November 2008</p>
|
||||||
<p>
|
<p>
|
||||||
Copyright 2007, 2008 Joseph Gauterin. Use, modification, and distribution are subject to the Boost Software License, Version 1.0.
|
Copyright 2007, 2008 Joseph Gauterin. Use, modification, and distribution are subject to the Boost Software License, Version 1.0.
|
||||||
(See accompanying file <a href="../../LICENSE_1_0.txt">LICENSE_1_0.txt</a> or a copy at <<a href="http://www.boost.org/LICENSE_1_0.txt">http://www.boost.org/LICENSE_1_0.txt</a>>.)
|
(See accompanying file <a href="../../LICENSE_1_0.txt">LICENSE_1_0.txt</a> or a copy at <<a href="http://www.boost.org/LICENSE_1_0.txt">http://www.boost.org/LICENSE_1_0.txt</a>>.)
|
||||||
|
@@ -29,5 +29,9 @@ test-suite utility/swap
|
|||||||
[ run std_vector_of_global.cpp ../../../test/build//boost_test_exec_monitor/<link>static ]
|
[ run std_vector_of_global.cpp ../../../test/build//boost_test_exec_monitor/<link>static ]
|
||||||
[ run std_vector_of_other.cpp ../../../test/build//boost_test_exec_monitor/<link>static ]
|
[ run std_vector_of_other.cpp ../../../test/build//boost_test_exec_monitor/<link>static ]
|
||||||
[ run no_ambiguity_in_boost.cpp ../../../test/build//boost_test_exec_monitor/<link>static ]
|
[ run no_ambiguity_in_boost.cpp ../../../test/build//boost_test_exec_monitor/<link>static ]
|
||||||
[ run swap_arrays.cpp ../../../test/build//boost_test_exec_monitor/<link>static ]
|
[ run array_of_array_of_class.cpp ../../../test/build//boost_test_exec_monitor/<link>static ]
|
||||||
|
[ run array_of_array_of_int.cpp ../../../test/build//boost_test_exec_monitor/<link>static ]
|
||||||
|
[ run array_of_class.cpp ../../../test/build//boost_test_exec_monitor/<link>static ]
|
||||||
|
[ run array_of_int.cpp ../../../test/build//boost_test_exec_monitor/<link>static ]
|
||||||
|
[ run array_of_template.cpp ../../../test/build//boost_test_exec_monitor/<link>static ]
|
||||||
;
|
;
|
||||||
|
@@ -4,6 +4,8 @@
|
|||||||
// (See accompanying file LICENSE_1_0.txt or copy at
|
// (See accompanying file LICENSE_1_0.txt or copy at
|
||||||
// http://www.boost.org/LICENSE_1_0.txt)
|
// http://www.boost.org/LICENSE_1_0.txt)
|
||||||
|
|
||||||
|
// Tests swapping an array of arrays of swap_test_class objects by means of boost::swap.
|
||||||
|
|
||||||
#include <boost/utility/swap.hpp>
|
#include <boost/utility/swap.hpp>
|
||||||
#define BOOST_INCLUDE_MAIN
|
#define BOOST_INCLUDE_MAIN
|
||||||
#include <boost/test/test_tools.hpp>
|
#include <boost/test/test_tools.hpp>
|
||||||
@@ -33,32 +35,8 @@ namespace std
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Tests swapping 1-dimensional arrays.
|
|
||||||
void test_swapping_1D_arrays()
|
|
||||||
{
|
|
||||||
const std::size_t dimension = 2;
|
|
||||||
const swap_test_class initial_array1[dimension] = { swap_test_class(1), swap_test_class(2) };
|
|
||||||
const swap_test_class initial_array2[dimension] = { swap_test_class(3), swap_test_class(4) };
|
|
||||||
|
|
||||||
swap_test_class array1[dimension];
|
|
||||||
swap_test_class array2[dimension];
|
|
||||||
|
|
||||||
std::copy(initial_array1, initial_array1 + dimension, array1);
|
int test_main(int, char*[])
|
||||||
std::copy(initial_array2, initial_array2 + dimension, array2);
|
|
||||||
|
|
||||||
swap_test_class::reset();
|
|
||||||
boost::swap(array1, array2);
|
|
||||||
|
|
||||||
BOOST_CHECK(std::equal(array1, array1 + dimension, initial_array2));
|
|
||||||
BOOST_CHECK(std::equal(array2, array2 + dimension, initial_array1));
|
|
||||||
|
|
||||||
BOOST_CHECK_EQUAL(swap_test_class::swap_count(), dimension);
|
|
||||||
BOOST_CHECK_EQUAL(swap_test_class::copy_count(), 0);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
// Tests swapping 2-dimensional arrays.
|
|
||||||
void test_swapping_2D_arrays()
|
|
||||||
{
|
{
|
||||||
const std::size_t first_dimension = 3;
|
const std::size_t first_dimension = 3;
|
||||||
const std::size_t second_dimension = 4;
|
const std::size_t second_dimension = 4;
|
||||||
@@ -76,7 +54,6 @@ void test_swapping_2D_arrays()
|
|||||||
ptr2[i].set_data( static_cast<int>(i + number_of_elements) );
|
ptr2[i].set_data( static_cast<int>(i + number_of_elements) );
|
||||||
}
|
}
|
||||||
|
|
||||||
swap_test_class::reset();
|
|
||||||
boost::swap(array1, array2);
|
boost::swap(array1, array2);
|
||||||
|
|
||||||
for (std::size_t i = 0; i < number_of_elements; ++i)
|
for (std::size_t i = 0; i < number_of_elements; ++i)
|
||||||
@@ -87,14 +64,6 @@ void test_swapping_2D_arrays()
|
|||||||
|
|
||||||
BOOST_CHECK_EQUAL(swap_test_class::swap_count(), number_of_elements);
|
BOOST_CHECK_EQUAL(swap_test_class::swap_count(), number_of_elements);
|
||||||
BOOST_CHECK_EQUAL(swap_test_class::copy_count(), 0);
|
BOOST_CHECK_EQUAL(swap_test_class::copy_count(), 0);
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
int test_main(int, char*[])
|
|
||||||
{
|
|
||||||
test_swapping_1D_arrays();
|
|
||||||
test_swapping_2D_arrays();
|
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
42
swap/test/array_of_array_of_int.cpp
Normal file
42
swap/test/array_of_array_of_int.cpp
Normal file
@@ -0,0 +1,42 @@
|
|||||||
|
// 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)
|
||||||
|
|
||||||
|
// Tests swapping an array of arrays of integers by means of boost::swap.
|
||||||
|
|
||||||
|
#include <boost/utility/swap.hpp>
|
||||||
|
#define BOOST_INCLUDE_MAIN
|
||||||
|
#include <boost/test/test_tools.hpp>
|
||||||
|
|
||||||
|
#include <algorithm> //for std::copy and std::equal
|
||||||
|
#include <cstddef> //for std::size_t
|
||||||
|
|
||||||
|
int test_main(int, char*[])
|
||||||
|
{
|
||||||
|
const std::size_t first_dimension = 3;
|
||||||
|
const std::size_t second_dimension = 4;
|
||||||
|
const std::size_t number_of_elements = first_dimension * second_dimension;
|
||||||
|
|
||||||
|
int array1[first_dimension][second_dimension];
|
||||||
|
int array2[first_dimension][second_dimension];
|
||||||
|
|
||||||
|
int* const ptr1 = array1[0];
|
||||||
|
int* const ptr2 = array2[0];
|
||||||
|
|
||||||
|
for (std::size_t i = 0; i < number_of_elements; ++i)
|
||||||
|
{
|
||||||
|
ptr1[i] = static_cast<int>(i);
|
||||||
|
ptr2[i] = static_cast<int>(i + number_of_elements);
|
||||||
|
}
|
||||||
|
|
||||||
|
boost::swap(array1, array2);
|
||||||
|
|
||||||
|
for (std::size_t i = 0; i < number_of_elements; ++i)
|
||||||
|
{
|
||||||
|
BOOST_CHECK_EQUAL(ptr1[i], static_cast<int>(i + number_of_elements) );
|
||||||
|
BOOST_CHECK_EQUAL(ptr2[i], static_cast<int>(i) );
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
61
swap/test/array_of_class.cpp
Normal file
61
swap/test/array_of_class.cpp
Normal file
@@ -0,0 +1,61 @@
|
|||||||
|
// 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)
|
||||||
|
|
||||||
|
// Tests swapping an array of arrays of swap_test_class objects by means of boost::swap.
|
||||||
|
|
||||||
|
#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"
|
||||||
|
|
||||||
|
#include <algorithm> //for std::copy and std::equal
|
||||||
|
#include <cstddef> //for std::size_t
|
||||||
|
|
||||||
|
//Provide swap function in both the namespace of swap_test_class
|
||||||
|
//(which is the global namespace), and the std namespace.
|
||||||
|
//It's common to provide a swap function for a class in both
|
||||||
|
//namespaces. Scott Meyers recommends doing so: Effective C++,
|
||||||
|
//Third Edition, item 25, "Consider support for a non-throwing swap".
|
||||||
|
void swap(swap_test_class& left, swap_test_class& right)
|
||||||
|
{
|
||||||
|
left.swap(right);
|
||||||
|
}
|
||||||
|
|
||||||
|
namespace std
|
||||||
|
{
|
||||||
|
template <>
|
||||||
|
void swap(swap_test_class& left, swap_test_class& right)
|
||||||
|
{
|
||||||
|
left.swap(right);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
int test_main(int, char*[])
|
||||||
|
{
|
||||||
|
const std::size_t array_size = 2;
|
||||||
|
const swap_test_class initial_array1[array_size] = { swap_test_class(1), swap_test_class(2) };
|
||||||
|
const swap_test_class initial_array2[array_size] = { swap_test_class(3), swap_test_class(4) };
|
||||||
|
|
||||||
|
swap_test_class array1[array_size];
|
||||||
|
swap_test_class array2[array_size];
|
||||||
|
|
||||||
|
std::copy(initial_array1, initial_array1 + array_size, array1);
|
||||||
|
std::copy(initial_array2, initial_array2 + array_size, array2);
|
||||||
|
|
||||||
|
swap_test_class::reset();
|
||||||
|
boost::swap(array1, array2);
|
||||||
|
|
||||||
|
BOOST_CHECK(std::equal(array1, array1 + array_size, initial_array2));
|
||||||
|
BOOST_CHECK(std::equal(array2, array2 + array_size, initial_array1));
|
||||||
|
|
||||||
|
BOOST_CHECK_EQUAL(swap_test_class::swap_count(), array_size);
|
||||||
|
BOOST_CHECK_EQUAL(swap_test_class::copy_count(), 0);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
35
swap/test/array_of_int.cpp
Normal file
35
swap/test/array_of_int.cpp
Normal file
@@ -0,0 +1,35 @@
|
|||||||
|
// 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)
|
||||||
|
|
||||||
|
// Tests swapping an array of integers by means of boost::swap.
|
||||||
|
|
||||||
|
#include <boost/utility/swap.hpp>
|
||||||
|
#define BOOST_INCLUDE_MAIN
|
||||||
|
#include <boost/test/test_tools.hpp>
|
||||||
|
|
||||||
|
#include <algorithm> //for std::copy and std::equal
|
||||||
|
#include <cstddef> //for std::size_t
|
||||||
|
|
||||||
|
|
||||||
|
int test_main(int, char*[])
|
||||||
|
{
|
||||||
|
const std::size_t array_size = 3;
|
||||||
|
const int initial_array1[array_size] = { 1, 2, 3 };
|
||||||
|
const int initial_array2[array_size] = { 4, 5, 6 };
|
||||||
|
|
||||||
|
int array1[array_size];
|
||||||
|
int array2[array_size];
|
||||||
|
|
||||||
|
std::copy(initial_array1, initial_array1 + array_size, array1);
|
||||||
|
std::copy(initial_array2, initial_array2 + array_size, array2);
|
||||||
|
|
||||||
|
boost::swap(array1, array2);
|
||||||
|
|
||||||
|
BOOST_CHECK(std::equal(array1, array1 + array_size, initial_array2));
|
||||||
|
BOOST_CHECK(std::equal(array2, array2 + array_size, initial_array1));
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
71
swap/test/array_of_template.cpp
Normal file
71
swap/test/array_of_template.cpp
Normal file
@@ -0,0 +1,71 @@
|
|||||||
|
// 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)
|
||||||
|
|
||||||
|
// Tests swapping an array of swap_test_template<int> objects by means of boost::swap.
|
||||||
|
|
||||||
|
#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"
|
||||||
|
|
||||||
|
#include <algorithm> //for std::copy and std::equal
|
||||||
|
#include <cstddef> //for std::size_t
|
||||||
|
|
||||||
|
template <class T>
|
||||||
|
class swap_test_template
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
typedef T template_argument;
|
||||||
|
swap_test_class swap_test_object;
|
||||||
|
};
|
||||||
|
|
||||||
|
template <class T>
|
||||||
|
inline bool operator==(const swap_test_template<T> & lhs, const swap_test_template<T> & rhs)
|
||||||
|
{
|
||||||
|
return lhs.swap_test_object == rhs.swap_test_object;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <class T>
|
||||||
|
inline bool operator!=(const swap_test_template<T> & lhs, const swap_test_template<T> & rhs)
|
||||||
|
{
|
||||||
|
return !(lhs == rhs);
|
||||||
|
}
|
||||||
|
|
||||||
|
//Provide swap function in the namespace of swap_test_template
|
||||||
|
//(which is the global namespace). Note that it isn't allowed to put
|
||||||
|
//an overload of this function within the std namespace.
|
||||||
|
template <class T>
|
||||||
|
void swap(swap_test_template<T>& left, swap_test_template<T>& right)
|
||||||
|
{
|
||||||
|
left.swap_test_object.swap(right.swap_test_object);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
int test_main(int, char*[])
|
||||||
|
{
|
||||||
|
const std::size_t array_size = 2;
|
||||||
|
const swap_test_template<int> initial_array1[array_size] = { swap_test_class(1), swap_test_class(2) };
|
||||||
|
const swap_test_template<int> initial_array2[array_size] = { swap_test_class(3), swap_test_class(4) };
|
||||||
|
|
||||||
|
swap_test_template<int> array1[array_size];
|
||||||
|
swap_test_template<int> array2[array_size];
|
||||||
|
|
||||||
|
std::copy(initial_array1, initial_array1 + array_size, array1);
|
||||||
|
std::copy(initial_array2, initial_array2 + array_size, array2);
|
||||||
|
|
||||||
|
swap_test_class::reset();
|
||||||
|
boost::swap(array1, array2);
|
||||||
|
|
||||||
|
BOOST_CHECK(std::equal(array1, array1 + array_size, initial_array2));
|
||||||
|
BOOST_CHECK(std::equal(array2, array2 + array_size, initial_array1));
|
||||||
|
|
||||||
|
BOOST_CHECK_EQUAL(swap_test_class::swap_count(), array_size);
|
||||||
|
BOOST_CHECK_EQUAL(swap_test_class::copy_count(), 0);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
38
test/CMakeLists.txt
Normal file
38
test/CMakeLists.txt
Normal file
@@ -0,0 +1,38 @@
|
|||||||
|
boost_additional_test_dependencies(utility BOOST_DEPENDS test)
|
||||||
|
|
||||||
|
boost_test_run(addressof_test ../addressof_test.cpp)
|
||||||
|
boost_test_run(assert_test ../assert_test.cpp)
|
||||||
|
boost_test_run(base_from_member_test ../base_from_member_test.cpp)
|
||||||
|
boost_test_run(binary_search_test ../binary_search_test.cpp)
|
||||||
|
boost_test_run(call_traits_test ../call_traits_test.cpp ARGS -u)
|
||||||
|
boost_test_compile_fail(checked_delete_test ../checked_delete_test.cpp)
|
||||||
|
boost_test_run(compressed_pair_test
|
||||||
|
../compressed_pair_test
|
||||||
|
DEPENDS boost_test_exec_monitor)
|
||||||
|
boost_test_run(current_function_test ../current_function_test.cpp)
|
||||||
|
boost_test_run(iterators_test
|
||||||
|
../iterators_test.cpp
|
||||||
|
DEPENDS boost_test_exec_monitor)
|
||||||
|
boost_test_run(next_prior_test DEPENDS boost_test_exec_monitor)
|
||||||
|
boost_test_compile_fail(noncopyable_test ../noncopyable_test.cpp)
|
||||||
|
boost_test_run(numeric_traits_test ../numeric_traits_test.cpp)
|
||||||
|
if (${CMAKE_SYSTEM} MATCHES "FreeBSD-.*")
|
||||||
|
boost_test_compile_fail("operators_test_compilerbug")
|
||||||
|
elseif(${CMAKE_SYSTEM} MATCHES "FreeBSD-.*")
|
||||||
|
boost_test_run(operators_test
|
||||||
|
../operators_test.cpp
|
||||||
|
DEPENDS boost_test_exec_monitor)
|
||||||
|
endif(${CMAKE_SYSTEM} MATCHES "FreeBSD-.*")
|
||||||
|
boost_test_compile(ref_ct_test ../ref_ct_test.cpp)
|
||||||
|
boost_test_run(ref_test
|
||||||
|
../ref_test.cpp
|
||||||
|
DEPENDS boost_test_exec_monitor)
|
||||||
|
boost_test_compile(result_of_test)
|
||||||
|
boost_test_run(shared_iterator_test ../shared_iterator_test.cpp)
|
||||||
|
boost_test_run(value_init_test ../value_init_test.cpp)
|
||||||
|
boost_test_compile_fail(value_init_test_fail1
|
||||||
|
../value_init_test_fail1.cpp)
|
||||||
|
boost_test_compile_fail(value_init_test_fail2
|
||||||
|
../value_init_test_fail2.cpp)
|
||||||
|
boost_test_compile_fail(value_init_test_fail3
|
||||||
|
../value_init_test_fail3.cpp)
|
@@ -1,58 +1,15 @@
|
|||||||
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
|
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
|
||||||
<html>
|
<html>
|
||||||
<head>
|
<head>
|
||||||
<title>Boost: throw_exception.hpp documentation</title>
|
<meta http-equiv=refresh content="0; URL=../exception/doc/throw_exception.html">
|
||||||
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
|
<title>Automatic redirection</title>
|
||||||
</head>
|
</head>
|
||||||
<body bgcolor="white" style="MARGIN-LEFT: 5%; MARGIN-RIGHT: 5%">
|
<body>
|
||||||
<table border="0" width="100%">
|
Automatic redirection failed, please go to
|
||||||
<tr>
|
<a href="../exception/doc/throw_exception.html">throw_exception.html</a>. <hr>
|
||||||
<td width="277"><A href="../../index.htm"> <img src="../../boost.png" alt="boost.png (6897 bytes)" width="277" height="86" border="0"></A>
|
<p><EFBFBD> Copyright Beman Dawes, 2001</p>
|
||||||
</td>
|
<p>Distributed under the Boost Software License, Version 1.0. (See accompanying
|
||||||
<td align="center">
|
file <a href="../../LICENSE_1_0.txt">LICENSE_1_0.txt</a> or copy
|
||||||
<h1>throw_exception.hpp</h1>
|
at <a href="http://www.boost.org/LICENSE_1_0.txt">www.boost.org/LICENSE_1_0.txt</a>)</p>
|
||||||
</td>
|
</body>
|
||||||
</tr>
|
|
||||||
<tr>
|
|
||||||
<td colspan="2" height="64"> </td>
|
|
||||||
</tr>
|
|
||||||
</table>
|
|
||||||
<p>
|
|
||||||
The header <STRONG><boost/throw_exception.hpp></STRONG> defines the
|
|
||||||
helper function <STRONG>boost::throw_exception</STRONG>. It is intended to be
|
|
||||||
used in Boost libraries that need to throw exceptions, but support
|
|
||||||
configurations and platforms where exceptions aren't available, as indicated by
|
|
||||||
the presence of the <STRONG>BOOST_NO_EXCEPTIONS</STRONG> <A href="../config/config.htm#macro_ref">
|
|
||||||
configuration macro</A>.
|
|
||||||
</p>
|
|
||||||
<P>When <STRONG>BOOST_NO_EXCEPTIONS</STRONG> is not defined, <tt>boost::throw_exception(e)</tt>
|
|
||||||
is equivalent to <tt>throw e</tt>. Otherwise, the function is left undefined,
|
|
||||||
and the user is expected to supply an appropriate definition. Callers of <tt>throw_exception</tt>
|
|
||||||
are allowed to assume that the function never returns; therefore, if the
|
|
||||||
user-defined <tt>throw_exception</tt> returns, the behavior is undefined.</P>
|
|
||||||
<h3><a name="Synopsis">Synopsis</a></h3>
|
|
||||||
<pre>
|
|
||||||
namespace boost
|
|
||||||
{
|
|
||||||
|
|
||||||
#ifdef BOOST_NO_EXCEPTIONS
|
|
||||||
|
|
||||||
void throw_exception(std::exception const & e); // user defined
|
|
||||||
|
|
||||||
#else
|
|
||||||
|
|
||||||
template<class E> void throw_exception(E const & e)
|
|
||||||
{
|
|
||||||
throw e;
|
|
||||||
}
|
|
||||||
|
|
||||||
#endif
|
|
||||||
|
|
||||||
}
|
|
||||||
</pre>
|
|
||||||
<p><br>
|
|
||||||
<small>Copyright <20> 2002 by Peter Dimov. Distributed under the Boost Software License, Version
|
|
||||||
1.0. See accompanying file <A href="../../LICENSE_1_0.txt">LICENSE_1_0.txt</A> or
|
|
||||||
copy at <A href="http://www.boost.org/LICENSE_1_0.txt">http://www.boost.org/LICENSE_1_0.txt</A>.</small></p>
|
|
||||||
</body>
|
|
||||||
</html>
|
</html>
|
||||||
|
@@ -253,7 +253,7 @@ its internal data, prior to constructing the object that it contains.
|
|||||||
|
|
||||||
<h2><a name="val_init"><code>template class value_initialized<T></code></a></h2>
|
<h2><a name="val_init"><code>template class value_initialized<T></code></a></h2>
|
||||||
|
|
||||||
<pre>namespace boost {<br><br>template<class T><br>class value_initialized<br>{<br> public :<br> value_initialized() : x() {}<br> operator T&() const { return x ; }<br> T& data() const { return x ; }<br><br> private :<br> <i>unspecified</i> x ;<br>} ;<br><br>template<class T><br>T const& get ( value_initialized<T> const& x )<br>{<br> return x.data() ;<br>}<br><br>template<class T><br>T& get ( value_initialized<T>& x )<br>{<br> return x.data() ;<br>}<br><br>} // namespace boost<br></pre>
|
<pre>namespace boost {<br><br>template<class T><br>class value_initialized<br>{<br> public :<br> value_initialized() : x() {}<br> operator T&() const { return x ; }<br> T& data() const { return x ; }<br> void swap( value_initialized<T>& );<br><br> private :<br> <i>unspecified</i> x ;<br>} ;<br><br>template<class T><br>T const& get ( value_initialized<T> const& x )<br>{<br> return x.data() ;<br>}<br><br>template<class T><br>T& get ( value_initialized<T>& x )<br>{<br> return x.data() ;<br>}<br><br>} // namespace boost<br></pre>
|
||||||
|
|
||||||
<p>An object of this template class is a <code>T</code>-wrapper convertible
|
<p>An object of this template class is a <code>T</code>-wrapper convertible
|
||||||
to <code>'T&'</code> whose wrapped object (data member of type <code>T</code>)
|
to <code>'T&'</code> whose wrapped object (data member of type <code>T</code>)
|
||||||
@@ -276,6 +276,10 @@ non-member function <code>get()</code>: </p>
|
|||||||
<p>Both <code>const</code> and non-<code>const</code> objects can be wrapped.
|
<p>Both <code>const</code> and non-<code>const</code> objects can be wrapped.
|
||||||
Mutable objects can be modified directly from within the wrapper but constant
|
Mutable objects can be modified directly from within the wrapper but constant
|
||||||
objects cannot:</p>
|
objects cannot:</p>
|
||||||
|
|
||||||
|
<p>When <code>T</code> is a <em>Swappable</em> type, <code>value_initialized<T></code>
|
||||||
|
is swappable as well, by calling its <code>swap</code> member function
|
||||||
|
as well as by calling <code>boost::swap</code>.</p>
|
||||||
|
|
||||||
<pre>value_initialized<int> x ; <br>static_cast<int&>(x) = 1 ; // OK<br>get(x) = 1 ; // OK<br><br>value_initialized<int const> y ; <br>static_cast<int&>(y) = 1 ; // ERROR: cannot cast to int&<br>static_cast<int const&>(y) = 1 ; // ERROR: cannot modify a const value<br>get(y) = 1 ; // ERROR: cannot modify a const value</pre>
|
<pre>value_initialized<int> x ; <br>static_cast<int&>(x) = 1 ; // OK<br>get(x) = 1 ; // OK<br><br>value_initialized<int const> y ; <br>static_cast<int&>(y) = 1 ; // ERROR: cannot cast to int&<br>static_cast<int const&>(y) = 1 ; // ERROR: cannot modify a const value<br>get(y) = 1 ; // ERROR: cannot modify a const value</pre>
|
||||||
|
|
||||||
@@ -311,7 +315,7 @@ wrapped object from within a constant wrapper can be avoided if access to
|
|||||||
the wrapped object is always performed with the <code>get()</code> idiom:</p>
|
the wrapped object is always performed with the <code>get()</code> idiom:</p>
|
||||||
|
|
||||||
<pre>value_initialized<int> x ;<br>get(x) = 1 ; // OK<br><br>value_initialized<int const> cx ;<br>get(x) = 1 ; // ERROR: Cannot modify a const object<br><br>value_initialized<int> const x_c ;<br>get(x_c) = 1 ; // ERROR: Cannot modify a const object<br><br>value_initialized<int const> const cx_c ;<br>get(cx_c) = 1 ; // ERROR: Cannot modify a const object<br></pre>
|
<pre>value_initialized<int> x ;<br>get(x) = 1 ; // OK<br><br>value_initialized<int const> cx ;<br>get(x) = 1 ; // ERROR: Cannot modify a const object<br><br>value_initialized<int> const x_c ;<br>get(x_c) = 1 ; // ERROR: Cannot modify a const object<br><br>value_initialized<int const> const cx_c ;<br>get(cx_c) = 1 ; // ERROR: Cannot modify a const object<br></pre>
|
||||||
|
|
||||||
<h2><a name="initialized_value"><code>initialized_value</code></a></h2>
|
<h2><a name="initialized_value"><code>initialized_value</code></a></h2>
|
||||||
|
|
||||||
<pre>
|
<pre>
|
||||||
@@ -379,7 +383,7 @@ for Boost release version 1.35 (2008), offering a workaround to various compiler
|
|||||||
</p>
|
</p>
|
||||||
|
|
||||||
<hr>
|
<hr>
|
||||||
<p>Revised 23 May 2008</p>
|
<p>Revised 28 August 2008</p>
|
||||||
|
|
||||||
<p>© Copyright Fernando Cacciola, 2002, 2008.</p>
|
<p>© Copyright Fernando Cacciola, 2002, 2008.</p>
|
||||||
|
|
||||||
@@ -390,4 +394,4 @@ for Boost release version 1.35 (2008), offering a workaround to various compiler
|
|||||||
<br>
|
<br>
|
||||||
|
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
@@ -9,6 +9,7 @@
|
|||||||
// 21 Ago 2002 (Created) Fernando Cacciola
|
// 21 Ago 2002 (Created) Fernando Cacciola
|
||||||
// 15 Jan 2008 (Added tests regarding compiler issues) Fernando Cacciola, Niels Dekker
|
// 15 Jan 2008 (Added tests regarding compiler issues) Fernando Cacciola, Niels Dekker
|
||||||
// 23 May 2008 (Added tests regarding initialized_value) Niels Dekker
|
// 23 May 2008 (Added tests regarding initialized_value) Niels Dekker
|
||||||
|
// 21 Ago 2008 (Added swap test) Niels Dekker
|
||||||
|
|
||||||
#include <cstring> // For memcmp.
|
#include <cstring> // For memcmp.
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
@@ -181,6 +182,35 @@ struct CopyFunctionCallTester
|
|||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
//
|
||||||
|
// A struct that allows testing whether its customized swap function is called.
|
||||||
|
//
|
||||||
|
struct SwapFunctionCallTester
|
||||||
|
{
|
||||||
|
bool is_custom_swap_called;
|
||||||
|
int data;
|
||||||
|
|
||||||
|
SwapFunctionCallTester()
|
||||||
|
: is_custom_swap_called(false), data(0) {}
|
||||||
|
|
||||||
|
SwapFunctionCallTester(const SwapFunctionCallTester & arg)
|
||||||
|
: is_custom_swap_called(false), data(arg.data) {}
|
||||||
|
|
||||||
|
void swap(SwapFunctionCallTester & arg)
|
||||||
|
{
|
||||||
|
std::swap(data, arg.data);
|
||||||
|
is_custom_swap_called = true;
|
||||||
|
arg.is_custom_swap_called = true;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
void swap(SwapFunctionCallTester & lhs, SwapFunctionCallTester & rhs)
|
||||||
|
{
|
||||||
|
lhs.swap(rhs);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
template<class T>
|
template<class T>
|
||||||
void check_initialized_value ( T const& y )
|
void check_initialized_value ( T const& y )
|
||||||
{
|
{
|
||||||
@@ -196,7 +226,7 @@ void check_initialized_value( NonPOD const& )
|
|||||||
// and this type (NonPOD), because the following statement
|
// and this type (NonPOD), because the following statement
|
||||||
// won't compile on this particular compiler version:
|
// won't compile on this particular compiler version:
|
||||||
// NonPOD initializedValue = boost::initialized_value() ;
|
// NonPOD initializedValue = boost::initialized_value() ;
|
||||||
//
|
//
|
||||||
// This is caused by a compiler bug, that is fixed with a newer version
|
// This is caused by a compiler bug, that is fixed with a newer version
|
||||||
// of the Borland compiler. The Release Notes for Delphi(R) 2007 for
|
// of the Borland compiler. The Release Notes for Delphi(R) 2007 for
|
||||||
// Win32(R) and C++Builder(R) 2007 (http://dn.codegear.com/article/36575)
|
// Win32(R) and C++Builder(R) 2007 (http://dn.codegear.com/article/36575)
|
||||||
@@ -323,9 +353,20 @@ int test_main(int, char **)
|
|||||||
BOOST_CHECK ( ! get(copyFunctionCallTester3).is_copy_constructed);
|
BOOST_CHECK ( ! get(copyFunctionCallTester3).is_copy_constructed);
|
||||||
BOOST_CHECK ( get(copyFunctionCallTester3).is_assignment_called);
|
BOOST_CHECK ( get(copyFunctionCallTester3).is_assignment_called);
|
||||||
|
|
||||||
|
boost::value_initialized<SwapFunctionCallTester> swapFunctionCallTester1;
|
||||||
|
boost::value_initialized<SwapFunctionCallTester> swapFunctionCallTester2;
|
||||||
|
get(swapFunctionCallTester1).data = 1;
|
||||||
|
get(swapFunctionCallTester2).data = 2;
|
||||||
|
boost::swap(swapFunctionCallTester1, swapFunctionCallTester2);
|
||||||
|
BOOST_CHECK( get(swapFunctionCallTester1).data == 2 );
|
||||||
|
BOOST_CHECK( get(swapFunctionCallTester2).data == 1 );
|
||||||
|
BOOST_CHECK( get(swapFunctionCallTester1).is_custom_swap_called );
|
||||||
|
BOOST_CHECK( get(swapFunctionCallTester2).is_custom_swap_called );
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
unsigned int expected_failures = 0;
|
unsigned int expected_failures = 0;
|
||||||
|
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user