Aggregate Class

class Aggregation::Aggregate

The Aggregate class defines a collection of related components that can be viewed as a unit. More...

Header: #include <aggregation/aggregate.h>
Inherits: QObject

Note: All functions in this class are thread-safe.

Public Functions

Aggregate(QObject *parent = nullptr)
virtual ~Aggregate() override
void add(QObject *component)
T *component()
QList<T *> components()
void remove(QObject *component)


void changed()

Static Public Members

Aggregation::Aggregate *parentAggregate(QObject *obj)
T *query(QObject *obj)
QList<T *> query_all(QObject *obj)

Detailed Description

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:

using namespace Aggregation;
class MyInterface : public QObject { ........ };
class MyInterfaceEx : public QObject { ........ };
MyInterface *object = new MyInterface; // this is single inheritance

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;

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: object, objectEx and aggregate:

delete objectEx;
// or delete object;
// or delete aggregate;

Aggregation-aware code never uses qobject_cast(). It always uses Aggregation::query(), which behaves like a qobject_cast() as a fallback.

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.

[signal] void Aggregate::changed()

This signal is emitted when a component is added to or removed from an aggregate.

See also add() and remove().

[override virtual] Aggregate::~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 <typename T> T *Aggregate::component()

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.

See also Aggregate::components() and add().

template <typename T> QList<T *> Aggregate::components()

Template function that returns all components with the given type, if there are any.

See also Aggregate::component() and add().

[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().

Related Non-Members

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().

template <typename T> QList<T *> query_all(QObject *obj)

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.