summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorThiago Macieira <thiago@kde.org>2011-07-08 14:10:02 +0200
committerQt by Nokia <qt-info@nokia.com>2011-07-11 11:15:53 +0200
commitcd80fcb5d6db9d99684b94a90d2c798b712442c4 (patch)
treea44c79429038f0574e640f93adda5f2c14f7a831
parent6efe729881303f2198d61817174e9695aea63246 (diff)
Use lambdas for generating static data for QStringLiteral
This means it will work on C++0x compilers that support lambdas (all of them once they've completed supporting the functionality). Olivier had the idea and the initial code. Change-Id: I11ef7da4058ed18f4ea99ada070891a68ed54f55 Reviewed-on: http://codereview.qt.nokia.com/1380 Reviewed-by: Qt Sanity Bot <qt_sanity_bot@ovi.com> Reviewed-by: Olivier Goffart <olivier.goffart@nokia.com>
-rw-r--r--src/corelib/tools/qstring.h59
1 files changed, 33 insertions, 26 deletions
diff --git a/src/corelib/tools/qstring.h b/src/corelib/tools/qstring.h
index de973a82fb..2251b64430 100644
--- a/src/corelib/tools/qstring.h
+++ b/src/corelib/tools/qstring.h
@@ -96,28 +96,16 @@ template<int N> struct QConstStringDataPtr
const QConstStringData<N> *ptr;
};
-#if defined(Q_CC_GNU)
-// We need to create a QStringData in the .rodata section of memory
-// and the only way to do that is to create a "static const" variable.
-// To do that, we need the __extension__ {( )} trick which only GCC supports
-
-# if defined(Q_COMPILER_UNICODE_STRINGS)
+#if defined(Q_COMPILER_UNICODE_STRINGS)
template<int n> struct QConstStringData
{
const QStringData str;
const char16_t data[n];
operator const QStringData &() const { return str; }
};
+#define QT_QSTRING_UNICODE_MARKER u""
-# define QStringLiteral(str) \
- __extension__ ({ \
- enum { Size = sizeof(u"" str)/2 }; \
- static const QConstStringData<Size> qstring_literal = \
- { { Q_REFCOUNT_INITIALIZER(-1), Size -1, 0, 0, { 0 } }, u"" str }; \
- QConstStringDataPtr<Size> holder = { &qstring_literal }; \
- holder; })
-
-# elif defined(Q_OS_WIN) || (defined(__SIZEOF_WCHAR_T__) && __SIZEOF_WCHAR_T__ == 2) || defined(WCHAR_MAX) && (WCHAR_MAX - 0 < 65536)
+#elif defined(Q_OS_WIN) || (defined(__SIZEOF_WCHAR_T__) && __SIZEOF_WCHAR_T__ == 2) || defined(WCHAR_MAX) && (WCHAR_MAX - 0 < 65536)
// wchar_t is 2 bytes
template<int n> struct QConstStringData
{
@@ -125,26 +113,45 @@ template<int n> struct QConstStringData
const wchar_t data[n];
operator const QStringData &() const { return str; }
};
-# define QStringLiteral(str) \
+#define QT_QSTRING_UNICODE_MARKER L""
+
+#else
+template<int n> struct QConstStringData
+{
+ const QStringData str;
+ const ushort data[n];
+ operator const QStringData &() const { return str; }
+};
+#endif
+
+#if defined(QT_QSTRING_UNICODE_MARKER)
+# if defined(Q_COMPILER_LAMBDA)
+# define QStringLiteral(str) ([]() { \
+ enum { Size = sizeof(QT_QSTRING_UNICODE_MARKER str)/2 }; \
+ static const QConstStringData<Size> qstring_literal = \
+ { { Q_REFCOUNT_INITIALIZER(-1), Size -1, 0, 0, { 0 } }, QT_QSTRING_UNICODE_MARKER str }; \
+ QConstStringDataPtr<Size> holder = { &qstring_literal }; \
+ return holder; }())
+
+# elif defined(Q_CC_GNU)
+// We need to create a QStringData in the .rodata section of memory
+// and the only way to do that is to create a "static const" variable.
+// To do that, we need the __extension__ {( )} trick which only GCC supports
+
+# define QStringLiteral(str) \
__extension__ ({ \
- enum { Size = sizeof(L"" str)/2 }; \
+ enum { Size = sizeof(QT_QSTRING_UNICODE_MARKER str)/2 }; \
static const QConstStringData<Size> qstring_literal = \
- { { Q_REFCOUNT_INITIALIZER(-1), Size -1, 0, 0, { 0 } }, L"" str }; \
+ { { Q_REFCOUNT_INITIALIZER(-1), Size -1, 0, 0, { 0 } }, QT_QSTRING_UNICODE_MARKER str }; \
QConstStringDataPtr<Size> holder = { &qstring_literal }; \
holder; })
# endif
#endif
#ifndef QStringLiteral
-// not GCC, or GCC in C++98 mode with 4-byte wchar_t
+// no lambdas, not GCC, or GCC in C++98 mode with 4-byte wchar_t
// fallback, uses QLatin1String as next best options
-template<int n> struct QConstStringData
-{
- const QStringData str;
- const ushort data[n];
- operator const QStringData &() const { return str; }
-};
# define QStringLiteral(str) QLatin1String(str)
#endif
@@ -576,7 +583,7 @@ public:
template <int n>
inline QString(const QConstStringData<n> &dd) : d(const_cast<QStringData *>(&dd.str)) {}
template <int N>
- inline QString(QConstStringDataPtr<N> dd) : d(const_cast<QStringData *>(&dd.ptr->str)) {}
+ Q_DECL_CONSTEXPR inline QString(QConstStringDataPtr<N> dd) : d(const_cast<QStringData *>(&dd.ptr->str)) {}
private:
#if defined(QT_NO_CAST_FROM_ASCII) && !defined(Q_NO_DECLARED_NOT_DEFINED)