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 | 7 |
8 #include <stddef.h> | 8 #include <stddef.h> |
9 | 9 |
| 10 #include <memory> |
10 #include <utility> | 11 #include <utility> |
11 | 12 |
12 #include "base/logging.h" | 13 #include "base/logging.h" |
13 #include "base/macros.h" | 14 #include "base/macros.h" |
14 #include "base/memory/scoped_ptr.h" | 15 #include "base/memory/ptr_util.h" |
15 #include "base/memory/scoped_vector.h" | 16 #include "base/memory/scoped_vector.h" |
16 #include "base/observer_list.h" | 17 #include "base/observer_list.h" |
17 #include "ui/base/models/list_model_observer.h" | 18 #include "ui/base/models/list_model_observer.h" |
18 | 19 |
19 namespace ui { | 20 namespace ui { |
20 | 21 |
21 // A list model that manages a list of ItemType pointers. Items added to the | 22 // A list model that manages a list of ItemType pointers. Items added to the |
22 // model are owned by the model. An item can be taken out of the model by | 23 // model are owned by the model. An item can be taken out of the model by |
23 // RemoveAt. | 24 // RemoveAt. |
24 template <class ItemType> | 25 template <class ItemType> |
25 class ListModel { | 26 class ListModel { |
26 public: | 27 public: |
27 ListModel() {} | 28 ListModel() {} |
28 ~ListModel() {} | 29 ~ListModel() {} |
29 | 30 |
30 // Adds |item| at the |index| into |items_|. Takes ownership of |item|. | 31 // Adds |item| at the |index| into |items_|. Takes ownership of |item|. |
31 void AddAt(size_t index, ItemType* item) { | 32 void AddAt(size_t index, ItemType* item) { |
32 DCHECK_LE(index, item_count()); | 33 DCHECK_LE(index, item_count()); |
33 items_.insert(items_.begin() + index, item); | 34 items_.insert(items_.begin() + index, item); |
34 NotifyItemsAdded(index, 1); | 35 NotifyItemsAdded(index, 1); |
35 } | 36 } |
36 | 37 |
37 // Convenience function to append an item to the model. | 38 // Convenience function to append an item to the model. |
38 void Add(ItemType* item) { | 39 void Add(ItemType* item) { |
39 AddAt(item_count(), item); | 40 AddAt(item_count(), item); |
40 } | 41 } |
41 | 42 |
42 // Removes the item at |index| from |items_| without deleting it. | 43 // Removes the item at |index| from |items_| without deleting it. |
43 // Returns a scoped pointer containing the removed item. | 44 // Returns a scoped pointer containing the removed item. |
44 scoped_ptr<ItemType> RemoveAt(size_t index) { | 45 std::unique_ptr<ItemType> RemoveAt(size_t index) { |
45 DCHECK_LT(index, item_count()); | 46 DCHECK_LT(index, item_count()); |
46 ItemType* item = items_[index]; | 47 ItemType* item = items_[index]; |
47 items_.weak_erase(items_.begin() + index); | 48 items_.weak_erase(items_.begin() + index); |
48 NotifyItemsRemoved(index, 1); | 49 NotifyItemsRemoved(index, 1); |
49 return make_scoped_ptr<ItemType>(item); | 50 return base::WrapUnique<ItemType>(item); |
50 } | 51 } |
51 | 52 |
52 // Removes all items from the model. This does NOT delete the items. | 53 // Removes all items from the model. This does NOT delete the items. |
53 void RemoveAll() { | 54 void RemoveAll() { |
54 size_t count = item_count(); | 55 size_t count = item_count(); |
55 items_.weak_clear(); | 56 items_.weak_clear(); |
56 NotifyItemsRemoved(0, count); | 57 NotifyItemsRemoved(0, count); |
57 } | 58 } |
58 | 59 |
59 // Removes the item at |index| from |items_| and deletes it. | 60 // Removes the item at |index| from |items_| and deletes it. |
60 void DeleteAt(size_t index) { | 61 void DeleteAt(size_t index) { |
61 scoped_ptr<ItemType> item = RemoveAt(index); | 62 std::unique_ptr<ItemType> item = RemoveAt(index); |
62 // |item| will be deleted on destruction. | 63 // |item| will be deleted on destruction. |
63 } | 64 } |
64 | 65 |
65 // Removes and deletes all items from the model. | 66 // Removes and deletes all items from the model. |
66 void DeleteAll() { | 67 void DeleteAll() { |
67 ScopedVector<ItemType> to_be_deleted(std::move(items_)); | 68 ScopedVector<ItemType> to_be_deleted(std::move(items_)); |
68 NotifyItemsRemoved(0, to_be_deleted.size()); | 69 NotifyItemsRemoved(0, to_be_deleted.size()); |
69 } | 70 } |
70 | 71 |
71 // Moves the item at |index| to |target_index|. |target_index| is in terms | 72 // Moves the item at |index| to |target_index|. |target_index| is in terms |
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
139 private: | 140 private: |
140 ScopedVector<ItemType> items_; | 141 ScopedVector<ItemType> items_; |
141 base::ObserverList<ListModelObserver> observers_; | 142 base::ObserverList<ListModelObserver> observers_; |
142 | 143 |
143 DISALLOW_COPY_AND_ASSIGN(ListModel<ItemType>); | 144 DISALLOW_COPY_AND_ASSIGN(ListModel<ItemType>); |
144 }; | 145 }; |
145 | 146 |
146 } // namespace ui | 147 } // namespace ui |
147 | 148 |
148 #endif // UI_BASE_MODELS_LIST_MODEL_H_ | 149 #endif // UI_BASE_MODELS_LIST_MODEL_H_ |
OLD | NEW |