Index: boost/utility/value_init.hpp =================================================================== --- boost/utility/value_init.hpp (revision 48283) +++ boost/utility/value_init.hpp (working copy) @@ -7,6 +7,7 @@ // 21 Ago 2002 (Created) Fernando Cacciola // 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 +// 21 Ago 2008 (Added swap) Niels Dekker, Fernando Cacciola // #ifndef BOOST_UTILITY_VALUE_INIT_21AGO2002_HPP #define BOOST_UTILITY_VALUE_INIT_21AGO2002_HPP @@ -22,6 +23,7 @@ #include #include #include +#include #include #include @@ -93,6 +95,11 @@ return wrapper_address()->data; } + void swap(value_initialized & arg) + { + ::boost::swap( this->data(), arg.data() ); + } + operator T&() const { return this->data(); } } ; @@ -110,7 +117,13 @@ return x.data() ; } +template +void swap ( value_initialized & lhs, value_initialized & rhs ) +{ + lhs.swap(rhs) ; +} + class initialized_value_t { public : Index: libs/utility/value_init_test.cpp =================================================================== --- libs/utility/value_init_test.cpp (revision 48283) +++ libs/utility/value_init_test.cpp (working copy) @@ -9,6 +9,7 @@ // 21 Ago 2002 (Created) Fernando Cacciola // 15 Jan 2008 (Added tests regarding compiler issues) Fernando Cacciola, Niels Dekker // 23 May 2008 (Added tests regarding initialized_value) Niels Dekker +// 21 Ago 2008 (Added swap test) Niels Dekker #include // For memcmp. #include @@ -181,6 +182,35 @@ }; +// +// 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 void check_initialized_value ( T const& y ) { @@ -323,9 +353,20 @@ BOOST_CHECK ( ! get(copyFunctionCallTester3).is_copy_constructed); BOOST_CHECK ( get(copyFunctionCallTester3).is_assignment_called); + boost::value_initialized swapFunctionCallTester1; + boost::value_initialized 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; } unsigned int expected_failures = 0; +