/** ****************************************************************************** * * @file aggregate.h * @author The OpenPilot Team, http://www.openpilot.org Copyright (C) 2010. * Parts by Nokia Corporation (qt-info@nokia.com) Copyright (C) 2009. * @brief * @see The GNU Public License (GPL) Version 3 * @defgroup * @{ * *****************************************************************************/ /* * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * for more details. * * You should have received a copy of the GNU General Public License along * with this program; if not, write to the Free Software Foundation, Inc., * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ #ifndef QAGGREGATION_H #define QAGGREGATION_H #include "aggregation_global.h" #include #include #include #include #include namespace Aggregation { class AGGREGATION_EXPORT Aggregate : public QObject { Q_OBJECT public: Aggregate(QObject *parent = 0); virtual ~Aggregate(); void add(QObject *component); void remove(QObject *component); template T *component() { QReadLocker(&lock()); foreach (QObject *component, m_components) { if (T *result = qobject_cast(component)) return result; } return (T *)0; } template QList components() { QReadLocker(&lock()); QList results; foreach (QObject *component, m_components) { if (T *result = qobject_cast(component)) { results << result; } } return results; } static Aggregate *parentAggregate(QObject *obj); static QReadWriteLock &lock(); private slots: void deleteSelf(QObject *obj); private: static QHash &aggregateMap(); QList m_components; }; // get a component via global template function template T *query(Aggregate *obj) { if (!obj) return (T *)0; return obj->template component(); } template T *query(QObject *obj) { if (!obj) return (T *)0; T *result = qobject_cast(obj); if (!result) { QReadLocker(&lock()); Aggregate *parentAggregation = Aggregate::parentAggregate(obj); result = (parentAggregation ? query(parentAggregation) : 0); } return result; } // get all components of a specific type via template function template QList query_all(Aggregate *obj) { if (!obj) return QList(); return obj->template components(); } template QList query_all(QObject *obj) { if (!obj) return QList(); QReadLocker(&lock()); Aggregate *parentAggregation = Aggregate::parentAggregate(obj); QList results; if (parentAggregation) results = query_all(parentAggregation); else if (T *result = qobject_cast(obj)) results.append(result); return results; } } // namespace Aggregation #endif // QAGGREGATION_H