Ticket #2243: swap_value_initialized.patch
File swap_value_initialized.patch, 3.3 KB (added by , 14 years ago) |
---|
-
boost/utility/value_init.hpp
7 7 // 21 Ago 2002 (Created) Fernando Cacciola 8 8 // 24 Dec 2007 (Refactored and worked around various compiler bugs) Fernando Cacciola, Niels Dekker 9 9 // 23 May 2008 (Fixed operator= const issue, added initialized_value) Niels Dekker, Fernando Cacciola 10 // 21 Ago 2008 (Added swap) Niels Dekker, Fernando Cacciola 10 11 // 11 12 #ifndef BOOST_UTILITY_VALUE_INIT_21AGO2002_HPP 12 13 #define BOOST_UTILITY_VALUE_INIT_21AGO2002_HPP … … 22 23 #include <boost/static_assert.hpp> 23 24 #include <boost/type_traits/cv_traits.hpp> 24 25 #include <boost/type_traits/alignment_of.hpp> 26 #include <boost/swap.hpp> 25 27 #include <cstring> 26 28 #include <new> 27 29 … … 93 95 return wrapper_address()->data; 94 96 } 95 97 98 void swap(value_initialized & arg) 99 { 100 ::boost::swap( this->data(), arg.data() ); 101 } 102 96 103 operator T&() const { return this->data(); } 97 104 98 105 } ; … … 110 117 return x.data() ; 111 118 } 112 119 120 template<class T> 121 void swap ( value_initialized<T> & lhs, value_initialized<T> & rhs ) 122 { 123 lhs.swap(rhs) ; 124 } 113 125 126 114 127 class initialized_value_t 115 128 { 116 129 public : -
libs/utility/value_init_test.cpp
9 9 // 21 Ago 2002 (Created) Fernando Cacciola 10 10 // 15 Jan 2008 (Added tests regarding compiler issues) Fernando Cacciola, Niels Dekker 11 11 // 23 May 2008 (Added tests regarding initialized_value) Niels Dekker 12 // 21 Ago 2008 (Added swap test) Niels Dekker 12 13 13 14 #include <cstring> // For memcmp. 14 15 #include <iostream> … … 181 182 }; 182 183 183 184 185 // 186 // A struct that allows testing whether its customized swap function is called. 187 // 188 struct SwapFunctionCallTester 189 { 190 bool is_custom_swap_called; 191 int data; 192 193 SwapFunctionCallTester() 194 : is_custom_swap_called(false), data(0) {} 195 196 SwapFunctionCallTester(const SwapFunctionCallTester & arg) 197 : is_custom_swap_called(false), data(arg.data) {} 198 199 void swap(SwapFunctionCallTester & arg) 200 { 201 std::swap(data, arg.data); 202 is_custom_swap_called = true; 203 arg.is_custom_swap_called = true; 204 } 205 }; 206 207 void swap(SwapFunctionCallTester & lhs, SwapFunctionCallTester & rhs) 208 { 209 lhs.swap(rhs); 210 } 211 212 213 184 214 template<class T> 185 215 void check_initialized_value ( T const& y ) 186 216 { … … 323 353 BOOST_CHECK ( ! get(copyFunctionCallTester3).is_copy_constructed); 324 354 BOOST_CHECK ( get(copyFunctionCallTester3).is_assignment_called); 325 355 356 boost::value_initialized<SwapFunctionCallTester> swapFunctionCallTester1; 357 boost::value_initialized<SwapFunctionCallTester> swapFunctionCallTester2; 358 get(swapFunctionCallTester1).data = 1; 359 get(swapFunctionCallTester2).data = 2; 360 boost::swap(swapFunctionCallTester1, swapFunctionCallTester2); 361 BOOST_CHECK( get(swapFunctionCallTester1).data == 2 ); 362 BOOST_CHECK( get(swapFunctionCallTester2).data == 1 ); 363 BOOST_CHECK( get(swapFunctionCallTester1).is_custom_swap_called ); 364 BOOST_CHECK( get(swapFunctionCallTester2).is_custom_swap_called ); 365 326 366 return 0; 327 367 } 328 368 329 369 330 370 unsigned int expected_failures = 0; 331 371 372