Bug 555798 - CheckedInt improvements, init by 0 - r=neil
authorBenoit Jacob <bjacob@mozilla.com>
Tue, 12 Oct 2010 17:01:59 -0400
changeset 55381 80fd91b34453968b2f5dcd9c96aab2d460f6f5e5
parent 55380 d3f448b20f2050c99c84da9dee8c6fe425da9486
child 55382 e149f78d512022a8649a647df4b616531ef8f68b
push id1
push userroot
push dateTue, 26 Apr 2011 22:38:44 +0000
treeherdermozilla-beta@bfdb6e623a36 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersneil
bugs555798
milestone2.0b8pre
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 555798 - CheckedInt improvements, init by 0 - r=neil
xpcom/ds/CheckedInt.h
--- a/xpcom/ds/CheckedInt.h
+++ b/xpcom/ds/CheckedInt.h
@@ -155,17 +155,17 @@ template<typename T, typename U,
          bool is_U_signed = integer_traits<U>::is_signed>
 struct is_in_range_impl {};
 
 template<typename T, typename U>
 struct is_in_range_impl<T, U, true, true>
 {
     static T run(U x)
     {
-        return (x <= integer_traits<T>::max_value()) &
+        return (x <= integer_traits<T>::max_value()) &&
                (x >= integer_traits<T>::min_value());
     }
 };
 
 template<typename T, typename U>
 struct is_in_range_impl<T, U, false, false>
 {
     static T run(U x)
@@ -189,17 +189,17 @@ struct is_in_range_impl<T, U, true, fals
 template<typename T, typename U>
 struct is_in_range_impl<T, U, false, true>
 {
     static T run(U x)
     {
         if (sizeof(T) >= sizeof(U))
             return x >= 0;
         else
-            return x >= 0 && x <= U(integer_traits<T>::max_value());
+            return (x >= 0) && (x <= U(integer_traits<T>::max_value()));
     }
 };
 
 template<typename T, typename U> inline T is_in_range(U x)
 {
     return is_in_range_impl<T, U>::run(x);
 }
 
@@ -278,17 +278,17 @@ template<typename T> inline T is_mul_val
 {
     return is_mul_valid_impl<T>::run(x, y);
 }
 
 template<typename T> inline T is_div_valid(T x, T y)
 {
     return integer_traits<T>::is_signed ?
                         // keep in mind that min/-1 is invalid because abs(min)>max
-                        y != 0 && (x != integer_traits<T>::min_value() || y != T(-1))
+                        (y != 0) && (x != integer_traits<T>::min_value() || y != T(-1))
                     :
                         y != 0;
 }
 
 } // end namespace CheckedInt_internal
 
 
 /*** Step 4: Now define the CheckedInt class.
@@ -348,42 +348,42 @@ template<typename T>
 class CheckedInt
 {
 protected:
     T mValue;
     T mIsValid; // stored as a T to limit the number of integer conversions when
                 // evaluating nested arithmetic expressions.
 
     template<typename U>
-    CheckedInt(const U& value, PRBool isValid) : mValue(value), mIsValid(isValid)
+    CheckedInt(U value, PRBool isValid) : mValue(value), mIsValid(isValid)
     {
         CheckedInt_internal::integer_type_manually_recorded_info<T>
             ::TYPE_NOT_SUPPORTED_BY_CheckedInt();
     }
 
 public:
     /** Constructs a checked integer with given \a value. The checked integer is initialized as valid or invalid
       * depending on whether the \a value is in range.
       *
       * This constructor is not explicit. Instead, the type of its argument is a separate template parameter,
       * ensuring that no conversion is performed before this constructor is actually called.
       * As explained in the above documentation for class CheckedInt, this constructor checks that its argument is
       * valid.
       */
     template<typename U>
-    CheckedInt(const U& value)
+    CheckedInt(U value)
         : mValue(value),
           mIsValid(CheckedInt_internal::is_in_range<T>(value))
     {
         CheckedInt_internal::integer_type_manually_recorded_info<T>
             ::TYPE_NOT_SUPPORTED_BY_CheckedInt();
     }
 
