Changeset View
Changeset View
Standalone View
Standalone View
src/maps.h
Show First 20 Lines • Show All 50 Lines • ▼ Show 20 Line(s) | 50 | { | |||
---|---|---|---|---|---|
51 | Q_OBJECT | 51 | Q_OBJECT | ||
52 | 52 | | |||
53 | public: | 53 | public: | ||
54 | virtual int count() const = 0; | 54 | virtual int count() const = 0; | ||
55 | virtual QObject *objectAt(int index) const = 0; | 55 | virtual QObject *objectAt(int index) const = 0; | ||
56 | virtual int indexOfObject(QObject *object) const = 0; | 56 | virtual int indexOfObject(QObject *object) const = 0; | ||
57 | 57 | | |||
58 | signals: | 58 | signals: | ||
59 | void aboutToBeAdded(int index); | ||||
59 | void added(int index); | 60 | void added(int index); | ||
60 | void aboutToBeRemoved(int index); | 61 | void aboutToBeRemoved(int index); | ||
61 | void removed(int index); | 62 | void removed(int index); | ||
62 | }; | 63 | }; | ||
63 | 64 | | |||
64 | /** | 65 | /** | ||
65 | * Maps a specific index to a specific object pointer. | 66 | * Maps a specific index to a specific object pointer. | ||
66 | * This is used to give the unique arbitrary PulseAudio index of a PulseObject a | 67 | * This is used to give the unique arbitrary PulseAudio index of a PulseObject a | ||
Show All 39 Lines | 104 | { | |||
106 | } | 107 | } | ||
107 | m_pendingRemovals.clear(); | 108 | m_pendingRemovals.clear(); | ||
108 | } | 109 | } | ||
109 | 110 | | |||
110 | void insert(Type *object) | 111 | void insert(Type *object) | ||
111 | { | 112 | { | ||
112 | Q_ASSERT(!m_data.contains(object->index())); | 113 | Q_ASSERT(!m_data.contains(object->index())); | ||
113 | 114 | | |||
114 | m_data.insert(object->index(), object); | 115 | int modelIndex = 0; | ||
116 | for (auto it = m_data.constBegin(); it != m_data.constEnd(); ++it) { | ||||
broulik: This creates a temporary list just to iterate it, better use iterators and then `key()`… | |||||
Some fancy std algorithm gives me iterator, but I need index (and std::distance is another traverse because map iterator is not RandomAccessIterator). drosca: Some fancy `std` algorithm gives me iterator, but I need index (and `std::distance` is another… | |||||
117 | if (object->index() < it.key()) { | ||||
118 | break; | ||||
119 | } | ||||
120 | modelIndex++; | ||||
121 | } | ||||
115 | 122 | | |||
116 | const int modelIndex = m_data.keys().indexOf(object->index()); | 123 | emit aboutToBeAdded(modelIndex); | ||
117 | Q_ASSERT(modelIndex >= 0); | 124 | m_data.insert(object->index(), object); | ||
125 | Q_ASSERT(modelIndex == m_data.keys().indexOf(object->index())); | ||||
118 | emit added(modelIndex); | 126 | emit added(modelIndex); | ||
119 | } | 127 | } | ||
120 | 128 | | |||
121 | // Context is passed in as parent because context needs to include the maps | 129 | // Context is passed in as parent because context needs to include the maps | ||
122 | // so we'd cause a circular dep if we were to try to use the instance here. | 130 | // so we'd cause a circular dep if we were to try to use the instance here. | ||
123 | // Plus that's weird separation anyway. | 131 | // Plus that's weird separation anyway. | ||
124 | void updateEntry(const PAInfo *info, QObject *parent) | 132 | void updateEntry(const PAInfo *info, QObject *parent) | ||
125 | { | 133 | { | ||
126 | Q_ASSERT(info); | 134 | Q_ASSERT(info); | ||
127 | 135 | | |||
128 | if (m_pendingRemovals.remove(info->index)) { | 136 | if (m_pendingRemovals.remove(info->index)) { | ||
129 | // Was already removed again. | 137 | // Was already removed again. | ||
130 | return; | 138 | return; | ||
131 | } | 139 | } | ||
132 | 140 | | |||
133 | const bool isNew = !m_data.contains(info->index); | | |||
134 | | ||||
135 | auto *obj = m_data.value(info->index, nullptr); | 141 | auto *obj = m_data.value(info->index, nullptr); | ||
136 | if (!obj) { | 142 | if (!obj) { | ||
137 | obj = new Type(parent); | 143 | obj = new Type(parent); | ||
138 | } | 144 | } | ||
139 | obj->update(info); | 145 | obj->update(info); | ||
140 | m_data.insert(info->index, obj); | | |||
141 | 146 | | |||
142 | if (isNew) { | 147 | if (!m_data.contains(info->index)) { | ||
143 | const int modelIndex = m_data.keys().indexOf(info->index); | 148 | insert(obj); | ||
144 | Q_ASSERT(modelIndex >= 0); | | |||
145 | emit added(modelIndex); | | |||
146 | } | 149 | } | ||
147 | } | 150 | } | ||
148 | 151 | | |||
149 | void removeEntry(quint32 index) | 152 | void removeEntry(quint32 index) | ||
150 | { | 153 | { | ||
151 | if (!m_data.contains(index)) { | 154 | if (!m_data.contains(index)) { | ||
152 | m_pendingRemovals.insert(index); | 155 | m_pendingRemovals.insert(index); | ||
153 | } else { | 156 | } else { | ||
Show All 24 Lines |
This creates a temporary list just to iterate it, better use iterators and then key() provided by it (I bet there's even a fancy std algorithm for what you're trying to do here)