diff options
author | Hugo Lima <hugo.lima@openbossa.org> | 2010-01-22 16:48:12 -0200 |
---|---|---|
committer | Hugo Lima <hugo.lima@openbossa.org> | 2010-01-22 16:48:12 -0200 |
commit | 502ae1c940487e65577f735fecfeecd2092d8c1c (patch) | |
tree | d651267d339e6e928ace4ef21636acd4849fcc12 /libshiboken | |
parent | c6bbc80f277b1c490fadef26ab64a613d45276aa (diff) |
Add overflow check to unsigned int, because we need it on 64bit platforms.
Diffstat (limited to 'libshiboken')
-rw-r--r-- | libshiboken/conversions.h | 31 |
1 files changed, 10 insertions, 21 deletions
diff --git a/libshiboken/conversions.h b/libshiboken/conversions.h index 0771a570a..cd94ffc66 100644 --- a/libshiboken/conversions.h +++ b/libshiboken/conversions.h @@ -255,6 +255,14 @@ struct Converter_PyInt } }; +template <typename T> +struct Converter_PyULongInt : Converter_PyInt<T> +{ + static inline PyObject* toPython(void* cppobj) { return toPython(*reinterpret_cast<T*>(cppobj)); } + static inline PyObject* toPython(const T& cppobj) { return PyLong_FromUnsignedLong(cppobj); } +}; + + /// Check if we can treat the pyobj as a char, i.e. it's a number or a string with just one character. #define SbkChar_Check(pyobj) (PyNumber_Check(pyobj) || (PyString_Check(pyobj) && PyString_Size(pyobj) == 1)) @@ -279,27 +287,8 @@ struct CharConverter } }; -template <> -struct Converter<unsigned long> -{ - static inline PyObject* toPython(void* cppobj) { return toPython(*reinterpret_cast<unsigned long*>(cppobj)); } - static inline PyObject* toPython(unsigned long cppobj) { return PyLong_FromUnsignedLong(cppobj); } - static inline unsigned long toCpp(PyObject* pyobj) - { - if (PyFloat_Check(pyobj)) { - // Need to check for negatives manually - double doubleResult = PyFloat_AS_DOUBLE(pyobj); - if (overflowCheck<unsigned long>(doubleResult)) - PyErr_SetObject(PyExc_OverflowError, 0); - return static_cast<unsigned long>(doubleResult); - } else { - return PyLong_AsUnsignedLong(pyobj); - } - } -}; -template <> struct Converter<unsigned int> : Converter<unsigned long> {}; - - +template <> struct Converter<unsigned long> : Converter_PyULongInt<unsigned long> {}; +template <> struct Converter<unsigned int> : Converter_PyULongInt<unsigned int> {}; template <> struct Converter<char> : CharConverter<char> {}; template <> struct Converter<signed char> : CharConverter<signed char> {}; template <> struct Converter<unsigned char> : CharConverter<unsigned char> {}; |