Move singleton instance code to static member functions.

This commit is contained in:
Felix Geyer 2012-05-31 14:51:44 +02:00
parent 8d6b200dd7
commit d3fcf2e0c4
10 changed files with 65 additions and 45 deletions

View File

@ -21,6 +21,8 @@
#include <QtCore/QSettings> #include <QtCore/QSettings>
#include <QtGui/QDesktopServices> #include <QtGui/QDesktopServices>
Config* Config::m_instance(0);
QVariant Config::get(const QString& key) QVariant Config::get(const QString& key)
{ {
return m_settings->value(key, m_defaults.value(key)); return m_settings->value(key, m_defaults.value(key));
@ -73,13 +75,11 @@ Config::Config()
m_defaults.insert("security/clearclipboardtimeout", 10); m_defaults.insert("security/clearclipboardtimeout", 10);
} }
Config* config() Config* Config::instance()
{ {
static Config* instance(0); if (!m_instance) {
m_instance = new Config();
if (!instance) {
instance = new Config();
} }
return instance; return m_instance;
} }

View File

@ -30,17 +30,21 @@ public:
QVariant get(const QString& key, const QVariant& defaultValue); QVariant get(const QString& key, const QVariant& defaultValue);
void set(const QString& key, const QVariant& value); void set(const QString& key, const QVariant& value);
static Config* instance();
private: private:
Config(); Config();
static Config* m_instance;
QScopedPointer<QSettings> m_settings; QScopedPointer<QSettings> m_settings;
QHash<QString, QVariant> m_defaults; QHash<QString, QVariant> m_defaults;
Q_DISABLE_COPY(Config) Q_DISABLE_COPY(Config)
friend Config* config();
}; };
Config* config(); inline Config* config() {
return Config::instance();
}
#endif // KEEPASSX_CONFIG_H #endif // KEEPASSX_CONFIG_H

View File

@ -22,6 +22,8 @@
#include "config-keepassx.h" #include "config-keepassx.h"
DataPath* DataPath::m_instance(0);
QString DataPath::path(const QString& name) QString DataPath::path(const QString& name)
{ {
return m_basePath + name; return m_basePath + name;
@ -99,13 +101,11 @@ bool DataPath::testSetDir(const QString& dir)
} }
} }
DataPath* dataPath() DataPath* DataPath::instance()
{ {
static DataPath* instance = 0; if (!m_instance) {
m_instance = new DataPath();
if (!instance) {
instance = new DataPath();
} }
return instance; return m_instance;
} }

View File

@ -28,17 +28,21 @@ public:
QIcon applicationIcon(); QIcon applicationIcon();
QIcon icon(const QString& category, const QString& name, bool fromTheme = true); QIcon icon(const QString& category, const QString& name, bool fromTheme = true);
static DataPath* instance();
private: private:
DataPath(); DataPath();
bool testSetDir(const QString& dir); bool testSetDir(const QString& dir);
static DataPath* m_instance;
QString m_basePath; QString m_basePath;
Q_DISABLE_COPY(DataPath) Q_DISABLE_COPY(DataPath)
friend DataPath* dataPath();
}; };
DataPath* dataPath(); inline DataPath* dataPath() {
return DataPath::instance();
}
#endif // KEEPASSX_DATAPATH_H #endif // KEEPASSX_DATAPATH_H

View File

