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 <QtGui/QDesktopServices>
Config* Config::m_instance(0);
QVariant Config::get(const QString& key)
{
return m_settings->value(key, m_defaults.value(key));
@ -73,13 +75,11 @@ Config::Config()
m_defaults.insert("security/clearclipboardtimeout", 10);
}
Config* config()
Config* Config::instance()
{
static Config* instance(0);
if (!instance) {
instance = new Config();
if (!m_instance) {
m_instance = new Config();
}
return instance;
return m_instance;
}

View File

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

View File

@ -22,6 +22,8 @@
#include "config-keepassx.h"
DataPath* DataPath::m_instance(0);
QString DataPath::path(const QString& 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 (!instance) {
instance = new DataPath();
if (!m_instance) {
m_instance = new DataPath();
}
return instance;
return m_instance;
}

View File

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

View File

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

View File

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

View File

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

View File

@ -30,17 +30,21 @@ public:
~Clipboard();
void setText(const QString& text);
static Clipboard* instance();
private Q_SLOTS:
void clearClipboard();
private:
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

View File

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

View File

@ -36,15 +36,19 @@ public:
*/
void setNextFileName(const QString& fileName);
static FileDialog* instance();
private:
FileDialog();
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