KDStateMachineEditor API Documentation 2.1
Loading...
Searching...
No Matches
treewalker.h
Go to the documentation of this file.
1/*
2 This file is part of the KDAB State Machine Editor Library.
3
4 SPDX-FileCopyrightText: 2014 Klarälvdalens Datakonsult AB, a KDAB Group company <info@kdab.com>
5 Author: Kevin Funk <kevin.funk@kdab.com>
6
7 SPDX-License-Identifier: LGPL-2.1-only OR LicenseRef-KDAB-KDStateMachineEditor
8
9 Licensees holding valid commercial KDAB State Machine Editor Library
10 licenses may use this file in accordance with the KDAB State Machine Editor
11 Library License Agreement provided with the Software.
12
13 Contact info@kdab.com if any conditions of this licensing are not clear to you.
14*/
15
16#ifndef KDSME_UTIL_TREEWALKER_H
17#define KDSME_UTIL_TREEWALKER_H
18
19#include "kdsme_core_export.h"
20
21#include <QObject>
22
23#include <functional>
24
25namespace KDSME {
26
27template<typename T>
29{
30 static QList<T> children(T item)
31 {
32 Q_UNUSED(item);
33 return QList<T>();
34 }
35};
36
42template<typename T>
44{
45public:
52
58
59 typedef std::function<VisitResult(T)> VisitFunction;
60
62 : m_traversalType(type)
63 {
64 }
65
74 bool walkItems(T item, const VisitFunction &visit)
75 {
76 if (!item)
77 return false;
78
79 bool continueWalk = true;
80 if (m_traversalType == PreOrderTraversal) {
81 continueWalk = (visit(item) == TreeWalker::RecursiveWalk);
82 }
83 Q_ASSERT(item);
84 const auto items = TreeWalkerTrait<T>::children(item);
85 if (std::any_of(items.begin(), items.end(),
86 [&](const T &child) { return !walkItems(child, visit); })) {
87 return false;
88 }
89
90 if (m_traversalType == PostOrderTraversal) {
91 continueWalk = (visit(item) == TreeWalker::RecursiveWalk);
92 }
93 return continueWalk;
94 }
95
101 bool walkChildren(T item, const VisitFunction &visit)
102 {
103 if (!item)
104 return false;
105
106 const auto items = TreeWalkerTrait<T>::children(item);
107 return !std::any_of(items.begin(), items.end(),
108 [&](const T &child) { return !walkItems(child, visit); });
109 }
110
111private:
112 TraversalType m_traversalType;
113};
114
115}
116
117#endif // TREEWALKER_H
bool walkChildren(T item, const VisitFunction &visit)
Definition treewalker.h:101
std::function< VisitResult(T)> VisitFunction
Definition treewalker.h:59
bool walkItems(T item, const VisitFunction &visit)
Definition treewalker.h:74
TreeWalker(TraversalType type=PreOrderTraversal)
Definition treewalker.h:61
@ ContinueWalk
Continues traversal with the next sibling of the item just visited, without visiting its children.
Definition treewalker.h:48
@ RecursiveWalk
Traverse the children of this item.
Definition treewalker.h:49
@ StopWalk
Terminate the traversal.
Definition treewalker.h:50
static QList< T > children(T item)
Definition treewalker.h:30

© Klarälvdalens Datakonsult AB (KDAB)
"The Qt, C++ and OpenGL Experts"
https://www.kdab.com/
KDStateMachineEditor
Create Qt State Machine metacode using a graphical user interface
https://github.com/KDAB/KDStateMachineEditor
Generated on Tue Jul 15 2025 15:21:47 for KDStateMachineEditor API Documentation by doxygen 1.9.8