diff options
author | João Abecasis <joao.abecasis@nokia.com> | 2011-11-02 13:11:31 +0100 |
---|---|---|
committer | Qt by Nokia <qt-info@nokia.com> | 2012-01-25 20:33:41 +0100 |
commit | b59d8319806ff0ed2e340126fd110413301a833b (patch) | |
tree | 59a28000c215f2ca6aac67a2469094d9747fa58e /src/corelib/tools/qarraydata.h | |
parent | f218213a42f4d1b1bbf291ee27730f83cfeed6ad (diff) |
Introducing Q_ARRAY_LITERAL
This provides the same functionality as the specialized QStringLiteral
and QByteArrayLiteral, but on top of QArrayData.
The macro has two variations, variadic and simple. The variadic version
depends on compiler support for (C99) variadic macros and enables
static initialization of arrays of any POD data. Use of this macro is
not recommended on code or applications that need to work in
configurations where variadic macros are not supported.
The simple version is more portable and is enough to support the use
cases of QStringLiteral and QByteArrayLiteral, also providing a fallback
that allocates and copies data when static initialization is not
available.
Change-Id: I7154a24dcae4bbbd7d5978653f620138467830c5
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
Diffstat (limited to 'src/corelib/tools/qarraydata.h')
-rw-r--r-- | src/corelib/tools/qarraydata.h | 79 |
1 files changed, 79 insertions, 0 deletions
diff --git a/src/corelib/tools/qarraydata.h b/src/corelib/tools/qarraydata.h index 5ed8d4ba95..d5d96efc08 100644 --- a/src/corelib/tools/qarraydata.h +++ b/src/corelib/tools/qarraydata.h @@ -43,6 +43,7 @@ #define QARRAYDATA_H #include <QtCore/qrefcount.h> +#include <string.h> QT_BEGIN_HEADER @@ -190,6 +191,84 @@ struct QArrayDataPointerRef & ~(Q_ALIGNOF(type) - 1) } \ /**/ +//////////////////////////////////////////////////////////////////////////////// +// Q_ARRAY_LITERAL + +// The idea here is to place a (read-only) copy of header and array data in an +// mmappable portion of the executable (typically, .rodata section). This is +// accomplished by hiding a static const instance of QStaticArrayData, which is +// POD. + +#if defined(Q_COMPILER_VARIADIC_MACROS) +#if defined(Q_COMPILER_LAMBDA) +// Hide array inside a lambda +#define Q_ARRAY_LITERAL(Type, ...) \ + ([]() -> QArrayDataPointerRef<Type> { \ + /* MSVC 2010 Doesn't support static variables in a lambda, but */ \ + /* happily accepts them in a static function of a lambda-local */ \ + /* struct :-) */ \ + struct StaticWrapper { \ + static QArrayDataPointerRef<Type> get() \ + { \ + Q_ARRAY_LITERAL_IMPL(Type, __VA_ARGS__) \ + return ref; \ + } \ + }; \ + return StaticWrapper::get(); \ + }()) \ + /**/ +#elif defined(Q_CC_GNU) +// Hide array within GCC's __extension__ {( )} block +#define Q_ARRAY_LITERAL(Type, ...) \ + __extension__ ({ \ + Q_ARRAY_LITERAL_IMPL(Type, __VA_ARGS__) \ + ref; \ + }) \ + /**/ +#endif +#endif // defined(Q_COMPILER_VARIADIC_MACROS) + +#if defined(Q_ARRAY_LITERAL) +#define Q_ARRAY_LITERAL_IMPL(Type, ...) \ + union { Type type_must_be_POD; } dummy; Q_UNUSED(dummy) \ + \ + /* Portable compile-time array size computation */ \ + Type data[] = { __VA_ARGS__ }; Q_UNUSED(data) \ + enum { Size = sizeof(data) / sizeof(data[0]) }; \ + \ + static const QStaticArrayData<Type, Size> literal = { \ + Q_STATIC_ARRAY_DATA_HEADER_INITIALIZER(Type, Size), { __VA_ARGS__ } }; \ + \ + QArrayDataPointerRef<Type> ref = \ + { static_cast<QTypedArrayData<Type> *>( \ + const_cast<QArrayData *>(&literal.header)) }; \ + /**/ +#else +// As a fallback, memory is allocated and data copied to the heap. + +// The fallback macro does NOT use variadic macros and does NOT support +// variable number of arguments. It is suitable for char arrays. + +namespace QtPrivate { + template <class T, size_t N> + inline QArrayDataPointerRef<T> qMakeArrayLiteral(const T (&array)[N]) + { + union { T type_must_be_POD; } dummy; Q_UNUSED(dummy) + + QArrayDataPointerRef<T> result = { QTypedArrayData<T>::allocate(N) }; + Q_CHECK_PTR(result.ptr); + + ::memcpy(result.ptr->data(), array, N * sizeof(T)); + result.ptr->size = N; + + return result; + } +} + +#define Q_ARRAY_LITERAL(Type, Array) \ + QT_PREPEND_NAMESPACE(QtPrivate::qMakeArrayLiteral)<Type>( Array ) +#endif // !defined(Q_ARRAY_LITERAL) + QT_END_NAMESPACE QT_END_HEADER |