| 1 | /*  This file is part of the KDE project.
 | 
|---|
| 2 | 
 | 
|---|
| 3 | Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
 | 
|---|
| 4 | 
 | 
|---|
| 5 | This library is free software: you can redistribute it and/or modify
 | 
|---|
| 6 | it under the terms of the GNU Lesser General Public License as published by
 | 
|---|
| 7 | the Free Software Foundation, either version 2.1 or 3 of the License.
 | 
|---|
| 8 | 
 | 
|---|
| 9 | This library is distributed in the hope that it will be useful,
 | 
|---|
| 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of
 | 
|---|
| 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 | 
|---|
| 12 | GNU Lesser General Public License for more details.
 | 
|---|
| 13 | 
 | 
|---|
| 14 | You should have received a copy of the GNU Lesser General Public License
 | 
|---|
| 15 | along with this library.  If not, see <http://www.gnu.org/licenses/>.
 | 
|---|
| 16 | 
 | 
|---|
| 17 | */
 | 
|---|
| 18 | 
 | 
|---|
| 19 | #ifndef OBJECTTREE_H
 | 
|---|
| 20 | #define OBJECTTREE_H
 | 
|---|
| 21 | 
 | 
|---|
| 22 | #include <QObject>
 | 
|---|
| 23 | #include <QStack>
 | 
|---|
| 24 | 
 | 
|---|
| 25 | QT_BEGIN_NAMESPACE
 | 
|---|
| 26 | 
 | 
|---|
| 27 | namespace ObjectTree
 | 
|---|
| 28 | {
 | 
|---|
| 29 | 
 | 
|---|
| 30 | /**
 | 
|---|
| 31 |  * Depth-first iterator for QObject tree
 | 
|---|
| 32 |  */
 | 
|---|
| 33 | class DepthFirstConstIterator
 | 
|---|
| 34 | {
 | 
|---|
| 35 | public:
 | 
|---|
| 36 |     DepthFirstConstIterator();
 | 
|---|
| 37 |     DepthFirstConstIterator(const QObject& root);
 | 
|---|
| 38 | 
 | 
|---|
| 39 |     DepthFirstConstIterator& operator++();
 | 
|---|
| 40 | 
 | 
|---|
| 41 |     inline bool operator==(const DepthFirstConstIterator& other) const
 | 
|---|
| 42 |     { return other.m_pointee == m_pointee; }
 | 
|---|
| 43 | 
 | 
|---|
| 44 |     inline bool operator!=(const DepthFirstConstIterator& other) const
 | 
|---|
| 45 |     { return other.m_pointee != m_pointee; }
 | 
|---|
| 46 | 
 | 
|---|
| 47 |     inline const QObject* operator->() const { return m_pointee; }
 | 
|---|
| 48 |     inline const QObject& operator*() const { return *m_pointee; }
 | 
|---|
| 49 | 
 | 
|---|
| 50 | private:
 | 
|---|
| 51 |     void backtrack();
 | 
|---|
| 52 | 
 | 
|---|
| 53 | private:
 | 
|---|
| 54 |     const QObject* m_pointee;
 | 
|---|
| 55 |     QStack<int> m_history;
 | 
|---|
| 56 | };
 | 
|---|
| 57 | 
 | 
|---|
| 58 | /**
 | 
|---|
| 59 |  * Ancestor iterator for QObject tree
 | 
|---|
| 60 |  */
 | 
|---|
| 61 | class AncestorConstIterator
 | 
|---|
| 62 | {
 | 
|---|
| 63 | public:
 | 
|---|
| 64 |     AncestorConstIterator();
 | 
|---|
| 65 |     AncestorConstIterator(const QObject& root);
 | 
|---|
| 66 | 
 | 
|---|
| 67 |     inline AncestorConstIterator& operator++()
 | 
|---|
| 68 |     { m_ancestors.pop(); return *this; }
 | 
|---|
| 69 | 
 | 
|---|
| 70 |     inline bool operator==(const AncestorConstIterator& other) const
 | 
|---|
| 71 |     { return other.m_ancestors == m_ancestors; }
 | 
|---|
| 72 | 
 | 
|---|
| 73 |     inline bool operator!=(const AncestorConstIterator& other) const
 | 
|---|
| 74 |     { return other.m_ancestors != m_ancestors; }
 | 
|---|
| 75 | 
 | 
|---|
| 76 |     inline const QObject* operator->() const { return m_ancestors.top(); }
 | 
|---|
| 77 |     inline const QObject& operator*() const { return *m_ancestors.top(); }
 | 
|---|
| 78 | 
 | 
|---|
| 79 | private:
 | 
|---|
| 80 |     QStack<const QObject*> m_ancestors;
 | 
|---|
| 81 | 
 | 
|---|
| 82 | };
 | 
|---|
| 83 | 
 | 
|---|
| 84 | /**
 | 
|---|
| 85 |  * Generic algorithm for visiting nodes in an object tree.  Nodes in the
 | 
|---|
| 86 |  * tree are visited in a const context, therefore they are not modified
 | 
|---|
| 87 |  * by this algorithm.
 | 
|---|
| 88 |  *
 | 
|---|
| 89 |  * Visitor must provide functions with the following signatures:
 | 
|---|
| 90 |  *
 | 
|---|
| 91 |  *      Called before visit begins
 | 
|---|
| 92 |  *          void visitPrepare()
 | 
|---|
| 93 |  *
 | 
|---|
| 94 |  *      Called on each node visited
 | 
|---|
| 95 |  *          void visitNode(const QObject& object)
 | 
|---|
| 96 |  *
 | 
|---|
| 97 |  *      Called when visit is complete
 | 
|---|
| 98 |  *          void visitComplete()
 | 
|---|
| 99 |  */
 | 
|---|
| 100 | template <class Iterator, class Visitor>
 | 
|---|
| 101 | void visit(Iterator begin, Iterator end, Visitor& visitor)
 | 
|---|
| 102 | {
 | 
|---|
| 103 |     visitor.visitPrepare();
 | 
|---|
| 104 | 
 | 
|---|
| 105 |     for ( ; begin != end; ++begin)
 | 
|---|
| 106 |         visitor.visitNode(*begin);
 | 
|---|
| 107 | 
 | 
|---|
| 108 |     visitor.visitComplete();
 | 
|---|
| 109 | }
 | 
|---|
| 110 | 
 | 
|---|
| 111 | } // namespace ObjectTree
 | 
|---|
| 112 | 
 | 
|---|
| 113 | QT_END_NAMESPACE
 | 
|---|
| 114 | 
 | 
|---|
| 115 | #endif // OBJECTTREE_H
 | 
|---|