@ -19,6 +19,8 @@
#include "core/DataPath.h" #include "core/DataPath.h"
DatabaseIcons* DatabaseIcons::m_instance(0);
QImage DatabaseIcons::icon(int index) QImage DatabaseIcons::icon(int index)
{ {
if (index < 0 || index >= iconCount()) { if (index < 0 || index >= iconCount()) {
@ -146,13 +148,11 @@ DatabaseIcons::DatabaseIcons()
m_pixmapCacheKeys.resize(iconCount()); m_pixmapCacheKeys.resize(iconCount());
} }
DatabaseIcons* databaseIcons() DatabaseIcons* DatabaseIcons::instance()
{ {
static DatabaseIcons* instance(0); if (!m_instance) {
m_instance = new DatabaseIcons();
if (!instance) {
instance = new DatabaseIcons();
} }
return instance; return m_instance;
} }

View File

@ -31,18 +31,22 @@ public:
int iconCount(); int iconCount();
int expiredIconIndex(); int expiredIconIndex();
static DatabaseIcons* instance();
private: private:
DatabaseIcons(); DatabaseIcons();
static DatabaseIcons* m_instance;
QVector<QString> m_indexToName; QVector<QString> m_indexToName;
QVector<QImage> m_iconCache; QVector<QImage> m_iconCache;
QVector<QPixmapCache::Key> m_pixmapCacheKeys; QVector<QPixmapCache::Key> m_pixmapCacheKeys;
Q_DISABLE_COPY(DatabaseIcons) Q_DISABLE_COPY(DatabaseIcons)
friend DatabaseIcons* databaseIcons();
}; };
DatabaseIcons* databaseIcons(); inline DatabaseIcons* databaseIcons() {
return DatabaseIcons::instance();
}
#endif // KEEPASSX_DATABASEICONS_H #endif // KEEPASSX_DATABASEICONS_H

View File

@ -28,6 +28,8 @@
#include "core/Config.h" #include "core/Config.h"
Clipboard* Clipboard::m_instance(0);
Clipboard::Clipboard(QObject* parent) Clipboard::Clipboard(QObject* parent)
: QObject(parent) : QObject(parent)
, m_timer(new QTimer(this)) , m_timer(new QTimer(this))
@ -75,13 +77,11 @@ void Clipboard::clearClipboard()
#endif #endif
} }
Clipboard* clipboard() Clipboard* Clipboard::instance()
{ {
static Clipboard* instance(0); if (!m_instance) {
m_instance = new Clipboard(qApp);
if (!instance) {
instance = new Clipboard(qApp);
} }
return instance; return m_instance;
} }

View File

@ -30,17 +30,21 @@ public:
~Clipboard(); ~Clipboard();
void setText(const QString& text); void setText(const QString& text);
static Clipboard* instance();
private Q_SLOTS: private Q_SLOTS:
void clearClipboard(); void clearClipboard();
private: private:
explicit Clipboard(QObject* parent = 0); explicit Clipboard(QObject* parent = 0);
QTimer* m_timer; static Clipboard* m_instance;
friend Clipboard* clipboard(); QTimer* m_timer;
}; };
Clipboard* clipboard(); inline Clipboard* clipboard() {
return Clipboard::instance();
}
#endif // KEEPASSX_CLIPBOARD_H #endif // KEEPASSX_CLIPBOARD_H

View File

@ -19,6 +19,8 @@
#include "core/Config.h" #include "core/Config.h"
FileDialog* FileDialog::m_instance(0);
QString FileDialog::getOpenFileName(QWidget* parent, const QString& caption, QString dir, QString FileDialog::getOpenFileName(QWidget* parent, const QString& caption, QString dir,
const QString& filter, QString* selectedFilter, const QString& filter, QString* selectedFilter,
QFileDialog::Options options) QFileDialog::Options options)
@ -78,13 +80,11 @@ FileDialog::FileDialog()
{ {
} }
FileDialog* fileDialog() FileDialog* FileDialog::instance()
{ {
static FileDialog* instance(0); if (!m_instance) {
m_instance = new FileDialog();
if (!instance) {
instance = new FileDialog();
} }
return instance; return m_instance;
} }

View File

@ -36,15 +36,19 @@ public:
*/ */
void setNextFileName(const QString& fileName); void setNextFileName(const QString& fileName);
static FileDialog* instance();
private: private:
FileDialog(); FileDialog();
QString m_nextFileName; QString m_nextFileName;
Q_DISABLE_COPY(FileDialog) static FileDialog* m_instance;
friend FileDialog* fileDialog(); Q_DISABLE_COPY(FileDialog)
}; };
FileDialog* fileDialog(); inline FileDialog* fileDialog() {
return FileDialog::instance();
}
#endif // KEEPASSX_FILEDIALOG_H #endif // KEEPASSX_FILEDIALOG_H