Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r82135 - in sandbox/static_vector: boost/container test
From: adam.wulkiewicz_at_[hidden]
Date: 2012-12-21 00:05:38


Author: awulkiew
Date: 2012-12-21 00:05:37 EST (Fri, 21 Dec 2012)
New Revision: 82135
URL: http://svn.boost.org/trac/boost/changeset/82135

Log:
Implemented static_vector move semantics
Text files modified:
   sandbox/static_vector/boost/container/static_vector.hpp | 87 ++++++++++++++++++++++++++++++++---
   sandbox/static_vector/test/static_vector.cpp | 97 ++++++++++++++++++++++++++++-----------
   2 files changed, 147 insertions(+), 37 deletions(-)

Modified: sandbox/static_vector/boost/container/static_vector.hpp
==============================================================================
--- sandbox/static_vector/boost/container/static_vector.hpp (original)
+++ sandbox/static_vector/boost/container/static_vector.hpp 2012-12-21 00:05:37 EST (Fri, 21 Dec 2012)
@@ -122,6 +122,19 @@
         Value, Capacity, Strategy
>::strategy errh;
 
+ BOOST_COPYABLE_AND_MOVABLE(static_vector)
+
+#if defined(BOOST_NO_RVALUE_REFERENCES)
+public:
+ template <std::size_t C, typename S>
+ static_vector & operator=(static_vector<Value, C, S> & t)
+ {
+ typedef static_vector<Value, C, S> O;
+ this->operator=(static_cast<const ::boost::rv<O> &>(const_cast<const O &>(t)));
+ return *this;
+ }
+#endif
+
 public:
     typedef Value value_type;
     typedef stored_size_type size_type;
@@ -155,6 +168,17 @@
     }
 
     // strong
+ template <typename Iterator>
+ static_vector(Iterator first, Iterator last)
+ : m_size(0)
+ {
+ // TODO - add MPL_ASSERT, check if Iterator is really an iterator
+ this->assign(first, last); // may throw
+ }
+
+ // Copy constructors
+
+ // strong
     static_vector(static_vector const& other)
         : m_size(other.size())
     {
@@ -173,17 +197,10 @@
         sv::uninitialized_copy(other.begin(), other.end(), this->begin()); // may throw
     }
 
- // strong
- template <typename Iterator>
- static_vector(Iterator first, Iterator last)
- : m_size(0)
- {
- // TODO - add MPL_ASSERT, check if Iterator is really an iterator
- this->assign(first, last); // may throw
- }
+ // Copy assignments
 
     // basic
