now using more pointers
authorHeiko Schlittermann (JUMPER) <hs@schlittermann.de>
Sun, 29 Jan 2012 01:51:02 +0100
changeset 28 c55e1f2a4db4
parent 27 dfefb6003498
child 29 830a0d1d9d7f
now using more pointers It's really confusing, when to use a pointer and who takes care about that pointer…
igc.cc
igc.h
mainwindow.cc
mainwindow.h
--- a/igc.cc	Sun Jan 29 00:38:40 2012 +0100
+++ b/igc.cc	Sun Jan 29 01:51:02 2012 +0100
@@ -1,5 +1,51 @@
 #include "igc.h"
 
+Fix::Fix(const QString &s)
+{
+    int pos = 0;
+
+    time_ = QTime::fromString(s.mid(pos, 6), "hhmmss"); 
+    pos += 6;	
+
+    gps_.lat = s.mid(pos, 8); pos += 8;	// DDMMmmm[NS]
+    gps_.lon = s.mid(pos, 9); pos += 9;	// DDDMMmmm[EW]
+
+    is3d_ = s.mid(pos, 1) == "A"; 
+    pos += 1;
+
+    alt_[baro] = s.mid(pos, 5).toInt(); pos += 5;
+    alt_[gps]  = s.mid(pos, 5).toInt(); pos += 5;
+}
+
+
+IGC::IGC()
+#ifdef __GXX_EXPERIMENTAL_CXX0X__
+    : altitude_({0})
+#endif
+{
+#ifndef __GXX_EXPERIMENTAL_CXX0X__
+    memset(altitude_, 0, sizeof(altitude_));
+#endif
+}
+
+IGC::IGC(QTextStream &s)
+#ifdef __GXX_EXPERIMENTAL_CXX0X__
+    : altitude_({0})
+#endif
+{
+#ifndef __GXX_EXPERIMENTAL_CXX0X__
+    memset(altitude_, 0, sizeof(altitude_));
+#endif
+    read(s);
+}
+
+IGC::~IGC()
+{
+    for (size_t i = 0; i < sizeof(altitude_)/sizeof(altitude_[0]); ++i) {
+	delete altitude_[i];
+    }
+}
+
 void IGC::read(QTextStream &in)
 {
     while (!in.atEnd()) {
@@ -37,19 +83,19 @@
     }
 }
 
-Fix::Fix(const QString &s)
+const QVector<QPointF>& IGC::altitude(Fix::Source src) const
 {
-    int pos = 0;
+   
+    // optimization: if already allocated, return the allocated vector
+    if (altitude_[src]) return *altitude_[src];
 
-    time_ = QTime::fromString(s.mid(pos, 6), "hhmmss"); 
-    pos += 6;	
+    altitude_[src] = new QVector<QPointF>;
 
-    gps_.lat = s.mid(pos, 8); pos += 8;	// DDMMmmm[NS]
-    gps_.lon = s.mid(pos, 9); pos += 9;	// DDDMMmmm[EW]
+    for (Fixes::const_iterator i = fixes_.begin();
+	     i != fixes_.end();
+	     i++) {
+	     *altitude_[src] << QPointF(QTime().secsTo(i->time()), i->altitude(src));
+    }
 
-    is3d_ = s.mid(pos, 1) == "A"; 
-    pos += 1;
-
-    alt_[baro] = s.mid(pos, 5).toInt(); pos += 5;
-    alt_[gps]  = s.mid(pos, 5).toInt(); pos += 5;
+    return *altitude_[src];
 }
