diff options
author | Christian Mollekopf <chrigi_1@fastmail.fm> | 2016-06-15 06:59:06 +0200 |
---|---|---|
committer | Christian Mollekopf <chrigi_1@fastmail.fm> | 2016-06-15 07:12:13 +0200 |
commit | 77115bab30aa789f9af9fe49006e8747488f8a4c (patch) | |
tree | 5a370c6f25fdce3396d8172b509306f8f501d34f /common/modelresult.cpp | |
parent | bb70bdcd0eaf72ffc304536267a66c5de5eaf2e9 (diff) | |
download | sink-77115bab30aa789f9af9fe49006e8747488f8a4c.tar.gz sink-77115bab30aa789f9af9fe49006e8747488f8a4c.zip |
Moved thread-boundary crossing to the model.
That way we avoid any unnecessary queuing for the sync API,
and enable fine-tuning in the model code at a later stage.
Diffstat (limited to 'common/modelresult.cpp')
-rw-r--r-- | common/modelresult.cpp | 31 |
1 files changed, 28 insertions, 3 deletions
diff --git a/common/modelresult.cpp b/common/modelresult.cpp index 4a85610..6feca43 100644 --- a/common/modelresult.cpp +++ b/common/modelresult.cpp | |||
@@ -20,9 +20,11 @@ | |||
20 | #include "modelresult.h" | 20 | #include "modelresult.h" |
21 | 21 | ||
22 | #include <QDebug> | 22 | #include <QDebug> |
23 | #include <QThread> | ||
23 | 24 | ||
24 | #include "domain/folder.h" | 25 | #include "domain/folder.h" |
25 | #include "log.h" | 26 | #include "log.h" |
27 | #include "threadboundary.h" | ||
26 | 28 | ||
27 | #undef DEBUG_AREA | 29 | #undef DEBUG_AREA |
28 | #define DEBUG_AREA "client.modelresult" | 30 | #define DEBUG_AREA "client.modelresult" |
@@ -63,12 +65,14 @@ qint64 ModelResult<T, Ptr>::parentId(const Ptr &value) | |||
63 | template <class T, class Ptr> | 65 | template <class T, class Ptr> |
64 | int ModelResult<T, Ptr>::rowCount(const QModelIndex &parent) const | 66 | int ModelResult<T, Ptr>::rowCount(const QModelIndex &parent) const |
65 | { | 67 | { |
68 | Q_ASSERT(QThread::currentThread() == this->thread()); | ||
66 | return mTree[getIdentifier(parent)].size(); | 69 | return mTree[getIdentifier(parent)].size(); |
67 | } | 70 | } |
68 | 71 | ||
69 | template <class T, class Ptr> | 72 | template <class T, class Ptr> |
70 | int ModelResult<T, Ptr>::columnCount(const QModelIndex &parent) const | 73 | int ModelResult<T, Ptr>::columnCount(const QModelIndex &parent) const |
71 | { | 74 | { |
75 | Q_ASSERT(QThread::currentThread() == this->thread()); | ||
72 | return mPropertyColumns.size(); | 76 | return mPropertyColumns.size(); |
73 | } | 77 | } |
74 | 78 | ||
@@ -86,6 +90,7 @@ QVariant ModelResult<T, Ptr>::headerData(int section, Qt::Orientation orientatio | |||
86 | template <class T, class Ptr> | 90 | template <class T, class Ptr> |
87 | QVariant ModelResult<T, Ptr>::data(const QModelIndex &index, int role) const | 91 | QVariant ModelResult<T, Ptr>::data(const QModelIndex &index, int role) const |
88 | { | 92 | { |
93 | Q_ASSERT(QThread::currentThread() == this->thread()); | ||
89 | if (role == DomainObjectRole && index.isValid()) { | 94 | if (role == DomainObjectRole && index.isValid()) { |
90 | Q_ASSERT(mEntities.contains(index.internalId())); | 95 | Q_ASSERT(mEntities.contains(index.internalId())); |
91 | return QVariant::fromValue(mEntities.value(index.internalId())); | 96 | return QVariant::fromValue(mEntities.value(index.internalId())); |
@@ -112,6 +117,7 @@ QVariant ModelResult<T, Ptr>::data(const QModelIndex &index, int role) const | |||
112 | template <class T, class Ptr> | 117 | template <class T, class Ptr> |
113 | QModelIndex ModelResult<T, Ptr>::index(int row, int column, const QModelIndex &parent) const | 118 | QModelIndex ModelResult<T, Ptr>::index(int row, int column, const QModelIndex &parent) const |
114 | { | 119 | { |
120 | Q_ASSERT(QThread::currentThread() == this->thread()); | ||
115 | const auto id = getIdentifier(parent); | 121 | const auto id = getIdentifier(parent); |
116 | const auto list = mTree.value(id); | 122 | const auto list = mTree.value(id); |
117 | if (list.size() > row) { | 123 | if (list.size() > row) { |
@@ -119,12 +125,14 @@ QModelIndex ModelResult<T, Ptr>::index(int row, int column, const QModelIndex &p | |||
119 | return createIndex(row, column, childId); | 125 | return createIndex(row, column, childId); |
120 | } | 126 | } |
121 | Warning() << "Index not available " << row << column << parent; | 127 | Warning() << "Index not available " << row << column << parent; |
128 | Q_ASSERT(false); | ||
122 | return QModelIndex(); | 129 | return QModelIndex(); |
123 | } | 130 | } |
124 | 131 | ||
125 | template <class T, class Ptr> | 132 | template <class T, class Ptr> |
126 | QModelIndex ModelResult<T, Ptr>::createIndexFromId(const qint64 &id) const | 133 | QModelIndex ModelResult<T, Ptr>::createIndexFromId(const qint64 &id) const |
127 | { | 134 | { |
135 | Q_ASSERT(QThread::currentThread() == this->thread()); | ||
128 | if (id == 0) { | 136 | if (id == 0) { |
129 | return QModelIndex(); | 137 | return QModelIndex(); |
130 | } | 138 | } |
@@ -185,6 +193,7 @@ void ModelResult<T, Ptr>::add(const Ptr &value) | |||
185 | } | 193 | } |
186 | if (mEntities.contains(childId)) { | 194 | if (mEntities.contains(childId)) { |
187 | Warning() << "Entity already in model " << value->identifier(); | 195 | Warning() << "Entity already in model " << value->identifier(); |
196 | Q_ASSERT(false); | ||
188 | return; | 197 | return; |
189 | } | 198 | } |
190 | // qDebug() << "Inserting rows " << index << parent; | 199 | // qDebug() << "Inserting rows " << index << parent; |
@@ -216,6 +225,7 @@ void ModelResult<T, Ptr>::remove(const Ptr &value) | |||
216 | template <class T, class Ptr> | 225 | template <class T, class Ptr> |
217 | void ModelResult<T, Ptr>::fetchEntities(const QModelIndex &parent) | 226 | void ModelResult<T, Ptr>::fetchEntities(const QModelIndex &parent) |
218 | { | 227 | { |
228 | Q_ASSERT(QThread::currentThread() == this->thread()); | ||
219 | const auto id = getIdentifier(parent); | 229 | const auto id = getIdentifier(parent); |
220 | mEntityChildrenFetchComplete.remove(id); | 230 | mEntityChildrenFetchComplete.remove(id); |
221 | mEntityChildrenFetched.insert(id); | 231 | mEntityChildrenFetched.insert(id); |
@@ -237,11 +247,26 @@ void ModelResult<T, Ptr>::setFetcher(const std::function<void(const Ptr &parent) | |||
237 | template <class T, class Ptr> | 247 | template <class T, class Ptr> |
238 | void ModelResult<T, Ptr>::setEmitter(const typename Sink::ResultEmitter<Ptr>::Ptr &emitter) | 248 | void ModelResult<T, Ptr>::setEmitter(const typename Sink::ResultEmitter<Ptr>::Ptr &emitter) |
239 | { | 249 | { |
250 | static async::ThreadBoundary threadBoundary; | ||
240 | setFetcher([this](const Ptr &parent) { mEmitter->fetch(parent); }); | 251 | setFetcher([this](const Ptr &parent) { mEmitter->fetch(parent); }); |
241 | emitter->onAdded([this](const Ptr &value) { this->add(value); }); | 252 | |
242 | emitter->onModified([this](const Ptr &value) { this->modify(value); }); | 253 | emitter->onAdded([this](const Ptr &value) { |
243 | emitter->onRemoved([this](const Ptr &value) { this->remove(value); }); | 254 | threadBoundary.callInMainThread([this, value]() { |
255 | add(value); | ||
256 | }); | ||
257 | }); | ||
258 | emitter->onModified([this](const Ptr &value) { | ||
259 | threadBoundary.callInMainThread([this, value]() { | ||
260 | modify(value); | ||
261 | }); | ||
262 | }); | ||
263 | emitter->onRemoved([this](const Ptr &value) { | ||
264 | threadBoundary.callInMainThread([this, value]() { | ||
265 | remove(value); | ||
266 | }); | ||
267 | }); | ||
244 | emitter->onInitialResultSetComplete([this](const Ptr &parent) { | 268 | emitter->onInitialResultSetComplete([this](const Ptr &parent) { |
269 | Trace() << "Initial result set complete"; | ||
245 | const qint64 parentId = parent ? qHash(*parent) : 0; | 270 | const qint64 parentId = parent ? qHash(*parent) : 0; |
246 | const auto parentIndex = createIndexFromId(parentId); | 271 | const auto parentIndex = createIndexFromId(parentId); |
247 | mEntityChildrenFetchComplete.insert(parentId); | 272 | mEntityChildrenFetchComplete.insert(parentId); |