-    /** Constructs a valid checked integer with uninitialized value */
-    CheckedInt() : mIsValid(1)
+    /** Constructs a valid checked integer with initial value 0 */
+    CheckedInt() : mValue(0), mIsValid(1)
     {
         CheckedInt_internal::integer_type_manually_recorded_info<T>
             ::TYPE_NOT_SUPPORTED_BY_CheckedInt();
     }
 
     /** \returns the actual value */
     T value() const { return mValue; }
 
@@ -393,43 +393,43 @@ public:
     PRBool valid() const
     {
         return mIsValid;
     }
 
     /** \returns the sum. Checks for overflow. */
     template<typename U> friend CheckedInt<U> operator +(const CheckedInt<U>& lhs, const CheckedInt<U>& rhs);
     /** Adds. Checks for overflow. \returns self reference */
-    template<typename U> CheckedInt& operator +=(const U &rhs);
+    template<typename U> CheckedInt& operator +=(U rhs);
     /** \returns the difference. Checks for overflow. */
     template<typename U> friend CheckedInt<U> operator -(const CheckedInt<U>& lhs, const CheckedInt<U> &rhs);
     /** Substracts. Checks for overflow. \returns self reference */
-    template<typename U> CheckedInt& operator -=(const U &rhs);
+    template<typename U> CheckedInt& operator -=(U rhs);
     /** \returns the product. Checks for overflow. */
     template<typename U> friend CheckedInt<U> operator *(const CheckedInt<U>& lhs, const CheckedInt<U> &rhs);
     /** Multiplies. Checks for overflow. \returns self reference */
-    template<typename U> CheckedInt& operator *=(const U &rhs);
+    template<typename U> CheckedInt& operator *=(U rhs);
     /** \returns the quotient. Checks for overflow and for divide-by-zero. */
     template<typename U> friend CheckedInt<U> operator /(const CheckedInt<U>& lhs, const CheckedInt<U> &rhs);
     /** Divides. Checks for overflow and for divide-by-zero. \returns self reference */
-    template<typename U> CheckedInt& operator /=(const U &rhs);
+    template<typename U> CheckedInt& operator /=(U rhs);
 
     /** \returns the opposite value. Checks for overflow. */
     CheckedInt operator -() const
     {
         T result = -value();
         /* give the compiler a good chance to perform RVO */
         return CheckedInt(result,
-                       mIsValid & CheckedInt_internal::is_sub_valid(T(0), value(), result));
+                          mIsValid & CheckedInt_internal::is_sub_valid(T(0), value(), result));
     }
 
     /** \returns true if the left and right hand sides are valid and have the same value. */
     PRBool operator ==(const CheckedInt& other) const
     {
-        return PRBool(mIsValid & other.mIsValid & T(value() == other.value()));
+        return PRBool(mIsValid & other.mIsValid & (value() == other.mValue));
     }
 
     /** prefix ++ */
     CheckedInt& operator++()
     {
         *this = *this + 1;
         return *this;
     }
@@ -457,25 +457,25 @@ public:
         return tmp;
     }
 
 private:
     /** operator!= is disabled. Indeed, (a!=b) should be the same as !(a==b) but that
       * would mean that if a or b is invalid, (a!=b) is always true, which is very tricky.
       */
     template<typename U>
-    PRBool operator !=(const U& other) const { return !(*this == other); }
+    PRBool operator !=(U other) const { return !(*this == other); }
 };
 
 #define CHECKEDINT_BASIC_BINARY_OPERATOR(NAME, OP)               \
 template<typename T>                                          \
 inline CheckedInt<T> operator OP(const CheckedInt<T> &lhs, const CheckedInt<T> &rhs) \
 {                                                                     \
-    T x = lhs.value();                                                \
-    T y = rhs.value();                                                \
+    T x = lhs.mValue;                                                \
+    T y = rhs.mValue;                                                \
     T result = x OP y;                                                \
     T is_op_valid                                                     \
         = CheckedInt_internal::is_##NAME##_valid(x, y, result);       \
     /* give the compiler a good chance to perform RVO */              \
     return CheckedInt<T>(result,                                      \
                          lhs.mIsValid & rhs.mIsValid & is_op_valid);  \
 }
 
