diff options
Diffstat (limited to 'src/corelib/global')
-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))); |