diff options
author | Giuseppe D'Angelo <giuseppe.dangelo@kdab.com> | 2021-11-30 13:44:34 +0100 |
---|---|---|
committer | Giuseppe D'Angelo <giuseppe.dangelo@kdab.com> | 2021-12-06 12:28:41 +0100 |
commit | ad5c5bb541ae20a205ac07122153b302dee1d3e1 (patch) | |
tree | d4f14ed3614e92553ba24209b4b778e812e59a58 /src | |
parent | 19072a177bf72c28082a377f37ed19cbb06f9740 (diff) |
qBound: add an assert on !(upper < lower)
It's a precondition and we might just as well check it, given that
people have actually got the order of the arguments wrong (cf.
QTBUG-69330).
Unfortunately, Q_ASSERT is defined below qBound in qglobal.h, so I had
to reshuffle some code around.
Change-Id: I82e52bcb863ff8c96594817e0cd5d7d2abe2e57e
Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org>
Reviewed-by: Lars Knoll <lars.knoll@qt.io>
Reviewed-by: Edward Welbourne <edward.welbourne@qt.io>
Diffstat (limited to 'src')
-rw-r--r-- | src/corelib/global/qglobal.h | 120 |
1 files changed, 67 insertions, 53 deletions
diff --git a/src/corelib/global/qglobal.h b/src/corelib/global/qglobal.h index 1a3f3d6482..25002efdca 100644 --- a/src/corelib/global/qglobal.h +++ b/src/corelib/global/qglobal.h @@ -809,59 +809,6 @@ constexpr inline qint64 qRound64(float d) { return d >= 0.0f ? qint64(d + 0.5f) : qint64(d - 0.5f); } #endif -namespace QTypeTraits { - -namespace detail { -template<typename T, typename U, - typename = std::enable_if_t<std::is_arithmetic_v<T> && std::is_arithmetic_v<U> && - std::is_floating_point_v<T> == std::is_floating_point_v<U> && - std::is_signed_v<T> == std::is_signed_v<U> && - !std::is_same_v<T, bool> && !std::is_same_v<U, bool> && - !std::is_same_v<T, char> && !std::is_same_v<U, char>>> -struct Promoted -{ - using type = decltype(T() + U()); -}; -} - -template <typename T, typename U> -using Promoted = typename detail::Promoted<T, U>::type; - -} - -template <typename T> -constexpr inline const T &qMin(const T &a, const T &b) { return (a < b) ? a : b; } -template <typename T> -constexpr inline const T &qMax(const T &a, const T &b) { return (a < b) ? b : a; } -template <typename T> -constexpr inline const T &qBound(const T &min, const T &val, const T &max) -{ return qMax(min, qMin(max, val)); } -template <typename T, typename U> -constexpr inline QTypeTraits::Promoted<T, U> qMin(const T &a, const U &b) -{ - using P = QTypeTraits::Promoted<T, U>; - P _a = a; - P _b = b; - return (_a < _b) ? _a : _b; -} -template <typename T, typename U> -constexpr inline QTypeTraits::Promoted<T, U> qMax(const T &a, const U &b) -{ - using P = QTypeTraits::Promoted<T, U>; - P _a = a; - P _b = b; - return (_a < _b) ? _b : _a; -} -template <typename T, typename U> -constexpr inline QTypeTraits::Promoted<T, U> qBound(const T &min, const U &val, const T &max) -{ return qMax(min, qMin(max, val)); } -template <typename T, typename U> -constexpr inline QTypeTraits::Promoted<T, U> qBound(const T &min, const T &val, const U &max) -{ return qMax(min, qMin(max, val)); } -template <typename T, typename U> -constexpr inline QTypeTraits::Promoted<T, U> qBound(const U &min, const T &val, const T &max) -{ return qMax(min, qMin(max, val)); } - #ifndef Q_FORWARD_DECLARE_OBJC_CLASS # ifdef __OBJC__ # define Q_FORWARD_DECLARE_OBJC_CLASS(classname) @class classname @@ -1084,6 +1031,73 @@ typedef void (*QFunctionPointer)(); # define Q_UNIMPLEMENTED() qWarning("Unimplemented code.") #endif +namespace QTypeTraits { + +namespace detail { +template<typename T, typename U, + typename = std::enable_if_t<std::is_arithmetic_v<T> && std::is_arithmetic_v<U> && + std::is_floating_point_v<T> == std::is_floating_point_v<U> && + std::is_signed_v<T> == std::is_signed_v<U> && + !std::is_same_v<T, bool> && !std::is_same_v<U, bool> && + !std::is_same_v<T, char> && !std::is_same_v<U, char>>> +struct Promoted +{ + using type = decltype(T() + U()); +}; +} + +template <typename T, typename U> +using Promoted = typename detail::Promoted<T, U>::type; + +} + +template <typename T> +constexpr inline const T &qMin(const T &a, const T &b) { return (a < b) ? a : b; } +template <typename T> +constexpr inline const T &qMax(const T &a, const T &b) { return (a < b) ? b : a; } +template <typename T> +constexpr inline const T &qBound(const T &min, const T &val, const T &max) +{ + Q_ASSERT(!(max < min)); + return qMax(min, qMin(max, val)); +} +template <typename T, typename U> +constexpr inline QTypeTraits::Promoted<T, U> qMin(const T &a, const U &b) +{ + using P = QTypeTraits::Promoted<T, U>; + P _a = a; + P _b = b; + return (_a < _b) ? _a : _b; +} +template <typename T, typename U> +constexpr inline QTypeTraits::Promoted<T, U> qMax(const T &a, const U &b) +{ + using P = QTypeTraits::Promoted<T, U>; + P _a = a; + P _b = b; + return (_a < _b) ? _b : _a; +} +template <typename T, typename U> +constexpr inline QTypeTraits::Promoted<T, U> qBound(const T &min, const U &val, const T &max) +{ + Q_ASSERT(!(max < min)); + return qMax(min, qMin(max, val)); +} +template <typename T, typename U> +constexpr inline QTypeTraits::Promoted<T, U> qBound(const T &min, const T &val, const U &max) +{ + using P = QTypeTraits::Promoted<T, U>; + Q_ASSERT(!(P(max) < P(min))); + return qMax(min, qMin(max, val)); +} +template <typename T, typename U> +constexpr inline QTypeTraits::Promoted<T, U> qBound(const U &min, const T &val, const T &max) +{ + using P = QTypeTraits::Promoted<T, U>; + Q_ASSERT(!(P(max) < P(min))); + return qMax(min, qMin(max, val)); +} + [[nodiscard]] constexpr bool qFuzzyCompare(double p1, double p2) { return (qAbs(p1 - p2) * 1000000000000. <= qMin(qAbs(p1), qAbs(p2))); |