OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #ifndef UI_BASE_MODELS_LIST_MODEL_H_ |
| 6 #define UI_BASE_MODELS_LIST_MODEL_H_ |
| 7 #pragma once |
| 8 |
| 9 #include "base/basictypes.h" |
| 10 #include "base/logging.h" |
| 11 #include "base/observer_list.h" |
| 12 #include "base/memory/scoped_vector.h" |
| 13 #include "ui/base/models/list_model_observer.h" |
| 14 |
| 15 namespace ui { |
| 16 |
| 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 |
| 19 // RemoveAt. |
| 20 template <class ItemType> |
| 21 class ListModel { |
| 22 public: |
| 23 typedef std::vector<ItemType*> Items; |
| 24 |
| 25 ListModel() {} |
| 26 virtual ~ListModel() {} |
| 27 |
| 28 // Adds |item| to the model at given |index|. |
| 29 virtual void AddAt(int index, ItemType* item) { |
| 30 DCHECK(index >= 0 && index <= item_count()); |
| 31 items_->insert(items_.begin() + index, item); |
| 32 NotifyItemsAdded(index, 1); |
| 33 } |
| 34 |
| 35 // Removes an item at given |index| from the model. Note the removed item |
| 36 // is NOT deleted and it's up to the caller to delete it. |
| 37 virtual ItemType* RemoveAt(int index) { |
| 38 DCHECK(index >= 0 && index < item_count()); |
| 39 ItemType* item = items_[index]; |
| 40 items_->erase(items_.begin() + index); |
| 41 NotifyItemsRemoved(index, 1); |
| 42 return item; |
| 43 } |
| 44 |
| 45 // Removes all items from the model. This does NOT delete the items. |
| 46 virtual void RemoveAll() { |
| 47 int count = item_count(); |
| 48 items_->clear(); |
| 49 NotifyItemsRemoved(0, count); |
| 50 } |
| 51 |
| 52 // Removes an item at given |index| from the model and deletes it. |
| 53 virtual void DeleteAt(int index) { |
| 54 delete RemoveAt(index); |
| 55 } |
| 56 |
| 57 // Removes and deletes all items from the model. |
| 58 virtual void DeleteAll() { |
| 59 int count = item_count(); |
| 60 items_.reset(); |
| 61 NotifyItemsRemoved(0, count); |
| 62 } |
| 63 |
| 64 // Convenience function to append an item to the model. |
| 65 void Add(ItemType* item) { |
| 66 AddAt(item_count(), item); |
| 67 } |
| 68 |
| 69 void AddObserver(ListModelObserver* observer) { |
| 70 observers_.AddObserver(observer); |
| 71 } |
| 72 |
| 73 void RemoveObserver(ListModelObserver* observer) { |
| 74 observers_.RemoveObserver(observer); |
| 75 } |
| 76 |
| 77 void NotifyItemsAdded(int start, int count) { |
| 78 FOR_EACH_OBSERVER(ListModelObserver, |
| 79 observers_, |
| 80 ListItemsAdded(start, count)); |
| 81 } |
| 82 |
| 83 void NotifyItemsRemoved(int start, int count) { |
| 84 FOR_EACH_OBSERVER(ListModelObserver, |
| 85 observers_, |
| 86 ListItemsRemoved(start, count)); |
| 87 } |
| 88 |
| 89 void NotifyItemsChanged(int start, int count) { |
| 90 FOR_EACH_OBSERVER(ListModelObserver, |
| 91 observers_, |
| 92 ListItemsChanged(start, count)); |
| 93 } |
| 94 |
| 95 int item_count() const { return static_cast<int>(items_.size()); } |
| 96 const Items& items() const { return items_.get(); } |
| 97 |
| 98 const ItemType* item_at(int index) const { |
| 99 DCHECK(index >= 0 && index < item_count()); |
| 100 return items_[index]; |
| 101 } |
| 102 ItemType* item_at(int index) { |
| 103 return const_cast<ItemType*>( |
| 104 const_cast<const ListModel<ItemType>*>(this)->item_at(index)); |
| 105 } |
| 106 |
| 107 private: |
| 108 ScopedVector<ItemType> items_; |
| 109 ObserverList<ListModelObserver> observers_; |
| 110 |
| 111 DISALLOW_COPY_AND_ASSIGN(ListModel<ItemType>); |
| 112 }; |
| 113 |
| 114 } // namespace ui |
| 115 |
| 116 #endif // UI_BASE_MODELS_LIST_MODEL_H_ |
OLD | NEW |