Improvements to QValueList. This may relate to bug 1820

pull/2/head
Michele Calgaro 10 years ago
parent cbeacf0de9
commit 71a6d7870f

@ -229,12 +229,6 @@ public:
QValueListPrivate(); QValueListPrivate();
QValueListPrivate( const QValueListPrivate<T>& _p ); QValueListPrivate( const QValueListPrivate<T>& _p );
void derefAndDelete() // ### hack to get around hp-cc brain damage
{
if ( deref() )
delete this;
}
#if defined(Q_TEMPLATEDLL) #if defined(Q_TEMPLATEDLL)
// Workaround MS bug in memory de/allocation in DLL vs. EXE // Workaround MS bug in memory de/allocation in DLL vs. EXE
virtual virtual
@ -258,14 +252,14 @@ public:
template <class T> template <class T>
Q_INLINE_TEMPLATES QValueListPrivate<T>::QValueListPrivate() Q_INLINE_TEMPLATES QValueListPrivate<T>::QValueListPrivate()
{ {
node = new Node; node->next = node->prev = node; nodes = 0; node = new Node(); node->next = node->prev = node; nodes = 0;
} }
template <class T> template <class T>
Q_INLINE_TEMPLATES QValueListPrivate<T>::QValueListPrivate( const QValueListPrivate<T>& _p ) Q_INLINE_TEMPLATES QValueListPrivate<T>::QValueListPrivate( const QValueListPrivate<T>& _p )
: QShared() : 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 b( _p.node->next );
Iterator e( _p.node ); Iterator e( _p.node );
Iterator i( node ); Iterator i( node );
@ -452,15 +446,23 @@ public:
qCopy( l.begin(), l.end(), std::back_inserter( *this ) ); qCopy( l.begin(), l.end(), std::back_inserter( *this ) );
} }
#endif #endif
~QValueList() { sh->derefAndDelete(); } ~QValueList()
{
if (sh->deref())
delete sh;
}
QValueList<T>& operator= ( const QValueList<T>& l ) QValueList<T>& operator= ( const QValueList<T>& l )
{ {
if (this == &l || sh == l.sh)
return *this; // Do nothing is self-assigning
l.sh->ref(); l.sh->ref();
sh->derefAndDelete(); if (sh->deref())
delete sh;
sh = l.sh; sh = l.sh;
return *this; return *this;
} }
#ifndef QT_NO_STL #ifndef QT_NO_STL
QValueList<T>& operator= ( const std::list<T>& l ) QValueList<T>& operator= ( const std::list<T>& l )
{ {
@ -468,6 +470,7 @@ public:
qCopy( l.begin(), l.end(), std::back_inserter( *this ) ); qCopy( l.begin(), l.end(), std::back_inserter( *this ) );
return *this; return *this;
} }
bool operator== ( const std::list<T>& l ) const bool operator== ( const std::list<T>& l ) const
{ {
if ( size() != l.size() ) if ( size() != l.size() )
@ -574,7 +577,14 @@ protected:
/** /**
* Helpers * Helpers
*/ */
void detach() { if ( sh->count > 1 ) detachInternal(); } void detach()
{
if (sh->count > 1)
{
sh->deref();
sh = new QValueListPrivate<T>(*sh);
}
}
/** /**
* Variables * Variables
@ -582,8 +592,6 @@ protected:
QValueListPrivate<T>* sh; QValueListPrivate<T>* sh;
private: private:
void detachInternal();
friend class QDeepCopy< QValueList<T> >; friend class QDeepCopy< QValueList<T> >;
}; };
@ -603,7 +611,7 @@ Q_INLINE_TEMPLATES bool QValueList<T>::operator== ( const QValueList<T>& l ) con
template <class T> template <class T>
Q_INLINE_TEMPLATES void QValueList<T>::clear() Q_INLINE_TEMPLATES void QValueList<T>::clear()
{ {
if ( sh->count == 1 ) sh->clear(); else { sh->deref(); sh = new QValueListPrivate<T>; } if ( sh->count == 1 ) sh->clear(); else { sh->deref(); sh = new QValueListPrivate<T>(); }
} }
template <class T> template <class T>
@ -640,12 +648,6 @@ Q_INLINE_TEMPLATES QValueList<T>& QValueList<T>::operator+= ( const QValueList<T
return *this; return *this;
} }
template <class T>
Q_INLINE_TEMPLATES void QValueList<T>::detachInternal()
{
sh->deref(); sh = new QValueListPrivate<T>( *sh );
}
#ifndef QT_NO_DATASTREAM #ifndef QT_NO_DATASTREAM
template <class T> template <class T>
Q_INLINE_TEMPLATES QDataStream& operator>>( QDataStream& s, QValueList<T>& l ) Q_INLINE_TEMPLATES QDataStream& operator>>( QDataStream& s, QValueList<T>& l )

Loading…
Cancel
Save