Extending Qt Creator Manual | Aggregate

Extending Qt Creator Manual 4.3.0

# Aggregate Class

(Aggregation::Aggregate)

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

Header: | #include <Aggregate>

- List of all members, including inherited members

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

## Public Functions

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

## Signals

void | changed()

## Static Public Members

Aggregate * | parentAggregate(QObject *obj)

## Related Non-Members

T * | query(QObject *obj)
QList<T *> | query_all(QObject *obj)

## Detailed Description

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

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, i.e. 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. Assume we have

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

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

Aggregation aware code never uses qobject_cast, but always uses Aggregation::query which behaves like a qobject_cast as a fallback.

## Member Function Documentation

### Aggregate::Aggregate(QObject *parent = 0)

Creates a new Aggregate with the given parent. The parent is passed directly passed to the QObject part of the class and is not used beside that.

### `[virtual]` Aggregate::~Aggregate()

Deleting the aggregate automatically deletes all its components.

### void Aggregate::add(QObject *component)

Adds the component to the aggregate. You can't add a component that is part of a different aggregate or an aggregate itself.

See also Aggregate::remove().

### `[signal]` void Aggregate::changed()

### 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 Aggregate::add().

### QList<T *> Aggregate::components()

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

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

### `[static]` 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 Aggregate::add().

## Related Non-Members

### T *Aggregation::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 then it is simply cast and returned. Otherwise, if obj belongs to an Aggregate all its components are checked, or if it doesn't belong to an Aggregate null is returned.

See also Aggregate::component().

### QList<T *> Aggregation::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().