Boost logo

Boost-Commit :

From: nielsdekker_at_[hidden]
Date: 2008-08-28 15:00:21


Author: niels_dekker
Date: 2008-08-28 15:00:20 EDT (Thu, 28 Aug 2008)
New Revision: 48425
URL: http://svn.boost.org/trac/boost/changeset/48425

Log:
Added value_initialized::swap documentation + test
Text files modified:
   trunk/libs/utility/value_init.htm | 8 +++++-
   trunk/libs/utility/value_init_test.cpp | 41 ++++++++++++++++++++++++++++++++++++++++
   2 files changed, 47 insertions(+), 2 deletions(-)

Modified: trunk/libs/utility/value_init.htm
==============================================================================
--- trunk/libs/utility/value_init.htm (original)
+++ trunk/libs/utility/value_init.htm 2008-08-28 15:00:20 EDT (Thu, 28 Aug 2008)
@@ -253,7 +253,7 @@
                    
 <h2><a name="val_init"><code>template class value_initialized&lt;T&gt;</code></a></h2>
                    
-<pre>namespace boost {<br><br>template&lt;class T&gt;<br>class value_initialized<br>{<br> public :<br> value_initialized() : x() {}<br> operator T&amp;() const { return x ; }<br> T&amp; data() const { return x ; }<br><br> private :<br> <i>unspecified</i> x ;<br>} ;<br><br>template&lt;class T&gt;<br>T const&amp; get ( value_initialized&lt;T&gt; const&amp; x )<br>{<br> return x.data() ;<br>}<br><br>template&lt;class T&gt;<br>T&amp; get ( value_initialized&lt;T&gt;&amp; x )<br>{<br> return x.data() ;<br>}<br><br>} // namespace boost<br></pre>
+<pre>namespace boost {<br><br>template&lt;class T&gt;<br>class value_initialized<br>{<br> public :<br> value_initialized() : x() {}<br> operator T&amp;() const { return x ; }<br> T&amp; data() const { return x ; }<br> void swap( value_initialized&lt;T&gt;&amp; );<br><br> private :<br> <i>unspecified</i> x ;<br>} ;<br><br>template&lt;class T&gt;<br>T const&amp; get ( value_initialized&lt;T&gt; const&amp; x )<br>{<br> return x.data() ;<br>}<br><br>template&lt;class T&gt;<br>T&amp; get ( value_initialized&lt;T&gt;&amp; 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
     to <code>'T&amp;'</code> whose wrapped object (data member of type <code>T</code>)
@@ -276,6 +276,10 @@
 <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
     objects cannot:</p>
+
+<p>When <code>T</code> is a <em>Swappable</em> type, <code>value_initialized&lt;T&gt;</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&lt;int&gt; x ; <br>static_cast&lt;int&amp;&gt;(x) = 1 ; // OK<br>get(x) = 1 ; // OK<br><br>value_initialized&lt;int const&gt; y ; <br>static_cast&lt;int&amp;&gt;(y) = 1 ; // ERROR: cannot cast to int&amp;<br>static_cast&lt;int const&amp;&gt;(y) = 1 ; // ERROR: cannot modify a const value<br>get(y) = 1 ; // ERROR: cannot modify a const value</pre>
                     
@@ -379,7 +383,7 @@
      </p>
                     
 <hr>
-<p>Revised 23 May 2008</p>
+<p>Revised 28 August 2008</p>
                    
 <p>&copy; Copyright Fernando Cacciola, 2002, 2008.</p>
                    

Modified: trunk/libs/utility/value_init_test.cpp
==============================================================================
--- trunk/libs/utility/value_init_test.cpp (original)
+++ trunk/libs/utility/value_init_test.cpp 2008-08-28 15:00:20 EDT (Thu, 28 Aug 2008)
@@ -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 <cstring> // For memcmp.
 #include <iostream>
@@ -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<class T>
 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<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;
 }
 
 
 unsigned int expected_failures = 0;
 
+


Boost-Commit list run by bdawes at acm.org, david.abrahams at rcn.com, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk