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 ~ListModel() {} | 24 ~ListModel() {} |
25 | 25 |
26 // Adds |item| to the model at given |index|. | 26 // Adds |item| to the model at given |index|. |
27 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. | 33 // Convenience function to append an item to the model. |
34 void Add(ItemType* item) { | 34 void Add(ItemType* item) { |
35 AddAt(item_count(), item); | 35 AddAt(item_count(), item); |
36 } | 36 } |
37 | 37 |
38 // 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 |
39 // 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. |
40 ItemType* RemoveAt(size_t index) { | 40 ItemType* RemoveAt(size_t index) { |
41 DCHECK_LT(index, item_count()); | 41 DCHECK_LT(index, item_count()); |
42 ItemType* item = items_[index]; | 42 ItemType* item = items_[index]; |
43 items_->erase(items_.begin() + index); | 43 items_.weak_erase(items_.begin() + index); |
44 NotifyItemsRemoved(index, 1); | 44 NotifyItemsRemoved(index, 1); |
45 return item; | 45 return item; |
46 } | 46 } |
47 | 47 |
48 // 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. |
49 void RemoveAll() { | 49 void RemoveAll() { |
50 size_t count = item_count(); | 50 size_t count = item_count(); |
51 items_->clear(); | 51 items_.clear(); |
52 NotifyItemsRemoved(0, count); | 52 NotifyItemsRemoved(0, count); |
53 } | 53 } |
54 | 54 |
55 // 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. |
56 void DeleteAt(size_t index) { | 56 void DeleteAt(size_t index) { |
57 delete RemoveAt(index); | 57 delete RemoveAt(index); |
58 } | 58 } |
59 | 59 |
60 // Removes and deletes all items from the model. | 60 // Removes and deletes all items from the model. |
61 void DeleteAll() { | 61 void DeleteAll() { |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
103 private: | 103 private: |
104 ScopedVector<ItemType> items_; | 104 ScopedVector<ItemType> items_; |
105 ObserverList<ListModelObserver> observers_; | 105 ObserverList<ListModelObserver> observers_; |
106 | 106 |
107 DISALLOW_COPY_AND_ASSIGN(ListModel<ItemType>); | 107 DISALLOW_COPY_AND_ASSIGN(ListModel<ItemType>); |
108 }; | 108 }; |
109 | 109 |
110 } // namespace ui | 110 } // namespace ui |
111 | 111 |
112 #endif // UI_BASE_MODELS_LIST_MODEL_H_ | 112 #endif // UI_BASE_MODELS_LIST_MODEL_H_ |
OLD | NEW |