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 committed by Dirk Hohndel
parent f20d6187f0
commit 05200f9266
13 changed files with 86 additions and 89 deletions

View file

@ -44,7 +44,6 @@
#include "core/settings/qPrefUnit.h"
#include "core/trip.h"
QMLManager *QMLManager::m_instance = NULL;
bool noCloudToCloud = false;
#define RED_FONT QLatin1Literal("<font color=\"red\">")
@ -150,7 +149,6 @@ QMLManager::QMLManager() : m_locationServiceEnabled(false),
m_showNonDiveComputers(false)
{
LOG_STP("qmlmgr starting");
m_instance = this;
m_lastDevicePixelRatio = qApp->devicePixelRatio();
timer.start();
connect(qobject_cast<QApplication *>(QApplication::instance()), &QApplication::applicationStateChanged, this, &QMLManager::applicationStateChanged);
@ -451,12 +449,6 @@ QMLManager::~QMLManager()
if (appLogFileOpen)
appLogFile.close();
#endif
m_instance = NULL;
}
QMLManager *QMLManager::instance()
{
return m_instance;
}
#define CLOUDURL QString(prefs.cloud_base_url)

View file

@ -13,13 +13,14 @@
#include "core/btdiscovery.h"
#include "core/gpslocation.h"
#include "core/downloadfromdcthread.h"
#include "core/singleton.h"
#include "qt-models/divelistmodel.h"
#include "qt-models/completionmodels.h"
#include "qt-models/divelocationmodel.h"
#define NOCLOUD_LOCALSTORAGE format_string("%s/cloudstorage/localrepo[master]", system_default_directory())
class QMLManager : public QObject {
class QMLManager : public QObject, public SillySingleton<QMLManager> {
Q_OBJECT
Q_PROPERTY(QString logText READ logText WRITE setLogText NOTIFY logTextChanged)
Q_PROPERTY(bool locationServiceEnabled MEMBER m_locationServiceEnabled WRITE setLocationServiceEnabled NOTIFY locationServiceEnabledChanged)
@ -89,7 +90,6 @@ public:
Q_INVOKABLE void setGitLocalOnly(const bool &value);
Q_INVOKABLE void setFilter(const QString filterText);
static QMLManager *instance();
Q_INVOKABLE void registerError(QString error);
QString consumeError();
@ -219,7 +219,6 @@ private:
bool m_verboseEnabled;
GpsLocation *locationProvider;
bool m_loadFromCloud;
static QMLManager *m_instance;
struct dive *deletedDive;
struct dive_trip *deletedTrip;
QString m_notificationText;

View file

@ -8,30 +8,13 @@
/*** Global and constructors ***/
QMLPrefs *QMLPrefs::m_instance = NULL;
QMLPrefs::QMLPrefs() :
m_credentialStatus(qPrefCloudStorage::CS_UNKNOWN),
m_oldStatus(qPrefCloudStorage::CS_UNKNOWN),
m_showPin(false)
{
// This strange construct is needed because QMLEngine calls new and that
// cannot be overwritten
if (!m_instance)
m_instance = this;
}
QMLPrefs::~QMLPrefs()
{
m_instance = NULL;
}
QMLPrefs *QMLPrefs::instance()
{
return m_instance;
}
/*** public functions ***/
const QString QMLPrefs::cloudPassword() const
{

View file

@ -5,9 +5,9 @@
#include <QObject>
#include "core/settings/qPrefCloudStorage.h"
#include "core/settings/qPrefDisplay.h"
#include "core/singleton.h"
class QMLPrefs : public QObject {
class QMLPrefs : public QObject, public SillySingleton<QMLPrefs> {
Q_OBJECT
Q_PROPERTY(QString cloudPassword
MEMBER m_cloudPassword
@ -35,9 +35,6 @@ class QMLPrefs : public QObject {
NOTIFY oldStatusChanged)
public:
QMLPrefs();
~QMLPrefs();
static QMLPrefs *instance();
const QString cloudPassword() const;
void setCloudPassword(const QString &cloudPassword);
@ -66,7 +63,6 @@ private:
QString m_cloudPin;
QString m_cloudUserName;
qPrefCloudStorage::cloud_status m_credentialStatus;
static QMLPrefs *m_instance;
qPrefCloudStorage::cloud_status m_oldStatus;
bool m_showPin;