Qt Utilities 6.14.4
Common Qt related C++ classes and routines used by my applications such as dialogs, widgets and models
Loading...
Searching...
No Matches
compat.h
Go to the documentation of this file.
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
22namespace QtUtilities {
23
25#if (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0))
26 char16_t
27#else
28 ushort
29#endif
30 ;
31
33#ifdef QT_UTILITIES_USE_Q_STRING_VIEW
34 QStringView
35#else
36 QStringRef
37#endif
38 ;
39
43inline StringView makeStringView(const QString &str)
44{
45 return StringView(
47 &
48#endif
49 str);
50}
51
55template <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>
58inline 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
70template <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))
84QT_BEGIN_NAMESPACE
88inline QString qEnvironmentVariable(const char *varName, const QString &defaultValue)
89{
90 const auto val = qgetenv(varName);
91 return !val.isEmpty() ? QString::fromLocal8Bit(val) : defaultValue;
92}
93QT_END_NAMESPACE
94#endif
95
96#endif // QT_UTILITIES_COMPAT_H
#define QT_UTILITIES_USE_Q_STRING_VIEW
Definition compat.h:11
QStringView StringView
Definition compat.h:32
StringView midRef(const QString &str, PosType1 pos, PosType2 n=-1)
Makes either a QStringView or a QStringRef depending on the Qt version, applying "mid()" parameters.
Definition compat.h:58
StringView makeStringView(const QString &str)
Makes either a QStringView or a QStringRef depending on the Qt version.
Definition compat.h:43
char16_t Utf16CharType
Definition compat.h:24
auto splitRef(const QString &str, SplitArgs &&...args)
Splits str into QStringViews, QStringRefs or QStrings depending on the Qt version.
Definition compat.h:70