summaryrefslogtreecommitdiffstats
path: root/src/core/aspects
diff options
context:
space:
mode:
authorSean Harmer <sean.harmer@kdab.com>2016-02-25 14:56:15 +0000
committerSean Harmer <sean.harmer@kdab.com>2016-03-16 15:49:37 +0000
commit851f6b874113ccfa63394906fdf32364aa732416 (patch)
treed06beafc78ec8ea059dde430e83b34e884749ede /src/core/aspects
parentf7f7ffdc93f7a85be38cd99f3c6785f641dc1d4d (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')
-rw-r--r--src/core/aspects/qaspectengine.cpp47
-rw-r--r--src/core/aspects/qaspectengine.h2
-rw-r--r--src/core/aspects/qaspectengine_p.h1
3 files changed, 47 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
diff --git a/src/core/aspects/qaspectengine.h b/src/core/aspects/qaspectengine.h
index bbeee0998..4401da606 100644
--- a/src/core/aspects/qaspectengine.h
+++ b/src/core/aspects/qaspectengine.h
@@ -69,6 +69,8 @@ public:
void registerAspect(QAbstractAspect *aspect);
void registerAspect(const QString &name);
+ void unregisterAspect(QAbstractAspect *aspect);
+ void unregisterAspect(const QString &name);
QVector<QAbstractAspect*> aspects() const;
diff --git a/src/core/aspects/qaspectengine_p.h b/src/core/aspects/qaspectengine_p.h
index 6c5b2abc0..334df28b2 100644
--- a/src/core/aspects/qaspectengine_p.h
+++ b/src/core/aspects/qaspectengine_p.h
@@ -79,6 +79,7 @@ public:
QScene *m_scene;
QSharedPointer<QEntity> m_root;
QVector<QAbstractAspect*> m_aspects;
+ QHash<QString, QAbstractAspect *> m_namedAspects;
void initialize();
void shutdown();