diff options
author | Thiago Macieira <thiago@kde.org> | 2011-07-07 19:51:09 +0200 |
---|---|---|
committer | Qt by Nokia <qt-info@nokia.com> | 2011-07-08 11:22:30 +0200 |
commit | 571785b31d21715857228b00f96cd24601b28c8c (patch) | |
tree | b5cbc9b7faf988c4d1ea7f3f29027d4abbb34174 | |
parent | f3d6589068450283e9f147851113d1795b47acc6 (diff) |
Make the new QStringLiteral produce a non-temporary
This is currently GCC-only code (the __extension ({ }) stuff), but it
was the only way I found to create a static variable and return it. I
had to add the QConstStringDataPtr container because I had to return a
pointer to the data -- it's impossible to return a reference through
this extension.
I could have returned a naked pointer directly too.
This isn't complete. GCC 4.6 is placing the string in the .data
section, not .rodata as we wanted. Investigating further.
Change-Id: I170030dca3eddbd69f6c879e952f652f7fe5d958
Reviewed-on: http://codereview.qt.nokia.com/1350
Reviewed-by: Lars Knoll <lars.knoll@nokia.com>
-rw-r--r-- | src/corelib/tools/qstring.h | 43 |
1 files changed, 33 insertions, 10 deletions
diff --git a/src/corelib/tools/qstring.h b/src/corelib/tools/qstring.h index e0473e411a..c10af450b3 100644 --- a/src/corelib/tools/qstring.h +++ b/src/corelib/tools/qstring.h @@ -90,32 +90,54 @@ struct QStringData { inline const ushort *data() const { return d + sizeof(qptrdiff)/sizeof(ushort) + offset; } }; +template<int N> struct QConstStringData; +template<int N> struct QConstStringDataPtr +{ + const QConstStringData<N> *ptr; +}; -#if defined(Q_COMPILER_UNICODE_STRINGS) +#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) template<int n> struct QConstStringData { const QStringData str; const char16_t data[n]; operator const QStringData &() const { return str; } }; -#define QStringLiteral(str) (const QConstStringData<sizeof(u"" str)/2>) \ -{ { Q_REFCOUNT_INITIALIZER(-1), sizeof(u"" str)/2 -1, 0, 0, { 0 } }, u"" str } -// wchar_t is 2 bytes -#elif defined(Q_OS_WIN) || (defined(__SIZEOF_WCHAR_T__) && __SIZEOF_WCHAR_T__ == 2) || defined(WCHAR_MAX) && (WCHAR_MAX - 0 < 65536) +# 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) +// wchar_t is 2 bytes template<int n> struct QConstStringData { const QStringData str; const wchar_t data[n]; operator const QStringData &() const { return str; } }; -#define QStringLiteral(str) (const QConstStringData<sizeof(L"" str)/2>) \ -{ { Q_REFCOUNT_INITIALIZER(-1), sizeof(L"" str)/2 -1, 0, 0, { 0 } }, L"" str } +# define QStringLiteral(str) \ + __extension__ ({ \ + enum { Size = sizeof(L"" str)/2 }; \ + static const QConstStringData<Size> qstring_literal = \ + { { Q_REFCOUNT_INITIALIZER(-1), Size -1, 0, 0, { 0 } }, L"" str }; \ + QConstStringDataPtr<Size> holder = { &qstring_literal }; \ + holder; }) +# endif +#endif +#ifndef QStringLiteral +// not GCC, or GCC in C++98 mode with 4-byte wchar_t // fallback, uses QLatin1String as next best options -#else template<int n> struct QConstStringData { @@ -123,8 +145,7 @@ template<int n> struct QConstStringData const ushort data[n]; operator const QStringData &() const { return str; } }; -#define QStringLiteral(str) QLatin1String(str) - +# define QStringLiteral(str) QLatin1String(str) #endif #ifndef QT_NO_KEYWORDS @@ -558,6 +579,8 @@ public: QString(int size, Qt::Initialization); 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)) {} private: #if defined(QT_NO_CAST_FROM_ASCII) && !defined(Q_NO_DECLARED_NOT_DEFINED) |