From 6b6e1c3252e018d13e51ef57d7a597bcd7b7ce3f Mon Sep 17 00:00:00 2001 From: Niels Dekker Date: Thu, 28 Aug 2008 19:00:20 +0000 Subject: [PATCH] Added value_initialized::swap documentation + test [SVN r48425] --- value_init.htm | 8 ++++++-- value_init_test.cpp | 41 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+), 2 deletions(-) diff --git a/value_init.htm b/value_init.htm index 6bbfcbc..5c1b20e 100644 --- a/value_init.htm +++ b/value_init.htm @@ -253,7 +253,7 @@ its internal data, prior to constructing the object that it contains.

template class value_initialized<T>

-
namespace boost {

template<class T>
class value_initialized
{
public :
value_initialized() : x() {}
operator T&() const { return x ; }
T& data() const { return x ; }

private :
unspecified x ;
} ;

template<class T>
T const& get ( value_initialized<T> const& x )
{
return x.data() ;
}

template<class T>
T& get ( value_initialized<T>& x )
{
return x.data() ;
}

} // namespace boost
+
namespace boost {

template<class T>
class value_initialized
{
public :
value_initialized() : x() {}
operator T&() const { return x ; }
T& data() const { return x ; }
void swap( value_initialized<T>& );

private :
unspecified x ;
} ;

template<class T>
T const& get ( value_initialized<T> const& x )
{
return x.data() ;
}

template<class T>
T& get ( value_initialized<T>& x )
{
return x.data() ;
}

} // namespace boost

An object of this template class is a T-wrapper convertible to 'T&' whose wrapped object (data member of type T) @@ -276,6 +276,10 @@ non-member function get():

Both const and non-const objects can be wrapped. Mutable objects can be modified directly from within the wrapper but constant objects cannot:

+ +

When T is a Swappable type, value_initialized<T> + is swappable as well, by calling its swap member function + as well as by calling boost::swap.

value_initialized<int> x ; 
static_cast<int&>(x) = 1 ; // OK
get(x) = 1 ; // OK

value_initialized<int const> y ;
static_cast<int&>(y) = 1 ; // ERROR: cannot cast to int&
static_cast<int const&>(y) = 1 ; // ERROR: cannot modify a const value
get(y) = 1 ; // ERROR: cannot modify a const value
@@ -379,7 +383,7 @@ for Boost release version 1.35 (2008), offering a workaround to various compiler


-

Revised 23 May 2008

+

Revised 28 August 2008

© Copyright Fernando Cacciola, 2002, 2008.

diff --git a/value_init_test.cpp b/value_init_test.cpp index 7b07b22..63f324d 100644 --- a/value_init_test.cpp +++ b/value_init_test.cpp @@ -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 @@ 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 void check_initialized_value ( T const& y ) { @@ -323,9 +353,20 @@ int test_main(int, char **) 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; +