dRonin
adbada4
dRonin GCS
|
Defines a collection of related components that can be viewed as a unit. More...
#include <aggregate.h>
Public Member Functions | |
Aggregate (QObject *parent=0) | |
virtual | ~Aggregate () |
void | add (QObject *component) |
void | remove (QObject *component) |
template<typename T > | |
T * | component () |
template<typename T > | |
QList< T * > | components () |
Static Public Member Functions | |
static Aggregate * | parentAggregate (QObject *obj) |
static QReadWriteLock & | lock () |
Related Functions | |
(Note that these are not member functions.) | |
T * | query (QObject *obj) |
QList< T * > | query_all (QObject *obj) |
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 methods). 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
The query method works like a qobject_cast with normal objects:
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:
The Aggregate bundles the two objects together. If we have any part of the collection we get all parts:
The following deletes all three: object, objectEx and aggregate:
Aggregation aware code never uses qobject_cast, but always uses Aggregation::query which behaves like a qobject_cast as a fallback.
Definition at line 41 of file aggregate.h.
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.
Definition at line 188 of file aggregate.cpp.
|
virtual |
Deleting the aggregate automatically deletes all its components.
Definition at line 200 of file aggregate.cpp.
void Aggregate::add | ( | QObject * | component | ) |
Adds the component to the aggregate.
Definition at line 229 of file aggregate.cpp.
|
inline |
Template method 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.
Definition at line 52 of file aggregate.h.
|
inline |
Template method that returns all components with the given type, if there are any.
Definition at line 61 of file aggregate.h.
|
static |
Definition at line 175 of file aggregate.cpp.
|
static |
Returns the Aggregate object of obj if there is one. Otherwise returns 0.
Definition at line 159 of file aggregate.cpp.
void Aggregate::remove | ( | QObject * | component | ) |
Removes the component from the aggregate.
Definition at line 251 of file aggregate.cpp.
|
related |
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.
Definition at line 92 of file aggregate.h.
|
related |
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.
Definition at line 113 of file aggregate.h.