Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r82030 - sandbox-branches/geometry/index/boost/geometry/extensions/index
From: adam.wulkiewicz_at_[hidden]
Date: 2012-12-16 14:09:11


Author: awulkiew
Date: 2012-12-16 14:09:10 EST (Sun, 16 Dec 2012)
New Revision: 82030
URL: http://svn.boost.org/trac/boost/changeset/82030

Log:
asserts/checks closed in separate functions:
check_capacity()
check_empty()
check_iterator_end_eq()
check_iterator_end_neq()
Text files modified:
   sandbox-branches/geometry/index/boost/geometry/extensions/index/static_vector.hpp | 137 +++++++++++++++++++++------------------
   1 files changed, 74 insertions(+), 63 deletions(-)

Modified: sandbox-branches/geometry/index/boost/geometry/extensions/index/static_vector.hpp
==============================================================================
--- sandbox-branches/geometry/index/boost/geometry/extensions/index/static_vector.hpp (original)
+++ sandbox-branches/geometry/index/boost/geometry/extensions/index/static_vector.hpp 2012-12-16 14:09:10 EST (Sun, 16 Dec 2012)
@@ -83,9 +83,8 @@
     static_vector(static_vector<value_type, C> const& other)
         : m_size(other.m_size)
     {
- BOOST_ASSERT_MSG(other.m_size <= Capacity, "size can't exceed the capacity");
- //if ( Capacity <= other.m_size ) throw std::bad_alloc();
-
+ check_capacity(other.m_size);
+
         this->uninitialized_copy(other.begin(), other.end(), this->begin()); // may throw
     }
 
@@ -129,8 +128,7 @@
         }
         else
         {
- BOOST_ASSERT_MSG(count <= Capacity, "size can't exceed the capacity");
- //if ( Capacity <= count ) throw std::bad_alloc();
+ check_capacity(count);
 
             this->construct(this->end(), this->begin() + count); // may throw
         }
@@ -146,9 +144,8 @@
         }
         else
         {
- BOOST_ASSERT_MSG(count <= Capacity, "size can't exceed the capacity");
- //if ( Capacity <= count ) throw std::bad_alloc();
-
+ check_capacity(count);
+
             std::uninitialized_fill(this->end(), this->begin() + count, value); // may throw
         }
         m_size = count; // update end
@@ -157,16 +154,14 @@
     // nothrow
     void reserve(size_type BOOST_GEOMETRY_INDEX_ASSERT_UNUSED_PARAM(count))
     {
- BOOST_ASSERT_MSG(count <= Capacity, "size can't exceed the capacity");
- //if ( Capacity <= count ) throw std::bad_alloc();
+ check_capacity(count);
     }
 
     // strong
     void push_back(value_type const& value)
     {
- BOOST_ASSERT_MSG(m_size < Capacity, "size can't exceed the capacity");
- //if ( Capacity <= m_size ) throw std::bad_alloc();
-
+ check_capacity(m_size + 1);
+
         this->uninitialized_fill(this->end(), value); // may throw
         ++m_size; // update end
     }
