From 3bffdfb381d69910750a7a8e61ad42e1b86ef25a Mon Sep 17 00:00:00 2001 From: Michele Calgaro Date: Sat, 2 Aug 2014 21:18:48 +0900 Subject: [PATCH] Improvements to QValueList. This may relate to bug 1820 (cherry picked from commit 71a6d7870f609df603d9520a8d292055ea5928c3) --- src/tools/qvaluelist.h | 42 ++++++++++++++++++++++-------------------- 1 file changed, 22 insertions(+), 20 deletions(-) diff --git a/src/tools/qvaluelist.h b/src/tools/qvaluelist.h index 95dc7ff..16c3f4b 100644 --- a/src/tools/qvaluelist.h +++ b/src/tools/qvaluelist.h @@ -233,12 +233,6 @@ public: QValueListPrivate(); QValueListPrivate( const QValueListPrivate& _p ); - void derefAndDelete() // ### hack to get around hp-cc brain damage - { - if ( deref() ) - delete this; - } - #if defined(Q_TEMPLATEDLL) // Workaround MS bug in memory de/allocation in DLL vs. EXE virtual @@ -261,14 +255,14 @@ public: template Q_INLINE_TEMPLATES QValueListPrivate::QValueListPrivate() { - node = new Node; node->next = node->prev = node; nodes = 0; + node = new Node(); node->next = node->prev = node; nodes = 0; } template Q_INLINE_TEMPLATES QValueListPrivate::QValueListPrivate( const QValueListPrivate& _p ) : QShared() { - node = new Node; node->next = node->prev = node; nodes = 0; + node = new Node(); node->next = node->prev = node; nodes = 0; Iterator b( _p.node->next ); Iterator e( _p.node ); Iterator i( node ); @@ -442,15 +436,23 @@ public: qCopy( l.begin(), l.end(), std::back_inserter( *this ) ); } #endif - ~QValueList() { sh->derefAndDelete(); } + ~QValueList() + { + if (sh->deref()) + delete sh; + } QValueList& operator= ( const QValueList& l ) { + if (this == &l || sh == l.sh) + return *this; // Do nothing is self-assigning l.sh->ref(); - sh->derefAndDelete(); + if (sh->deref()) + delete sh; sh = l.sh; return *this; } + #ifndef QT_NO_STL QValueList& operator= ( const std::list& l ) { @@ -458,6 +460,7 @@ public: qCopy( l.begin(), l.end(), std::back_inserter( *this ) ); return *this; } + bool operator== ( const std::list& l ) const { if ( size() != l.size() ) @@ -563,7 +566,14 @@ protected: /** * Helpers */ - void detach() { if ( sh->count > 1 ) detachInternal(); } + void detach() + { + if (sh->count > 1) + { + sh->deref(); + sh = new QValueListPrivate(*sh); + } + } /** * Variables @@ -571,8 +581,6 @@ protected: QValueListPrivate* sh; private: - void detachInternal(); - friend class QDeepCopy< QValueList >; }; @@ -592,7 +600,7 @@ Q_INLINE_TEMPLATES bool QValueList::operator== ( const QValueList& l ) con template Q_INLINE_TEMPLATES void QValueList::clear() { - if ( sh->count == 1 ) sh->clear(); else { sh->deref(); sh = new QValueListPrivate; } + if ( sh->count == 1 ) sh->clear(); else { sh->deref(); sh = new QValueListPrivate(); } } template @@ -629,12 +637,6 @@ Q_INLINE_TEMPLATES QValueList& QValueList::operator+= ( const QValueList -Q_INLINE_TEMPLATES void QValueList::detachInternal() -{ - sh->deref(); sh = new QValueListPrivate( *sh ); -} - #ifndef QT_NO_DATASTREAM template Q_INLINE_TEMPLATES QDataStream& operator>>( QDataStream& s, QValueList& l )