diff options
author | Hugo Lima <hugo.lima@openbossa.org> | 2010-02-22 17:33:07 -0300 |
---|---|---|
committer | Hugo Lima <hugo.lima@openbossa.org> | 2010-02-22 18:12:50 -0300 |
commit | 2650f48bf987b3fb7bd80cbfaef9f9a993d32999 (patch) | |
tree | a8022acbdb48048b313b77045a11720e270d70c1 /libshiboken | |
parent | bb887c264a35f550ce9551e84192f73886d2b191 (diff) |
Remove warnings related to comparison between signed and unsigned integer expressions.
Diffstat (limited to 'libshiboken')
-rw-r--r-- | libshiboken/conversions.h | 62 |
1 files changed, 41 insertions, 21 deletions
diff --git a/libshiboken/conversions.h b/libshiboken/conversions.h index 436ea3366..c78e65e13 100644 --- a/libshiboken/conversions.h +++ b/libshiboken/conversions.h @@ -226,35 +226,55 @@ struct Converter<bool> }; /** - * Helper template for checking if a value of SourceT overflows when cast to TargetT + * Helper template for checking if a value overflows when casted to type T */ +template<typename T, bool isSigned = std::numeric_limits<T>::is_signed > +struct OverFlowChecker; + template<typename T> -inline bool overflowCheck(const PY_LONG_LONG& value) +struct OverFlowChecker<T, true> { - return value < static_cast<PY_LONG_LONG>(std::numeric_limits<T>::min()) - || value > std::numeric_limits<T>::max(); -} + static bool check(const PY_LONG_LONG& value) + { + return value < std::numeric_limits<T>::min() || value > std::numeric_limits<T>::max(); + } +}; + +template<typename T> +struct OverFlowChecker<T, false> +{ + static bool check(const PY_LONG_LONG& value) + { + return value < 0 || static_cast<unsigned long long>(value) > std::numeric_limits<T>::max(); + } +}; -// If the type is long, there'll be no overflows template<> -inline bool overflowCheck<PY_LONG_LONG>(const PY_LONG_LONG&) +struct OverFlowChecker<PY_LONG_LONG, true> { - return false; -} + static bool check(const PY_LONG_LONG& value) + { + return false; + } +}; -// Version for floating point values (without sign check) -template<typename TargetT> -inline bool overflowCheck(const double& value) +template<> +struct OverFlowChecker<double, true> { - return value < std::numeric_limits<TargetT>::min() - || value > std::numeric_limits<TargetT>::max(); -} + static bool check(const double& value) + { + return false; + } +}; template<> -inline bool overflowCheck<double>(const double&) +struct OverFlowChecker<float, true> { - return false; -} + static bool check(const double& value) + { + return value < std::numeric_limits<float>::min() || value > std::numeric_limits<float>::max(); + } +}; template <typename PyIntEquiv> struct Converter_PyInt @@ -267,12 +287,12 @@ struct Converter_PyInt if (PyFloat_Check(pyobj)) { double d_result = PyFloat_AS_DOUBLE(pyobj); // If cast to long directly it could overflow silently - if (overflowCheck<PyIntEquiv>(d_result)) + if (OverFlowChecker<PyIntEquiv>::check(d_result)) PyErr_SetObject(PyExc_OverflowError, 0); return static_cast<PyIntEquiv>(d_result); } else { PY_LONG_LONG result = PyLong_AsLongLong(pyobj); - if (overflowCheck<PyIntEquiv>(result)) + if (OverFlowChecker<PyIntEquiv>::check(result)) PyErr_SetObject(PyExc_OverflowError, 0); return static_cast<PyIntEquiv>(result); } @@ -304,7 +324,7 @@ struct CharConverter return PyString_AS_STRING(pyobj)[0]; } else { PY_LONG_LONG result = PyLong_AsLongLong(pyobj); - if (overflowCheck<CharType>(result)) + if (OverFlowChecker<CharType>::check(result)) PyErr_SetObject(PyExc_OverflowError, 0); return result; } |