From 9b95ed1fe39853534b859a64095febeaf0975cf7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C4=99drzej=20Nowacki?= Date: Mon, 4 Jun 2012 16:52:32 +0200 Subject: Introduce QTypeInfoMerger. QTypeInfoMerger class was created to allow "inheriting" QTypeInfo traits. The class implementation was based on the QTypeInfo> specialization, therefore the specialization was refactored to use the new class. Change-Id: I4ff3e5eac1d55da086dad84274cce2b2c0a721be Reviewed-by: Marc Mutz Reviewed-by: Stephen Kelly Reviewed-by: Thiago Macieira --- .../snippets/code/src_corelib_global_qglobal.cpp | 14 ++++++++++ src/corelib/global/qtypeinfo.h | 32 ++++++++++++++++++++++ src/corelib/tools/qpair.h | 13 +-------- 3 files changed, 47 insertions(+), 12 deletions(-) (limited to 'src') diff --git a/src/corelib/doc/snippets/code/src_corelib_global_qglobal.cpp b/src/corelib/doc/snippets/code/src_corelib_global_qglobal.cpp index dc6c3aa616..8feac30d1d 100644 --- a/src/corelib/doc/snippets/code/src_corelib_global_qglobal.cpp +++ b/src/corelib/doc/snippets/code/src_corelib_global_qglobal.cpp @@ -538,6 +538,20 @@ CApaApplication *myApplicationFactory(); void myMessageHandler(QtMsgType, const QMessageLogContext &, const char *); //! [49] +//! [50] +class B {...}; +class C {...}; +class D {...}; +struct A : public B { + C c; + D d; +}; +//! [50] + +//! [51] +template<> class QTypeInfo : public QTypeInfoMerger {}; +//! [51] + //! [qlikely] // the condition inside the "if" will be successful most of the times for (int i = 1; i <= 365; i++) { diff --git a/src/corelib/global/qtypeinfo.h b/src/corelib/global/qtypeinfo.h index 4255548b0d..e8aa883c9d 100644 --- a/src/corelib/global/qtypeinfo.h +++ b/src/corelib/global/qtypeinfo.h @@ -97,6 +97,38 @@ public: }; }; +/*! + \class QTypeInfoMerger + \internal + + \brief QTypeInfoMerger merges the QTypeInfo flags of T1, T2... and presents them + as a QTypeInfo would do. + + Let's assume that we have a simple set of structs: + + \snippet code/src_corelib_global_qglobal.cpp 50 + + To create a proper QTypeInfo specialization for A struct, we have to check + all sub-components; B, C and D, then take the lowest common denominator and call + Q_DECLATE_TYPEINFO with the resulting flags. An easier and less fragile approach is to + use QTypeInfoMerger, which does that automatically. So struct A would have + the following QTypeInfo definition: + + \snippet code/src_corelib_global_qglobal.cpp 51 +*/ +template +class QTypeInfoMerger +{ +public: + enum { + isComplex = QTypeInfo::isComplex || QTypeInfo::isComplex || QTypeInfo::isComplex || QTypeInfo::isComplex, + isStatic = QTypeInfo::isStatic || QTypeInfo::isStatic || QTypeInfo::isStatic || QTypeInfo::isStatic, + isLarge = sizeof(T) > sizeof(void*), + isPointer = false, + isDummy = false, + sizeOf = sizeof(T) + }; +}; #define Q_DECLARE_MOVABLE_CONTAINER(CONTAINER) \ template class CONTAINER; \ diff --git a/src/corelib/tools/qpair.h b/src/corelib/tools/qpair.h index f8455339f3..b73bf6706a 100644 --- a/src/corelib/tools/qpair.h +++ b/src/corelib/tools/qpair.h @@ -66,18 +66,7 @@ struct QPair // mark QPair as complex/movable/primitive depending on the // typeinfos of the constituents: template -class QTypeInfo< QPair > -{ -public: - enum { - isComplex = QTypeInfo::isComplex || QTypeInfo::isComplex, - isStatic = QTypeInfo::isStatic || QTypeInfo::isStatic, - isLarge = sizeof(QPair) > sizeof(void*), - isPointer = false, - isDummy = false, - sizeOf = sizeof(QPair) - }; -}; +class QTypeInfo > : public QTypeInfoMerger, T1, T2> {}; // Q_DECLARE_TYPEINFO template Q_INLINE_TEMPLATE bool operator==(const QPair &p1, const QPair &p2) -- cgit v1.2.3