From f4612715e53449f94a62562b402f5d47fb9b1e02 Mon Sep 17 00:00:00 2001
From: Eric Niebler
Date: Fri, 5 Sep 2008 04:13:30 +0000
Subject: [PATCH] merged from trunk
[SVN r48605]
---
include/boost/exception.hpp | 4 +--
include/boost/utility/value_init.hpp | 13 +++++++++
value_init.htm | 8 ++++--
value_init_test.cpp | 41 ++++++++++++++++++++++++++++
4 files changed, 61 insertions(+), 5 deletions(-)
diff --git a/include/boost/exception.hpp b/include/boost/exception.hpp
index d805002..c77f215 100644
--- a/include/boost/exception.hpp
+++ b/include/boost/exception.hpp
@@ -7,13 +7,11 @@
#define UUID_1D94A7C6054E11DB9804B622A1EF5492
#include
-#include
-#include
#include
#include
+#include
#include
#include
#include
-#include
#endif
diff --git a/include/boost/utility/value_init.hpp b/include/boost/utility/value_init.hpp
index 67127c0..aa7ecb4 100644
--- a/include/boost/utility/value_init.hpp
+++ b/include/boost/utility/value_init.hpp
@@ -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 @@ class value_initialized
return wrapper_address()->data;
}
+ void swap(value_initialized & arg)
+ {
+ ::boost::swap( this->data(), arg.data() );
+ }
+
operator T&() const { return this->data(); }
} ;
@@ -110,6 +117,12 @@ T& get ( value_initialized& x )
return x.data() ;
}
+template
+void swap ( value_initialized & lhs, value_initialized & rhs )
+{
+ lhs.swap(rhs) ;
+}
+
class initialized_value_t
{
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.
-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;
+