diff options
author | Stephan Bergmann <sbergman@redhat.com> | 2022-04-13 17:45:52 +0200 |
---|---|---|
committer | Stephan Bergmann <sbergman@redhat.com> | 2022-04-14 10:21:42 +0200 |
commit | 9a42c99d7b3e8a8429f14d7d851f3d186fa04594 (patch) | |
tree | b9e854eb4a67656c52f7bf22cb91fdd6d90136f4 /cppu | |
parent | fc2413e9a8f509bc6835cae473bf5053728c419a (diff) |
Simplify equality comparison among the various UNOIDL integer types
Change-Id: I67b11049938de470a2a5096cac376fdb0529ab5c
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/132992
Tested-by: Jenkins
Reviewed-by: Stephan Bergmann <sbergman@redhat.com>
Diffstat (limited to 'cppu')
-rw-r--r-- | cppu/source/uno/eq.hxx | 128 |
1 files changed, 71 insertions, 57 deletions
diff --git a/cppu/source/uno/eq.hxx b/cppu/source/uno/eq.hxx index 6287cdcc3506..e81f0972e801 100644 --- a/cppu/source/uno/eq.hxx +++ b/cppu/source/uno/eq.hxx @@ -20,6 +20,7 @@ #include <cmath> #include <string.h> +#include <type_traits> #include <osl/diagnose.h> #include <rtl/ustring.hxx> @@ -259,6 +260,27 @@ inline bool _equalSequence( } } +template<typename T1, typename T2> +std::enable_if_t<std::is_signed_v<T1> && std::is_signed_v<T2>, bool> equal(T1 value1, T2 value2) { + return value1 == value2; +} + +template<typename T1, typename T2> +std::enable_if_t<std::is_signed_v<T1> && std::is_unsigned_v<T2>, bool> equal(T1 value1, T2 value2) { + return value1 >= 0 && static_cast<std::make_unsigned_t<T1>>(value1) == value2; +} + +template<typename T1, typename T2> +std::enable_if_t<std::is_unsigned_v<T1> && std::is_signed_v<T2>, bool> equal(T1 value1, T2 value2) { + return value2 >= 0 && value1 == static_cast<std::make_unsigned_t<T2>>(value2); +} + +template<typename T1, typename T2> +std::enable_if_t<std::is_unsigned_v<T1> && std::is_unsigned_v<T2>, bool> equal(T1 value1, T2 value2) +{ + return value1 == value2; +} + inline bool _equalData( void * pDest, typelib_TypeDescriptionReference * pDestType, typelib_TypeDescription * pDestTypeDescr, @@ -294,20 +316,19 @@ inline bool _equalData( switch (eSourceTypeClass) { case typelib_TypeClass_BYTE: - return (*static_cast<sal_Int8 *>(pDest) == *static_cast<sal_Int8 *>(pSource)); + return equal(*static_cast<sal_Int8 *>(pDest), *static_cast<sal_Int8 *>(pSource)); case typelib_TypeClass_SHORT: - return (static_cast<sal_Int16>(*static_cast<sal_Int8 *>(pDest)) == *static_cast<sal_Int16 *>(pSource)); + return equal(*static_cast<sal_Int8 *>(pDest), *static_cast<sal_Int16 *>(pSource)); case typelib_TypeClass_UNSIGNED_SHORT: - return (static_cast<sal_Int32>(*static_cast<sal_Int8 *>(pDest)) == static_cast<sal_Int32>(*static_cast<sal_uInt16 *>(pSource))); + return equal(*static_cast<sal_Int8 *>(pDest), *static_cast<sal_uInt16 *>(pSource)); case typelib_TypeClass_LONG: - return (static_cast<sal_Int32>(*static_cast<sal_Int8 *>(pDest)) == *static_cast<sal_Int32 *>(pSource)); + return equal(*static_cast<sal_Int8 *>(pDest), *static_cast<sal_Int32 *>(pSource)); case typelib_TypeClass_UNSIGNED_LONG: - return (static_cast<sal_Int64>(*static_cast<sal_Int8 *>(pDest)) == static_cast<sal_Int64>(*static_cast<sal_uInt32 *>(pSource))); + return equal(*static_cast<sal_Int8 *>(pDest), *static_cast<sal_uInt32 *>(pSource)); case typelib_TypeClass_HYPER: - return (static_cast<sal_Int64>(*static_cast<sal_Int8 *>(pDest)) == *static_cast<sal_Int64 *>(pSource)); + return equal(*static_cast<sal_Int8 *>(pDest), *static_cast<sal_Int64 *>(pSource)); case typelib_TypeClass_UNSIGNED_HYPER: - return (*static_cast<sal_Int8 *>(pDest) >= 0 && - static_cast<sal_Int64>(*static_cast<sal_Int8 *>(pDest)) == *static_cast<sal_Int64 *>(pSource)); // same size + return equal(*static_cast<sal_Int8 *>(pDest), *static_cast<sal_Int64 *>(pSource)); case typelib_TypeClass_FLOAT: return (static_cast<float>(*static_cast<sal_Int8 *>(pDest)) == *static_cast<float *>(pSource)); case typelib_TypeClass_DOUBLE: @@ -319,20 +340,19 @@ inline bool _equalData( switch (eSourceTypeClass) { case typelib_TypeClass_BYTE: - return (*static_cast<sal_Int16 *>(pDest) == static_cast<sal_Int16>(*static_cast<sal_Int8 *>(pSource))); + return equal(*static_cast<sal_Int16 *>(pDest), *static_cast<sal_Int8 *>(pSource)); case typelib_TypeClass_SHORT: - return (*static_cast<sal_Int16 *>(pDest) == *static_cast<sal_Int16 *>(pSource)); + return equal(*static_cast<sal_Int16 *>(pDest), *static_cast<sal_Int16 *>(pSource)); case typelib_TypeClass_UNSIGNED_SHORT: - return (static_cast<sal_Int32>(*static_cast<sal_Int16 *>(pDest)) == static_cast<sal_Int32>(*static_cast<sal_uInt16 *>(pSource))); + return equal(*static_cast<sal_Int16 *>(pDest), *static_cast<sal_uInt16 *>(pSource)); case typelib_TypeClass_LONG: - return (static_cast<sal_Int32>(*static_cast<sal_Int16 *>(pDest)) == *static_cast<sal_Int32 *>(pSource)); + return equal(*static_cast<sal_Int16 *>(pDest), *static_cast<sal_Int32 *>(pSource)); case typelib_TypeClass_UNSIGNED_LONG: - return (static_cast<sal_Int64>(*static_cast<sal_Int16 *>(pDest)) == static_cast<sal_Int64>(*static_cast<sal_uInt32 *>(pSource))); + return equal(*static_cast<sal_Int16 *>(pDest), *static_cast<sal_uInt32 *>(pSource)); case typelib_TypeClass_HYPER: - return (static_cast<sal_Int64>(*static_cast<sal_Int16 *>(pDest)) == *static_cast<sal_Int64 *>(pSource)); + return equal(*static_cast<sal_Int16 *>(pDest), *static_cast<sal_Int64 *>(pSource)); case typelib_TypeClass_UNSIGNED_HYPER: - return (*static_cast<sal_Int16 *>(pDest) >= 0 && - static_cast<sal_Int64>(*static_cast<sal_Int16 *>(pDest)) == *static_cast<sal_Int64 *>(pSource)); // same size + return equal(*static_cast<sal_Int16 *>(pDest), *static_cast<sal_Int64 *>(pSource)); case typelib_TypeClass_FLOAT: return (static_cast<float>(*static_cast<sal_Int16 *>(pDest)) == *static_cast<float *>(pSource)); case typelib_TypeClass_DOUBLE: @@ -344,19 +364,19 @@ inline bool _equalData( switch (eSourceTypeClass) { case typelib_TypeClass_BYTE: - return (static_cast<sal_Int32>(*static_cast<sal_uInt16 *>(pDest)) == static_cast<sal_Int32>(*static_cast<sal_Int8 *>(pSource))); + return equal(*static_cast<sal_uInt16 *>(pDest), *static_cast<sal_Int8 *>(pSource)); case typelib_TypeClass_SHORT: - return (static_cast<sal_Int32>(*static_cast<sal_uInt16 *>(pDest)) == static_cast<sal_Int32>(*static_cast<sal_Int16 *>(pSource))); + return equal(*static_cast<sal_uInt16 *>(pDest), *static_cast<sal_Int16 *>(pSource)); case typelib_TypeClass_UNSIGNED_SHORT: - return (*static_cast<sal_uInt16 *>(pDest) == *static_cast<sal_uInt16 *>(pSource)); + return equal(*static_cast<sal_uInt16 *>(pDest), *static_cast<sal_uInt16 *>(pSource)); case typelib_TypeClass_LONG: - return (static_cast<sal_Int32>(*static_cast<sal_uInt16 *>(pDest)) == *static_cast<sal_Int32 *>(pSource)); + return equal(*static_cast<sal_uInt16 *>(pDest), *static_cast<sal_Int32 *>(pSource)); case typelib_TypeClass_UNSIGNED_LONG: - return (static_cast<sal_uInt32>(*static_cast<sal_uInt16 *>(pDest)) == *static_cast<sal_uInt32 *>(pSource)); + return equal(*static_cast<sal_uInt16 *>(pDest), *static_cast<sal_uInt32 *>(pSource)); case typelib_TypeClass_HYPER: - return (static_cast<sal_Int64>(*static_cast<sal_uInt16 *>(pDest)) == *static_cast<sal_Int64 *>(pSource)); + return equal(*static_cast<sal_uInt16 *>(pDest), *static_cast<sal_Int64 *>(pSource)); case typelib_TypeClass_UNSIGNED_HYPER: - return (static_cast<sal_uInt64>(*static_cast<sal_uInt16 *>(pDest)) == *static_cast<sal_uInt64 *>(pSource)); + return equal(*static_cast<sal_uInt16 *>(pDest), *static_cast<sal_uInt64 *>(pSource)); case typelib_TypeClass_FLOAT: return (static_cast<float>(*static_cast<sal_uInt16 *>(pDest)) == *static_cast<float *>(pSource)); case typelib_TypeClass_DOUBLE: @@ -368,20 +388,19 @@ inline bool _equalData( switch (eSourceTypeClass) { case typelib_TypeClass_BYTE: - return (*static_cast<sal_Int32 *>(pDest) == static_cast<sal_Int32>(*static_cast<sal_Int8 *>(pSource))); + return equal(*static_cast<sal_Int32 *>(pDest), *static_cast<sal_Int8 *>(pSource)); case typelib_TypeClass_SHORT: - return (*static_cast<sal_Int32 *>(pDest) == static_cast<sal_Int32>(*static_cast<sal_Int16 *>(pSource))); + return equal(*static_cast<sal_Int32 *>(pDest), *static_cast<sal_Int16 *>(pSource)); case typelib_TypeClass_UNSIGNED_SHORT: - return (*static_cast<sal_Int32 *>(pDest) == static_cast<sal_Int32>(*static_cast<sal_uInt16 *>(pSource))); + return equal(*static_cast<sal_Int32 *>(pDest), *static_cast<sal_uInt16 *>(pSource)); case typelib_TypeClass_LONG: - return (*static_cast<sal_Int32 *>(pDest) == *static_cast<sal_Int32 *>(pSource)); + return equal(*static_cast<sal_Int32 *>(pDest), *static_cast<sal_Int32 *>(pSource)); case typelib_TypeClass_UNSIGNED_LONG: - return (static_cast<sal_Int64>(*static_cast<sal_Int32 *>(pDest)) == static_cast<sal_Int64>(*static_cast<sal_uInt32 *>(pSource))); + return equal(*static_cast<sal_Int32 *>(pDest), *static_cast<sal_uInt32 *>(pSource)); case typelib_TypeClass_HYPER: - return (static_cast<sal_Int64>(*static_cast<sal_Int32 *>(pDest)) == *static_cast<sal_Int64 *>(pSource)); + return equal(*static_cast<sal_Int32 *>(pDest), *static_cast<sal_Int64 *>(pSource)); case typelib_TypeClass_UNSIGNED_HYPER: - return (*static_cast<sal_Int32 *>(pDest) >= 0 && - static_cast<sal_Int64>(*static_cast<sal_Int32 *>(pDest)) == *static_cast<sal_Int64 *>(pSource)); // same size + return equal(*static_cast<sal_Int32 *>(pDest), *static_cast<sal_Int64 *>(pSource)); case typelib_TypeClass_FLOAT: return (static_cast<float>(*static_cast<sal_Int32 *>(pDest)) == *static_cast<float *>(pSource)); case typelib_TypeClass_DOUBLE: @@ -393,19 +412,19 @@ inline bool _equalData( switch (eSourceTypeClass) { case typelib_TypeClass_BYTE: - return (static_cast<sal_Int64>(*static_cast<sal_uInt32 *>(pDest)) == static_cast<sal_Int64>(*static_cast<sal_Int8 *>(pSource))); + return equal(*static_cast<sal_uInt32 *>(pDest), *static_cast<sal_Int8 *>(pSource)); case typelib_TypeClass_SHORT: - return (static_cast<sal_Int64>(*static_cast<sal_uInt32 *>(pDest)) == static_cast<sal_Int64>(*static_cast<sal_Int16 *>(pSource))); + return equal(*static_cast<sal_uInt32 *>(pDest), *static_cast<sal_Int16 *>(pSource)); case typelib_TypeClass_UNSIGNED_SHORT: - return (*static_cast<sal_uInt32 *>(pDest) == static_cast<sal_uInt32>(*static_cast<sal_uInt16 *>(pSource))); + return equal(*static_cast<sal_uInt32 *>(pDest), *static_cast<sal_uInt16 *>(pSource)); case typelib_TypeClass_LONG: - return (static_cast<sal_Int64>(*static_cast<sal_uInt32 *>(pDest)) == static_cast<sal_Int64>(*static_cast<sal_Int32 *>(pSource))); + return equal(*static_cast<sal_uInt32 *>(pDest), *static_cast<sal_Int32 *>(pSource)); case typelib_TypeClass_UNSIGNED_LONG: - return (*static_cast<sal_uInt32 *>(pDest) == *static_cast<sal_uInt32 *>(pSource)); + return equal(*static_cast<sal_uInt32 *>(pDest), *static_cast<sal_uInt32 *>(pSource)); case typelib_TypeClass_HYPER: - return (static_cast<sal_Int64>(*static_cast<sal_uInt32 *>(pDest)) == *static_cast<sal_Int64 *>(pSource)); + return equal(*static_cast<sal_uInt32 *>(pDest), *static_cast<sal_Int64 *>(pSource)); case typelib_TypeClass_UNSIGNED_HYPER: - return (static_cast<sal_uInt64>(*static_cast<sal_uInt32 *>(pDest)) == *static_cast<sal_uInt64 *>(pSource)); + return equal(*static_cast<sal_uInt32 *>(pDest), *static_cast<sal_uInt64 *>(pSource)); case typelib_TypeClass_FLOAT: return (static_cast<float>(*static_cast<sal_uInt32 *>(pDest)) == *static_cast<float *>(pSource)); case typelib_TypeClass_DOUBLE: @@ -417,20 +436,19 @@ inline bool _equalData( switch (eSourceTypeClass) { case typelib_TypeClass_BYTE: - return (*static_cast<sal_Int64 *>(pDest) == static_cast<sal_Int64>(*static_cast<sal_Int8 *>(pSource))); + return equal(*static_cast<sal_Int64 *>(pDest), *static_cast<sal_Int8 *>(pSource)); case typelib_TypeClass_SHORT: - return (*static_cast<sal_Int64 *>(pDest) == static_cast<sal_Int64>(*static_cast<sal_Int16 *>(pSource))); + return equal(*static_cast<sal_Int64 *>(pDest), *static_cast<sal_Int16 *>(pSource)); case typelib_TypeClass_UNSIGNED_SHORT: - return (*static_cast<sal_Int64 *>(pDest) == static_cast<sal_Int64>(*static_cast<sal_uInt16 *>(pSource))); + return equal(*static_cast<sal_Int64 *>(pDest), *static_cast<sal_uInt16 *>(pSource)); case typelib_TypeClass_LONG: - return (*static_cast<sal_Int64 *>(pDest) == static_cast<sal_Int64>(*static_cast<sal_Int32 *>(pSource))); + return equal(*static_cast<sal_Int64 *>(pDest), *static_cast<sal_Int32 *>(pSource)); case typelib_TypeClass_UNSIGNED_LONG: - return (*static_cast<sal_Int64 *>(pDest) == static_cast<sal_Int64>(*static_cast<sal_uInt32 *>(pSource))); + return equal(*static_cast<sal_Int64 *>(pDest), *static_cast<sal_uInt32 *>(pSource)); case typelib_TypeClass_HYPER: - return (*static_cast<sal_Int64 *>(pDest) == *static_cast<sal_Int64 *>(pSource)); + return equal(*static_cast<sal_Int64 *>(pDest), *static_cast<sal_Int64 *>(pSource)); case typelib_TypeClass_UNSIGNED_HYPER: - return (*static_cast<sal_Int64 *>(pDest) >= 0 && - *static_cast<sal_Int64 *>(pDest) == *static_cast<sal_Int64 *>(pSource)); // same size + return equal(*static_cast<sal_Int64 *>(pDest), *static_cast<sal_Int64 *>(pSource)); case typelib_TypeClass_FLOAT: return (static_cast<float>(*static_cast<sal_Int64 *>(pDest)) == *static_cast<float *>(pSource)); case typelib_TypeClass_DOUBLE: @@ -442,23 +460,19 @@ inline bool _equalData( switch (eSourceTypeClass) { case typelib_TypeClass_BYTE: - return (*static_cast<sal_Int8 *>(pSource) >= 0 && - *static_cast<sal_uInt64 *>(pDest) == static_cast<sal_uInt64>(*static_cast<sal_Int8 *>(pSource))); + return equal(*static_cast<sal_uInt64 *>(pDest), *static_cast<sal_Int8 *>(pSource)); case typelib_TypeClass_SHORT: - return (*static_cast<sal_Int16 *>(pSource) >= 0 && - *static_cast<sal_uInt64 *>(pDest) == static_cast<sal_uInt64>(*static_cast<sal_Int16 *>(pSource))); + return equal(*static_cast<sal_uInt64 *>(pDest), *static_cast<sal_Int16 *>(pSource)); case typelib_TypeClass_UNSIGNED_SHORT: - return (*static_cast<sal_uInt64 *>(pDest) == static_cast<sal_uInt64>(*static_cast<sal_uInt16 *>(pSource))); + return equal(*static_cast<sal_uInt64 *>(pDest), *static_cast<sal_uInt16 *>(pSource)); case typelib_TypeClass_LONG: - return (*static_cast<sal_Int32 *>(pSource) >= 0 && - *static_cast<sal_uInt64 *>(pDest) == static_cast<sal_uInt64>(*static_cast<sal_Int32 *>(pSource))); + return equal(*static_cast<sal_uInt64 *>(pDest), *static_cast<sal_Int32 *>(pSource)); case typelib_TypeClass_UNSIGNED_LONG: - return (*static_cast<sal_uInt64 *>(pDest) == static_cast<sal_uInt64>(*static_cast<sal_uInt32 *>(pSource))); + return equal(*static_cast<sal_uInt64 *>(pDest), *static_cast<sal_uInt32 *>(pSource)); case typelib_TypeClass_HYPER: - return (*static_cast<sal_Int64 *>(pSource) >= 0 && - *static_cast<sal_uInt64 *>(pDest) == static_cast<sal_uInt64>(*static_cast<sal_Int64 *>(pSource))); + return equal(*static_cast<sal_uInt64 *>(pDest), *static_cast<sal_Int64 *>(pSource)); case typelib_TypeClass_UNSIGNED_HYPER: - return (*static_cast<sal_uInt64 *>(pDest) == *static_cast<sal_uInt64 *>(pSource)); + return equal(*static_cast<sal_uInt64 *>(pDest), *static_cast<sal_uInt64 *>(pSource)); case typelib_TypeClass_FLOAT: if (::floor( *static_cast<float *>(pSource) ) != *static_cast<float *>(pSource) || *static_cast<float *>(pSource) < 0) return false; |