From d874c059d36fe125c577dde9e980407fbd43beac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C4=99drzej=20Nowacki?= Date: Tue, 24 Jan 2012 15:42:01 +0100 Subject: Introduce QTypeInfo::sizeOf. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit In QMetaType "void" is a regular type, lack of c++ sizeof operator force us to write a template specialization for the type. Change-Id: I9a56e135223b416b8031836d29ef33ef3fb750e4 Reviewed-by: João Abecasis --- src/corelib/global/qglobal.h | 15 ++++++++++----- src/corelib/kernel/qmetatype.cpp | 3 +-- tests/auto/corelib/kernel/qmetatype/tst_qmetatype.cpp | 5 +---- 3 files changed, 12 insertions(+), 11 deletions(-) diff --git a/src/corelib/global/qglobal.h b/src/corelib/global/qglobal.h index 43632cb41c..b38583c771 100644 --- a/src/corelib/global/qglobal.h +++ b/src/corelib/global/qglobal.h @@ -2020,7 +2020,8 @@ public: isComplex = true, isStatic = true, isLarge = (sizeof(T)>sizeof(void*)), - isDummy = false + isDummy = false, + sizeOf = sizeof(T) }; }; @@ -2033,7 +2034,8 @@ public: isComplex = false, isStatic = false, isLarge = false, - isDummy = false + isDummy = false, + sizeOf = 0 }; }; @@ -2046,7 +2048,8 @@ public: isComplex = false, isStatic = false, isLarge = false, - isDummy = false + isDummy = false, + sizeOf = sizeof(T*) }; }; @@ -2062,7 +2065,8 @@ public: \ isComplex = true, \ isStatic = false, \ isLarge = (sizeof(CONTAINER) > sizeof(void*)), \ - isDummy = false \ + isDummy = false, \ + sizeOf = sizeof(CONTAINER) \ }; \ }; @@ -2100,7 +2104,8 @@ public: \ isStatic = (((FLAGS) & (Q_MOVABLE_TYPE | Q_PRIMITIVE_TYPE)) == 0), \ isLarge = (sizeof(TYPE)>sizeof(void*)), \ isPointer = false, \ - isDummy = (((FLAGS) & Q_DUMMY_TYPE) != 0) \ + isDummy = (((FLAGS) & Q_DUMMY_TYPE) != 0), \ + sizeOf = sizeof(TYPE) \ }; \ static inline const char *name() { return #TYPE; } \ } diff --git a/src/corelib/kernel/qmetatype.cpp b/src/corelib/kernel/qmetatype.cpp index a0a1295426..38ca0211e5 100644 --- a/src/corelib/kernel/qmetatype.cpp +++ b/src/corelib/kernel/qmetatype.cpp @@ -1606,7 +1606,7 @@ namespace { class SizeOf { template::IsAccepted> struct SizeOfImpl { - static int Size(const int) { return sizeof(T); } + static int Size(const int) { return QTypeInfo::sizeOf; } }; template struct SizeOfImpl { @@ -1632,7 +1632,6 @@ public: template int delegate(const T*) { return SizeOfImpl::Size(m_type); } - int delegate(const void*) { return 0; } int delegate(const QMetaTypeSwitcher::UnknownType*) { return customTypeSizeOf(m_type); } private: static int customTypeSizeOf(const int type) diff --git a/tests/auto/corelib/kernel/qmetatype/tst_qmetatype.cpp b/tests/auto/corelib/kernel/qmetatype/tst_qmetatype.cpp index 19aa6877b2..43537fc28f 100644 --- a/tests/auto/corelib/kernel/qmetatype/tst_qmetatype.cpp +++ b/tests/auto/corelib/kernel/qmetatype/tst_qmetatype.cpp @@ -571,15 +571,12 @@ FOR_EACH_CORE_METATYPE(RETURN_CREATE_COPY_FUNCTION) TypeTestFunctionGetter::get(type)(); } -template struct SafeSizeOf { enum {Size = sizeof(T)}; }; -template<> struct SafeSizeOf { enum {Size = 0}; }; - void tst_QMetaType::sizeOf_data() { QTest::addColumn("type"); QTest::addColumn("size"); #define ADD_METATYPE_TEST_ROW(MetaTypeName, MetaTypeId, RealType) \ - QTest::newRow(#RealType) << QMetaType::MetaTypeName << int(SafeSizeOf::Size); + QTest::newRow(#RealType) << QMetaType::MetaTypeName << int(QTypeInfo::sizeOf); FOR_EACH_CORE_METATYPE(ADD_METATYPE_TEST_ROW) #undef ADD_METATYPE_TEST_ROW } -- cgit v1.2.3