[Kst] branches/work/kst/portto4/kst/src

Mike Fenton mike at staikos.net
Wed Nov 28 20:01:18 CET 2007


SVN commit 742717 by fenton:

Add proper cleanup of objects when removed from the ObjectStore.


 M  +7 -12     libkst/matrix.cpp  
 M  +2 -0      libkst/matrix.h  
 M  +1 -2      libkst/object.cpp  
 M  +1 -1      libkst/object.h  
 M  +1 -0      libkst/objectstore.cpp  
 M  +7 -12     libkst/vector.cpp  
 M  +2 -0      libkst/vector.h  
 M  +53 -57    libkstmath/dataobject.cpp  
 M  +1 -1      libkstmath/dataobject.h  
 M  +91 -2     libkstmath/relation.cpp  
 M  +3 -1      libkstmath/relation.h  


--- branches/work/kst/portto4/kst/src/libkst/matrix.cpp #742716:742717
@@ -63,18 +63,6 @@
 
 
 Matrix::~Matrix() {
-  // TODO: get rid of the stat scalars
-#if 0
-  scalarList.lock().writeLock();
-  scalarList.setUpdateDisplayTags(false);
-  for (QHash<QString, Scalar*>::Iterator iter = _statScalars.begin(); iter != _statScalars.end(); ++iter) {
-    scalarList.remove(iter.value());
-    iter.value()->_KShared_unref();
-  }
-  scalarList.setUpdateDisplayTags(true);
-  scalarList.lock().unlock();
-#endif
-
   if (_z) {
     free(_z);
     _z = 0L;
@@ -82,6 +70,13 @@
 }
 
 
+void Matrix::deleteDependents() {
+  for (QHash<QString, Scalar*>::Iterator it = _statScalars.begin(); it != _statScalars.end(); ++it) {
+    _store->removeObject(it.value());
+  }
+}
+
+
 const QString& Matrix::typeString() const {
   return staticTypeString;
 }
--- branches/work/kst/portto4/kst/src/libkst/matrix.h #742716:742717
@@ -136,6 +136,8 @@
 
     virtual bool resize(int xSize, int ySize, bool reinit = true);
 
+    virtual void deleteDependents();
+
   protected:
     int _NS;
     int _NRealS; // number of samples with real values
--- branches/work/kst/portto4/kst/src/libkst/object.cpp #742716:742717
@@ -118,8 +118,7 @@
 }
 
 
-bool Object::deleteDependents() {
-  return false;
+void Object::deleteDependents() {
 }
 
 
--- branches/work/kst/portto4/kst/src/libkst/object.h #742716:742717
@@ -65,7 +65,7 @@
     // TODO: do we need this?
 //    int operator==(const QString&) const;
 
-    virtual bool deleteDependents();
+    virtual void deleteDependents();
 
     // @since 1.1.0
     virtual void setDirty(bool dirty = true);
--- branches/work/kst/portto4/kst/src/libkst/objectstore.cpp #742716:742717
@@ -274,6 +274,7 @@
     if (ds) {
       _dataSourceList.removeAll(ds);
     } else {
+      o->deleteDependents();
       _list.removeAll(o);
     }
   }
