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_MODEL_H_ | 5 #ifndef UI_BASE_MODELS_TREE_NODE_MODEL_H_ |
6 #define UI_BASE_MODELS_TREE_NODE_MODEL_H_ | 6 #define UI_BASE_MODELS_TREE_NODE_MODEL_H_ |
7 #pragma once | 7 #pragma once |
8 | 8 |
9 #include <algorithm> | 9 #include <algorithm> |
10 #include <vector> | 10 #include <vector> |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
67 TreeNode() : parent_(NULL) {} | 67 TreeNode() : parent_(NULL) {} |
68 | 68 |
69 explicit TreeNode(const string16& title) | 69 explicit TreeNode(const string16& title) |
70 : title_(title), parent_(NULL) {} | 70 : title_(title), parent_(NULL) {} |
71 | 71 |
72 virtual ~TreeNode() {} | 72 virtual ~TreeNode() {} |
73 | 73 |
74 // Adds |node| as a child of this one, at |index|. | 74 // Adds |node| as a child of this one, at |index|. |
75 virtual void Add(NodeType* node, int index) { | 75 virtual void Add(NodeType* node, int index) { |
76 DCHECK(node); | 76 DCHECK(node); |
77 DCHECK_LE(0, index); | 77 DCHECK_GE(index, 0); |
78 DCHECK_GE(child_count(), index); | 78 DCHECK_LE(index, child_count()); |
79 // If the node has a parent, remove it from its parent. | 79 // If the node has a parent, remove it from its parent. |
80 NodeType* parent = node->parent_; | 80 NodeType* parent = node->parent_; |
81 if (parent) | 81 if (parent) |
82 parent->Remove(node); | 82 parent->Remove(node); |
83 node->parent_ = static_cast<NodeType*>(this); | 83 node->parent_ = static_cast<NodeType*>(this); |
84 children_->insert(children_->begin() + index, node); | 84 children_->insert(children_->begin() + index, node); |
85 } | 85 } |
86 | 86 |
87 // Removes |node| from this node and returns it. It's up to the caller to | 87 // Removes |node| from this node and returns it. It's up to the caller to |
88 // delete it. | 88 // delete it. |
(...skipping 20 matching lines...) Expand all Loading... |
109 // including this node. | 109 // including this node. |
110 int GetTotalNodeCount() const { | 110 int GetTotalNodeCount() const { |
111 int count = 1; // Start with one to include the node itself. | 111 int count = 1; // Start with one to include the node itself. |
112 for (size_t i = 0; i < children_->size(); ++i) | 112 for (size_t i = 0; i < children_->size(); ++i) |
113 count += children_[i]->GetTotalNodeCount(); | 113 count += children_[i]->GetTotalNodeCount(); |
114 return count; | 114 return count; |
115 } | 115 } |
116 | 116 |
117 // Returns the node at |index|. | 117 // Returns the node at |index|. |
118 const NodeType* GetChild(int index) const { | 118 const NodeType* GetChild(int index) const { |
119 DCHECK_LE(0, index); | 119 DCHECK_GE(index, 0); |
120 DCHECK_GT(child_count(), index); | 120 DCHECK_LT(index, child_count()); |
121 return children_[index]; | 121 return children_[index]; |
122 } | 122 } |
123 NodeType* GetChild(int index) { | 123 NodeType* GetChild(int index) { |
124 return const_cast<NodeType*>( | 124 return const_cast<NodeType*>( |
125 static_cast<const NodeType&>(*this).GetChild(index)); | 125 static_cast<const NodeType&>(*this).GetChild(index)); |
126 } | 126 } |
127 | 127 |
128 // Returns the parent node, or NULL if this is the root node. | 128 // Returns the parent node, or NULL if this is the root node. |
129 const NodeType* parent() const { return parent_; } | 129 const NodeType* parent() const { return parent_; } |
130 NodeType* parent() { return parent_; } | 130 NodeType* parent() { return parent_; } |
(...skipping 156 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
287 | 287 |
288 // The root. | 288 // The root. |
289 scoped_ptr<NodeType> root_; | 289 scoped_ptr<NodeType> root_; |
290 | 290 |
291 DISALLOW_COPY_AND_ASSIGN(TreeNodeModel); | 291 DISALLOW_COPY_AND_ASSIGN(TreeNodeModel); |
292 }; | 292 }; |
293 | 293 |
294 } // namespace ui | 294 } // namespace ui |
295 | 295 |
296 #endif // UI_BASE_MODELS_TREE_NODE_MODEL_H_ | 296 #endif // UI_BASE_MODELS_TREE_NODE_MODEL_H_ |
OLD | NEW |