Author: wyoung
Date: Tue Nov 18 17:05:32 2008
New Revision: 2412

URL: http://svn.gna.org/viewcvs/mysqlpp?rev=2412&view=rev
Log:
- operator[]() and at() for SQLTypeAdapter, Row, and String all throw
  BadIndex on all out-of-range index attempts.
- Added another parameter to BadIndex exception, taking max allowed
  index value, so exception's what() message can report why given index
  isn't acceptable.

Modified:
    trunk/Wishlist
    trunk/doc/userman/breakages.dbx
    trunk/lib/exceptions.h
    trunk/lib/mystring.cpp
    trunk/lib/mystring.h
    trunk/lib/row.cpp
    trunk/lib/row.h
    trunk/lib/stadapter.cpp
    trunk/lib/stadapter.h

Modified: trunk/Wishlist
URL: 
http://svn.gna.org/viewcvs/mysqlpp/trunk/Wishlist?rev=2412&r1=2411&r2=2412&view=diff
==============================================================================
--- trunk/Wishlist (original)
+++ trunk/Wishlist Tue Nov 18 17:05:32 2008
@@ -6,12 +6,6 @@
     o Any time you must hand-roll some SQL code in your program,
       consider whether it could be generalized to a widely-useful
       API feature.
-
-
-v3.0.7
-------
-       o Throw BadIndex from all operator[]'s and at() methods on bad
-         indices, and add them to test/array_index.cpp.
 
 
 v3.1 Tentative Plan

Modified: trunk/doc/userman/breakages.dbx
URL: 
http://svn.gna.org/viewcvs/mysqlpp/trunk/doc/userman/breakages.dbx?rev=2412&r1=2411&r2=2412&view=diff
==============================================================================
--- trunk/doc/userman/breakages.dbx (original)
+++ trunk/doc/userman/breakages.dbx Tue Nov 18 17:05:32 2008
@@ -1001,6 +1001,22 @@
         collision in the global macro namespace.</para>
       </sect4>
     </sect3>
+
+
+    <sect3 id="api-3.0.7">
+      <title>v3.0.7</title>
+
+      <para>Most MySQL++ classes with <methodname>at()</methodname>
+      or <methodname>operator []()</methodname> methods now
+      throw the new <ulink type="structref" url="BadIndex"/>
+      exception when you pass an out-of-range index. These
+      methods variously either did not check their indices,
+      or threw <classname>std::out_of_range</classname> when
+      passed a bad index. The one exception that comes to mind is
+      <classname>Fields</classname>, which is just a typedef for a
+      specialization of <classname>std::vector</classname>, and the
+      Standard has its own rules for index checking.</para>
+    </sect3>
   </sect2>
 
 

Modified: trunk/lib/exceptions.h
URL: 
http://svn.gna.org/viewcvs/mysqlpp/trunk/lib/exceptions.h?rev=2412&r1=2411&r2=2412&view=diff
==============================================================================
--- trunk/lib/exceptions.h (original)
+++ trunk/lib/exceptions.h Tue Nov 18 17:05:32 2008
@@ -183,12 +183,13 @@
        ///
        /// \param bad_index type of object bad index tried on
        /// \param bad_index index value the container didn't like
-       explicit BadIndex(const char* what, int bad_index) :
+       /// \param max_index largest legal index value for container
+       explicit BadIndex(const char* what, int bad_index, int max_index) :
        Exception()
        {
                std::ostringstream outs;
                outs << "Index " << bad_index << " on " << what <<
-                               " out of range";
+                               " out of range, max legal index is " << 
max_index;
                what_ = outs.str();
        }
 

Modified: trunk/lib/mystring.cpp
URL: 
http://svn.gna.org/viewcvs/mysqlpp/trunk/lib/mystring.cpp?rev=2412&r1=2411&r2=2412&view=diff
==============================================================================
--- trunk/lib/mystring.cpp (original)
+++ trunk/lib/mystring.cpp Tue Nov 18 17:05:32 2008
@@ -38,7 +38,7 @@
 String::at(size_type pos) const
 {
        if (pos >= size()) {
-               throw std::out_of_range("String");
+               throw BadIndex("String", pos, size());
        }
        else {
                return buffer_->data()[pos];
@@ -219,12 +219,6 @@
        }
 }
 
-
-char
-String::operator [](size_type pos) const
-{
-       return buffer_ ? buffer_->data()[pos] : 0;
-}
 
 /// \brief Stream insertion operator for String objects
 ///

Modified: trunk/lib/mystring.h
URL: 
http://svn.gna.org/viewcvs/mysqlpp/trunk/lib/mystring.h?rev=2412&r1=2411&r2=2412&view=diff
==============================================================================
--- trunk/lib/mystring.h (original)
+++ trunk/lib/mystring.h Tue Nov 18 17:05:32 2008
@@ -276,8 +276,8 @@
 
        /// \brief Return a character within the string.
        ///
-       /// Unlike \c operator[](), this function throws an 
-       /// \c std::out_of_range exception if the index isn't within range.
+       /// \throw mysqlpp::BadIndex if the row is not initialized or there
+       /// are less than \c i fields in the row.
        char at(size_type pos) const;
 
        /// \brief Return iterator pointing to the first character of
