mirror of
https://github.com/subsurface/subsurface.git
synced 2025-02-19 22:16:15 +00:00
Revert the singleton PR
It turns out that this isn't working the way it was intended to. Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
This commit is contained in:
parent
400b218f76
commit
9ae7040a91
15 changed files with 118 additions and 89 deletions
|
|
@ -15,6 +15,7 @@ extern QMap<QString, dc_descriptor_t *> descriptorLookup;
|
|||
namespace {
|
||||
QHash<QString, QBluetoothDeviceInfo> btDeviceInfo;
|
||||
}
|
||||
BTDiscovery *BTDiscovery::m_instance = NULL;
|
||||
|
||||
static dc_descriptor_t *getDeviceType(QString btName)
|
||||
// central function to convert a BT name to a Subsurface known vendor/model pair
|
||||
|
|
@ -138,6 +139,11 @@ BTDiscovery::BTDiscovery(QObject*) : m_btValid(false),
|
|||
m_showNonDiveComputers(false),
|
||||
discoveryAgent(nullptr)
|
||||
{
|
||||
if (m_instance) {
|
||||
qDebug() << "trying to create an additional BTDiscovery object";
|
||||
return;
|
||||
}
|
||||
m_instance = this;
|
||||
#if defined(BT_SUPPORT)
|
||||
QLoggingCategory::setFilterRules(QStringLiteral("qt.bluetooth* = true"));
|
||||
BTDiscoveryReDiscover();
|
||||
|
|
@ -196,11 +202,19 @@ void BTDiscovery::BTDiscoveryReDiscover()
|
|||
|
||||
BTDiscovery::~BTDiscovery()
|
||||
{
|
||||
m_instance = NULL;
|
||||
#if defined(BT_SUPPORT)
|
||||
delete discoveryAgent;
|
||||
#endif
|
||||
}
|
||||
|
||||
BTDiscovery *BTDiscovery::instance()
|
||||
{
|
||||
if (!m_instance)
|
||||
m_instance = new BTDiscovery();
|
||||
return m_instance;
|
||||
}
|
||||
|
||||
#if defined(BT_SUPPORT)
|
||||
extern void addBtUuid(QBluetoothUuid uuid);
|
||||
extern QHash<QString, QStringList> productList;
|
||||
|
|
|
|||
|
|
@ -10,7 +10,6 @@
|
|||
#include <QBluetoothDeviceDiscoveryAgent>
|
||||
#include <QBluetoothUuid>
|
||||
#include "core/libdivecomputer.h"
|
||||
#include "core/singleton.h"
|
||||
|
||||
#if defined(Q_OS_ANDROID)
|
||||
#include <QAndroidJniObject>
|
||||
|
|
@ -24,12 +23,13 @@ QString extractBluetoothAddress(const QString &address);
|
|||
QString extractBluetoothNameAddress(const QString &address, QString &name);
|
||||
QBluetoothDeviceInfo getBtDeviceInfo(const QString &devaddr);
|
||||
|
||||
class BTDiscovery : public QObject, public SillySingleton<BTDiscovery> {
|
||||
class BTDiscovery : public QObject {
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
BTDiscovery(QObject *parent = NULL);
|
||||
~BTDiscovery();
|
||||
static BTDiscovery *instance();
|
||||
|
||||
struct btPairedDevice {
|
||||
QString address;
|
||||
|
|
@ -57,6 +57,7 @@ public:
|
|||
void discoverAddress(QString address);
|
||||
|
||||
private:
|
||||
static BTDiscovery *m_instance;
|
||||
bool m_btValid;
|
||||
bool m_showNonDiveComputers;
|
||||
|
||||
|
|
|
|||
|
|
@ -15,12 +15,16 @@
|
|||
#include <QApplication>
|
||||
#include <QTimer>
|
||||
|
||||
GpsLocation *GpsLocation::m_Instance = NULL;
|
||||
|
||||
GpsLocation::GpsLocation(void (*showMsgCB)(const char *), QObject *parent) :
|
||||
QObject(parent),
|
||||
m_GpsSource(0),
|
||||
waitingForPosition(false),
|
||||
haveSource(UNKNOWN)
|
||||
{
|
||||
Q_ASSERT_X(m_Instance == NULL, "GpsLocation", "GpsLocation recreated");
|
||||
m_Instance = this;
|
||||
showMessageCB = showMsgCB;
|
||||
// create a QSettings object that's separate from the main application settings
|
||||
geoSettings = new QSettings(QSettings::NativeFormat, QSettings::UserScope,
|
||||
|
|
@ -33,6 +37,23 @@ GpsLocation::GpsLocation(void (*showMsgCB)(const char *), QObject *parent) :
|
|||
connect(qPrefLocationService::instance(), SIGNAL(time_thresholdChanged(int)), this, SLOT(setGpsTimeThreshold(int)));
|
||||
}
|
||||
|
||||
GpsLocation *GpsLocation::instance()
|
||||
{
|
||||
Q_ASSERT(m_Instance != NULL);
|
||||
|
||||
return m_Instance;
|
||||
}
|
||||
|
||||
bool GpsLocation::hasInstance()
|
||||
{
|
||||
return m_Instance != NULL;
|
||||
}
|
||||
|
||||
GpsLocation::~GpsLocation()
|
||||
{
|
||||
m_Instance = NULL;
|
||||
}
|
||||
|
||||
void GpsLocation::setGpsTimeThreshold(int seconds)
|
||||
{
|
||||
if (m_GpsSource) {
|
||||
|
|
|
|||
|
|
@ -24,6 +24,9 @@ class GpsLocation : public QObject {
|
|||
Q_OBJECT
|
||||
public:
|
||||
GpsLocation(void (*showMsgCB)(const char *msg), QObject *parent);
|
||||
~GpsLocation();
|
||||
static GpsLocation *instance();
|
||||
static bool hasInstance();
|
||||
bool applyLocations();
|
||||
int getGpsNum() const;
|
||||
bool hasLocationsSource();
|
||||
|
|
@ -40,6 +43,7 @@ private:
|
|||
QNetworkReply *reply;
|
||||
QString userAgent;
|
||||
void (*showMessageCB)(const char *msg);
|
||||
static GpsLocation *m_Instance;
|
||||
bool waitingForPosition;
|
||||
QMap<qint64, gpsTracker> m_trackers;
|
||||
QList<gpsTracker> m_deletedTrackers;
|
||||
|
|
|
|||
|
|
@ -1,74 +0,0 @@
|
|||
// SPDX-License-Identifier: GPL-2.0
|
||||
/*
|
||||
* We use singletons in numerous places. In combination with QML this gives
|
||||
* us a very fundamental problem, because QML likes to allocate objects by
|
||||
* itself. There are known workarounds, but currently we use idiosyncratic
|
||||
* singleton classes, which initialize the global instance pointer in the
|
||||
* constructor. Things get even more complicated if the same singleton is used
|
||||
* on mobile and on desktop. The latter might want to simply use the classical
|
||||
* instance() function without having to initialize the singleton first, as
|
||||
* that would beat the purpose of the instance() method.
|
||||
*
|
||||
* The template defined here, SillySingleton, codifies all this. Simply derive
|
||||
* a class from this template:
|
||||
* class X : public SillySingleton<X> {
|
||||
* ...
|
||||
* };
|
||||
* This will generate an instance() method. This will do the right thing for
|
||||
* both methods: explicit construction of the singleton via new or implicit
|
||||
* by calling the instance() method. It will also generate warnings if a
|
||||
* singleton class is generated more than once (i.e. first instance() is called
|
||||
* and _then_ new).
|
||||
*
|
||||
* In the long run we should get rid of all users of this class.
|
||||
*/
|
||||
#ifndef SINGLETON_H
|
||||
#define SINGLETON_H
|
||||
|
||||
#include <typeinfo>
|
||||
#include <QtGlobal>
|
||||
|
||||
// 1) Declaration
|
||||
template<typename T>
|
||||
class SillySingleton {
|
||||
static T *self;
|
||||
protected:
|
||||
SillySingleton();
|
||||
~SillySingleton();
|
||||
public:
|
||||
static T *instance();
|
||||
};
|
||||
|
||||
template<typename T>
|
||||
T *SillySingleton<T>::self = nullptr;
|
||||
|
||||
// 2) Implementation
|
||||
|
||||
template<typename T>
|
||||
SillySingleton<T>::SillySingleton()
|
||||
{
|
||||
if (self)
|
||||
qWarning("Generating second instance of singleton %s", typeid(T).name());
|
||||
self = static_cast<T *>(this);
|
||||
qDebug("Generated singleton %s", typeid(T).name());
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
SillySingleton<T>::~SillySingleton()
|
||||
{
|
||||
if (self == this)
|
||||
self = nullptr;
|
||||
else
|
||||
qWarning("Destroying unknown instance of singleton %s", typeid(T).name());
|
||||
qDebug("Destroyed singleton %s", typeid(T).name());
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
T *SillySingleton<T>::instance()
|
||||
{
|
||||
if (!self)
|
||||
new T;
|
||||
return self;
|
||||
}
|
||||
|
||||
#endif
|
||||
Loading…
Add table
Add a link
Reference in a new issue