diff options
author | Ahmad Samir <a.samirh78@gmail.com> | 2022-06-24 19:17:53 +0200 |
---|---|---|
committer | Ahmad Samir <a.samirh78@gmail.com> | 2022-07-17 13:11:48 +0000 |
commit | 491f546463df18c9d8831d803b6382bdd05d7622 (patch) | |
tree | f8455896017920a58f82c157fa03564bb507d80c | |
parent | d96f19bc22fa622c9f65b817714b555540920e80 (diff) | |
download | kconfig-491f546463df18c9d8831d803b6382bdd05d7622.tar.gz kconfig-491f546463df18c9d8831d803b6382bdd05d7622.tar.bz2 |
Don't inherit from containers
Containers (both Qt and STL) don't have virtual destructors, which could
be an issue when trying to delete an object of a derived class via a
pointer to the base class.
-rw-r--r-- | src/core/ksharedconfig.cpp | 33 |
1 files changed, 18 insertions, 15 deletions
diff --git a/src/core/ksharedconfig.cpp b/src/core/ksharedconfig.cpp index 09cd35f1..5d0661aa 100644 --- a/src/core/ksharedconfig.cpp +++ b/src/core/ksharedconfig.cpp @@ -16,10 +16,12 @@ void _k_globalMainConfigSync(); -class GlobalSharedConfigList : public QList<KSharedConfig *> +using SharedConfigList = QList<KSharedConfig *>; + +class GlobalSharedConfig { public: - GlobalSharedConfigList() + GlobalSharedConfig() : wasTestModeEnabled(false) { if (!qApp || QThread::currentThread() == qApp->thread()) { @@ -33,13 +35,14 @@ public: // the thread exits. } + SharedConfigList configList; // in addition to the list, we need to hold the main config, // so that it's not created and destroyed all the time. KSharedConfigPtr mainConfig; bool wasTestModeEnabled; }; -static QThreadStorage<GlobalSharedConfigList *> s_storage; +static QThreadStorage<GlobalSharedConfig *> s_storage; template<typename T> T *perThreadGlobalStatic() { @@ -50,14 +53,14 @@ T *perThreadGlobalStatic() } // Q_GLOBAL_STATIC(GlobalSharedConfigList, globalSharedConfigList), but per thread: -static GlobalSharedConfigList *globalSharedConfigList() +static GlobalSharedConfig *globalSharedConfig() { - return perThreadGlobalStatic<GlobalSharedConfigList>(); + return perThreadGlobalStatic<GlobalSharedConfig>(); } void _k_globalMainConfigSync() { - KSharedConfigPtr mainConfig = globalSharedConfigList()->mainConfig; + KSharedConfigPtr mainConfig = globalSharedConfig()->mainConfig; if (mainConfig) { mainConfig->sync(); } @@ -66,19 +69,19 @@ void _k_globalMainConfigSync() KSharedConfigPtr KSharedConfig::openConfig(const QString &_fileName, OpenFlags flags, QStandardPaths::StandardLocation resType) { QString fileName(_fileName); - GlobalSharedConfigList *list = globalSharedConfigList(); + GlobalSharedConfig *global = globalSharedConfig(); if (fileName.isEmpty() && !flags.testFlag(KConfig::SimpleConfig)) { // Determine the config file name that KConfig will make up (see KConfigPrivate::changeFileName) fileName = KConfig::mainConfigName(); } - if (!list->wasTestModeEnabled && QStandardPaths::isTestModeEnabled()) { - list->wasTestModeEnabled = true; - list->clear(); - list->mainConfig = nullptr; + if (!global->wasTestModeEnabled && QStandardPaths::isTestModeEnabled()) { + global->wasTestModeEnabled = true; + global->configList.clear(); + global->mainConfig = nullptr; } - for (auto *cfg : std::as_const(*list)) { + for (auto *cfg : std::as_const(global->configList)) { if (cfg->name() == fileName && cfg->d_ptr->openFlags == flags && cfg->locationType() == resType // cfg->backend()->type() == backend ) { @@ -89,7 +92,7 @@ KSharedConfigPtr KSharedConfig::openConfig(const QString &_fileName, OpenFlags f KSharedConfigPtr ptr(new KSharedConfig(fileName, flags, resType)); if (_fileName.isEmpty() && flags == FullConfig && resType == QStandardPaths::GenericConfigLocation) { - list->mainConfig = ptr; + global->mainConfig = ptr; const bool isMainThread = !qApp || QThread::currentThread() == qApp->thread(); static bool userWarned = false; @@ -123,13 +126,13 @@ KSharedConfig::Ptr KSharedConfig::openStateConfig(const QString &_fileName) KSharedConfig::KSharedConfig(const QString &fileName, OpenFlags flags, QStandardPaths::StandardLocation resType) : KConfig(fileName, flags, resType) { - globalSharedConfigList()->append(this); + globalSharedConfig()->configList.append(this); } KSharedConfig::~KSharedConfig() { if (s_storage.hasLocalData()) { - globalSharedConfigList()->removeAll(this); + globalSharedConfig()->configList.removeAll(this); } } |