From 5ca5a64a5c714b18ef23c093307f9f4061235731 Mon Sep 17 00:00:00 2001 From: Thierry Bastian Date: Thu, 26 Mar 2009 10:41:53 +0100 Subject: Improved qVariantSetValue by reusing the internals if possible This is possible if the type is the same of type < Char (simple types) --- src/corelib/kernel/qvariant.h | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/src/corelib/kernel/qvariant.h b/src/corelib/kernel/qvariant.h index d7b7e3c..284e68c 100644 --- a/src/corelib/kernel/qvariant.h +++ b/src/corelib/kernel/qvariant.h @@ -443,7 +443,18 @@ inline QVariant qVariantFromValue(const QVariant &t) { return t; } template inline void qVariantSetValue(QVariant &v, const T &t) { - v = QVariant(qMetaTypeId(reinterpret_cast(0)), &t); + //if possible we reuse the current QVariant private + const int type = qMetaTypeId(reinterpret_cast(0)); + QVariant::Private &d = v.data_ptr(); + if (type <= int(QVariant::Char) || (type == d.type && v.isDetached())) { + d.type = type; + T *old = reinterpret_cast(d.is_shared ? d.data.shared->ptr : &d.data.ptr); + if (QTypeInfo::isComplex) + old->~T(); + new (old) T(t); //call the copy constructor + } else { + v = QVariant(type, &t); + } } inline QVariant::QVariant() {} -- cgit v0.12