@@ -483,82 +483,82 @@ CHECKEDINT_BASIC_BINARY_OPERATOR(add, +)
 CHECKEDINT_BASIC_BINARY_OPERATOR(sub, -)
 CHECKEDINT_BASIC_BINARY_OPERATOR(mul, *)
 
 // division can't be implemented by CHECKEDINT_BASIC_BINARY_OPERATOR
 // because if rhs == 0, we are not allowed to even try to compute the quotient.
 template<typename T>
 inline CheckedInt<T> operator /(const CheckedInt<T> &lhs, const CheckedInt<T> &rhs)
 {
-    T x = lhs.value();
-    T y = rhs.value();
+    T x = lhs.mValue;
+    T y = rhs.mValue;
     T is_op_valid = CheckedInt_internal::is_div_valid(x, y);
     T result = is_op_valid ? (x / y) : 0;
     /* give the compiler a good chance to perform RVO */
     return CheckedInt<T>(result,
                          lhs.mIsValid & rhs.mIsValid & is_op_valid);
 }
 
 // implement cast_to_CheckedInt<T>(x), making sure that
 //  - it allows x to be either a CheckedInt<T> or any integer type that can be casted to T
 //  - if x is already a CheckedInt<T>, we just return a reference to it, instead of copying it (optimization)
 
 template<typename T, typename U>
 struct cast_to_CheckedInt_impl
 {
     typedef CheckedInt<T> return_type;
-    static CheckedInt<T> run(const U& u) { return u; }
+    static CheckedInt<T> run(U u) { return u; }
 };
 
 template<typename T>
 struct cast_to_CheckedInt_impl<T, CheckedInt<T> >
 {
     typedef const CheckedInt<T>& return_type;
     static const CheckedInt<T>& run(const CheckedInt<T>& u) { return u; }
 };
 
 template<typename T, typename U>
 inline typename cast_to_CheckedInt_impl<T, U>::return_type
-cast_to_CheckedInt(const U& u)
+cast_to_CheckedInt(U u)
 {
     return cast_to_CheckedInt_impl<T, U>::run(u);
 }
 
 #define CHECKEDINT_CONVENIENCE_BINARY_OPERATORS(OP, COMPOUND_OP) \
 template<typename T>                                          \
 template<typename U>                                          \
-CheckedInt<T>& CheckedInt<T>::operator COMPOUND_OP(const U &rhs)    \
+CheckedInt<T>& CheckedInt<T>::operator COMPOUND_OP(U rhs)    \
 {                                                             \
     *this = *this OP cast_to_CheckedInt<T>(rhs);                 \
     return *this;                                             \
 }                                                             \
 template<typename T, typename U>                              \
-inline CheckedInt<T> operator OP(const CheckedInt<T> &lhs, const U &rhs) \
+inline CheckedInt<T> operator OP(const CheckedInt<T> &lhs, U rhs) \
 {                                                             \
     return lhs OP cast_to_CheckedInt<T>(rhs);                    \
 }                                                             \
 template<typename T, typename U>                              \
-inline CheckedInt<T> operator OP(const U & lhs, const CheckedInt<T> &rhs) \
+inline CheckedInt<T> operator OP(U lhs, const CheckedInt<T> &rhs) \
 {                                                             \
     return cast_to_CheckedInt<T>(lhs) OP rhs;                    \
 }
 
 CHECKEDINT_CONVENIENCE_BINARY_OPERATORS(+, +=)
 CHECKEDINT_CONVENIENCE_BINARY_OPERATORS(*, *=)
 CHECKEDINT_CONVENIENCE_BINARY_OPERATORS(-, -=)
 CHECKEDINT_CONVENIENCE_BINARY_OPERATORS(/, /=)
 
 template<typename T, typename U>
-inline PRBool operator ==(const CheckedInt<T> &lhs, const U &rhs)
+inline PRBool operator ==(const CheckedInt<T> &lhs, U rhs)
 {
     return lhs == cast_to_CheckedInt<T>(rhs);
 }
 
 template<typename T, typename U>
-inline PRBool operator ==(const U & lhs, const CheckedInt<T> &rhs)
+inline PRBool operator ==(U  lhs, const CheckedInt<T> &rhs)
 {
     return cast_to_CheckedInt<T>(lhs) == rhs;
 }
 
 // convenience typedefs.
 // the use of a macro here helps make sure that we don't let a typo slip into some of these.
 #define CHECKEDINT_MAKE_TYPEDEF(Type) \
 typedef CheckedInt<PR##Type> Checked##Type;