Introduce QTypeInfo::sizeOf.

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 <joao.abecasis@nokia.com>
This commit is contained in:
Jędrzej Nowacki 2012-01-24 15:42:01 +01:00 committed by Qt by Nokia
parent b08b254a79
commit d874c059d3
3 changed files with 12 additions and 11 deletions

View File

@ -2020,7 +2020,8 @@ public:
isComplex = true, isComplex = true,
isStatic = true, isStatic = true,
isLarge = (sizeof(T)>sizeof(void*)), isLarge = (sizeof(T)>sizeof(void*)),
isDummy = false isDummy = false,
sizeOf = sizeof(T)
}; };
}; };
@ -2033,7 +2034,8 @@ public:
isComplex = false, isComplex = false,
isStatic = false, isStatic = false,
isLarge = false, isLarge = false,
isDummy = false isDummy = false,
sizeOf = 0
}; };
}; };
@ -2046,7 +2048,8 @@ public:
isComplex = false, isComplex = false,
isStatic = false, isStatic = false,
isLarge = false, isLarge = false,
isDummy = false isDummy = false,
sizeOf = sizeof(T*)
}; };
}; };
@ -2062,7 +2065,8 @@ public: \
isComplex = true, \ isComplex = true, \
isStatic = false, \ isStatic = false, \
isLarge = (sizeof(CONTAINER<T>) > sizeof(void*)), \ isLarge = (sizeof(CONTAINER<T>) > sizeof(void*)), \
isDummy = false \ isDummy = false, \
sizeOf = sizeof(CONTAINER<T>) \
}; \ }; \
}; };
@ -2100,7 +2104,8 @@ public: \
isStatic = (((FLAGS) & (Q_MOVABLE_TYPE | Q_PRIMITIVE_TYPE)) == 0), \ isStatic = (((FLAGS) & (Q_MOVABLE_TYPE | Q_PRIMITIVE_TYPE)) == 0), \
isLarge = (sizeof(TYPE)>sizeof(void*)), \ isLarge = (sizeof(TYPE)>sizeof(void*)), \
isPointer = false, \ isPointer = false, \
isDummy = (((FLAGS) & Q_DUMMY_TYPE) != 0) \ isDummy = (((FLAGS) & Q_DUMMY_TYPE) != 0), \
sizeOf = sizeof(TYPE) \
}; \ }; \
static inline const char *name() { return #TYPE; } \ static inline const char *name() { return #TYPE; } \
} }

View File

@ -1606,7 +1606,7 @@ namespace {
class SizeOf { class SizeOf {
template<typename T, bool IsAcceptedType = DefinedTypesFilter::Acceptor<T>::IsAccepted> template<typename T, bool IsAcceptedType = DefinedTypesFilter::Acceptor<T>::IsAccepted>
struct SizeOfImpl { struct SizeOfImpl {
static int Size(const int) { return sizeof(T); } static int Size(const int) { return QTypeInfo<T>::sizeOf; }
}; };
template<typename T> template<typename T>
struct SizeOfImpl<T, /* IsAcceptedType = */ false> { struct SizeOfImpl<T, /* IsAcceptedType = */ false> {
@ -1632,7 +1632,6 @@ public:
template<typename T> template<typename T>
int delegate(const T*) { return SizeOfImpl<T>::Size(m_type); } int delegate(const T*) { return SizeOfImpl<T>::Size(m_type); }
int delegate(const void*) { return 0; }
int delegate(const QMetaTypeSwitcher::UnknownType*) { return customTypeSizeOf(m_type); } int delegate(const QMetaTypeSwitcher::UnknownType*) { return customTypeSizeOf(m_type); }
private: private:
static int customTypeSizeOf(const int type) static int customTypeSizeOf(const int type)

View File

@ -571,15 +571,12 @@ FOR_EACH_CORE_METATYPE(RETURN_CREATE_COPY_FUNCTION)
TypeTestFunctionGetter::get(type)(); TypeTestFunctionGetter::get(type)();
} }
template<typename T> struct SafeSizeOf { enum {Size = sizeof(T)}; };
template<> struct SafeSizeOf<void> { enum {Size = 0}; };
void tst_QMetaType::sizeOf_data() void tst_QMetaType::sizeOf_data()
{ {
QTest::addColumn<QMetaType::Type>("type"); QTest::addColumn<QMetaType::Type>("type");
QTest::addColumn<int>("size"); QTest::addColumn<int>("size");
#define ADD_METATYPE_TEST_ROW(MetaTypeName, MetaTypeId, RealType) \ #define ADD_METATYPE_TEST_ROW(MetaTypeName, MetaTypeId, RealType) \
QTest::newRow(#RealType) << QMetaType::MetaTypeName << int(SafeSizeOf<RealType>::Size); QTest::newRow(#RealType) << QMetaType::MetaTypeName << int(QTypeInfo<RealType>::sizeOf);
FOR_EACH_CORE_METATYPE(ADD_METATYPE_TEST_ROW) FOR_EACH_CORE_METATYPE(ADD_METATYPE_TEST_ROW)
#undef ADD_METATYPE_TEST_ROW #undef ADD_METATYPE_TEST_ROW
} }