1// Copyright (C) 2022 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4#ifndef QMINMAX_H
5#define QMINMAX_H
6
7#if 0
8#pragma qt_class(QtMinMax)
9#pragma qt_sync_stop_processing
10#endif
11
12#include <QtCore/qassert.h>
13#include <QtCore/qtconfigmacros.h>
14
15#include <type_traits>
16
17QT_BEGIN_NAMESPACE
18
19namespace QTypeTraits {
20
21namespace detail {
22template<typename T, typename U,
23 typename = std::enable_if_t<std::is_arithmetic_v<T> && std::is_arithmetic_v<U> &&
24 std::is_floating_point_v<T> == std::is_floating_point_v<U> &&
25 std::is_signed_v<T> == std::is_signed_v<U> &&
26 !std::is_same_v<T, bool> && !std::is_same_v<U, bool> &&
27 !std::is_same_v<T, char> && !std::is_same_v<U, char>>>
28struct Promoted
29{
30 using type = decltype(T() + U());
31};
32}
33
34template <typename T, typename U>
35using Promoted = typename detail::Promoted<T, U>::type;
36
37}
38
39template <typename T>
40constexpr inline const T &qMin(const T &a, const T &b) { return (a < b) ? a : b; }
41template <typename T>
42constexpr inline const T &qMax(const T &a, const T &b) { return (a < b) ? b : a; }
43template <typename T>
44constexpr inline const T &qBound(const T &min, const T &val, const T &max)
45{
46 Q_ASSERT(!(max < min));
47 return qMax(min, qMin(max, val));
48}
49template <typename T, typename U>
50constexpr inline QTypeTraits::Promoted<T, U> qMin(const T &a, const U &b)
51{
52 using P = QTypeTraits::Promoted<T, U>;
53 P _a = a;
54 P _b = b;
55 return (_a < _b) ? _a : _b;
56}
57template <typename T, typename U>
58constexpr inline QTypeTraits::Promoted<T, U> qMax(const T &a, const U &b)
59{
60 using P = QTypeTraits::Promoted<T, U>;
61 P _a = a;
62 P _b = b;
63 return (_a < _b) ? _b : _a;
64}
65template <typename T, typename U>
66constexpr inline QTypeTraits::Promoted<T, U> qBound(const T &min, const U &val, const T &max)
67{
68 Q_ASSERT(!(max < min));
69 return qMax(min, qMin(max, val));
70}
71template <typename T, typename U>
72constexpr inline QTypeTraits::Promoted<T, U> qBound(const T &min, const T &val, const U &max)
73{
74 using P = QTypeTraits::Promoted<T, U>;
75 Q_ASSERT(!(P(max) < P(min)));
76 return qMax(min, qMin(max, val));
77}
78template <typename T, typename U>
79constexpr inline QTypeTraits::Promoted<T, U> qBound(const U &min, const T &val, const T &max)
80{
81 using P = QTypeTraits::Promoted<T, U>;
82 Q_ASSERT(!(P(max) < P(min)));
83 return qMax(min, qMin(max, val));
84}
85
86QT_END_NAMESPACE
87
88#endif // QMINMAX_H
89

source code of qtbase/src/corelib/global/qminmax.h