source: trunk/src/3rdparty/phonon/mmf/objecttree.h

Last change on this file was 561, checked in by Dmitry A. Kuminov, 16 years ago

trunk: Merged in qt 4.6.1 sources.

  • Property svn:eol-style set to native
File size: 2.9 KB
Line 
1/* This file is part of the KDE project.
2
3Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
4
5This library is free software: you can redistribute it and/or modify
6it under the terms of the GNU Lesser General Public License as published by
7the Free Software Foundation, either version 2.1 or 3 of the License.
8
9This library is distributed in the hope that it will be useful,
10but WITHOUT ANY WARRANTY; without even the implied warranty of
11MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12GNU Lesser General Public License for more details.
13
14You should have received a copy of the GNU Lesser General Public License
15along 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
25QT_BEGIN_NAMESPACE
26
27namespace ObjectTree
28{
29
30/**
31 * Depth-first iterator for QObject tree
32 */
33class DepthFirstConstIterator
34{
35public:
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
50private:
51 void backtrack();
52
53private:
54 const QObject* m_pointee;
55 QStack<int> m_history;
56};
57
58/**
59 * Ancestor iterator for QObject tree
60 */
61class AncestorConstIterator
62{
63public:
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
79private:
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 */
100template <class Iterator, class Visitor>
101void 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
113QT_END_NAMESPACE
114
115#endif // OBJECTTREE_H
Note: See TracBrowser for help on using the repository browser.