dRonin  adbada4
dRonin GCS
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Groups Pages
Aggregation::Aggregate Class Reference

Defines a collection of related components that can be viewed as a unit. More...

#include <aggregate.h>

Inheritance diagram for Aggregation::Aggregate:

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 AggregateparentAggregate (QObject *obj)
 
static QReadWriteLock & lock ()
 

Related Functions

(Note that these are not member functions.)

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

Detailed Description

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

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

The query method 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.

Definition at line 41 of file aggregate.h.

Constructor & Destructor 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.

Definition at line 188 of file aggregate.cpp.

Aggregate::~Aggregate ( )
virtual

Deleting the aggregate automatically deletes all its components.

Definition at line 200 of file aggregate.cpp.

Member Function Documentation

void Aggregate::add ( QObject *  component)

Adds the component to the aggregate.

See Also
Aggregate::remove()

Definition at line 229 of file aggregate.cpp.

template<typename T >
T * Aggregate::component ( )
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.

See Also
Aggregate::components()
Aggregate::add()

Definition at line 52 of file aggregate.h.

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

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

See Also
Aggregate::component()
Aggregate::add()

Definition at line 61 of file aggregate.h.

QReadWriteLock & Aggregate::lock ( )
static

Definition at line 175 of file aggregate.cpp.

Aggregate * Aggregate::parentAggregate ( QObject *  obj)
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.

See Also
Aggregate::add()

Definition at line 251 of file aggregate.cpp.

Friends And Related Function Documentation

T * query< T * > ( QObject *  obj)
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.

See Also
Aggregate::component()

Definition at line 92 of file aggregate.h.

QList< T * > query_all< T * > ( QObject *  obj)
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.

See Also
Aggregate::components()

Definition at line 113 of file aggregate.h.


The documentation for this class was generated from the following files: