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_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/memory/scoped_vector.h" |
12 #include "base/observer_list.h" | 12 #include "base/observer_list.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 ListModel() {} | 23 ListModel() {} |
24 virtual ~ListModel() {} | 24 virtual ~ListModel() {} |
25 | 25 |
26 // Adds |item| to the model at given |index|. | 26 // Adds |item| to the model at given |index|. |
27 virtual void AddAt(int index, ItemType* item) { | 27 virtual void AddAt(size_t index, ItemType* item) { |
28 DCHECK(index >= 0 && index <= item_count()); | 28 DCHECK_LE(index, item_count()); |
29 items_->insert(items_.begin() + index, item); | 29 items_->insert(items_.begin() + index, item); |
30 NotifyItemsAdded(index, 1); | 30 NotifyItemsAdded(index, 1); |
31 } | 31 } |
32 | 32 |
33 // Removes an item at given |index| from the model. Note the removed item | 33 // Removes an item at given |index| from the model. Note the removed item |
34 // is NOT deleted and it's up to the caller to delete it. | 34 // is NOT deleted and it's up to the caller to delete it. |
35 virtual ItemType* RemoveAt(int index) { | 35 virtual ItemType* RemoveAt(size_t index) { |
36 DCHECK(index >= 0 && index < item_count()); | 36 DCHECK_LT(index, item_count()); |
37 ItemType* item = items_[index]; | 37 ItemType* item = items_[index]; |
38 items_->erase(items_.begin() + index); | 38 items_->erase(items_.begin() + index); |
39 NotifyItemsRemoved(index, 1); | 39 NotifyItemsRemoved(index, 1); |
40 return item; | 40 return item; |
41 } | 41 } |
42 | 42 |
43 // Removes all items from the model. This does NOT delete the items. | 43 // Removes all items from the model. This does NOT delete the items. |
44 virtual void RemoveAll() { | 44 virtual void RemoveAll() { |
45 int count = item_count(); | 45 size_t count = item_count(); |
46 items_->clear(); | 46 items_->clear(); |
47 NotifyItemsRemoved(0, count); | 47 NotifyItemsRemoved(0, count); |
48 } | 48 } |
49 | 49 |
50 // Removes an item at given |index| from the model and deletes it. | 50 // Removes an item at given |index| from the model and deletes it. |
51 virtual void DeleteAt(int index) { | 51 virtual void DeleteAt(size_t index) { |
52 delete RemoveAt(index); | 52 delete RemoveAt(index); |
53 } | 53 } |
54 | 54 |
55 // Removes and deletes all items from the model. | 55 // Removes and deletes all items from the model. |
56 virtual void DeleteAll() { | 56 virtual void DeleteAll() { |
57 int count = item_count(); | 57 size_t count = item_count(); |
58 items_.reset(); | 58 items_.reset(); |
59 NotifyItemsRemoved(0, count); | 59 NotifyItemsRemoved(0, count); |
60 } | 60 } |
61 | 61 |
62 // Convenience function to append an item to the model. | 62 // Convenience function to append an item to the model. |
63 void Add(ItemType* item) { | 63 void Add(ItemType* item) { |
64 AddAt(item_count(), item); | 64 AddAt(item_count(), item); |
65 } | 65 } |
66 | 66 |
67 void AddObserver(ListModelObserver* observer) { | 67 void AddObserver(ListModelObserver* observer) { |
68 observers_.AddObserver(observer); | 68 observers_.AddObserver(observer); |
69 } | 69 } |
70 | 70 |
71 void RemoveObserver(ListModelObserver* observer) { | 71 void RemoveObserver(ListModelObserver* observer) { |
72 observers_.RemoveObserver(observer); | 72 observers_.RemoveObserver(observer); |
73 } | 73 } |
74 | 74 |
75 void NotifyItemsAdded(int start, int count) { | 75 void NotifyItemsAdded(size_t start, size_t count) { |
76 FOR_EACH_OBSERVER(ListModelObserver, | 76 FOR_EACH_OBSERVER(ListModelObserver, |
77 observers_, | 77 observers_, |
78 ListItemsAdded(start, count)); | 78 ListItemsAdded(start, count)); |
79 } | 79 } |
80 | 80 |
81 void NotifyItemsRemoved(int start, int count) { | 81 void NotifyItemsRemoved(size_t start, size_t count) { |
82 FOR_EACH_OBSERVER(ListModelObserver, | 82 FOR_EACH_OBSERVER(ListModelObserver, |
83 observers_, | 83 observers_, |
84 ListItemsRemoved(start, count)); | 84 ListItemsRemoved(start, count)); |
85 } | 85 } |
86 | 86 |
87 void NotifyItemsChanged(int start, int count) { | 87 void NotifyItemsChanged(size_t start, size_t count) { |
88 FOR_EACH_OBSERVER(ListModelObserver, | 88 FOR_EACH_OBSERVER(ListModelObserver, |
89 observers_, | 89 observers_, |
90 ListItemsChanged(start, count)); | 90 ListItemsChanged(start, count)); |
91 } | 91 } |
92 | 92 |
93 int item_count() const { return static_cast<int>(items_.size()); } | 93 size_t item_count() const { return items_.size(); } |
94 | 94 |
95 const ItemType* GetItemAt(int index) const { | 95 const ItemType* GetItemAt(size_t index) const { |
96 DCHECK(index >= 0 && index < item_count()); | 96 DCHECK_LT(index, item_count()); |
97 return items_[index]; | 97 return items_[index]; |
98 } | 98 } |
99 ItemType* GetItemAt(int index) { | 99 ItemType* GetItemAt(size_t index) { |
100 return const_cast<ItemType*>( | 100 return const_cast<ItemType*>( |
101 const_cast<const ListModel<ItemType>*>(this)->GetItemAt(index)); | 101 const_cast<const ListModel<ItemType>*>(this)->GetItemAt(index)); |
102 } | 102 } |
103 | 103 |
104 private: | 104 private: |
105 ScopedVector<ItemType> items_; | 105 ScopedVector<ItemType> items_; |
106 ObserverList<ListModelObserver> observers_; | 106 ObserverList<ListModelObserver> observers_; |
107 | 107 |
108 DISALLOW_COPY_AND_ASSIGN(ListModel<ItemType>); | 108 DISALLOW_COPY_AND_ASSIGN(ListModel<ItemType>); |
109 }; | 109 }; |
110 | 110 |
111 } // namespace ui | 111 } // namespace ui |
112 | 112 |
113 #endif // UI_BASE_MODELS_LIST_MODEL_H_ | 113 #endif // UI_BASE_MODELS_LIST_MODEL_H_ |
OLD | NEW |