1 /**************************************************************************** 2 ** 3 ** DQt - D bindings for the Qt Toolkit 4 ** 5 ** GNU Lesser General Public License Usage 6 ** This file may be used under the terms of the GNU Lesser 7 ** General Public License version 3 as published by the Free Software 8 ** Foundation and appearing in the file LICENSE.LGPL3 included in the 9 ** packaging of this file. Please review the following information to 10 ** ensure the GNU Lesser General Public License version 3 requirements 11 ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. 12 ** 13 ****************************************************************************/ 14 module qt.core.stringliteral; 15 extern(C++): 16 17 import qt.config; 18 import qt.core.arraydata; 19 import qt.helpers; 20 21 /+ #if 0 22 #pragma qt_class(QStringLiteral) 23 #endif +/ 24 25 26 alias QStringData = QTypedArrayData!(ushort); 27 28 // all our supported compilers support Unicode string literals, 29 // even if their Q_COMPILER_UNICODE_STRING has been revoked due 30 // to lacking stdlib support. But QStringLiteral only needs the 31 // core language feature, so just use u"" here unconditionally: 32 33 alias qunicodechar = wchar; 34 35 /+ Q_STATIC_ASSERT_X(sizeof(qunicodechar) == 2, 36 "qunicodechar must typedef an integral type of size 2"); 37 38 #define QT_UNICODE_LITERAL(str) u"" str 39 #define QStringLiteral(str) \ 40 ([]() noexcept -> QString { \ 41 enum { Size = sizeof(QT_UNICODE_LITERAL(str))/2 - 1 }; \ 42 static const QStaticStringData<Size> qstring_literal = { \ 43 Q_STATIC_STRING_DATA_HEADER_INITIALIZER(Size), \ 44 QT_UNICODE_LITERAL(str) }; \ 45 QStringDataPtr holder = { qstring_literal.data_ptr() }; \ 46 return QString(holder); \ 47 }()) \ 48 /**/ 49 50 #define Q_STATIC_STRING_DATA_HEADER_INITIALIZER_WITH_OFFSET(size, offset) \ 51 { Q_REFCOUNT_INITIALIZE_STATIC, size, 0, 0, offset } \ 52 /**/ 53 54 #define Q_STATIC_STRING_DATA_HEADER_INITIALIZER(size) \ 55 Q_STATIC_STRING_DATA_HEADER_INITIALIZER_WITH_OFFSET(size, sizeof(QStringData)) \ 56 /**/ 57 58 #if QT_DEPRECATED_SINCE(5, 14) 59 # define QStringViewLiteral(str) QStringView(QT_UNICODE_LITERAL(str), QtPrivate::Deprecated) 60 #endif 61 62 template <int N> 63 struct QStaticStringData 64 { 65 QArrayData str; 66 qunicodechar data[N + 1]; 67 68 QStringData *data_ptr() const 69 { 70 Q_ASSERT(str.ref.isStatic()); 71 return const_cast<QStringData *>(static_cast<const QStringData*>(&str)); 72 } 73 }; +/ 74 75 struct QStringDataPtr 76 { 77 QStringData* ptr; 78 } 79