--- a/igc.h	Sun Jan 29 00:38:40 2012 +0100
+++ b/igc.h	Sun Jan 29 01:51:02 2012 +0100
@@ -54,6 +54,10 @@
 class IGC {
   public:
 
+    IGC();
+    IGC(QTextStream&);
+    ~IGC();
+
     //! Read the IGC records.
     //!	\param s a (file) stream containing the records
     void read(QTextStream& s);
@@ -66,11 +70,13 @@
 
     Fix start() const { return start_; }	//!< the very first fix
     Fix landing() const { return landing_; }	//!< the very last fix
+    const QVector<QPointF>& altitude(Fix::Source) const;
 
   private:
     QString device_, pilot_, glider_;
     QDate date_;
     Fixes fixes_;
+    mutable QVector<QPointF>* altitude_[2];
 
     Fix start_;
     Fix landing_;
--- a/mainwindow.cc	Sun Jan 29 00:38:40 2012 +0100
+++ b/mainwindow.cc	Sun Jan 29 01:51:02 2012 +0100
@@ -9,6 +9,7 @@
 
 MainWindow::MainWindow(QWidget *parent)
     : QMainWindow(parent), ui_(new Ui::MainWindow),
+    igc_(0),
     plot_(new QwtPlot(tr("Altitude"), this))
 {
     ui_->setupUi(this);
@@ -41,6 +42,8 @@
 MainWindow::~MainWindow()
 {
     delete ui_;
+    delete igc_;
+    delete plot_;
 }
 
 void MainWindow::open(const QString &s)
@@ -72,9 +75,16 @@
     setWindowTitle(QFileInfo(fileName).baseName());
     statusBar()->showMessage(tr("opening %1").arg(fileName), 5000);
 
-    igc_.read(input_);
+    delete igc_;
+    igc_ = new IGC(input_);
 
+    showInfo();
+    showProfile();
 
+}
+
+void MainWindow::showInfo()
+{
     // now let's fill the table
     QTableWidget *t = ui_->table;
     int r = 0;
@@ -88,57 +98,32 @@
     // fill
     t->setRowCount(t->rowCount() + 1);
     t->setItem(r, K, new QTableWidgetItem(tr("Pilot")));
-    t->setItem(r, V, new QTableWidgetItem(igc_.pilot()));
+    t->setItem(r, V, new QTableWidgetItem(igc_->pilot()));
     ++r;
 
     t->setRowCount(t->rowCount() + 1);
     t->setItem(r, K, new QTableWidgetItem(tr("Date")));
-    t->setItem(r, V, new QTableWidgetItem(igc_
-	.date()
+    t->setItem(r, V, new QTableWidgetItem(igc_->date()
 	.toString(DefaultLocaleLongDate)));
     ++r;
 
     t->setRowCount(t->rowCount() + 1);
     t->setItem(r, K, new QTableWidgetItem(tr("Glider")));
-    t->setItem(r, V, new QTableWidgetItem(igc_.glider()));
+    t->setItem(r, V, new QTableWidgetItem(igc_->glider()));
     ++r;
 
     t->setRowCount(t->rowCount() + 1);
     t->setItem(r, K, new QTableWidgetItem(tr("Place")));
-    t->setItem(r, V, new QTableWidgetItem(igc_.start().longitude() + " / " + igc_.start().latitude()));
+    t->setItem(r, V, new QTableWidgetItem(igc_->start().longitude() + " / " + igc_->start().latitude()));
     ++r;
 
-    showProfile();
 
 }
 
 void MainWindow::showProfile()
 {
-	const Fixes f = igc_.fixes();
-#ifdef MYPLOT
-	QList< QPair<QTime, int> > altitudesBaro;
-	QList< QPair<QTime, int> > altitudesGPS;
-#endif
-	QVector<QPointF> pointsGPS;
-	QVector<QPointF> pointsBaro;
-
-
-	for (Fixes::const_iterator i = f.begin();
-	     i != f.end();
-	     i++) {
-#ifdef MYPLOT
-	     altitudesBaro << i->altitudeFix(Fix::baro);
-	     altitudesGPS << i->altitudeFix(Fix::gps);
-#endif
-	     pointsGPS << QPointF(QTime().secsTo(i->time()), i->altitude(Fix::gps));
-	     pointsBaro << QPointF(QTime().secsTo(i->time()), i->altitude(Fix::baro));
-	}
-
-#ifdef MYPLOT
-	myPlot_.draw(altitudesBaro.begin(), altitudesBaro.end());
-	myPlot_.draw(altitudesGPS.begin(), altitudesGPS.end());
-	ui_->altitude->setScene(&myPlot_);
-#endif
+	QVector<QPointF> pointsGPS = igc_->altitude(Fix::gps);
+	QVector<QPointF> pointsBaro = igc_->altitude(Fix::baro);
 
 	QwtPlotCurve *gps = new QwtPlotCurve(tr("Altitude GPS"));
 	QwtPlotCurve *baro = new QwtPlotCurve(tr("Altitude barometric"));
--- a/mainwindow.h	Sun Jan 29 00:38:40 2012 +0100
+++ b/mainwindow.h	Sun Jan 29 01:51:02 2012 +0100
@@ -12,23 +12,27 @@
 class MainWindow : public QMainWindow
 {
   Q_OBJECT
+
   public:
    MainWindow(QWidget *parent = 0);
    ~MainWindow();
+
   public slots:
    void open(const QString& = "");
+
   private:
+
    void showProfile();
+   void showInfo();
 
    Ui::MainWindow *ui_; 
+
    QFile currentFile_;
    QTextStream input_;
 
-   IGC	    igc_;
-#ifdef MYPLOT
-   Plot	    *myplot_;
-#endif
+   IGC	    *igc_;
    QwtPlot  *plot_;
+   
 
 };