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 ~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(size_t index, ItemType* item) { | 27 void AddAt(size_t index, ItemType* item) { |
28 DCHECK_LE(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 // Convenience function to append an item to the model. |
| 34 void Add(ItemType* item) { |
| 35 AddAt(item_count(), item); |
| 36 } |
| 37 |
33 // Removes an item at given |index| from the model. Note the removed item | 38 // 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. | 39 // is NOT deleted and it's up to the caller to delete it. |
35 virtual ItemType* RemoveAt(size_t index) { | 40 ItemType* RemoveAt(size_t index) { |
36 DCHECK_LT(index, item_count()); | 41 DCHECK_LT(index, item_count()); |
37 ItemType* item = items_[index]; | 42 ItemType* item = items_[index]; |
38 items_->erase(items_.begin() + index); | 43 items_->erase(items_.begin() + index); |
39 NotifyItemsRemoved(index, 1); | 44 NotifyItemsRemoved(index, 1); |
40 return item; | 45 return item; |
41 } | 46 } |
42 | 47 |
43 // Removes all items from the model. This does NOT delete the items. | 48 // Removes all items from the model. This does NOT delete the items. |
44 virtual void RemoveAll() { | 49 void RemoveAll() { |
45 size_t count = item_count(); | 50 size_t count = item_count(); |
46 items_->clear(); | 51 items_->clear(); |
47 NotifyItemsRemoved(0, count); | 52 NotifyItemsRemoved(0, count); |
48 } | 53 } |
49 | 54 |
50 // Removes an item at given |index| from the model and deletes it. | 55 // Removes an item at given |index| from the model and deletes it. |
51 virtual void DeleteAt(size_t index) { | 56 void DeleteAt(size_t index) { |
52 delete RemoveAt(index); | 57 delete RemoveAt(index); |
53 } | 58 } |
54 | 59 |
55 // Removes and deletes all items from the model. | 60 // Removes and deletes all items from the model. |
56 virtual void DeleteAll() { | 61 void DeleteAll() { |
57 size_t count = item_count(); | 62 size_t count = item_count(); |
58 items_.reset(); | 63 items_.reset(); |
59 NotifyItemsRemoved(0, count); | 64 NotifyItemsRemoved(0, count); |
60 } | 65 } |
61 | 66 |
62 // Convenience function to append an item to the model. | |
63 void Add(ItemType* item) { | |
64 AddAt(item_count(), item); | |
65 } | |
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(size_t start, size_t count) { | 75 void NotifyItemsAdded(size_t start, size_t count) { |
76 FOR_EACH_OBSERVER(ListModelObserver, | 76 FOR_EACH_OBSERVER(ListModelObserver, |
(...skipping 27 matching lines...) Expand all Loading... |
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 |