File: list_model.h

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (147 lines) | stat: -rw-r--r-- 4,544 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef UI_BASE_MODELS_LIST_MODEL_H_
#define UI_BASE_MODELS_LIST_MODEL_H_

#include <stddef.h>

#include <memory>
#include <utility>
#include <vector>

#include "base/check_op.h"
#include "base/observer_list.h"
#include "ui/base/models/list_model_observer.h"

namespace ui {

// A list model that manages a list of ItemType pointers. Items added to the
// model are owned by the model. An item can be taken out of the model by
// RemoveAt.
template <class ItemType>
class ListModel {
 public:
  using ItemList = std::vector<std::unique_ptr<ItemType>>;

  ListModel() {}

  ListModel(const ListModel&) = delete;
  ListModel& operator=(const ListModel&) = delete;

  ~ListModel() {}

  // Adds |item| at the |index| into |items_|. Returns a raw pointer.
  ItemType* AddAt(size_t index, std::unique_ptr<ItemType> item) {
    DCHECK_LE(index, item_count());
    ItemType* item_ptr = item.get();
    items_.insert(items_.begin() + index, std::move(item));
    NotifyItemsAdded(index, 1);
    return item_ptr;
  }

  // Convenience function to append an item to the model.
  ItemType* Add(std::unique_ptr<ItemType> item) {
    return AddAt(item_count(), std::move(item));
  }

  // Removes the item at |index| from |items_| without deleting it.
  // Returns a scoped pointer containing the removed item.
  std::unique_ptr<ItemType> RemoveAt(size_t index) {
    DCHECK_LT(index, item_count());
    std::unique_ptr<ItemType> item = std::move(items_[index]);
    items_.erase(items_.begin() + index);
    NotifyItemsRemoved(index, 1);
    return item;
  }

  // Removes all items from the model without deleting them.
  // Returns a vector containing the removed items.
  ItemList RemoveAll() {
    ItemList result;
    result.swap(items_);
    NotifyItemsRemoved(0, result.size());
    return result;
  }

  // Removes the item at |index| from |items_| and deletes it.
  void DeleteAt(size_t index) {
    std::unique_ptr<ItemType> item = RemoveAt(index);
    // |item| will be deleted on destruction.
  }

  // Removes and deletes all items from the model.
  void DeleteAll() {
    ItemList to_be_deleted;
    to_be_deleted.swap(items_);
    NotifyItemsRemoved(0, to_be_deleted.size());
  }

  // Moves the item at |index| to |target_index|. |target_index| is in terms
  // of the model *after* the item at |index| is removed.
  void Move(size_t index, size_t target_index) {
    DCHECK_LT(index, item_count());
    DCHECK_LT(target_index, item_count());

    if (index == target_index)
      return;

    std::unique_ptr<ItemType> item = std::move(items_[index]);
    items_.erase(items_.begin() + index);
    items_.insert(items_.begin() + target_index, std::move(item));
    NotifyItemMoved(index, target_index);
  }

  void AddObserver(ListModelObserver* observer) const {
    observers_.AddObserver(observer);
  }

  void RemoveObserver(ListModelObserver* observer) const {
    observers_.RemoveObserver(observer);
  }

  void NotifyItemsAdded(size_t start, size_t count) {
    observers_.Notify(&ListModelObserver::ListItemsAdded, start, count);
  }

  void NotifyItemsRemoved(size_t start, size_t count) {
    observers_.Notify(&ListModelObserver::ListItemsRemoved, start, count);
  }

  void NotifyItemMoved(size_t index, size_t target_index) {
    observers_.Notify(&ListModelObserver::ListItemMoved, index, target_index);
  }

  void NotifyItemsChanged(size_t start, size_t count) {
    observers_.Notify(&ListModelObserver::ListItemsChanged, start, count);
  }

  size_t item_count() const { return items_.size(); }

  const ItemType* GetItemAt(size_t index) const {
    DCHECK_LT(index, item_count());
    return items_[index].get();
  }
  ItemType* GetItemAt(size_t index) {
    return const_cast<ItemType*>(
        const_cast<const ListModel<ItemType>*>(this)->GetItemAt(index));
  }

  // Iteration interface.
  typename ItemList::iterator begin() { return items_.begin(); }
  typename ItemList::const_iterator begin() const { return items_.begin(); }
  typename ItemList::iterator end() { return items_.end(); }
  typename ItemList::const_iterator end() const { return items_.end(); }

 private:
  ItemList items_;

  // Mutable to allow adding/removing `ListModelObserver`'s through a const
  // ListModel in order to preserve underlying data const-ness.
  mutable base::ObserverList<ListModelObserver>::Unchecked observers_;
};

}  // namespace ui

#endif  // UI_BASE_MODELS_LIST_MODEL_H_