diff options
author | Sean Harmer <sean.harmer@kdab.com> | 2016-02-25 14:56:15 +0000 |
---|---|---|
committer | Sean Harmer <sean.harmer@kdab.com> | 2016-03-16 15:49:37 +0000 |
commit | 851f6b874113ccfa63394906fdf32364aa732416 (patch) | |
tree | d06beafc78ec8ea059dde430e83b34e884749ede /src/core/aspects/qaspectengine.cpp | |
parent | f7f7ffdc93f7a85be38cd99f3c6785f641dc1d4d (diff) |
Add skeleton unRegisterAspect functions to QAspectEngine
Also add note to docs about ownership of aspects.
Task-number: QTBUG-51421
Change-Id: I9855b81ca587ddb0b943e747aeec28a496a49ef7
Reviewed-by: Paul Lemire <paul.lemire@kdab.com>
Reviewed-by: Kevin Ottens <kevin.ottens@kdab.com>
Diffstat (limited to 'src/core/aspects/qaspectengine.cpp')
-rw-r--r-- | src/core/aspects/qaspectengine.cpp | 47 |
1 files changed, 44 insertions, 3 deletions
diff --git a/src/core/aspects/qaspectengine.cpp b/src/core/aspects/qaspectengine.cpp index 2045d623b..f159710f9 100644 --- a/src/core/aspects/qaspectengine.cpp +++ b/src/core/aspects/qaspectengine.cpp @@ -167,8 +167,9 @@ void QAspectEnginePrivate::shutdown() } /*! - * Registers a new \a aspect to the AspectManager. - */ + Registers a new \a aspect to the AspectManager. The QAspectEngine takes + ownership of the aspect and will delete it when the aspect is unregistered. +*/ // Called in the main thread void QAspectEngine::registerAspect(QAbstractAspect *aspect) { @@ -194,8 +195,48 @@ void QAspectEngine::registerAspect(const QString &name) { Q_D(QAspectEngine); QAbstractAspect *aspect = d->m_factory.createAspect(name); - if (aspect) + if (aspect) { registerAspect(aspect); + d->m_namedAspects.insert(name, aspect); + } +} + +void QAspectEngine::unregisterAspect(QAbstractAspect *aspect) +{ + Q_D(QAspectEngine); + if (!d->m_aspects.contains(aspect)) { + qWarning() << "Attempting to unregister an aspect that is not registered"; + return; + } + + // Cleanly shutdown this aspect by setting its root entity to null which + // will cause its onEngineShutdown() virtual to be called to allow it to + // cleanup any resources. Then remove it from the QAspectManager's list + // of aspects. + // TODO: Implement this once we are able to cleanly shutdown + + // Remove from our collection of named aspects (if present) + const auto it = std::find_if(d->m_namedAspects.begin(), d->m_namedAspects.end(), + [aspect](QAbstractAspect *v) { return v == aspect; }); + if (it != d->m_namedAspects.end()) + d->m_namedAspects.erase(it); + + // Finally, scheduly the aspect for deletion. Do this via the event loop + // in case we are unregistering the aspect in response to a signal from it. + aspect->deleteLater(); +} + +void QAspectEngine::unregisterAspect(const QString &name) +{ + Q_D(QAspectEngine); + if (!d->m_namedAspects.contains(name)) { + qWarning() << "Attempting to unregister an aspect that is not registered"; + return; + } + + // Delegate unregistering and removal to the overload + QAbstractAspect *aspect = d->m_namedAspects.value(name); + unregisterAspect(aspect); } QVector<QAbstractAspect *> QAspectEngine::aspects() const |