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 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
74 // Adds |node| as a child of this node, at |index|. | 74 // Adds |node| as a child of this node, 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_GE(index, 0); | 77 DCHECK_GE(index, 0); |
78 DCHECK_LE(index, child_count()); | 78 DCHECK_LE(index, child_count()); |
79 // If |node| has a parent, remove it from its parent. | 79 // If |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. |
89 virtual NodeType* Remove(NodeType* node) { | 89 virtual NodeType* Remove(NodeType* node) { |
90 typename std::vector<NodeType*>::iterator i = | 90 typename std::vector<NodeType*>::iterator i = |
91 std::find(children_->begin(), children_->end(), node); | 91 std::find(children_.begin(), children_.end(), node); |
92 DCHECK(i != children_->end()); | 92 DCHECK(i != children_.end()); |
93 node->parent_ = NULL; | 93 node->parent_ = NULL; |
94 children_->erase(i); | 94 children_.weak_erase(i); |
95 return node; | 95 return node; |
96 } | 96 } |
97 | 97 |
98 // Removes all the children from this node. This does NOT delete the nodes. | 98 // Removes all the children from this node. This does NOT delete the nodes. |
99 void RemoveAll() { | 99 void RemoveAll() { |
100 for (size_t i = 0; i < children_->size(); ++i) | 100 for (size_t i = 0; i < children_.size(); ++i) |
101 children_[i]->parent_ = NULL; | 101 children_[i]->parent_ = NULL; |
102 children_->clear(); | 102 children_.clear(); |
103 } | 103 } |
104 | 104 |
105 // Returns the parent node, or NULL if this is the root node. | 105 // Returns the parent node, or NULL if this is the root node. |
106 const NodeType* parent() const { return parent_; } | 106 const NodeType* parent() const { return parent_; } |
107 NodeType* parent() { return parent_; } | 107 NodeType* parent() { return parent_; } |
108 | 108 |
109 // Returns true if this is the root node. | 109 // Returns true if this is the root node. |
110 bool is_root() const { return parent_ == NULL; } | 110 bool is_root() const { return parent_ == NULL; } |
111 | 111 |
112 // Returns the number of children. | 112 // Returns the number of children. |
113 int child_count() const { return static_cast<int>(children_->size()); } | 113 int child_count() const { return static_cast<int>(children_.size()); } |
114 | 114 |
115 // Returns true if this node has no children. | 115 // Returns true if this node has no children. |
116 bool empty() const { return children_->empty(); } | 116 bool empty() const { return children_.empty(); } |
117 | 117 |
118 // Returns the number of all nodes in the subtree rooted at this node, | 118 // Returns the number of all nodes in the subtree rooted at this node, |
119 // including this node. | 119 // including this node. |
120 int GetTotalNodeCount() const { | 120 int GetTotalNodeCount() const { |
121 int count = 1; // Start with one to include the node itself. | 121 int count = 1; // Start with one to include the node itself. |
122 for (size_t i = 0; i < children_->size(); ++i) | 122 for (size_t i = 0; i < children_.size(); ++i) |
123 count += children_[i]->GetTotalNodeCount(); | 123 count += children_[i]->GetTotalNodeCount(); |
124 return count; | 124 return count; |
125 } | 125 } |
126 | 126 |
127 // Returns the node at |index|. | 127 // Returns the node at |index|. |
128 const NodeType* GetChild(int index) const { | 128 const NodeType* GetChild(int index) const { |
129 DCHECK_GE(index, 0); | 129 DCHECK_GE(index, 0); |
130 DCHECK_LT(index, child_count()); | 130 DCHECK_LT(index, child_count()); |
131 return children_[index]; | 131 return children_[index]; |
132 } | 132 } |
133 NodeType* GetChild(int index) { | 133 NodeType* GetChild(int index) { |
134 return const_cast<NodeType*>( | 134 return const_cast<NodeType*>( |
135 static_cast<const NodeType&>(*this).GetChild(index)); | 135 static_cast<const NodeType&>(*this).GetChild(index)); |
136 } | 136 } |
137 | 137 |
138 // Returns the index of |node|, or -1 if |node| is not a child of this. | 138 // Returns the index of |node|, or -1 if |node| is not a child of this. |
139 int GetIndexOf(const NodeType* node) const { | 139 int GetIndexOf(const NodeType* node) const { |
140 DCHECK(node); | 140 DCHECK(node); |
141 typename std::vector<NodeType*>::const_iterator i = | 141 typename std::vector<NodeType*>::const_iterator i = |
142 std::find(children_->begin(), children_->end(), node); | 142 std::find(children_.begin(), children_.end(), node); |
143 return | 143 return i != children_.end() ? static_cast<int>(i - children_.begin()) : -1; |
144 i != children_->end() ? static_cast<int>(i - children_->begin()) : -1; | |
145 } | 144 } |
146 | 145 |
147 // Sets the title of the node. | 146 // Sets the title of the node. |
148 virtual void SetTitle(const string16& title) { title_ = title; } | 147 virtual void SetTitle(const string16& title) { title_ = title; } |
149 | 148 |
150 // TreeModelNode: | 149 // TreeModelNode: |
151 virtual const string16& GetTitle() const OVERRIDE { return title_; } | 150 virtual const string16& GetTitle() const OVERRIDE { return title_; } |
152 | 151 |
153 // Returns true if this == ancestor, or one of this nodes parents is | 152 // Returns true if this == ancestor, or one of this nodes parents is |
154 // ancestor. | 153 // ancestor. |
(...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
289 | 288 |
290 // The root. | 289 // The root. |
291 scoped_ptr<NodeType> root_; | 290 scoped_ptr<NodeType> root_; |
292 | 291 |
293 DISALLOW_COPY_AND_ASSIGN(TreeNodeModel); | 292 DISALLOW_COPY_AND_ASSIGN(TreeNodeModel); |
294 }; | 293 }; |
295 | 294 |
296 } // namespace ui | 295 } // namespace ui |
297 | 296 |
298 #endif // UI_BASE_MODELS_TREE_NODE_MODEL_H_ | 297 #endif // UI_BASE_MODELS_TREE_NODE_MODEL_H_ |
OLD | NEW |