Branch: master

6fb1d18e 2014-08-02 07:23:15 Michele Calgaro
Improvements to TQValueList. This may relate to bug 1820
M src/tools/ntqvaluelist.h
diff --git a/src/tools/ntqvaluelist.h b/src/tools/ntqvaluelist.h
index 9ee9ee5..b9fc730 100644
--- a/src/tools/ntqvaluelist.h
+++ b/src/tools/ntqvaluelist.h
@@ -229,12 +229,6 @@
     TQValueListPrivate();
     TQValueListPrivate( const TQValueListPrivate<T>& _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
@@ -258,14 +252,14 @@
 template <class T>
 Q_INLINE_TEMPLATES TQValueListPrivate<T>::TQValueListPrivate()
 {
-    node = new Node; node->next = node->prev = node; nodes = 0;
+    node = new Node(); node->next = node->prev = node; nodes = 0;
 }
 
 template <class T>
 Q_INLINE_TEMPLATES TQValueListPrivate<T>::TQValueListPrivate( const TQValueListPrivate<T>& _p )
     : TQShared()
 {
-    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 );
@@ -452,15 +446,23 @@
 	tqCopy( l.begin(), l.end(), std::back_inserter( *this ) );
     }
 #endif
-    ~TQValueList() { sh->derefAndDelete(); }
+    ~TQValueList()
+    {
+        if (sh->deref())
+	    delete sh;
+    }
 
     TQValueList<T>& operator= ( const TQValueList<T>& 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
     TQValueList<T>& operator= ( const std::list<T>& l )
     {
@@ -468,6 +470,7 @@
 	tqCopy( l.begin(), l.end(), std::back_inserter( *this ) );
 	return *this;
     }
+    
     bool operator== ( const std::list<T>& l ) const
     {
 	if ( size() != l.size() )
@@ -574,7 +577,14 @@
     /**
      * Helpers
      */
-    void detach() { if ( sh->count > 1 ) detachInternal(); }
+    void detach()
+    { 
+        if (sh->count > 1) 
+        {
+            sh->deref();
+            sh = new TQValueListPrivate<T>(*sh);        
+        }
+    }
 
     /**
      * Variables
@@ -582,8 +592,6 @@
     TQValueListPrivate<T>* sh;
 
 private:
-    void detachInternal();
-
     friend class TQDeepCopy< TQValueList<T> >;
 };
 
@@ -603,7 +611,7 @@
 template <class T>
 Q_INLINE_TEMPLATES void TQValueList<T>::clear()
 {
-    if ( sh->count == 1 ) sh->clear(); else { sh->deref(); sh = new TQValueListPrivate<T>; }
+    if ( sh->count == 1 ) sh->clear(); else { sh->deref(); sh = new TQValueListPrivate<T>(); }
 }
 
 template <class T>
@@ -638,12 +646,6 @@
     for( const_iterator it = copy.begin(); it != copy.end(); ++it )
 	append( *it );
     return *this;
-}
-
-template <class T>
-Q_INLINE_TEMPLATES void TQValueList<T>::detachInternal()
-{
-    sh->deref(); sh = new TQValueListPrivate<T>( *sh );
 }
 
 #ifndef QT_NO_DATASTREAM