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>
This commit is contained in:
parent
19072a177b
commit
ad5c5bb541
@ -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)));
|
||||
|
Loading…
x
Reference in New Issue
Block a user