| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef UI_BASE_MODELS_TREE_NODE_ITERATOR_H_ | 5 #ifndef UI_BASE_MODELS_TREE_NODE_ITERATOR_H_ |
| 6 #define UI_BASE_MODELS_TREE_NODE_ITERATOR_H_ | 6 #define UI_BASE_MODELS_TREE_NODE_ITERATOR_H_ |
| 7 #pragma once | 7 #pragma once |
| 8 | 8 |
| 9 #include <stack> | 9 #include <stack> |
| 10 | 10 |
| 11 #include "base/basictypes.h" | 11 #include "base/basictypes.h" |
| 12 #include "base/logging.h" | 12 #include "base/logging.h" |
| 13 | 13 |
| 14 namespace ui { | 14 namespace ui { |
| 15 | 15 |
| 16 // Iterator that iterates over the descendants of a node. The iteration does | 16 // Iterator that iterates over the descendants of a node. The iteration does |
| 17 // not include the node itself, only the descendants. The following illustrates | 17 // not include the node itself, only the descendants. The following illustrates |
| 18 // typical usage: | 18 // typical usage: |
| 19 // while (iterator.has_next()) { | 19 // while (iterator.has_next()) { |
| 20 // Node* node = iterator.Next(); | 20 // Node* node = iterator.Next(); |
| 21 // // do something with node. | 21 // // do something with node. |
| 22 // } | 22 // } |
| 23 template <class NodeType> | 23 template <class NodeType> |
| 24 class TreeNodeIterator { | 24 class TreeNodeIterator { |
| 25 public: | 25 public: |
| 26 explicit TreeNodeIterator(NodeType* node) { | 26 explicit TreeNodeIterator(NodeType* node) { |
| 27 if (node->child_count() > 0) | 27 if (!node->empty()) |
| 28 positions_.push(Position<NodeType>(node, 0)); | 28 positions_.push(Position<NodeType>(node, 0)); |
| 29 } | 29 } |
| 30 | 30 |
| 31 // Returns true if there are more descendants. | 31 // Returns true if there are more descendants. |
| 32 bool has_next() const { return !positions_.empty(); } | 32 bool has_next() const { return !positions_.empty(); } |
| 33 | 33 |
| 34 // Returns the next descendant. | 34 // Returns the next descendant. |
| 35 NodeType* Next() { | 35 NodeType* Next() { |
| 36 if (!has_next()) { | 36 if (!has_next()) { |
| 37 NOTREACHED(); | 37 NOTREACHED(); |
| (...skipping 28 matching lines...) Expand all Loading... |
| 66 }; | 66 }; |
| 67 | 67 |
| 68 std::stack<Position<NodeType> > positions_; | 68 std::stack<Position<NodeType> > positions_; |
| 69 | 69 |
| 70 DISALLOW_COPY_AND_ASSIGN(TreeNodeIterator); | 70 DISALLOW_COPY_AND_ASSIGN(TreeNodeIterator); |
| 71 }; | 71 }; |
| 72 | 72 |
| 73 } // namespace ui | 73 } // namespace ui |
| 74 | 74 |
| 75 #endif // UI_BASE_MODELS_TREE_NODE_ITERATOR_H_ | 75 #endif // UI_BASE_MODELS_TREE_NODE_ITERATOR_H_ |
| OLD | NEW |