diff options
author | Thierry Bastian <thierry.bastian@nokia.com> | 2009-03-26 09:27:34 (GMT) |
---|---|---|
committer | Thierry Bastian <thierry.bastian@nokia.com> | 2009-03-26 09:27:34 (GMT) |
commit | 51467afe563ca66d01e41dfd0f287d886117c415 (patch) | |
tree | 926b48ff0cc677cedbd130f2f3fe1f41a0f90ea0 | |
parent | 211ae2145228d300c3743d25c689ab0275b23e33 (diff) | |
download | Qt-51467afe563ca66d01e41dfd0f287d886117c415.zip Qt-51467afe563ca66d01e41dfd0f287d886117c415.tar.gz Qt-51467afe563ca66d01e41dfd0f287d886117c415.tar.bz2 |
Added template to the private classes to avoid double allocation in QVariant
-rw-r--r-- | src/corelib/kernel/qvariant_p.h | 25 |
1 files changed, 18 insertions, 7 deletions
diff --git a/src/corelib/kernel/qvariant_p.h b/src/corelib/kernel/qvariant_p.h index 727a390..72c09ec 100644 --- a/src/corelib/kernel/qvariant_p.h +++ b/src/corelib/kernel/qvariant_p.h @@ -91,13 +91,25 @@ inline T *v_cast(QVariant::Private *d, T * = 0) #endif + +//a simple template that avoids to allocate 2 memory chunks when creating a QVariant +template <class T> class QVariantPrivateSharedEx : public QVariant::PrivateShared +{ +public: + QVariantPrivateSharedEx() : QVariant::PrivateShared(&m_t) { } + QVariantPrivateSharedEx(const T&t) : QVariant::PrivateShared(&m_t), m_t(t) { } + +private: + T m_t; +}; + // constructs a new variant if copy is 0, otherwise copy-constructs template <class T> inline void v_construct(QVariant::Private *x, const void *copy, T * = 0) { if (sizeof(T) > sizeof(QVariant::Private::Data)) { - x->data.shared = copy ? new QVariant::PrivateShared(new T(*static_cast<const T *>(copy))) - : new QVariant::PrivateShared(new T); + x->data.shared = copy ? new QVariantPrivateSharedEx<T>(*static_cast<const T *>(copy)) + : new QVariantPrivateSharedEx<T>; x->is_shared = true; } else { if (copy) @@ -111,12 +123,11 @@ inline void v_construct(QVariant::Private *x, const void *copy, T * = 0) template <class T> inline void v_clear(QVariant::Private *d, T* = 0) { - if (sizeof(T) > sizeof(QVariant::Private::Data)) { - delete v_cast<T>(d); + //now we need to call the destructor in any case + //because QVariant::PrivateShared doesn't have a virtual destructor + v_cast<T>(d)->~T(); + if (sizeof(T) > sizeof(QVariant::Private::Data)) delete d->data.shared; - } else { - v_cast<T>(d)->~T(); - } } QT_END_NAMESPACE |