Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(173)

Side by Side Diff: ui/base/models/list_model.h

Issue 8890049: [Aura] Implement views-based applist. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: rebased and refactored Created 9 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(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 stores ItemType pointer.
sky 2011/12/14 16:51:33 Document ownership.
xiyuan 2011/12/20 00:14:42 Done.
18 template <class ItemType>
19 class ListModel {
20 public:
21 typedef std::vector<ItemType*> Items;
22
23 ListModel() {}
24 virtual ~ListModel() {}
25
26 // Adds |item| to the model at given |index|.
27 virtual void AddAt(int index, ItemType* item) {
28 DCHECK(index >= 0 && index <= item_count());
29 items_->insert(items_.begin() + index, item);
30 NotifyItemsAdded(index, 1);
31 }
32
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.
35 virtual ItemType* RemoveAt(int index) {
36 DCHECK(index >= 0 && index < item_count());
37 ItemType* item = items_[index];
38 items_->erase(items_.begin() + index);
39 NotifyItemsRemoved(index, 1);
40 return item;
41 }
42
43 // Removes all items from the model. This does NOT delete the items.
44 virtual void RemoveAll() {
45 int count = item_count();
46 items_->clear();
47 NotifyItemsRemoved(0, count);
48 }
49
50 // Removes an item at given |index| from the model and deletes it.
51 virtual void DeleteAt(int index) {
52 delete RemoveAt(index);
53 }
54
55 // Removes and deletes all items from the model.
56 virtual void DeleteAll() {
57 int count = item_count();
58 items_.reset();
59 NotifyItemsRemoved(0, count);
60 }
61
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) {
68 observers_.AddObserver(observer);
69 }
70
71 void RemoveObserver(ListModelObserver* observer) {
72 observers_.RemoveObserver(observer);
73 }
74
75 void NotifyItemsAdded(int start, int count) {
76 FOR_EACH_OBSERVER(ListModelObserver,
77 observers_,
78 ListItemsAdded(start, count));
79 }
80
81 void NotifyItemsRemoved(int start, int count) {
82 FOR_EACH_OBSERVER(ListModelObserver,
83 observers_,
84 ListItemsRemoved(start, count));
85 }
86
87 void NotifyItemsChanged(int start, int count) {
88 FOR_EACH_OBSERVER(ListModelObserver,
89 observers_,
90 ListItemsChanged(start, count));
91 }
92
93 int item_count() const { return static_cast<int>(items_.size()); }
94 const Items& items() const { return items_.get(); }
95
96 const ItemType* item_at(int index) const {
97 DCHECK(index >= 0 && index < item_count());
98 return items_[index];
99 }
100 ItemType* item_at(int index) {
101 return const_cast<ItemType*>(
102 const_cast<const ListModel<ItemType>*>(this)->item_at(index));
103 }
104
105 private:
106 ScopedVector<ItemType> items_;
107 ObserverList<ListModelObserver> observers_;
108
109 DISALLOW_COPY_AND_ASSIGN(ListModel<ItemType>);
110 };
111
112 } // namespace ui
113
114 #endif // UI_BASE_MODELS_LIST_MODEL_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698