- static_vector & operator=(static_vector const& other)
+ static_vector & operator=(BOOST_COPY_ASSIGN_REF(static_vector) other)
     {
         this->assign(other.begin(), other.end()); // may throw
 
@@ -192,13 +209,65 @@
 
     // basic
     template <std::size_t C, typename S>
+#if defined(BOOST_NO_RVALUE_REFERENCES)
+ static_vector & operator=(boost::rv< static_vector<value_type, C, S> > const& other)
+#else
     static_vector & operator=(static_vector<value_type, C, S> const& other)
+#endif
     {
         this->assign(other.begin(), other.end()); // may throw
 
         return *this;
     }
 
+ // Move constructors
+
+ // nothrow or basic (depends on traits)
+ static_vector(BOOST_RV_REF(static_vector) other)
+ : m_size(0)
+ {
+ this->swap(other); // may throw
+ }
+
+ // basic
+ template <std::size_t C, typename S>
+#if defined(BOOST_NO_RVALUE_REFERENCES)
+ static_vector(boost::rv< static_vector<value_type, C, S> > & other)
+#else
+ static_vector(static_vector<value_type, C, S> && other)
+#endif
+ : m_size(0)
+ {
+ this->swap(other); // may throw
+ }
+
+ // Move assignments
+
+ // nothrow or basic (depends on traits)
+ static_vector & operator=(BOOST_RV_REF(static_vector) other)
+ {
+ this->clear();
+ this->swap(other);
+ //this->swap(other);
+ //other.clear();
+ return *this;
+ }
+
+ // nothrow or basic (depends on traits)
+ template <std::size_t C, typename S>
+#if defined(BOOST_NO_RVALUE_REFERENCES)
+ static_vector & operator=(boost::rv< static_vector<value_type, C, S> > & other)
+#else
+ static_vector & operator=(static_vector<value_type, C, S> && other)
+#endif
+ {
+ this->clear();
+ this->swap(other);
+ //this->swap(other);
+ //other.clear();
+ return *this;
+ }
+
     // nothrow
     ~static_vector()
     {

Modified: sandbox/static_vector/test/static_vector.cpp
==============================================================================
--- sandbox/static_vector/test/static_vector.cpp (original)
+++ sandbox/static_vector/test/static_vector.cpp 2012-12-21 00:05:37 EST (Fri, 21 Dec 2012)
@@ -563,48 +563,89 @@
 }
 
 template <typename T, size_t N>
-void test_swap_nd()
+void test_swap_and_move_nd()
 {
     {
- static_vector<T, N> v;
- static_vector<T, N> s;
+ static_vector<T, N> v1, v2, v3;
+ static_vector<T, N> s1, s2;
 
         for (size_t i = 0 ; i < N ; ++i )
- v.push_back(T(i));
+ {
+ v1.push_back(T(i));
+ v2.push_back(T(i));
+ v3.push_back(T(i));
+ }
         for (size_t i = 0 ; i < N/2 ; ++i )
- s.push_back(T(i));
-
- s.swap(v);
-
- BOOST_CHECK(v.size() == N/2);
- BOOST_CHECK(s.size() == N);
+ {
+ s1.push_back(T(100 + i));
+ s2.push_back(T(100 + i));
+ }
+
+ s1.swap(v1);
+ s2 = boost::move(v2);
+ static_vector<T, N> s3(boost::move(v3));
+
+ BOOST_CHECK(v1.size() == N/2);
+ BOOST_CHECK(s1.size() == N);
+ BOOST_CHECK(v2.size() == 0);
+ BOOST_CHECK(s2.size() == N);
+ BOOST_CHECK(v3.size() == 0);
+ BOOST_CHECK(s3.size() == N);
         for (size_t i = 0 ; i < N/2 ; ++i )
- BOOST_CHECK(v[i] == T(i));
+ BOOST_CHECK(v1[i] == T(100 + i));
         for (size_t i = 0 ; i < N ; ++i )
- BOOST_CHECK(s[i] == T(i));
+ {
+ BOOST_CHECK(s1[i] == T(i));
+ BOOST_CHECK(s2[i] == T(i));
+ BOOST_CHECK(s3[i] == T(i));
+ }
     }
     {
- static_vector<T, N> v;
- static_vector<T, N/2> s;
+ static_vector<T, N> v1, v2, v3;
+ static_vector<T, N/2> s1, s2;
 
         for (size_t i = 0 ; i < N/2 ; ++i )
- v.push_back(T(i));
+ {
+ v1.push_back(T(i));
+ v2.push_back(T(i));
+ v3.push_back(T(i));
+ }
         for (size_t i = 0 ; i < N/3 ; ++i )
- s.push_back(T(i));
-
- s.swap(v);
-
- BOOST_CHECK(v.size() == N/3);
- BOOST_CHECK(s.size() == N/2);
+ {
+ s1.push_back(T(100 + i));
+ s2.push_back(T(100 + i));
+ }
+
+ s1.swap(v1);
+ s2 = boost::move(v2);
+ static_vector<T, N/2> s3(boost::move(v3));
+
+ BOOST_CHECK(v1.size() == N/3);
+ BOOST_CHECK(s1.size() == N/2);
+ BOOST_CHECK(v2.size() == 0);
+ BOOST_CHECK(s2.size() == N/2);
+ BOOST_CHECK(v3.size() == 0);
+ BOOST_CHECK(s3.size() == N/2);
         for (size_t i = 0 ; i < N/3 ; ++i )
- BOOST_CHECK(v[i] == T(i));
+ BOOST_CHECK(v1[i] == T(100 + i));
         for (size_t i = 0 ; i < N/2 ; ++i )
- BOOST_CHECK(s[i] == T(i));
+ {
+ BOOST_CHECK(s1[i] == T(i));
+ BOOST_CHECK(s2[i] == T(i));
+ BOOST_CHECK(s3[i] == T(i));
+ }
     }
     {
         static_vector<T, N> v(N, T(0));
         static_vector<T, N/2, bad_alloc_strategy> s(N/2, T(1));
         BOOST_CHECK_THROW(s.swap(v), std::bad_alloc);
+ v.resize(N, T(0));
+ BOOST_CHECK_THROW(s = boost::move(v), std::bad_alloc);
+ v.resize(N, T(0));
+ try {
+ static_vector<T, N/2, bad_alloc_strategy> s2(boost::move(v));
+ BOOST_CHECK(false);
+ } catch (std::bad_alloc &) {}
     }
 }
 
@@ -707,12 +748,12 @@
     test_exceptions_nd<shptr_value, 10>();
     test_exceptions_nd<copy_movable, 10>();
 
- test_swap_nd<int, 10>();
- test_swap_nd<value_nd, 10>();
- test_swap_nd<counting_value, 10>();
+ test_swap_and_move_nd<int, 10>();
+ test_swap_and_move_nd<value_nd, 10>();
+ test_swap_and_move_nd<counting_value, 10>();
     BOOST_CHECK(counting_value::count() == 0);
- test_swap_nd<shptr_value, 10>();
- test_swap_nd<copy_movable, 10>();
+ test_swap_and_move_nd<shptr_value, 10>();
+ test_swap_and_move_nd<copy_movable, 10>();
 
 #ifndef BOOST_SINGLE_HEADER_UTF
     return 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