OLD | NEW |
1 // Copyright (c) 2011 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_LIST_MODEL_H_ | 5 #ifndef UI_BASE_MODELS_LIST_MODEL_H_ |
6 #define UI_BASE_MODELS_LIST_MODEL_H_ | 6 #define UI_BASE_MODELS_LIST_MODEL_H_ |
7 #pragma once | 7 #pragma once |
8 | 8 |
9 #include "base/basictypes.h" | 9 #include "base/basictypes.h" |
10 #include "base/logging.h" | 10 #include "base/logging.h" |
| 11 #include "base/memory/scoped_vector.h" |
11 #include "base/observer_list.h" | 12 #include "base/observer_list.h" |
12 #include "base/memory/scoped_vector.h" | |
13 #include "ui/base/models/list_model_observer.h" | 13 #include "ui/base/models/list_model_observer.h" |
14 | 14 |
15 namespace ui { | 15 namespace ui { |
16 | 16 |
17 // A list model that manages a list of ItemType pointers. Items added to the | 17 // A list model that manages a list of ItemType pointers. Items added to the |
18 // model are owned by the model. An item can be taken out of the model by | 18 // model are owned by the model. An item can be taken out of the model by |
19 // RemoveAt. | 19 // RemoveAt. |
20 template <class ItemType> | 20 template <class ItemType> |
21 class ListModel { | 21 class ListModel { |
22 public: | 22 public: |
23 typedef std::vector<ItemType*> Items; | |
24 | |
25 ListModel() {} | 23 ListModel() {} |
26 virtual ~ListModel() {} | 24 virtual ~ListModel() {} |
27 | 25 |
28 // Adds |item| to the model at given |index|. | 26 // Adds |item| to the model at given |index|. |
29 virtual void AddAt(int index, ItemType* item) { | 27 virtual void AddAt(int index, ItemType* item) { |
30 DCHECK(index >= 0 && index <= item_count()); | 28 DCHECK(index >= 0 && index <= item_count()); |
31 items_->insert(items_.begin() + index, item); | 29 items_->insert(items_.begin() + index, item); |
32 NotifyItemsAdded(index, 1); | 30 NotifyItemsAdded(index, 1); |
33 } | 31 } |
34 | 32 |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
86 ListItemsRemoved(start, count)); | 84 ListItemsRemoved(start, count)); |
87 } | 85 } |
88 | 86 |
89 void NotifyItemsChanged(int start, int count) { | 87 void NotifyItemsChanged(int start, int count) { |
90 FOR_EACH_OBSERVER(ListModelObserver, | 88 FOR_EACH_OBSERVER(ListModelObserver, |
91 observers_, | 89 observers_, |
92 ListItemsChanged(start, count)); | 90 ListItemsChanged(start, count)); |
93 } | 91 } |
94 | 92 |
95 int item_count() const { return static_cast<int>(items_.size()); } | 93 int item_count() const { return static_cast<int>(items_.size()); } |
96 const Items& items() const { return items_.get(); } | |
97 | 94 |
98 const ItemType* item_at(int index) const { | 95 const ItemType* GetItemAt(int index) const { |
99 DCHECK(index >= 0 && index < item_count()); | 96 DCHECK(index >= 0 && index < item_count()); |
100 return items_[index]; | 97 return items_[index]; |
101 } | 98 } |
102 ItemType* item_at(int index) { | 99 ItemType* GetItemAt(int index) { |
103 return const_cast<ItemType*>( | 100 return const_cast<ItemType*>( |
104 const_cast<const ListModel<ItemType>*>(this)->item_at(index)); | 101 const_cast<const ListModel<ItemType>*>(this)->GetItemAt(index)); |
105 } | 102 } |
106 | 103 |
107 private: | 104 private: |
108 ScopedVector<ItemType> items_; | 105 ScopedVector<ItemType> items_; |
109 ObserverList<ListModelObserver> observers_; | 106 ObserverList<ListModelObserver> observers_; |
110 | 107 |
111 DISALLOW_COPY_AND_ASSIGN(ListModel<ItemType>); | 108 DISALLOW_COPY_AND_ASSIGN(ListModel<ItemType>); |
112 }; | 109 }; |
113 | 110 |
114 } // namespace ui | 111 } // namespace ui |
115 | 112 |
116 #endif // UI_BASE_MODELS_LIST_MODEL_H_ | 113 #endif // UI_BASE_MODELS_LIST_MODEL_H_ |
OLD | NEW |