diff options
Diffstat (limited to 'src/control/control.cpp')
-rw-r--r-- | src/control/control.cpp | 141 |
1 files changed, 81 insertions, 60 deletions
diff --git a/src/control/control.cpp b/src/control/control.cpp index d46fcb1d78..99f72ce37f 100644 --- a/src/control/control.cpp +++ b/src/control/control.cpp @@ -1,46 +1,38 @@ -#include <QtDebug> -#include <QSharedPointer> - #include "control/control.h" +#include "control/controlobject.h" #include "util/stat.h" -// Static member variable definition +//static UserSettingsPointer ControlDoublePrivate::s_pUserConfig; -QHash<ConfigKey, QWeakPointer<ControlDoublePrivate> > ControlDoublePrivate::s_qCOHash -GUARDED_BY(ControlDoublePrivate::s_qCOHashMutex); +//static +QHash<ConfigKey, QWeakPointer<ControlDoublePrivate>> ControlDoublePrivate::s_qCOHash + GUARDED_BY(ControlDoublePrivate::s_qCOHashMutex); +//static QHash<ConfigKey, ConfigKey> ControlDoublePrivate::s_qCOAliasHash -GUARDED_BY(ControlDoublePrivate::s_qCOHashMutex); + GUARDED_BY(ControlDoublePrivate::s_qCOHashMutex); +//static MMutex ControlDoublePrivate::s_qCOHashMutex; -/* -ControlDoublePrivate::ControlDoublePrivate() - : m_bIgnoreNops(true), - m_bTrack(false), - m_trackType(Stat::UNSPECIFIED), - m_trackFlags(Stat::COUNT | Stat::SUM | Stat::AVERAGE | - Stat::SAMPLE_VARIANCE | Stat::MIN | Stat::MAX), - m_confirmRequired(false) { - initialize(); -} -*/ - -ControlDoublePrivate::ControlDoublePrivate(ConfigKey key, - ControlObject* pCreatorCO, - bool bIgnoreNops, bool bTrack, - bool bPersist, double defaultValue) +ControlDoublePrivate::ControlDoublePrivate( + ConfigKey key, + ControlObject* pCreatorCO, + bool bIgnoreNops, + bool bTrack, + bool bPersist, + double defaultValue) : m_key(key), + m_pCreatorCO(pCreatorCO), m_bPersistInConfiguration(bPersist), m_bIgnoreNops(bIgnoreNops), m_bTrack(bTrack), m_trackType(Stat::UNSPECIFIED), m_trackFlags(Stat::COUNT | Stat::SUM | Stat::AVERAGE | - Stat::SAMPLE_VARIANCE | Stat::MIN | Stat::MAX), - m_confirmRequired(false), - m_pCreatorCO(pCreatorCO) { + Stat::SAMPLE_VARIANCE | Stat::MIN | Stat::MAX), + m_confirmRequired(false) { initialize(defaultValue); } @@ -112,59 +104,88 @@ QSharedPointer<ControlDoublePrivate> ControlDoublePrivate::getControl( VERIFY_OR_DEBUG_ASSERT(!key.isEmpty()) { qWarning() << "ControlDoublePrivate::getControl returning NULL" << "for empty ConfigKey."; - return QSharedPointer<ControlDoublePrivate>(); + return nullptr; } - QSharedPointer<ControlDoublePrivate> pControl; // Scope for MMutexLocker. { - MMutexLocker locker(&s_qCOHashMutex); - auto it = s_qCOHash.constFind(key); - if (it != s_qCOHash.constEnd()) { - if (pCreatorCO) { - qWarning() << "ControlObject" << key.group << key.item << "already created"; - DEBUG_ASSERT(!"ControlObject already created"); + const MMutexLocker locker(&s_qCOHashMutex); + const auto it = s_qCOHash.find(key); + if (it != s_qCOHash.end()) { + auto pControl = it.value().lock(); + if (pControl) { + // Control object already exists + VERIFY_OR_DEBUG_ASSERT(!pCreatorCO) { + qWarning() + << "ControlObject" + << key.group << key.item + << "already created"; + return nullptr; + } + return pControl; } else { - pControl = it.value(); + // The weak pointer has become invalid and can be cleaned up + s_qCOHash.erase(it); } } } - if (pControl == NULL) { - if (pCreatorCO) { - pControl = QSharedPointer<ControlDoublePrivate>( - new ControlDoublePrivate(key, pCreatorCO, bIgnoreNops, - bTrack, bPersist, defaultValue)); - MMutexLocker locker(&s_qCOHashMutex); - //qDebug() << "ControlDoublePrivate::s_qCOHash.insert(" << key.group << "," << key.item << ")"; - s_qCOHash.insert(key, pControl); - } else if (!flags.testFlag(ControlFlag::NoWarnIfMissing)) { - qWarning() << "ControlDoublePrivate::getControl returning NULL for (" - << key.group << "," << key.item << ")"; - DEBUG_ASSERT(flags.testFlag(ControlFlag::NoAssertIfMissing)); - } + if (pCreatorCO) { + auto pControl = QSharedPointer<ControlDoublePrivate>( + new ControlDoublePrivate(key, + pCreatorCO, + bIgnoreNops, + bTrack, + bPersist, + defaultValue)); + const MMutexLocker locker(&s_qCOHashMutex); + //qDebug() << "ControlDoublePrivate::s_qCOHash.insert(" << key.group << "," << key.item << ")"; + s_qCOHash.insert(key, pControl); + return pControl; + } + + if (!flags.testFlag(ControlFlag::NoWarnIfMissing)) { + qWarning() << "ControlDoublePrivate::getControl returning NULL for (" + << key.group << "," << key.item << ")"; + DEBUG_ASSERT(flags.testFlag(ControlFlag::NoAssertIfMissing)); } - return pControl; + return nullptr; } // static -void ControlDoublePrivate::getControls( - QList<QSharedPointer<ControlDoublePrivate> >* pControlList) { - s_qCOHashMutex.lock(); - pControlList->clear(); - for (auto it = s_qCOHash.constBegin(); it != s_qCOHash.constEnd(); ++it) { - QSharedPointer<ControlDoublePrivate> pControl = it.value(); - if (!pControl.isNull()) { - pControlList->push_back(pControl); +QList<QSharedPointer<ControlDoublePrivate>> ControlDoublePrivate::getAllInstances() { + QList<QSharedPointer<ControlDoublePrivate>> result; + MMutexLocker locker(&s_qCOHashMutex); + result.reserve(s_qCOHash.size()); + for (auto it = s_qCOHash.begin(); it != s_qCOHash.end(); ++it) { + auto pControl = it.value().lock(); + if (pControl) { + result.append(std::move(pControl)); + } else { + // The weak pointer has become invalid and can be cleaned up + s_qCOHash.erase(it); } } - s_qCOHashMutex.unlock(); + return result; } // static -QHash<ConfigKey, ConfigKey> ControlDoublePrivate::getControlAliases() { +QList<QSharedPointer<ControlDoublePrivate>> ControlDoublePrivate::takeAllInstances() { + QList<QSharedPointer<ControlDoublePrivate>> result; MMutexLocker locker(&s_qCOHashMutex); - return s_qCOAliasHash; + result.reserve(s_qCOHash.size()); + for (auto it = s_qCOHash.begin(); it != s_qCOHash.end(); ++it) { + auto pControl = it.value().lock(); + if (pControl) { + result.append(std::move(pControl)); + } + } + s_qCOHash.clear(); + return result; +} + +void ControlDoublePrivate::deleteCreatorCO() { + delete m_pCreatorCO.fetchAndStoreOrdered(nullptr); } void ControlDoublePrivate::reset() { |