Aggregate Classclass Aggregation::Aggregate
The Aggregate class defines a collection of related components that can be viewed as a unit. More...
Note: All functions in this class are thread-safe.
|Aggregate(QObject *parent = nullptr)|
|Aggregation::Aggregate *||parentAggregate(QObject *obj)|
An aggregate is a collection of components that are handled as a unit, such that each component exposes the properties and behavior of the other components in the aggregate to the outside. Specifically that means:
- They can be cast to each other (using query() and query_all() functions).
- Their life cycle is coupled. That is, whenever one is deleted, all of them are.
Components can be of any QObject derived type.
You can use an aggregate to simulate multiple inheritance by aggregation. Assuming we have the following code:
The query function works like a qobject_cast() with normal objects:
Q_ASSERT(query<MyInterface>(object) == object); Q_ASSERT(query<MyInterfaceEx>(object) == 0);
If we want
object to also implement the class
MyInterfaceEx, but don't want to or cannot use multiple inheritance, we can do it at any point using an aggregate:
MyInterfaceEx *objectEx = new MyInterfaceEx; Aggregate *aggregate = new Aggregate; aggregate->add(object); aggregate->add(objectEx);
The aggregate bundles the two objects together. If we have any part of the collection we get all parts:
Q_ASSERT(query<MyInterface>(object) == object); Q_ASSERT(query<MyInterfaceEx>(object) == objectEx); Q_ASSERT(query<MyInterface>(objectEx) == object); Q_ASSERT(query<MyInterfaceEx>(objectEx) == objectEx);
The following deletes all three:
delete objectEx; // or delete object; // or delete aggregate;
Member Function Documentation
Aggregate::Aggregate(QObject *parent = nullptr)
Creates a new aggregate with the given parent. The parent is directly passed to the QObject part of the class and is not used beside that.
This signal is emitted when a component is added to or removed from an aggregate.
Deleting the aggregate automatically deletes all its components.
void Aggregate::add(QObject *component)
Adds the component to the aggregate. You cannot add a component that is part of a different aggregate or an aggregate itself.
See also remove().
Template function that returns the component with the given type, if there is one. If there are multiple components with that type, a random one is returned.
template <typename T> QList<T *> Aggregate::components()
Template function that returns all components with the given type, if there are any.
[static] Aggregation::Aggregate *Aggregate::parentAggregate(QObject *obj)
Returns the aggregate object of obj if there is one. Otherwise returns 0.
void Aggregate::remove(QObject *component)
Removes the component from the aggregate.
See also add().
template <typename T> T *query(QObject *obj)
Performs a dynamic cast that is aware of a possible aggregate that obj might belong to. If obj itself is of the requested type, it is simply cast and returned. Otherwise, if obj belongs to an aggregate, all its components are checked. If it doesn't belong to an aggregate, null is returned.
See also Aggregate::component().
If obj belongs to an aggregate, all components that can be cast to the given type are returned. Otherwise, obj is returned if it is of the requested type.
See also Aggregate::components().
© 2020 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.