summaryrefslogtreecommitdiffstats
path: root/app/tree_node_iterator.h
diff options
context:
space:
mode:
authorphajdan.jr@chromium.org <phajdan.jr@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2009-05-13 18:54:20 +0000
committerphajdan.jr@chromium.org <phajdan.jr@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2009-05-13 18:54:20 +0000
commit992c625bf2d6dfaaa46cf5b45cdcd7de68e7817e (patch)
treeae6467a99cf17115e04af5bbe358a6219f9c9808 /app/tree_node_iterator.h
parentb83fed38db16e462afa3051dcad21318c62b2357 (diff)
downloadchromium_src-992c625bf2d6dfaaa46cf5b45cdcd7de68e7817e.zip
chromium_src-992c625bf2d6dfaaa46cf5b45cdcd7de68e7817e.tar.gz
chromium_src-992c625bf2d6dfaaa46cf5b45cdcd7de68e7817e.tar.bz2
Move tree-related classes that Linux code depends on from views/ to app/
TEST=If it compiles and unit_tests pass, it's ok. Just moving files around. http://crbug.com/11066 Review URL: http://codereview.chromium.org/115185 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@15982 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'app/tree_node_iterator.h')
-rw-r--r--app/tree_node_iterator.h70
1 files changed, 70 insertions, 0 deletions
diff --git a/app/tree_node_iterator.h b/app/tree_node_iterator.h
new file mode 100644
index 0000000..7e7398c
--- /dev/null
+++ b/app/tree_node_iterator.h
@@ -0,0 +1,70 @@
+// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#ifndef APP_TREE_NODE_ITERATOR_H_
+#define APP_TREE_NODE_ITERATOR_H_
+
+#include <stack>
+
+#include "base/basictypes.h"
+#include "base/logging.h"
+
+// Iterator that iterates over the descendants of a node. The iteration does
+// not include the node itself, only the descendants. The following illustrates
+// typical usage:
+// while (iterator.has_next()) {
+// Node* node = iterator.Next();
+// // do something with node.
+// }
+template <class NodeType>
+class TreeNodeIterator {
+ public:
+ explicit TreeNodeIterator(NodeType* node) {
+ if (node->GetChildCount() > 0)
+ positions_.push(Position<NodeType>(node, 0));
+ }
+
+ // Returns true if there are more descendants.
+ bool has_next() const { return !positions_.empty(); }
+
+ // Returns the next descendant.
+ NodeType* Next() {
+ if (!has_next()) {
+ NOTREACHED();
+ return NULL;
+ }
+
+ NodeType* result = positions_.top().node->GetChild(positions_.top().index);
+
+ // Make sure we don't attempt to visit result again.
+ positions_.top().index++;
+
+ // Iterate over result's children.
+ positions_.push(Position<NodeType>(result, 0));
+
+ // Advance to next position.
+ while (!positions_.empty() && positions_.top().index >=
+ positions_.top().node->GetChildCount()) {
+ positions_.pop();
+ }
+
+ return result;
+ }
+
+ private:
+ template <class PositionNodeType>
+ struct Position {
+ Position(PositionNodeType* node, int index) : node(node), index(index) {}
+ Position() : node(NULL), index(-1) {}
+
+ PositionNodeType* node;
+ int index;
+ };
+
+ std::stack<Position<NodeType> > positions_;
+
+ DISALLOW_COPY_AND_ASSIGN(TreeNodeIterator);
+};
+
+#endif // APP_TREE_NODE_ITERATOR_H_