Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 10:07:31

0001 // Copyright (C) 2022 The Qt Company Ltd.
0002 // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
0003 
0004 #ifndef QMINMAX_H
0005 #define QMINMAX_H
0006 
0007 #if 0
0008 #pragma qt_class(QtMinMax)
0009 #pragma qt_sync_stop_processing
0010 #endif
0011 
0012 #include <QtCore/qassert.h>
0013 #include <QtCore/qtconfigmacros.h>
0014 
0015 #include <type_traits>
0016 
0017 QT_BEGIN_NAMESPACE
0018 
0019 namespace QTypeTraits {
0020 
0021 namespace detail {
0022 template<typename T, typename U,
0023          typename = std::enable_if_t<std::is_arithmetic_v<T> && std::is_arithmetic_v<U> &&
0024                                      std::is_floating_point_v<T> == std::is_floating_point_v<U> &&
0025                                      std::is_signed_v<T> == std::is_signed_v<U> &&
0026                                      !std::is_same_v<T, bool> && !std::is_same_v<U, bool> &&
0027                                      !std::is_same_v<T, char> && !std::is_same_v<U, char>>>
0028 struct Promoted
0029 {
0030     using type = decltype(T() + U());
0031 };
0032 }
0033 
0034 template <typename T, typename U>
0035 using Promoted = typename detail::Promoted<T, U>::type;
0036 
0037 }
0038 
0039 template <typename T>
0040 constexpr inline const T &qMin(const T &a, const T &b) { return (a < b) ? a : b; }
0041 template <typename T>
0042 constexpr inline const T &qMax(const T &a, const T &b) { return (a < b) ? b : a; }
0043 template <typename T>
0044 constexpr inline const T &qBound(const T &min, const T &val, const T &max)
0045 {
0046     Q_ASSERT(!(max < min));
0047     return qMax(min, qMin(max, val));
0048 }
0049 template <typename T, typename U>
0050 constexpr inline QTypeTraits::Promoted<T, U> qMin(const T &a, const U &b)
0051 {
0052     using P = QTypeTraits::Promoted<T, U>;
0053     P _a = a;
0054     P _b = b;
0055     return (_a < _b) ? _a : _b;
0056 }
0057 template <typename T, typename U>
0058 constexpr inline QTypeTraits::Promoted<T, U> qMax(const T &a, const U &b)
0059 {
0060     using P = QTypeTraits::Promoted<T, U>;
0061     P _a = a;
0062     P _b = b;
0063     return (_a < _b) ? _b : _a;
0064 }
0065 template <typename T, typename U>
0066 constexpr inline QTypeTraits::Promoted<T, U> qBound(const T &min, const U &val, const T &max)
0067 {
0068     Q_ASSERT(!(max < min));
0069     return qMax(min, qMin(max, val));
0070 }
0071 template <typename T, typename U>
0072 constexpr inline QTypeTraits::Promoted<T, U> qBound(const T &min, const T &val, const U &max)
0073 {
0074     using P = QTypeTraits::Promoted<T, U>;
0075     Q_ASSERT(!(P(max) < P(min)));
0076     return qMax(min, qMin(max, val));
0077 }
0078 template <typename T, typename U>
0079 constexpr inline QTypeTraits::Promoted<T, U> qBound(const U &min, const T &val, const T &max)
0080 {
0081     using P = QTypeTraits::Promoted<T, U>;
0082     Q_ASSERT(!(P(max) < P(min)));
0083     return qMax(min, qMin(max, val));
0084 }
0085 
0086 QT_END_NAMESPACE
0087 
0088 #endif // QMINMAX_H