0
|
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 "objectdump_global.h"
|
|
23 |
|
|
24 |
#include <QObject>
|
|
25 |
#include <QStack>
|
|
26 |
|
|
27 |
QT_BEGIN_NAMESPACE
|
|
28 |
|
|
29 |
namespace ObjectTree
|
|
30 |
{
|
|
31 |
|
|
32 |
/**
|
|
33 |
* Depth-first iterator for QObject tree
|
|
34 |
*/
|
|
35 |
class OBJECTDUMP_EXPORT DepthFirstConstIterator
|
|
36 |
{
|
|
37 |
public:
|
|
38 |
DepthFirstConstIterator();
|
|
39 |
DepthFirstConstIterator(const QObject& root);
|
|
40 |
|
|
41 |
DepthFirstConstIterator& operator++();
|
|
42 |
|
|
43 |
inline bool operator==(const DepthFirstConstIterator& other) const
|
|
44 |
{ return other.m_pointee == m_pointee; }
|
|
45 |
|
|
46 |
inline bool operator!=(const DepthFirstConstIterator& other) const
|
|
47 |
{ return other.m_pointee != m_pointee; }
|
|
48 |
|
|
49 |
inline const QObject* operator->() const { return m_pointee; }
|
|
50 |
inline const QObject& operator*() const { return *m_pointee; }
|
|
51 |
|
|
52 |
private:
|
|
53 |
void backtrack();
|
|
54 |
|
|
55 |
private:
|
|
56 |
const QObject* m_pointee;
|
|
57 |
QStack<int> m_history;
|
|
58 |
};
|
|
59 |
|
|
60 |
/**
|
|
61 |
* Ancestor iterator for QObject tree
|
|
62 |
*/
|
|
63 |
class OBJECTDUMP_EXPORT AncestorConstIterator
|
|
64 |
{
|
|
65 |
public:
|
|
66 |
AncestorConstIterator();
|
|
67 |
AncestorConstIterator(const QObject& root);
|
|
68 |
|
|
69 |
inline AncestorConstIterator& operator++()
|
|
70 |
{ m_ancestors.pop(); return *this; }
|
|
71 |
|
|
72 |
inline bool operator==(const AncestorConstIterator& other) const
|
|
73 |
{ return other.m_ancestors == m_ancestors; }
|
|
74 |
|
|
75 |
inline bool operator!=(const AncestorConstIterator& other) const
|
|
76 |
{ return other.m_ancestors != m_ancestors; }
|
|
77 |
|
|
78 |
inline const QObject* operator->() const { return m_ancestors.top(); }
|
|
79 |
inline const QObject& operator*() const { return *m_ancestors.top(); }
|
|
80 |
|
|
81 |
private:
|
|
82 |
QStack<const QObject*> m_ancestors;
|
|
83 |
|
|
84 |
};
|
|
85 |
|
|
86 |
/**
|
|
87 |
* Generic algorithm for visiting nodes in an object tree. Nodes in the
|
|
88 |
* tree are visited in a const context, therefore they are not modified
|
|
89 |
* by this algorithm.
|
|
90 |
*
|
|
91 |
* Visitor must provide functions with the following signatures:
|
|
92 |
*
|
|
93 |
* Called before visit begins
|
|
94 |
* void visitPrepare()
|
|
95 |
*
|
|
96 |
* Called on each node visited
|
|
97 |
* void visitNode(const QObject& object)
|
|
98 |
*
|
|
99 |
* Called when visit is complete
|
|
100 |
* void visitComplete()
|
|
101 |
*/
|
|
102 |
template <class Iterator, class Visitor>
|
|
103 |
void visit(Iterator begin, Iterator end, Visitor& visitor)
|
|
104 |
{
|
|
105 |
visitor.visitPrepare();
|
|
106 |
|
|
107 |
for ( ; begin != end; ++begin)
|
|
108 |
visitor.visitNode(*begin);
|
|
109 |
|
|
110 |
visitor.visitComplete();
|
|
111 |
}
|
|
112 |
|
|
113 |
} // namespace ObjectTree
|
|
114 |
|
|
115 |
QT_END_NAMESPACE
|
|
116 |
|
|
117 |
#endif // OBJECTTREE_H
|