Dokit
Internal development documentation
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Macros Pages
stringliterals_p.h
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2022-2025 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_USE_STRINGLITERALS
19 *
20 * Internal macro for using either official Qt string literals (added in Qt 6.4), or our own equivalent ones for older Qt
21 * versions. This macro can, and will, be removed entirely when Dokit no longer supports Qt versions earlier than 6.4.
22 */
23
24#include <QtGlobal>
25#if (QT_VERSION < QT_VERSION_CHECK(6, 4, 0))
26
27#if (QT_VERSION < QT_VERSION_CHECK(6, 0, 0))
28#define __DOKIT_DEST_SIZE_TYPE int
29#else
30#define __DOKIT_DEST_SIZE_TYPE qsizetype
31#endif
32
33namespace _dokit {
34inline namespace Literals {
35inline namespace StringLiterals {
36
37constexpr inline QLatin1Char operator"" _L1(char ch) noexcept
38{
39 return QLatin1Char(ch);
40}
41
42constexpr inline QLatin1String/*View*/ operator"" _L1(const char *str, size_t size) noexcept
43{
44 //return {str, qsizetype(size)};
45 return QLatin1String(str, __DOKIT_DEST_SIZE_TYPE(size));
46}
47
48inline QString operator"" _s(const char16_t *str, size_t size) noexcept
49{
50 //return QString(QStringPrivate(nullptr, const_cast<char16_t *>(str), qsizetype(size)));
51 return QString::fromUtf16(str, __DOKIT_DEST_SIZE_TYPE(size));
52}
53
54} } } // _dokit::Literals::StringLiterals
55
56#undef __DOKIT_DEST_SIZE_TYPE
57
58#define DOKIT_USE_STRINGLITERALS using namespace _dokit::Literals::StringLiterals;
59#else
60#define DOKIT_USE_STRINGLITERALS using namespace Qt::Literals::StringLiterals;
61#endif
62
63/// \endcond
64
65#endif // DOKIT_STRINGLITERALS_P_H
QString fromUtf16(const ushort *unicode, int size)