Debian Codegraph @

misc/compat.h

raw symbols SCIP
1 #ifndef QT_UTILITIES_COMPAT_H
2 #define QT_UTILITIES_COMPAT_H
3
4 #include "../global.h"
5
6 #include <c++utilities/misc/traits.h>
7
8 #include <QtGlobal>
9
10 #if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
11 #define QT_UTILITIES_USE_Q_STRING_VIEW
12 #endif
13 // note: QStringView has been available since Qt 5.10 but for a consistent ABI/API regardless which
14 // Qt 5.x version is used it makes sense to stick to QStringRef when using Qt 5.
15
16 #ifdef QT_UTILITIES_USE_Q_STRING_VIEW
17 #include <QStringView>
18 #else
19 #include <QStringRef>
20 #endif
21
22 namespace QtUtilities {
23
24 using Utf16CharType =
25 #if (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0))
26 char16_t
27 #else
28 ushort
29 #endif
30 ;
31
32 using StringView =
33 #ifdef QT_UTILITIES_USE_Q_STRING_VIEW
34 QStringView
35 #else
36 QStringRef
37 #endif
38 ;
39
40 /*!
41 * \brief Makes either a QStringView or a QStringRef depending on the Qt version.
42 */
43 inline StringView makeStringView(const QString &str)
44 {
45 return StringView(
46 #ifndef QT_UTILITIES_USE_Q_STRING_VIEW
47 &
48 #endif
49 str);
50 }
51
52 /*!
53 * \brief Makes either a QStringView or a QStringRef depending on the Qt version, applying "mid()" parameters.
54 */
55 template <typename PosType1, typename PosType2 = PosType1,
56 CppUtilities::Traits::EnableIf<std::is_integral<PosType1>, std::is_signed<PosType1>, std::is_integral<PosType2>, std::is_signed<PosType2>>
57 * = nullptr>
58 inline StringView midRef(const QString &str, PosType1 pos, PosType2 n = -1)
59 {
60 #ifdef QT_UTILITIES_USE_Q_STRING_VIEW
61 return QStringView(str).mid(pos, n);
62 #else
63 return str.midRef(pos, n);
64 #endif
65 }
66
67 /*!
68 * \brief Splits \a str into QStringViews, QStringRefs or QStrings depending on the Qt version.
69 */
70 template <class... SplitArgs> inline auto splitRef(const QString &str, SplitArgs &&...args)
71 {
72 #if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
73 return QStringView(str).split(std::forward<SplitArgs>(args)...);
74 #elif QT_VERSION >= QT_VERSION_CHECK(5, 4, 0)
75 return str.splitRef(std::forward<SplitArgs>(args)...);
76 #else
77 return str.split(std::forward<SplitArgs>(args)...);
78 #endif
79 }
80
81 } // namespace QtUtilities
82
83 #if (QT_VERSION < QT_VERSION_CHECK(5, 10, 0))
84 QT_BEGIN_NAMESPACE
85 /*!
86 * \brief Provides a fallback for qEnvironmentVariable() when using old Qt versions.
87 */
88 inline QString qEnvironmentVariable(const char *varName, const QString &defaultValue)
89 {
90 const auto val = qgetenv(varName);
91 return !val.isEmpty() ? QString::fromLocal8Bit(val) : defaultValue;
92 }
93 QT_END_NAMESPACE
94 #endif
95
96 #endif // QT_UTILITIES_COMPAT_H