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 | const auto keys = m_data.keys(); | ||||
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 | for (quint32 key : keys) { | ||||
118 | if (object->index() < key) { | ||||
119 | break; | ||||
120 | } | ||||
121 | modelIndex++; | ||||
122 | } | ||||
115 | 123 | | |||
116 | const int modelIndex = m_data.keys().indexOf(object->index()); | 124 | emit aboutToBeAdded(modelIndex); | ||
117 | Q_ASSERT(modelIndex >= 0); | 125 | m_data.insert(object->index(), object); | ||
126 | Q_ASSERT(modelIndex == m_data.keys().indexOf(object->index())); | ||||
118 | emit added(modelIndex); | 127 | emit added(modelIndex); | ||
119 | } | 128 | } | ||
120 | 129 | | |||
121 | // Context is passed in as parent because context needs to include the maps | 130 | // 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. | 131 | // so we'd cause a circular dep if we were to try to use the instance here. | ||
123 | // Plus that's weird separation anyway. | 132 | // Plus that's weird separation anyway. | ||
124 | void updateEntry(const PAInfo *info, QObject *parent) | 133 | void updateEntry(const PAInfo *info, QObject *parent) | ||
125 | { | 134 | { | ||
126 | Q_ASSERT(info); | 135 | Q_ASSERT(info); | ||
127 | 136 | | |||
128 | if (m_pendingRemovals.remove(info->index)) { | 137 | if (m_pendingRemovals.remove(info->index)) { | ||
129 | // Was already removed again. | 138 | // Was already removed again. | ||
130 | return; | 139 | return; | ||
131 | } | 140 | } | ||
132 | 141 | | |||
133 | const bool isNew = !m_data.contains(info->index); | | |||
134 | | ||||
135 | auto *obj = m_data.value(info->index, nullptr); | 142 | auto *obj = m_data.value(info->index, nullptr); | ||
136 | if (!obj) { | 143 | if (!obj) { | ||
137 | obj = new Type(parent); | 144 | obj = new Type(parent); | ||
138 | } | 145 | } | ||
139 | obj->update(info); | 146 | obj->update(info); | ||
140 | m_data.insert(info->index, obj); | | |||
141 | 147 | | |||
142 | if (isNew) { | 148 | if (!m_data.contains(info->index)) { | ||
143 | const int modelIndex = m_data.keys().indexOf(info->index); | 149 | insert(obj); | ||
144 | Q_ASSERT(modelIndex >= 0); | | |||
145 | emit added(modelIndex); | | |||
146 | } | 150 | } | ||
147 | } | 151 | } | ||
148 | 152 | | |||
149 | void removeEntry(quint32 index) | 153 | void removeEntry(quint32 index) | ||
150 | { | 154 | { | ||
151 | if (!m_data.contains(index)) { | 155 | if (!m_data.contains(index)) { | ||
152 | m_pendingRemovals.insert(index); | 156 | m_pendingRemovals.insert(index); | ||
153 | } else { | 157 | } 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)