1// Copyright (C) 2021 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#include <QtCore/qglobal.h>
5#include <QtCore/qversionnumber.h>
6
7#ifndef QOPERATINGSYSTEMVERSION_H
8#define QOPERATINGSYSTEMVERSION_H
9
10QT_BEGIN_NAMESPACE
11
12class QString;
13
14class QOperatingSystemVersionBase
15{
16public:
17 // ### Qt 7: Keep synchronized with the copy in QOperatingSystemVersion until Qt7,
18 // then remove this comment :)
19 enum OSType {
20 Unknown = 0,
21 Windows,
22 MacOS,
23 IOS,
24 TvOS,
25 WatchOS,
26 Android
27 };
28
29 constexpr QOperatingSystemVersionBase(OSType osType,
30 int vmajor, int vminor = -1, int vmicro = -1)
31 : m_os(osType),
32 m_major(vmajor),
33 m_minor(vminor),
34 m_micro(vmicro)
35 { }
36
37 static Q_CORE_EXPORT QOperatingSystemVersionBase current();
38 static Q_CORE_EXPORT QString name(QOperatingSystemVersionBase osversion);
39 static Q_CORE_EXPORT bool isAnyOfType(std::initializer_list<OSType> types, OSType type);
40
41 static constexpr OSType currentType()
42 {
43#if defined(Q_OS_WIN)
44 return Windows;
45#elif defined(Q_OS_MACOS)
46 return MacOS;
47#elif defined(Q_OS_IOS)
48 return IOS;
49#elif defined(Q_OS_TVOS)
50 return TvOS;
51#elif defined(Q_OS_WATCHOS)
52 return WatchOS;
53#elif defined(Q_OS_ANDROID)
54 return Android;
55#else
56 return Unknown;
57#endif
58 }
59
60 inline QVersionNumber version() const { return QVersionNumber(m_major, m_minor, m_micro); }
61
62 constexpr int majorVersion() const { return m_major; }
63 constexpr int minorVersion() const { return m_minor; }
64 constexpr int microVersion() const { return m_micro; }
65
66 constexpr int segmentCount() const
67 { return m_micro >= 0 ? 3 : m_minor >= 0 ? 2 : m_major >= 0 ? 1 : 0; }
68
69 inline bool isAnyOfType(std::initializer_list<OSType> types) const
70 {
71 return QOperatingSystemVersionBase::isAnyOfType(types, type: type());
72 }
73 constexpr OSType type() const { return m_os; }
74 inline QString name() const { return name(osversion: *this); }
75
76 friend bool operator>(QOperatingSystemVersionBase lhs, QOperatingSystemVersionBase rhs)
77 { return lhs.type() == rhs.type() && QOperatingSystemVersionBase::compare(v1: lhs, v2: rhs) > 0; }
78
79 friend bool operator>=(QOperatingSystemVersionBase lhs, QOperatingSystemVersionBase rhs)
80 { return lhs.type() == rhs.type() && QOperatingSystemVersionBase::compare(v1: lhs, v2: rhs) >= 0; }
81
82 friend bool operator<(QOperatingSystemVersionBase lhs, QOperatingSystemVersionBase rhs)
83 { return lhs.type() == rhs.type() && QOperatingSystemVersionBase::compare(v1: lhs, v2: rhs) < 0; }
84
85 friend bool operator<=(QOperatingSystemVersionBase lhs, QOperatingSystemVersionBase rhs)
86 { return lhs.type() == rhs.type() && QOperatingSystemVersionBase::compare(v1: lhs, v2: rhs) <= 0; }
87
88protected:
89 static Q_CORE_EXPORT int compare(QOperatingSystemVersionBase v1,
90 QOperatingSystemVersionBase v2);
91
92 QOperatingSystemVersionBase() = default;
93private:
94 static QOperatingSystemVersionBase current_impl();
95
96
97 OSType m_os;
98 int m_major;
99 int m_minor;
100 int m_micro;
101};
102
103// ### Qt 7: Un-export the class, export relevant functions. Remove the enum.
104class Q_CORE_EXPORT QOperatingSystemVersion : public QOperatingSystemVersionBase
105{
106public:
107 // ### Qt7: Remove. Keep synchronized with QOperatingSystemVersionBase::OSType until then!
108#if QT_VERSION < QT_VERSION_CHECK(7, 0, 0) && !defined(QT_BOOTSTRAPPED)
109 enum OSType {
110 Unknown = 0,
111 Windows,
112 MacOS,
113 IOS,
114 TvOS,
115 WatchOS,
116 Android
117 };
118#endif
119
120 // ### Qt7: remove the branch with static const variables. Then group and sort the inline ones.
121 // Since the exported variables emit symbols they cannot be cherry-picked back to patch-releases
122 // without breaking our BC promises. They must be fully inline but we cannot make that change
123 // until Qt7
124 // @note: New entries should be added after the if-def-ery until Qt 7!!
125#if QT_VERSION < QT_VERSION_CHECK(7, 0, 0) && !defined(QT_BOOTSTRAPPED)
126 static const QOperatingSystemVersion Windows7;
127 static const QOperatingSystemVersion Windows8;
128 static const QOperatingSystemVersion Windows8_1;
129 static const QOperatingSystemVersion Windows10;
130
131 static const QOperatingSystemVersion OSXMavericks;
132 static const QOperatingSystemVersion OSXYosemite;
133 static const QOperatingSystemVersion OSXElCapitan;
134 static const QOperatingSystemVersion MacOSSierra;
135 static const QOperatingSystemVersion MacOSHighSierra;
136 static const QOperatingSystemVersion MacOSMojave;
137 static const QOperatingSystemVersion MacOSCatalina;
138 static const QOperatingSystemVersion MacOSBigSur;
139 static const QOperatingSystemVersion MacOSMonterey;
140
141 static const QOperatingSystemVersion AndroidJellyBean;
142 static const QOperatingSystemVersion AndroidJellyBean_MR1;
143 static const QOperatingSystemVersion AndroidJellyBean_MR2;
144 static const QOperatingSystemVersion AndroidKitKat;
145 static const QOperatingSystemVersion AndroidLollipop;
146 static const QOperatingSystemVersion AndroidLollipop_MR1;
147 static const QOperatingSystemVersion AndroidMarshmallow;
148 static const QOperatingSystemVersion AndroidNougat;
149 static const QOperatingSystemVersion AndroidNougat_MR1;
150 static const QOperatingSystemVersion AndroidOreo;
151 static const QOperatingSystemVersion AndroidOreo_MR1;
152 static const QOperatingSystemVersion AndroidPie;
153 static const QOperatingSystemVersion Android10;
154 static const QOperatingSystemVersion Android11;
155#else
156 static constexpr QOperatingSystemVersionBase Windows7 { QOperatingSystemVersionBase::Windows, 6, 1 };
157 static constexpr QOperatingSystemVersionBase Windows8 { QOperatingSystemVersionBase::Windows, 6, 2 };
158 static constexpr QOperatingSystemVersionBase Windows8_1 { QOperatingSystemVersionBase::Windows, 6, 3 };
159 static constexpr QOperatingSystemVersionBase Windows10 { QOperatingSystemVersionBase::Windows, 10 };
160
161 static constexpr QOperatingSystemVersionBase OSXMavericks { QOperatingSystemVersionBase::MacOS, 10, 9 };
162 static constexpr QOperatingSystemVersionBase OSXYosemite { QOperatingSystemVersionBase::MacOS, 10, 10 };
163 static constexpr QOperatingSystemVersionBase OSXElCapitan { QOperatingSystemVersionBase::MacOS, 10, 11 };
164 static constexpr QOperatingSystemVersionBase MacOSSierra { QOperatingSystemVersionBase::MacOS, 10, 12 };
165 static constexpr QOperatingSystemVersionBase MacOSHighSierra { QOperatingSystemVersionBase::MacOS, 10, 13 };
166 static constexpr QOperatingSystemVersionBase MacOSMojave { QOperatingSystemVersionBase::MacOS, 10, 14 };
167 static constexpr QOperatingSystemVersionBase MacOSCatalina { QOperatingSystemVersionBase::MacOS, 10, 15 };
168 static constexpr QOperatingSystemVersionBase MacOSBigSur = { QOperatingSystemVersionBase::MacOS, 11, 0 };
169 static constexpr QOperatingSystemVersionBase MacOSMonterey = { QOperatingSystemVersionBase::MacOS, 12, 0 };
170
171 static constexpr QOperatingSystemVersionBase AndroidJellyBean { QOperatingSystemVersionBase::Android, 4, 1 };
172 static constexpr QOperatingSystemVersionBase AndroidJellyBean_MR1 { QOperatingSystemVersionBase::Android, 4, 2 };
173 static constexpr QOperatingSystemVersionBase AndroidJellyBean_MR2 { QOperatingSystemVersionBase::Android, 4, 3 };
174 static constexpr QOperatingSystemVersionBase AndroidKitKat { QOperatingSystemVersionBase::Android, 4, 4 };
175 static constexpr QOperatingSystemVersionBase AndroidLollipop { QOperatingSystemVersionBase::Android, 5, 0 };
176 static constexpr QOperatingSystemVersionBase AndroidLollipop_MR1 { QOperatingSystemVersionBase::Android, 5, 1 };
177 static constexpr QOperatingSystemVersionBase AndroidMarshmallow { QOperatingSystemVersionBase::Android, 6, 0 };
178 static constexpr QOperatingSystemVersionBase AndroidNougat { QOperatingSystemVersionBase::Android, 7, 0 };
179 static constexpr QOperatingSystemVersionBase AndroidNougat_MR1 { QOperatingSystemVersionBase::Android, 7, 1 };
180 static constexpr QOperatingSystemVersionBase AndroidOreo { QOperatingSystemVersionBase::Android, 8, 0 };
181 static constexpr QOperatingSystemVersionBase AndroidOreo_MR1 { QOperatingSystemVersionBase::Android, 8, 1 };
182 static constexpr QOperatingSystemVersionBase AndroidPie { QOperatingSystemVersionBase::Android, 9, 0 };
183 static constexpr QOperatingSystemVersionBase Android10 { QOperatingSystemVersionBase::Android, 10, 0 };
184 static constexpr QOperatingSystemVersionBase Android11 { QOperatingSystemVersionBase::Android, 11, 0 };
185#endif // New (static constexpr) entries go here, only cherry-pick as far back as 6.3 (QTBUG-97808):
186
187 static constexpr QOperatingSystemVersionBase Windows10_1809 { QOperatingSystemVersionBase::Windows, 10, 0, 17763 }; // RS5
188 static constexpr QOperatingSystemVersionBase Windows10_1903 { QOperatingSystemVersionBase::Windows, 10, 0, 18362 }; // 19H1
189 static constexpr QOperatingSystemVersionBase Windows10_1909 { QOperatingSystemVersionBase::Windows, 10, 0, 18363 }; // 19H2
190 static constexpr QOperatingSystemVersionBase Windows10_2004 { QOperatingSystemVersionBase::Windows, 10, 0, 19041 }; // 20H1
191 static constexpr QOperatingSystemVersionBase Windows10_20H2 { QOperatingSystemVersionBase::Windows, 10, 0, 19042 };
192 static constexpr QOperatingSystemVersionBase Windows10_21H1 { QOperatingSystemVersionBase::Windows, 10, 0, 19043 };
193 static constexpr QOperatingSystemVersionBase Windows10_21H2 { QOperatingSystemVersionBase::Windows, 10, 0, 19044 };
194 static constexpr QOperatingSystemVersionBase Windows10_22H2 { QOperatingSystemVersionBase::Windows, 10, 0, 19045 };
195 static constexpr QOperatingSystemVersionBase Windows11 { QOperatingSystemVersionBase::Windows, 10, 0, 22000 };
196 static constexpr QOperatingSystemVersionBase Windows11_21H2 = Windows11;
197 static constexpr QOperatingSystemVersionBase Windows11_22H2 { QOperatingSystemVersionBase::Windows, 10, 0, 22621 };
198
199 static constexpr QOperatingSystemVersionBase Android12 { QOperatingSystemVersionBase::Android, 12, 0 };
200 static constexpr QOperatingSystemVersionBase Android12L { QOperatingSystemVersionBase::Android, 12, 0 };
201 static constexpr QOperatingSystemVersionBase Android13 { QOperatingSystemVersionBase::Android, 13, 0 };
202
203 static constexpr QOperatingSystemVersionBase MacOSVentura { QOperatingSystemVersionBase::MacOS, 13, 0 };
204 static constexpr QOperatingSystemVersionBase MacOSSonoma { QOperatingSystemVersionBase::MacOS, 14, 0 };
205
206 constexpr QOperatingSystemVersion(const QOperatingSystemVersionBase &osversion)
207 : QOperatingSystemVersionBase(osversion) {}
208
209 constexpr QOperatingSystemVersion(OSType osType, int vmajor, int vminor = -1, int vmicro = -1)
210 : QOperatingSystemVersionBase(QOperatingSystemVersionBase::OSType(osType), vmajor, vminor,
211 vmicro)
212 {
213 }
214
215 static QOperatingSystemVersion current();
216
217 static constexpr OSType currentType()
218 {
219 return OSType(QOperatingSystemVersionBase::currentType());
220 }
221
222 QVersionNumber version() const { return QOperatingSystemVersionBase::version(); }
223
224 constexpr int majorVersion() const { return QOperatingSystemVersionBase::majorVersion(); }
225 constexpr int minorVersion() const { return QOperatingSystemVersionBase::minorVersion(); }
226 constexpr int microVersion() const { return QOperatingSystemVersionBase::microVersion(); }
227
228 constexpr int segmentCount() const
229 { return QOperatingSystemVersionBase::segmentCount(); }
230
231 constexpr OSType type() const { return OSType(QOperatingSystemVersionBase::type()); }
232 bool isAnyOfType(std::initializer_list<OSType> types) const;
233 QString name() const;
234
235private:
236 QOperatingSystemVersion() = default;
237
238#if QT_CORE_REMOVED_SINCE(6, 3)
239 // ### Qt 7: Remove. It's only here for backwards compat with previous inline calls.
240 [[maybe_unused]] static int compare(const QOperatingSystemVersion &v1,
241 const QOperatingSystemVersion &v2);
242#endif
243};
244Q_DECLARE_TYPEINFO(QOperatingSystemVersion, Q_PRIMITIVE_TYPE);
245
246#ifndef QT_NO_DEBUG_STREAM
247class QDebug;
248Q_CORE_EXPORT QDebug operator<<(QDebug debug, const QOperatingSystemVersion &ov);
249#endif
250
251QT_END_NAMESPACE
252
253#endif // QOPERATINGSYSTEMVERSION_H
254

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