OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 | 7 |
8 #include <stddef.h> | 8 #include <stddef.h> |
9 | 9 |
10 #include <algorithm> | 10 #include <algorithm> |
11 #include <vector> | 11 #include <vector> |
12 | 12 |
13 #include "base/compiler_specific.h" | |
14 #include "base/logging.h" | 13 #include "base/logging.h" |
15 #include "base/macros.h" | 14 #include "base/macros.h" |
16 #include "base/memory/scoped_ptr.h" | 15 #include "base/memory/scoped_ptr.h" |
17 #include "base/memory/scoped_vector.h" | |
18 #include "base/observer_list.h" | 16 #include "base/observer_list.h" |
17 #include "base/stl_util.h" | |
19 #include "base/strings/string16.h" | 18 #include "base/strings/string16.h" |
20 #include "ui/base/models/tree_model.h" | 19 #include "ui/base/models/tree_model.h" |
21 | 20 |
22 namespace ui { | 21 namespace ui { |
23 | 22 |
24 // TreeNodeModel and TreeNodes provide an implementation of TreeModel around | 23 // TreeNodeModel and TreeNodes provide an implementation of TreeModel around |
25 // TreeNodes. | 24 // TreeNodes. |
26 // | 25 // |
27 // TreeNodes own their children, so that deleting a node deletes all | 26 // TreeNodes own their children, so that deleting a node deletes all |
28 // descendants. | 27 // descendants. |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
63 // TreeNode ------------------------------------------------------------------- | 62 // TreeNode ------------------------------------------------------------------- |
64 | 63 |
65 template <class NodeType> | 64 template <class NodeType> |
66 class TreeNode : public TreeModelNode { | 65 class TreeNode : public TreeModelNode { |
67 public: | 66 public: |
68 TreeNode() : parent_(NULL) {} | 67 TreeNode() : parent_(NULL) {} |
69 | 68 |
70 explicit TreeNode(const base::string16& title) | 69 explicit TreeNode(const base::string16& title) |
71 : title_(title), parent_(NULL) {} | 70 : title_(title), parent_(NULL) {} |
72 | 71 |
73 ~TreeNode() override {} | 72 ~TreeNode() override { |
73 STLDeleteElements(&children_); | |
74 } | |
74 | 75 |
75 // Adds |node| as a child of this node, at |index|. | 76 // Adds |node| as a child of this node, at |index|. |
76 virtual void Add(NodeType* node, int index) { | 77 virtual void Add(NodeType* node, int index) { |
77 DCHECK(node); | 78 DCHECK(node); |
78 DCHECK_GE(index, 0); | 79 DCHECK_GE(index, 0); |
79 DCHECK_LE(index, child_count()); | 80 DCHECK_LE(index, child_count()); |
80 // If |node| has a parent, remove it from its parent. | 81 // If |node| has a parent, remove it from its parent. |
81 NodeType* parent = node->parent_; | 82 NodeType* parent = node->parent_; |
82 if (parent) | 83 if (parent) |
83 parent->Remove(node); | 84 parent->Remove(node); |
84 node->parent_ = static_cast<NodeType*>(this); | 85 node->parent_ = static_cast<NodeType*>(this); |
85 children_.insert(children_.begin() + index, node); | 86 children_.insert(children_.begin() + index, node); |
86 } | 87 } |
87 | 88 |
88 // Removes |node| from this node and returns it. It's up to the caller to | 89 // Removes |node| from this node and returns it. It's up to the caller to |
89 // delete it. | 90 // delete it. |
90 virtual NodeType* Remove(NodeType* node) { | 91 virtual NodeType* Remove(NodeType* node) { |
91 typename std::vector<NodeType*>::iterator i = | 92 typename std::vector<NodeType*>::iterator i = |
92 std::find(children_.begin(), children_.end(), node); | 93 std::find(children_.begin(), children_.end(), node); |
93 DCHECK(i != children_.end()); | 94 DCHECK(i != children_.end()); |
94 node->parent_ = NULL; | 95 node->parent_ = NULL; |
95 children_.weak_erase(i); | 96 children_.erase(i); |
96 return node; | 97 return node; |
97 } | 98 } |
98 | 99 |
99 // Removes all the children from this node. This does NOT delete the nodes. | 100 // Removes all the children from this node. This does NOT delete the nodes. |
100 void RemoveAll() { | 101 void RemoveAll() { |
101 for (size_t i = 0; i < children_.size(); ++i) | 102 for (size_t i = 0; i < children_.size(); ++i) |
102 children_[i]->parent_ = NULL; | 103 children_[i]->parent_ = NULL; |
103 children_.weak_clear(); | 104 children_.clear(); |
104 } | 105 } |
105 | 106 |
106 // Removes all existing children without deleting the nodes and adds all nodes | 107 // Removes all existing children without deleting the nodes and adds all nodes |
107 // contained in |children| into this node as children. | 108 // contained in |children| into this node as children. |
108 void SetChildren(const std::vector<NodeType*>& children) { | 109 void SetChildren(const std::vector<NodeType*>& children) { |
109 RemoveAll(); | 110 RemoveAll(); |
110 for (size_t i = 0; i < children.size(); ++i) | 111 for (size_t i = 0; i < children.size(); ++i) |
111 Add(children[i], static_cast<int>(i)); | 112 Add(children[i], static_cast<int>(i)); |
112 } | 113 } |
113 | 114 |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
162 // ancestor. | 163 // ancestor. |
163 bool HasAncestor(const NodeType* ancestor) const { | 164 bool HasAncestor(const NodeType* ancestor) const { |
164 if (ancestor == this) | 165 if (ancestor == this) |
165 return true; | 166 return true; |
166 if (!ancestor) | 167 if (!ancestor) |
167 return false; | 168 return false; |
168 return parent_ ? parent_->HasAncestor(ancestor) : false; | 169 return parent_ ? parent_->HasAncestor(ancestor) : false; |
169 } | 170 } |
170 | 171 |
171 protected: | 172 protected: |
172 std::vector<NodeType*>& children() { return children_.get(); } | 173 std::vector<NodeType*>& children() { return children_; } |
tfarina
2016/01/21 14:24:45
I had to use std::vector<NodeType*> because of thi
sky
2016/01/21 18:53:54
I disagree. It's an implementation detail as to ho
vabr (Chromium)
2016/01/22 13:57:51
Does that mean that the accessor is meant to abstr
sky
2016/01/22 16:42:09
Good point. I missed that it isn't const. Callers
vabr (Chromium)
2016/01/22 16:44:49
Right, in which case it seems like exposing the sc
| |
173 | 174 |
174 private: | 175 private: |
175 // Title displayed in the tree. | 176 // Title displayed in the tree. |
176 base::string16 title_; | 177 base::string16 title_; |
177 | 178 |
178 // This node's parent. | 179 // This node's parent. |
179 NodeType* parent_; | 180 NodeType* parent_; |
180 | 181 |
181 // This node's children. | 182 // This node's children. |
182 ScopedVector<NodeType> children_; | 183 std::vector<NodeType*> children_; |
183 | 184 |
184 DISALLOW_COPY_AND_ASSIGN(TreeNode); | 185 DISALLOW_COPY_AND_ASSIGN(TreeNode); |
185 }; | 186 }; |
186 | 187 |
187 // TreeNodeWithValue ---------------------------------------------------------- | 188 // TreeNodeWithValue ---------------------------------------------------------- |
188 | 189 |
189 template <class ValueType> | 190 template <class ValueType> |
190 class TreeNodeWithValue : public TreeNode< TreeNodeWithValue<ValueType> > { | 191 class TreeNodeWithValue : public TreeNode< TreeNodeWithValue<ValueType> > { |
191 public: | 192 public: |
192 TreeNodeWithValue() {} | 193 TreeNodeWithValue() {} |
(...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
298 | 299 |
299 // The root. | 300 // The root. |
300 scoped_ptr<NodeType> root_; | 301 scoped_ptr<NodeType> root_; |
301 | 302 |
302 DISALLOW_COPY_AND_ASSIGN(TreeNodeModel); | 303 DISALLOW_COPY_AND_ASSIGN(TreeNodeModel); |
303 }; | 304 }; |
304 | 305 |
305 } // namespace ui | 306 } // namespace ui |
306 | 307 |
307 #endif // UI_BASE_MODELS_TREE_NODE_MODEL_H_ | 308 #endif // UI_BASE_MODELS_TREE_NODE_MODEL_H_ |
OLD | NEW |