Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r72188 - in trunk/boost/spirit/home/support/utree: . detail
From: blelbach_at_[hidden]
Date: 2011-05-26 14:16:38


Author: wash
Date: 2011-05-26 14:16:38 EDT (Thu, 26 May 2011)
New Revision: 72188
URL: http://svn.boost.org/trac/boost/changeset/72188

Log:
Emit more accurate exceptions when back() or front() is called on an empty utree
list.

Text files modified:
   trunk/boost/spirit/home/support/utree/detail/utree_detail2.hpp | 48 ++++++++++++++++++++++++++-------------
   trunk/boost/spirit/home/support/utree/utree.hpp | 19 +++++++++++++++
   2 files changed, 50 insertions(+), 17 deletions(-)

Modified: trunk/boost/spirit/home/support/utree/detail/utree_detail2.hpp
==============================================================================
--- trunk/boost/spirit/home/support/utree/detail/utree_detail2.hpp (original)
+++ trunk/boost/spirit/home/support/utree/detail/utree_detail2.hpp 2011-05-26 14:16:38 EDT (Thu, 26 May 2011)
@@ -1302,16 +1302,20 @@
             return p->front();
         if (get_type() == type::range_type)
         {
- BOOST_ASSERT(r.first != 0);
+ if (!r.first)
+ BOOST_THROW_EXCEPTION(
+ empty_exception("front() called on empty utree range"));
             return r.first->val;
         }
 
         // otherwise...
- if (get_type() != type::list_type || l.first == 0)
+ if (get_type() != type::list_type)
             BOOST_THROW_EXCEPTION(
                 bad_type_exception
- ("front() called on non-list utree type",
- get_type()));
+ ("front() called on non-list utree type", get_type()));
+ else if (!l.first)
+ BOOST_THROW_EXCEPTION(
+ empty_exception("front() called on empty utree list"));
 
         return l.first->val;
     }
@@ -1322,16 +1326,20 @@
             return p->back();
         if (get_type() == type::range_type)
         {
- BOOST_ASSERT(r.last != 0);
+ if (!r.last)
+ BOOST_THROW_EXCEPTION(
+ empty_exception("back() called on empty utree range"));
             return r.last->val;
         }
 
         // otherwise...
- if (get_type() != type::list_type || l.last == 0)
+ if (get_type() != type::list_type)
             BOOST_THROW_EXCEPTION(
                 bad_type_exception
- ("back() called on non-list utree type",
- get_type()));
+ ("back() called on non-list utree type", get_type()));
+ else if (!l.last)
+ BOOST_THROW_EXCEPTION(
+ empty_exception("back() called on empty utree list"));
 
         return l.last->val;
     }
@@ -1342,16 +1350,20 @@
             return ((utree const*)p)->front();
         if (get_type() == type::range_type)
         {
- BOOST_ASSERT(r.first != 0);
+ if (!r.first)
+ BOOST_THROW_EXCEPTION(
+ empty_exception("front() called on empty utree range"));
             return r.first->val;
         }
 
         // otherwise...
- if (get_type() != type::list_type || l.first == 0)
+ if (get_type() != type::list_type)
             BOOST_THROW_EXCEPTION(
                 bad_type_exception
- ("front() called on non-list utree type",
- get_type()));
+ ("front() called on non-list utree type", get_type()));
+ else if (!l.first)
+ BOOST_THROW_EXCEPTION(
+ empty_exception("front() called on empty utree list"));
 
         return l.first->val;
     }
@@ -1362,16 +1374,20 @@
             return ((utree const*)p)->back();
         if (get_type() == type::range_type)
         {
- BOOST_ASSERT(r.last != 0);
+ if (!r.last)
+ BOOST_THROW_EXCEPTION(
+ empty_exception("back() called on empty utree range"));
             return r.last->val;
         }
 
         // otherwise...
- if (get_type() != type::list_type || l.last == 0)
+ if (get_type() != type::list_type)
             BOOST_THROW_EXCEPTION(
                 bad_type_exception
- ("back() called on non-list utree type",
- get_type()));
+ ("back() called on non-list utree type", get_type()));
+ else if (!l.last)
+ BOOST_THROW_EXCEPTION(
+ empty_exception("back() called on empty utree list"));
 
         return l.last->val;
     }

Modified: trunk/boost/spirit/home/support/utree/utree.hpp
==============================================================================
--- trunk/boost/spirit/home/support/utree/utree.hpp (original)
+++ trunk/boost/spirit/home/support/utree/utree.hpp 2011-05-26 14:16:38 EDT (Thu, 26 May 2011)
@@ -48,6 +48,11 @@
        precondition is violated as the `utree` instance holds some other type.
     */
     struct bad_type_exception /*: utree_exception*/;
+
+ /*`The `empty_exception` is thrown whenever a precondition of a list
+ or range utree method is violated due to the list or range being empty.
+ */
+ struct empty_exception /*: utree_exception*/;
     //]
 
     //[utree_types
@@ -137,9 +142,21 @@
 
         virtual ~bad_type_exception() throw() {}
 
- virtual const char* what() const throw()
+ virtual char const* what() const throw()
         { return msg.c_str(); }
     };
+
+ struct empty_exception : utree_exception
+ {
+ char const* msg;
+
+ empty_exception(char const* error) : msg(error) {}
+
+ virtual ~empty_exception() throw() {}
+
+ virtual char const* what() const throw()
+ { return msg; }
+ };
 
     ///////////////////////////////////////////////////////////////////////////
     // A typed string with parametric Base storage. The storage can be any


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