Dokit
Internal development documentation
Loading...
Searching...
No Matches
stringliterals_p.h
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2022-2026 Paul Colby <git@colby.id.au>
2// SPDX-License-Identifier: LGPL-3.0-or-later
3
4/*!
5 * \file
6 * Declares the DOKIT_USE_STRINGLITERALS macro, and related functions.
7 *
8 * This is only required to support Qt versions earlier than 6.4, since Qt added string literal operators in that
9 * versions. This header can, and will, be removed entirely when Dokit no longer supports Qt versions earlier than 6.4.
10 */
11
12#ifndef DOKIT_STRINGLITERALS_P_H
13#define DOKIT_STRINGLITERALS_P_H
14
15/// \cond internal
16
17/*!
18 * \def DOKIT_STRING_INDEX_TYPE
19 *
20 * Internal macro for matching the index type used by QString methods. Specifically, from Qt 6.0.0 onwards, QString
21 * (and related classes, and methods) use qsizetype for index positions, while Qt5 used int instead.
22 *
23 * \todo Remove this macro when Dokit no longer supports Qt5.
24 */
25
26/*!
27 * \def DOKIT_USE_STRINGLITERALS
28 *
29 * Internal macro for using either official Qt string literals (added in Qt 6.4), or our own equivalent ones for older
30 * Qt versions.
31 *
32 * \todo Remove this macro when Dokit no longer supports Qt versions earlier than 6.4.
33 */
34
35#include <QtGlobal>
36
37// Although qsizetype was added in Qt 5.10.0, it wasn't used for QString indexes until 6.0.
38#if (QT_VERSION < QT_VERSION_CHECK(6, 0, 0))
39#define DOKIT_STRING_INDEX_TYPE int
40#else
41#define DOKIT_STRING_INDEX_TYPE qsizetype
42#endif
43
44#if (QT_VERSION < QT_VERSION_CHECK(6, 4, 0)) // String literals added in Qt 6.4.0.
45
46namespace _dokit {
47inline namespace Literals {
48inline namespace StringLiterals {
49
50constexpr inline QLatin1Char operator""_L1(char ch) noexcept
51{
52 return QLatin1Char(ch);
53}
54
55constexpr inline QLatin1String/*View*/ operator""_L1(const char *str, size_t size) noexcept
56{
57 //return {str, qsizetype(size)};
58 return QLatin1String(str, DOKIT_STRING_INDEX_TYPE(size));
59}
60
61inline QString operator""_s(const char16_t *str, size_t size) noexcept
62{
63 //return QString(QStringPrivate(nullptr, const_cast<char16_t *>(str), qsizetype(size)));
65}
66
67} } } // _dokit::Literals::StringLiterals
68
69#define DOKIT_USE_STRINGLITERALS using namespace _dokit::Literals::StringLiterals;
70#else
71#define DOKIT_USE_STRINGLITERALS using namespace Qt::Literals::StringLiterals;
72#endif
73
74/// \endcond
75
76#endif // DOKIT_STRINGLITERALS_P_H
QString fromUtf16(const ushort *unicode, int size)
#define DOKIT_STRING_INDEX_TYPE
Internal macro for matching the index type used by QString methods.