mirror of
https://github.com/boostorg/utility.git
synced 2025-05-09 15:04:00 +00:00
Merged value_initialized::swap from trunk [48424] and [48425], according to ticket #2243, as was agreed with Fernando Cacciola.
[SVN r49967]
This commit is contained in:
parent
633832e872
commit
9e73b2c6ae
@ -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
|
||||||
{
|
{
|
||||||
|
@ -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;
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user