Qt Utilities  6.4.1
Common Qt related C++ classes and routines used by my applications such as dialogs, widgets and models
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 <QtGlobal>
7 
8 #if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
9 #define QT_UTILITIES_USE_Q_STRING_VIEW
10 #endif
11 // note: QStringView has been available since Qt 5.10 but for a consistent ABI/API regardless which
12 // Qt 5.x version is used it makes sense to stick to QStringRef when using Qt 5.
13 
14 #ifdef QT_UTILITIES_USE_Q_STRING_VIEW
15 #include <QStringView>
16 #else
17 #include <QStringRef>
18 #endif
19 
20 namespace QtUtilities {
21 
23 #if (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0))
24  char16_t
25 #else
26  ushort
27 #endif
28  ;
29 
30 using StringView =
31 #ifdef QT_UTILITIES_USE_Q_STRING_VIEW
32  QStringView
33 #else
34  QStringRef
35 #endif
36  ;
37 
41 inline StringView makeStringView(const QString &str)
42 {
43  return StringView(
45  &
46 #endif
47  str);
48 }
49 
53 inline StringView midRef(const QString &str, int pos, int n = -1)
54 {
55 #ifdef QT_UTILITIES_USE_Q_STRING_VIEW
56  return QStringView(str).mid(pos, n);
57 #else
58  return str.midRef(pos, n);
59 #endif
60 }
61 
65 template <class... SplitArgs> inline auto splitRef(const QString &str, SplitArgs &&...args)
66 {
67 #if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
68  return QStringView(str).split(std::forward<SplitArgs>(args)...);
69 #elif QT_VERSION >= QT_VERSION_CHECK(5, 4, 0)
70  return str.splitRef(std::forward<SplitArgs>(args)...);
71 #else
72  return str.split(std::forward<SplitArgs>(args)...);
73 #endif
74 }
75 
76 } // namespace QtUtilities
77 
78 #endif // QT_UTILITIES_COMPAT_H
#define QT_UTILITIES_USE_Q_STRING_VIEW
Definition: compat.h:9
StringView makeStringView(const QString &str)
Makes either a QStringView or a QStringRef depending on the Qt version.
Definition: compat.h:41
StringView midRef(const QString &str, int pos, int n=-1)
Makes either a QStringView or a QStringRef depending on the Qt version, applying "mid()" parameters.
Definition: compat.h:53
char16_t Utf16CharType
Definition: compat.h:28
QStringView StringView
Definition: compat.h:36
auto splitRef(const QString &str, SplitArgs &&...args)
Splits str into QStringViews, QStringRefs or QStrings depending on the Qt version.
Definition: compat.h:65