KDStateMachineEditor  1.1.0
A framework for creating Qt State Machine metacode using a graphical user interface
treewalker.h
1 /*
2  treewalker.h
3 
4  This file is part of the KDAB State Machine Editor Library.
5 
6  Copyright (C) 2014-2016 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com.
7  All rights reserved.
8  Author: Kevin Funk <kevin.funk@kdab.com>
9 
10  Licensees holding valid commercial KDAB State Machine Editor Library
11  licenses may use this file in accordance with the KDAB State Machine Editor
12  Library License Agreement provided with the Software.
13 
14  This file may be distributed and/or modified under the terms of the
15  GNU Lesser General Public License version 2.1 as published by the
16  Free Software Foundation and appearing in the file LICENSE.LGPL.txt included.
17 
18  This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
19  WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
20 
21  Contact info@kdab.com if any conditions of this licensing are not
22  clear to you.
23 */
24 
25 #ifndef KDSME_UTIL_TREEWALKER_H
26 #define KDSME_UTIL_TREEWALKER_H
27 
28 #include "kdsme_core_export.h"
29 
30 #include <QObject>
31 
32 #include <functional>
33 
34 namespace KDSME {
35 
36 template<typename T>
38 {
39  static QList<T> children(T item) { Q_UNUSED(item); return QList<T>(); }
40 };
41 
47 template<typename T>
49 {
50 public:
51  enum VisitResult {
55  };
56 
57  enum TraversalType {
58  PreOrderTraversal,
59  PostOrderTraversal,
60  };
61 
62  typedef std::function<VisitResult(T)> VisitFunction;
63 
64  explicit TreeWalker(TraversalType type = PreOrderTraversal)
65  : m_traversalType(type) {}
66 
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  foreach (T child, TreeWalkerTrait<T>::children(item)) {
85  if (!walkItems(child, visit))
86  return false;
87  }
88  if (m_traversalType == PostOrderTraversal) {
89  continueWalk = (visit(item) == TreeWalker::RecursiveWalk);
90  }
91  return continueWalk;
92  }
93 
99  bool walkChildren(T item, const VisitFunction& visit)
100  {
101  if (!item)
102  return false;
103 
104  foreach (T child, TreeWalkerTrait<T>::children(item)) {
105  if (!walkItems(child, visit))
106  return false;
107  }
108  return true;
109  }
110 
111 private:
112  TraversalType m_traversalType;
113 };
114 
115 }
116 
117 #endif // TREEWALKER_H
bool walkItems(T item, const VisitFunction &visit)
Definition: treewalker.h:74
bool walkChildren(T item, const VisitFunction &visit)
Definition: treewalker.h:99
Definition: treewalker.h:37
Terminate the traversal.
Definition: treewalker.h:54
Traverse the children of this item.
Definition: treewalker.h:53
VisitResult
Definition: treewalker.h:51
Definition: treewalker.h:48
Continues traversal with the next sibling of the item just visited, without visiting its children...
Definition: treewalker.h:52
Definition: abstractexporter.h:33

Klarälvdalens Datakonsult AB (KDAB)
Qt-related services and products
http://www.kdab.com/
https://github.com/KDAB/KDStateMachineEditor