@@ -523,9 +523,11 @@
 
        /// \brief Return a character within the string.
        ///
-       /// Unlike at(), this access method doesn't check the index for
-       /// sanity.
-       char operator [](size_type pos) const;
+       /// This function is just syntactic sugar, wrapping the at() method.
+       ///
+       /// \throw mysqlpp::BadIndex if the string is not initialized or there
+       /// are less than \c i fields in the string.
+       char operator [](size_type pos) const { return at(pos); }
 
        /// \brief Returns a const char pointer to the object's raw data
        operator const char*() const { return data(); }

Modified: trunk/lib/row.cpp
URL: 
http://svn.gna.org/viewcvs/mysqlpp/trunk/lib/row.cpp?rev=2412&r1=2411&r2=2412&view=diff
==============================================================================
--- trunk/lib/row.cpp (original)
+++ trunk/lib/row.cpp Tue Nov 18 17:05:32 2008
@@ -69,7 +69,7 @@
                return data_[i];
        }
        else {
-               throw BadIndex("Row", i);
+               throw BadIndex("Row", i, size());
        }
 }
 

Modified: trunk/lib/row.h
URL: 
http://svn.gna.org/viewcvs/mysqlpp/trunk/lib/row.h?rev=2412&r1=2411&r2=2412&view=diff
==============================================================================
--- trunk/lib/row.h (original)
+++ trunk/lib/row.h Tue Nov 18 17:05:32 2008
@@ -3,7 +3,7 @@
 
 /***********************************************************************
  Copyright (c) 1998 by Kevin Atkinson, (c) 1999-2001 by MySQL AB, and
- (c) 2004-2007 by Educational Technology Resources, Inc.  Others may
+ (c) 2004-2008 by Educational Technology Resources, Inc.  Others may
  also hold copyrights on code in this file.  See the CREDITS file in
  the top directory of the distribution for details.
 
@@ -142,8 +142,8 @@
 
        /// \brief Get a const reference to the field given its index
        ///
-       /// If the index value is bad, the underlying std::vector is
-       /// supposed to throw an exception, according to the Standard.
+       /// \throw mysqlpp::BadIndex if the row is not initialized or there
+       /// are less than \c i fields in the row.
        const_reference at(size_type i) const;
 
        /// \brief Get a reference to the last element of the vector
@@ -307,7 +307,8 @@
        /// \brief Get the value of a field given its name.
        ///
        /// If the field does not exist in this row, we throw a BadFieldName
-       /// exception.
+       /// exception if exceptions are enabled, or an empty row if not.
+       /// An empty row tests as false in bool context.
        ///
        /// This operator is fairly inefficient.  operator[](int) is faster.
        const_reference operator [](const char* field) const;
@@ -320,6 +321,9 @@
        /// \c size_type, because it will interfere with the \c const
        /// \c char* overload otherwise.  row[0] is ambiguous when there
        /// isn't an int overload.
+       ///
+       /// \throw mysqlpp::BadIndex if the row is not initialized or there
+       /// are less than \c i fields in the row.
        const_reference operator [](int i) const
                        { return at(static_cast<size_type>(i)); }
 

Modified: trunk/lib/stadapter.cpp
URL: 
http://svn.gna.org/viewcvs/mysqlpp/trunk/lib/stadapter.cpp?rev=2412&r1=2411&r2=2412&view=diff
==============================================================================
--- trunk/lib/stadapter.cpp (original)
+++ trunk/lib/stadapter.cpp Tue Nov 18 17:05:32 2008
@@ -392,11 +392,12 @@
                        return *(buffer_->data() + i);
                }
                else {
-                       throw out_of_range("Not enough chars in 
SQLTypeAdapter");
+                       throw BadIndex("Not enough chars in SQLTypeAdapter", i,
+                                       length());
                }
        }
        else {
-               throw out_of_range("SQLTypeAdapter buffer not initialized");
+               throw BadIndex("SQLTypeAdapter buffer not initialized", i, -1);
        }
 }
 

Modified: trunk/lib/stadapter.h
URL: 
http://svn.gna.org/viewcvs/mysqlpp/trunk/lib/stadapter.h?rev=2412&r1=2411&r2=2412&view=diff
==============================================================================
--- trunk/lib/stadapter.h (original)
+++ trunk/lib/stadapter.h Tue Nov 18 17:05:32 2008
@@ -214,9 +214,13 @@
        /// \brief Returns the character at a given position within the
        /// string buffer.
        ///
-       /// \throw out_of_range if the internal buffer is not initialized
-       /// (default ctor called, and no assignment operator subsequently)
-       /// or if there are not at least i + 1 characters in the buffer
+       /// \throw mysqlpp::BadIndex if the internal buffer is not
+       /// initialized (default ctor called, and no subsequent assignment)
+       /// or if there are not at least i + 1 characters in the buffer.
+       ///
+       /// WARNING: The throw-spec is incorrect, but it can't be changed
+       /// until v4, where we can break the ABI.  Throw-specs shouldn't be
+       /// relied on anyway.
        char at(size_type i) const throw(std::out_of_range);
 
        /// \brief Compare the internal buffer to the given string


_______________________________________________
Mysqlpp-commits mailing list
[email protected]
https://mail.gna.org/listinfo/mysqlpp-commits

Reply via email to