Qt Utilities: misc/compat.h Source File

Qt Utilities 6.22.0 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
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
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
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>
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
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))
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
QT_UTILITIES_USE_Q_STRING_VIEW
#define QT_UTILITIES_USE_Q_STRING_VIEW
Definition compat.h:11
global.h
QtUtilities
!
Definition trylocker.h:8
QtUtilities::midRef
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
QtUtilities::makeStringView
StringView makeStringView(const QString &str)
Makes either a QStringView or a QStringRef depending on the Qt version.
Definition compat.h:43
QtUtilities::Utf16CharType
char16_t Utf16CharType
Definition compat.h:24
QtUtilities::StringView
QStringView StringView
Definition compat.h:32
QtUtilities::splitRef
auto splitRef(const QString &str, SplitArgs &&...args)
Splits str into QStringViews, QStringRefs or QStrings depending on the Qt version.
Definition compat.h:70