--- branches/work/kst/portto4/kst/src/libkst/vector.cpp #742716:742717
@@ -106,18 +106,6 @@
 
 Vector::~Vector() {
   // qDebug() << "+++ DELETING VECTOR: " << (void*) this;
-  // FIXME: cleanup scalars
-#if 0
-  scalarList.lock().writeLock();
-  scalarList.setUpdateDisplayTags(false);
-  for (QHash<QString, Scalar*>::Iterator it = _scalars.begin(); it != _scalars.end(); ++it) {
-    scalarList.remove(it.value());
-    it.value()->_KShared_unref();
-  }
-  scalarList.setUpdateDisplayTags(true);
-  scalarList.lock().unlock();
-#endif
-
   if (_v) {
     free(_v);
     _v = 0;
@@ -125,6 +113,13 @@
 }
 
 
+void Vector::deleteDependents() {
+  for (QHash<QString, Scalar*>::Iterator it = _scalars.begin(); it != _scalars.end(); ++it) {
+    _store->removeObject(it.value());
+  }
+}
+
+
 const QString& Vector::typeString() const {
   return staticTypeString;
 }
--- branches/work/kst/portto4/kst/src/libkst/vector.h #742716:742717
@@ -201,6 +201,8 @@
     void CreateScalars(ObjectStore *store);
     void RenameScalars();
 
+    virtual void deleteDependents();
+
     QString _label;
 
     friend class DataObject;
--- branches/work/kst/portto4/kst/src/libkstmath/dataobject.cpp #742716:742717
@@ -23,6 +23,8 @@
 #include "dataplugin.h"
 #include "debug.h"
 #include "kst_i18n.h"
+#include "objectstore.h"
+#include "relation.h"
 
 #include <qdebug.h>
 #include <qtimer.h>
@@ -50,44 +52,11 @@
 
 
 DataObject::~DataObject() {
-  // TODO: Remove our slave vectors, scalars, and strings, and matrices
-#if 0
-  stringList.lock().writeLock();
-  for (StringMap::Iterator it = _outputStrings.begin();
-                               it != _outputStrings.end();
-                                                      ++it) {
-    stringList.remove(it.value());
-  }
-  stringList.lock().unlock();
-
-  scalarList.lock().writeLock();
-  for (ScalarMap::Iterator it = _outputScalars.begin();
-                               it != _outputScalars.end();
-                                                      ++it) {
-    scalarList.remove(it.value());
-  }
-  scalarList.lock().unlock();
-
-  vectorList.lock().writeLock();
-  for (VectorMap::Iterator it = _outputVectors.begin();
-                               it != _outputVectors.end();
-                                                      ++it) {
-    vectorList.remove(it.value());
-  }
-  vectorList.lock().unlock();
-
-  matrixList.lock().writeLock();
-  for (MatrixMap::Iterator it = _outputMatrices.begin();
-       it != _outputMatrices.end();
-       ++it) {
-    matrixList.remove(it.value());
-  }
-  matrixList.lock().unlock();
-#endif
 //  qDebug() << "Destroying Data Object: " << tag().displayString() << endl;
   delete _curveHints;
 }
 
+
 static QMap<QString, DataObjectPtr> pluginInfo;
 void DataObject::cleanupForExit() {
   pluginInfo.clear(); //FIXME?
@@ -521,36 +490,63 @@
 }
 
 
-bool DataObject::deleteDependents() {
-  // FIXME: redo
-#if 0
-  dataObjectList.lock().readLock();
-  DataObjectList dol = dataObjectList;
-  dataObjectList.lock().unlock();
-  for (DataObjectList::Iterator i = dol.begin(); i != dol.end(); ++i) {
-    bool user = (*i)->uses(this);
-    if (!user) {
-      for (VectorMap::Iterator j = _outputVectors.begin(); !user && j != _outputVectors.end(); ++j) {
-        user = (*i)->uses(j.value().data());
+void DataObject::deleteDependents() {
+  DataObjectList dataObjects = _store->getObjects<DataObject>();
+  foreach (DataObjectPtr object, dataObjects) {
+    bool usesObject = object->uses(this);
+    if (!usesObject) {
+      for (VectorMap::Iterator j = _outputVectors.begin(); !usesObject && j != _outputVectors.end(); ++j) {
+        usesObject = object->uses(j.value().data());
       }
-      for (ScalarMap::Iterator j = _outputScalars.begin(); !user && j != _outputScalars.end(); ++j) {
-        user = (*i)->uses(j.value().data());
+      for (ScalarMap::Iterator j = _outputScalars.begin(); !usesObject && j != _outputScalars.end(); ++j) {
+        usesObject = object->uses(j.value().data());
       }
-      for (StringMap::Iterator j = _outputStrings.begin(); !user && j != _outputStrings.end(); ++j) {
-        user = (*i)->uses(j.value().data());
+      for (StringMap::Iterator j = _outputStrings.begin(); !usesObject && j != _outputStrings.end(); ++j) {
+        usesObject = object->uses(j.value().data());
       }
+      for (MatrixMap::Iterator j = _outputMatrices.begin(); !usesObject && j != _outputMatrices.end(); ++j) {
+        usesObject = object->uses(j.value().data());
+      }
     }
-    if (user) {
-      DataObjectPtr dop = *i;
-      dataObjectList.lock().writeLock();
-      dataObjectList.removeAll(dop);
-      dataObjectList.lock().unlock();
-      dop->deleteDependents();
+    if (usesObject) {
+      _store->removeObject(object);
     }
   }
-#endif
 
-  return true;
+  RelationList relations = _store->getObjects<Relation>();
+  foreach (RelationPtr relation, relations) {
+    bool usesRelation = relation->uses(this);
+    if (!usesRelation) {
+      for (VectorMap::Iterator j = _outputVectors.begin(); !usesRelation && j != _outputVectors.end(); ++j) {
+        usesRelation = relation->uses(j.value().data());
+      }
+      for (ScalarMap::Iterator j = _outputScalars.begin(); !usesRelation && j != _outputScalars.end(); ++j) {
+        usesRelation = relation->uses(j.value().data());
+      }
+      for (StringMap::Iterator j = _outputStrings.begin(); !usesRelation && j != _outputStrings.end(); ++j) {
+        usesRelation = relation->uses(j.value().data());
+      }
+      for (MatrixMap::Iterator j = _outputMatrices.begin(); !usesRelation && j != _outputMatrices.end(); ++j) {
+        usesRelation = relation->uses(j.value().data());
+      }
+    }
+    if (usesRelation) {
+      _store->removeObject(relation);
+    }
+  }
+
+  foreach (VectorPtr vector, _outputVectors) {
+    _store->removeObject(vector);
+  }
+  foreach (MatrixPtr matrix, _outputMatrices) {
+    _store->removeObject(matrix);
+  }
+  foreach (ScalarPtr scalar, _outputScalars) {
+    _store->removeObject(scalar);
+  }
+  foreach (StringPtr string, _outputStrings) {
+    _store->removeObject(string);
+  }
 }
 
 
--- branches/work/kst/portto4/kst/src/libkstmath/dataobject.h #742716:742717
@@ -99,7 +99,7 @@
 
     virtual const CurveHintList* curveHints() const;
 
-    virtual bool deleteDependents();
+    virtual void deleteDependents();
 
     bool duplicateDependents(DataObjectDataObjectMap& duplicatedMap);
 
--- branches/work/kst/portto4/kst/src/libkstmath/relation.cpp #742716:742717
@@ -58,9 +58,8 @@
 }
 
 
-bool Relation::deleteDependents() {
+void Relation::deleteDependents() {
   Data::self()->removeCurveFromPlots(this);
-  return true;
 }
 
 
@@ -259,5 +258,95 @@
   }
 }
 
+
+bool Relation::uses(ObjectPtr p) const {
+  VectorPtr v = kst_cast<Vector>(p);
+  if (v) {
+    for (VectorMap::ConstIterator j = _inputVectors.begin(); j != _inputVectors.end(); ++j) {
+      if (j.value() == v) {
+        return true;
+      }
+    }
+    QHashIterator<QString, Scalar*> scalarDictIter(v->scalars());
+    for (ScalarMap::ConstIterator j = _inputScalars.begin(); j != _inputScalars.end(); ++j) {
+      while (scalarDictIter.hasNext()) {
+        scalarDictIter.next();
+        if (scalarDictIter.value() == j.value()) {
+          return true;
+        }
+      }
+    }
+  } else if (MatrixPtr matrix = kst_cast<Matrix>(p)) {
+    for (MatrixMap::ConstIterator j = _inputMatrices.begin(); j != _inputMatrices.end(); ++j) {
+      if (j.value() == matrix) {
+        return true;
+      }
+    }
+    QHashIterator<QString, Scalar*> scalarDictIter(matrix->scalars());
+    for (ScalarMap::ConstIterator j = _inputScalars.begin(); j != _inputScalars.end(); ++j) {
+      while (scalarDictIter.hasNext()) {
+        scalarDictIter.next();
+        if (scalarDictIter.value() == j.value()) {
+          return true;
+        }
+      }
+    }
+  } else if (DataObjectPtr obj = kst_cast<DataObject>(p) ) {
+    // check all connections from this object to p
+    for (VectorMap::Iterator j = obj->outputVectors().begin(); j != obj->outputVectors().end(); ++j) {
+      for (VectorMap::ConstIterator k = _inputVectors.begin(); k != _inputVectors.end(); ++k) {
+        if (j.value() == k.value()) {
+          return true;
+        }
+      }
+      // also check dependencies on vector stats
+      QHashIterator<QString, Scalar*> scalarDictIter(j.value()->scalars());
+      for (ScalarMap::ConstIterator k = _inputScalars.begin(); k != _inputScalars.end(); ++k) {
+        while (scalarDictIter.hasNext()) {
+          scalarDictIter.next();
+          if (scalarDictIter.value() == k.value()) {
+            return true;
+          }
+        }
+      }
+    }
+
+    for (MatrixMap::Iterator j = obj->outputMatrices().begin(); j != obj->outputMatrices().end(); ++j) {
+      for (MatrixMap::ConstIterator k = _inputMatrices.begin(); k != _inputMatrices.end(); ++k) {
+        if (j.value() == k.value()) {
+          return true;
+        }
+      }
+      // also check dependencies on vector stats
+      QHashIterator<QString, Scalar*> scalarDictIter(j.value()->scalars());
+      for (ScalarMap::ConstIterator k = _inputScalars.begin(); k != _inputScalars.end(); ++k) {
+        while (scalarDictIter.hasNext()) {
+          scalarDictIter.next();
+          if (scalarDictIter.value() == k.value()) {
+            return true;
+          }
+        }
+      }
+    }
+
+    for (ScalarMap::Iterator j = obj->outputScalars().begin(); j != obj->outputScalars().end(); ++j) {
+      for (ScalarMap::ConstIterator k = _inputScalars.begin(); k != _inputScalars.end(); ++k) {
+        if (j.value() == k.value()) {
+          return true;
+        }
+      }
+    }
+
+    for (StringMap::Iterator j = obj->outputStrings().begin(); j != obj->outputStrings().end(); ++j) {
+      for (StringMap::ConstIterator k = _inputStrings.begin(); k != _inputStrings.end(); ++k) {
+        if (j.value() == k.value()) {
+          return true;
+        }
+      }
+    }
+  }
+  return false;
 }
+
+}
 // vim: ts=2 sw=2 et
--- branches/work/kst/portto4/kst/src/libkstmath/relation.h #742716:742717
@@ -93,7 +93,7 @@
 
     virtual int samplesPerFrame() const { return 1; }
 
-    virtual bool deleteDependents();
+    virtual void deleteDependents();
 
     virtual double maxX() const { return MaxX; }
     virtual double minX() const { return MinX; }
@@ -109,6 +109,8 @@
     virtual double midY() const { return (MaxY+MinY)*0.5; }
     virtual void yRange(double xFrom, double xTo, double* yMin, double* yMax) = 0;
 
+    virtual bool uses(ObjectPtr p) const;
+
     // this returns the data object providing the data for this basecurve.
     // E.g. for VCurves, it returns the data object providing the y vector
     // E.g. for Images, it returns the data object providing the matrix


More information about the Kst mailing list