@@ -174,9 +169,11 @@
     // nothrow
     void pop_back()
     {
- BOOST_ASSERT_MSG(0 < m_size, "the container is empty");
+ check_empty();
+
         //--m_size; // update end
         //this->destroy(this->end());
+
         // safer and more intuitive version
         this->destroy(this->end() - 1);
         --m_size; // update end
@@ -185,12 +182,8 @@
     // basic
     void insert(iterator position, value_type const& value)
     {
- // TODO change name of this macro
- BOOST_GEOMETRY_INDEX_ASSERT_UNUSED_PARAM(difference_type dist = std::distance(this->begin(), position));
- BOOST_ASSERT_MSG(0 <= dist && (sizeof(dist)<=sizeof(m_size)?((size_type)dist<=m_size):(dist<=(difference_type)m_size)), "invalid iterator");
-
- BOOST_ASSERT_MSG(m_size < Capacity, "size can't exceed the capacity");
- //if ( Capacity <= m_size ) throw std::bad_alloc();
+ check_iterator_end_eq(position);
+ check_capacity(m_size + 1);
 
         if ( position == this->end() )
         {
@@ -211,12 +204,8 @@
     // basic
     void insert(iterator position, size_type count, value_type const& value)
     {
- // TODO change name of this macro
- BOOST_GEOMETRY_INDEX_ASSERT_UNUSED_PARAM(difference_type dist = std::distance(this->begin(), position));
- BOOST_ASSERT_MSG(0 <= dist && (sizeof(dist)<=sizeof(m_size)?((size_type)dist<=m_size):(dist<=(difference_type)m_size)), "invalid iterator");
-
- BOOST_ASSERT_MSG(m_size + count <= Capacity, "size can't exceed the capacity");
- //if ( Capacity < m_size + count ) throw std::bad_alloc();
+ check_iterator_end_eq(position);
+ check_capacity(m_size + count);
 
         if ( position == this->end() )
         {
@@ -258,9 +247,7 @@
     // basic
     void erase(iterator position)
     {
- // TODO change name of this macro
- BOOST_GEOMETRY_INDEX_ASSERT_UNUSED_PARAM(difference_type dist = std::distance(this->begin(), position));
- BOOST_ASSERT_MSG(0 <= dist && (sizeof(dist)<=sizeof(m_size)?((size_type)dist<m_size):(dist<(difference_type)m_size)), "invalid iterator");
+ check_iterator_end_neq(position);
 
         this->move(position + 1, this->end(), position); // may throw
         this->destroy(this->end() - 1);
@@ -270,14 +257,11 @@
     // basic
     void erase(iterator first, iterator last)
     {
- // TODO change name of this macro
- BOOST_GEOMETRY_INDEX_ASSERT_UNUSED_PARAM(difference_type distf = std::distance(this->begin(), first));
- BOOST_GEOMETRY_INDEX_ASSERT_UNUSED_PARAM(difference_type distl = std::distance(this->begin(), last));
- BOOST_ASSERT_MSG(0 <= distf && (sizeof(distf)<=sizeof(m_size)?((size_type)distf<m_size):(distf<(difference_type)m_size)), "invalid iterator");
- BOOST_ASSERT_MSG(0 <= distl && (sizeof(distl)<=sizeof(m_size)?((size_type)distl<m_size):(distl<(difference_type)m_size)), "invalid iterator");
-
+ check_iterator_end_neq(first);
+ check_iterator_end_neq(last);
+
         difference_type n = std::distance(first, last);
- BOOST_ASSERT_MSG(0 <= n, "invalid iterator");
+ BOOST_ASSERT_MSG(0 <= n, "invalid range");
 
         this->move(last, this->end(), first); // may throw
         this->destroy(this->end() - n, this->end());
@@ -302,8 +286,7 @@
         }
         else
         {
- BOOST_ASSERT_MSG(count <= Capacity, "size can't exceed the capacity");
- //if ( Capacity <= count ) throw std::bad_alloc();
+ check_capacity(count);
 
             std::fill_n(this->begin(), m_size, value);
             std::uninitialized_fill(this->end(), this->begin() + count, value); // may throw
@@ -322,7 +305,7 @@
     Value & at(size_type i)
     {
         if ( m_size <= i )
- throw std::out_of_range("static_vector element index out of bounds");
+ throw std::out_of_range("index out of bounds");
         return *(this->begin() + i);
     }
 
@@ -330,7 +313,7 @@
     Value const& at(size_type i) const
     {
         if ( m_size <= i )
- throw std::out_of_range("static_vector element index out of bounds");
+ throw std::out_of_range("index out of bounds");
         return *(this->begin() + i);
     }
 
@@ -351,28 +334,28 @@
     // nothrow
     Value & front()
     {
- BOOST_ASSERT_MSG(0 < m_size, "the container is empty");
+ check_empty();
         return *(this->begin());
     }
 
     // nothrow
     Value const& front() const
     {
- BOOST_ASSERT_MSG(0 < m_size, "the container is empty");
+ check_empty();
         return *(this->begin());
     }
 
     // nothrow
     Value & back()
     {
- BOOST_ASSERT_MSG(0 < m_size, "the container is empty");
+ check_empty();
         return *(this->end() - 1);
     }
 
     // nothrow
     Value const& back() const
     {
- BOOST_ASSERT_MSG(0 < m_size, "the container is empty");
+ check_empty();
         return *(this->end() - 1);
     }
 
@@ -408,15 +391,11 @@
     template <typename Iterator>
     void insert_dispatch(iterator position, Iterator first, Iterator last, boost::random_access_traversal_tag const&)
     {
- // TODO change name of this macro
- BOOST_GEOMETRY_INDEX_ASSERT_UNUSED_PARAM(difference_type dist = std::distance(this->begin(), position));
- // TODO dist < distance(begin(), end())
- BOOST_ASSERT_MSG(0 <= dist && (sizeof(dist)<=sizeof(m_size)?((size_type)dist<=m_size):(dist<=(difference_type)m_size)), "invalid iterator");
-
+ check_iterator_end_eq(position);
+
         difference_type count = std::distance(first, last);
 
- BOOST_ASSERT_MSG(m_size + count <= Capacity, "size can't exceed the capacity");
- //if ( Capacity < m_size + count ) throw std::bad_alloc();
+ check_capacity(m_size + count);
 
         if ( position == this->end() )
         {
@@ -432,18 +411,14 @@
     template <typename Iterator, typename Traversal>
     void insert_dispatch(iterator position, Iterator first, Iterator last, Traversal const& /*not_random_access*/)
     {
- // TODO change name of this macro
- BOOST_GEOMETRY_INDEX_ASSERT_UNUSED_PARAM(difference_type dist = std::distance(this->begin(), position));
- // TODO dist < distance(begin(), end())
- BOOST_ASSERT_MSG(0 <= dist && (sizeof(dist)<=sizeof(m_size)?((size_type)dist<=m_size):(dist<=(difference_type)m_size)), "invalid iterator");
+ check_iterator_end_eq(position);
 
         if ( position == this->end() )
         {
             std::pair<bool, size_type> copy_data =
                 this->uninitialized_copy_checked(first, last, position, std::distance(position, this->begin() + Capacity)); // may throw
             
- BOOST_ASSERT_MSG(copy_data.first, "size can't exceed the capacity");
- // eventually throw bad_alloc
+ check_capacity(copy_data.first ? m_size + copy_data.second : Capacity + 1);
 
             m_size += copy_data.second;
         }
@@ -451,8 +426,7 @@
         {
             difference_type count = std::distance(first, last);
             
- BOOST_ASSERT_MSG(m_size + count <= Capacity, "size can't exceed the capacity");
- //if ( Capacity < m_size + count ) throw std::bad_alloc();
+ check_capacity(m_size + count);
 
             this->insert_in_the_middle(position, first, last, count); // may throw
         }
@@ -492,8 +466,7 @@
     {
         size_type s = std::distance(first, last);
 
- BOOST_ASSERT_MSG(s <= Capacity, "size can't exceed the capacity");
- //if ( Capacity <= m_size ) throw std::bad_alloc();
+ check_capacity(s);
 
         if ( m_size <= s )
         {
@@ -524,8 +497,7 @@
             this->uninitialized_copy_checked(first, last, it, std::distance(it, this->begin() + Capacity)); // may throw
         s += copy_data.second;
 
- BOOST_ASSERT_MSG(copy_data.first, "size can't exceed the capacity");
- // eventually throw bad_alloc
+ check_capacity(copy_data.first ? s : Capacity + 1);
 
         m_size = s; // update end
     }
@@ -771,6 +743,45 @@
         }
     }
 
+ void check_capacity(size_type s)
+ {
+ BOOST_ASSERT_MSG(s <= Capacity, "size can't exceed the capacity");
+ //if ( Capacity < s ) throw std::bad_alloc();
+ }
+
+ void check_empty()
+ {
+ BOOST_ASSERT_MSG(0 < m_size, "the container is empty");
+ }
+
+ void check_iterator_end_neq(iterator position)
+ {
+ BOOST_GEOMETRY_INDEX_ASSERT_UNUSED_PARAM(
+ difference_type dist = std::distance(this->begin(), position);
+ )
+ BOOST_ASSERT_MSG(
+ 0 <= dist &&
+ ( sizeof(dist) <= sizeof(m_size) ?
+ (static_cast<size_type>(dist) < m_size) :
+ ( dist < static_cast<difference_type>(m_size))
+ ), "invalid iterator"
+ );
+ }
+
+ void check_iterator_end_eq(iterator position)
+ {
+ BOOST_GEOMETRY_INDEX_ASSERT_UNUSED_PARAM(
+ difference_type dist = std::distance(this->begin(), position);
+ )
+ BOOST_ASSERT_MSG(
+ 0 <= dist &&
+ ( sizeof(dist) <= sizeof(m_size) ?
+ (static_cast<size_type>(dist) <= m_size) :
+ ( dist <= static_cast<difference_type>(m_size))
+ ), "invalid iterator"
+ );
+ }
+
     Value * ptr()
     {
         return (reinterpret_cast<Value*>(m_storage.address()));


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