Cleanup: unify idiosyncratic singletons

The way we handle singletons in QML, QML insists on allocating the
objects. This leads to a very idiosyncratic way of handling
singletons: The global instance pointer is set in the constructor.

Unify all these by implementing a "SillySingleton" template. All
of the weird singleton-classes can derive from this template and
don't have to bother with reimplementing the instance() function
with all the safety-checks, etc.

This serves firstly as documentation but also improves debugging
as we will now see wanted and unwanted creation and destruction
of these weird singletons.

Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
This commit is contained in:
Berthold Stoeger 2019-09-22 14:33:33 +02:00
parent a004c95e52
commit eae9a99615
13 changed files with 86 additions and 89 deletions

View file

@ -134,11 +134,8 @@ QString DiveListSortModel::tripShortDate(const QString &section)
return QStringLiteral("%1\n'%2").arg(firstMonth,firstTime.toString("yy"));
}
DiveListModel *DiveListModel::m_instance = NULL;
DiveListModel::DiveListModel(QObject *parent) : QAbstractListModel(parent)
{
m_instance = this;
}
void DiveListModel::insertDive(int i)
@ -274,11 +271,6 @@ QString DiveListModel::startAddDive()
return QString::number(d->id);
}
DiveListModel *DiveListModel::instance()
{
return m_instance;
}
struct dive *DiveListModel::getDive(int i)
{
if (i < 0 || i >= dive_table.nr) {

View file

@ -6,6 +6,7 @@
#include <QSortFilterProxyModel>
#include "core/subsurface-qt/DiveObjectHelper.h"
#include "core/singleton.h"
class DiveListSortModel : public QSortFilterProxyModel
{
@ -28,7 +29,7 @@ private:
void updateFilterState();
};
class DiveListModel : public QAbstractListModel
class DiveListModel : public QAbstractListModel, public SillySingleton<DiveListModel>
{
Q_OBJECT
public:
@ -45,7 +46,6 @@ public:
DepthDurationRole,
};
static DiveListModel *instance();
DiveListModel(QObject *parent = 0);
void addDive(const QList<dive *> &listOfDives);
void addAllDives();
@ -63,8 +63,6 @@ public:
QString startAddDive();
void resetInternalData();
Q_INVOKABLE DiveObjectHelper at(int i);
private:
static DiveListModel *m_instance;
};
#endif // DIVELISTMODEL_H

View file

@ -3,11 +3,8 @@
#include "core/qthelper.h"
#include <QVector>
GpsListModel *GpsListModel::m_instance = NULL;
GpsListModel::GpsListModel(QObject *parent) : QAbstractListModel(parent)
{
m_instance = this;
}
void GpsListModel::update()
@ -62,9 +59,3 @@ QHash<int, QByteArray> GpsListModel::roleNames() const
roles[GpsLongitudeRole] = "longitude";
return roles;
}
GpsListModel *GpsListModel::instance()
{
return m_instance;
}

View file

@ -3,10 +3,10 @@
#define GPSLISTMODEL_H
#include "core/gpslocation.h"
#include <QObject>
#include "core/singleton.h"
#include <QAbstractListModel>
class GpsListModel : public QAbstractListModel
class GpsListModel : public QAbstractListModel, public SillySingleton<GpsListModel>
{
Q_OBJECT
public:
@ -19,7 +19,6 @@ public:
GpsWhenRole
};
static GpsListModel *instance();
GpsListModel(QObject *parent = 0);
void clear();
int rowCount(const QModelIndex &parent = QModelIndex()) const;
@ -28,7 +27,6 @@ public:
void update();
private:
QVector<gpsTracker> m_gpsFixes;
static GpsListModel *m_instance;
};
#endif // GPSLISTMODEL_H