summaryrefslogtreecommitdiffstats
path: root/common/clientapi.h
diff options
context:
space:
mode:
authorChristian Mollekopf <chrigi_1@fastmail.fm>2015-11-19 23:23:56 +0100
committerChristian Mollekopf <chrigi_1@fastmail.fm>2015-11-19 23:23:56 +0100
commit94a2cd6ec21bf0466a9a50d6e4a0a956ed47bc82 (patch)
tree8f05e8ed03691b006b1f2bf8f08bc21e582aad26 /common/clientapi.h
parentc4a6746e4420b580fe35cc89783de4dbc3205ac6 (diff)
downloadsink-94a2cd6ec21bf0466a9a50d6e4a0a956ed47bc82.tar.gz
sink-94a2cd6ec21bf0466a9a50d6e4a0a956ed47bc82.zip
Move implementations to the cpp file.
I finally figured out how to do that with cpp files. It requires instantiating the code with all expected classes, but that's not a big problem since we know all types. This will hopefully greatly reduce the compiletimes...
Diffstat (limited to 'common/clientapi.h')
-rw-r--r--common/clientapi.h109
1 files changed, 7 insertions, 102 deletions
diff --git a/common/clientapi.h b/common/clientapi.h
index 6b11ad5..c48c6e9 100644
--- a/common/clientapi.h
+++ b/common/clientapi.h
@@ -22,21 +22,17 @@
22 22
23#include <QString> 23#include <QString>
24#include <QSharedPointer> 24#include <QSharedPointer>
25#include <QEventLoop>
26#include <QAbstractItemModel>
27#include <functional>
28#include <memory>
29 25
30#include <Async/Async> 26#include <Async/Async>
31 27
32#include "query.h" 28#include "query.h"
33#include "resultprovider.h" 29#include "resultprovider.h"
34#include "applicationdomaintype.h" 30#include "applicationdomaintype.h"
35#include "facadefactory.h"
36#include "log.h"
37 31
38Q_DECLARE_METATYPE(std::shared_ptr<void>); 32Q_DECLARE_METATYPE(std::shared_ptr<void>);
39 33
34class QAbstractItemModel;
35
40namespace async { 36namespace async {
41 //This should abstract if we execute from eventloop or in thread. 37 //This should abstract if we execute from eventloop or in thread.
42 //It supposed to allow the caller to finish the current method before executing the runner. 38 //It supposed to allow the caller to finish the current method before executing the runner.
@@ -62,43 +58,7 @@ public:
62 * Asynchronusly load a dataset 58 * Asynchronusly load a dataset
63 */ 59 */
64 template <class DomainType> 60 template <class DomainType>
65 static QSharedPointer<ResultEmitter<typename DomainType::Ptr> > load(Query query) 61 static QSharedPointer<ResultEmitter<typename DomainType::Ptr> > load(Query query);
66 {
67 auto resultSet = QSharedPointer<ResultProvider<typename DomainType::Ptr> >::create();
68
69 //Execute the search in a thread.
70 //We must guarantee that the emitter is returned before the first result is emitted.
71 //The result provider must be threadsafe.
72 async::run([query, resultSet](){
73 QEventLoop eventLoop;
74 resultSet->onDone([&eventLoop](){
75 eventLoop.quit();
76 });
77 // Query all resources and aggregate results
78 KAsync::iterate(getResources(query.resources, ApplicationDomain::getTypeName<DomainType>()))
79 .template each<void, QByteArray>([query, resultSet](const QByteArray &resource, KAsync::Future<void> &future) {
80 if (auto facade = FacadeFactory::instance().getFacade<DomainType>(resourceName(resource), resource)) {
81 facade->load(query, *resultSet).template then<void>([&future](){future.setFinished();}).exec();
82 //Keep the facade alive for the lifetime of the resultSet.
83 resultSet->setFacade(facade);
84 } else {
85 //Ignore the error and carry on
86 future.setFinished();
87 }
88 }).template then<void>([query, resultSet]() {
89 resultSet->initialResultSetComplete();
90 if (!query.liveQuery) {
91 resultSet->complete();
92 }
93 }).exec();
94
95 //Keep the thread alive until the result is ready
96 if (!resultSet->isDone()) {
97 eventLoop.exec();
98 }
99 });
100 return resultSet->emitter();
101 }
102 62
103 enum Roles { 63 enum Roles {
104 DomainObjectRole = Qt::UserRole + 1 //Must be the same as in ModelResult 64 DomainObjectRole = Qt::UserRole + 1 //Must be the same as in ModelResult
@@ -108,56 +68,13 @@ public:
108 * Asynchronusly load a dataset with tree structure information 68 * Asynchronusly load a dataset with tree structure information
109 */ 69 */
110 template <class DomainType> 70 template <class DomainType>
111 static QSharedPointer<QAbstractItemModel> loadModel(Query query) 71 static QSharedPointer<QAbstractItemModel> loadModel(Query query);
112 {
113 auto model = QSharedPointer<ModelResult<DomainType, typename DomainType::Ptr> >::create(query, query.requestedProperties.toList());
114 auto resultProvider = std::make_shared<ModelResultProvider<DomainType, typename DomainType::Ptr> >(model);
115 //Keep the resultprovider alive for as long as the model lives
116 model->setProperty("resultProvider", QVariant::fromValue(std::shared_ptr<void>(resultProvider)));
117
118 // Query all resources and aggregate results
119 KAsync::iterate(getResources(query.resources, ApplicationDomain::getTypeName<DomainType>()))
120 .template each<void, QByteArray>([query, resultProvider](const QByteArray &resource, KAsync::Future<void> &future) {
121 auto facade = FacadeFactory::instance().getFacade<DomainType>(resourceName(resource), resource);
122 if (facade) {
123 facade->load(query, *resultProvider).template then<void>([&future](){future.setFinished();}).exec();
124 //Keep the facade alive for the lifetime of the resultSet.
125 //FIXME this would have to become a list
126 resultProvider->setFacade(facade);
127 } else {
128 //Ignore the error and carry on
129 future.setFinished();
130 }
131 }).template then<void>([query, resultProvider]() {
132 resultProvider->initialResultSetComplete();
133 if (!query.liveQuery) {
134 resultProvider->complete();
135 }
136 }).exec();
137
138 return model;
139 }
140
141 template <class DomainType>
142 static std::shared_ptr<StoreFacade<DomainType> > getFacade(const QByteArray &resourceInstanceIdentifier)
143 {
144 if (auto facade = FacadeFactory::instance().getFacade<DomainType>(resourceName(resourceInstanceIdentifier), resourceInstanceIdentifier)) {
145 return facade;
146 }
147 return std::make_shared<NullFacade<DomainType> >();
148 }
149 72
150 /** 73 /**
151 * Create a new entity. 74 * Create a new entity.
152 */ 75 */
153 template <class DomainType> 76 template <class DomainType>
154 static KAsync::Job<void> create(const DomainType &domainObject) { 77 static KAsync::Job<void> create(const DomainType &domainObject);
155 //Potentially move to separate thread as well
156 auto facade = getFacade<DomainType>(domainObject.resourceInstanceIdentifier());
157 return facade->create(domainObject).template then<void>([facade](){}, [](int errorCode, const QString &error) {
158 Warning() << "Failed to create";
159 });
160 }
161 78
162 /** 79 /**
163 * Modify an entity. 80 * Modify an entity.
@@ -165,25 +82,13 @@ public:
165 * This includes moving etc. since these are also simple settings on a property. 82 * This includes moving etc. since these are also simple settings on a property.
166 */ 83 */
167 template <class DomainType> 84 template <class DomainType>
168 static KAsync::Job<void> modify(const DomainType &domainObject) { 85 static KAsync::Job<void> modify(const DomainType &domainObject);
169 //Potentially move to separate thread as well
170 auto facade = getFacade<DomainType>(domainObject.resourceInstanceIdentifier());
171 return facade->modify(domainObject).template then<void>([facade](){}, [](int errorCode, const QString &error) {
172 Warning() << "Failed to modify";
173 });
174 }
175 86
176 /** 87 /**
177 * Remove an entity. 88 * Remove an entity.
178 */ 89 */
179 template <class DomainType> 90 template <class DomainType>
180 static KAsync::Job<void> remove(const DomainType &domainObject) { 91 static KAsync::Job<void> remove(const DomainType &domainObject);
181 //Potentially move to separate thread as well
182 auto facade = getFacade<DomainType>(domainObject.resourceInstanceIdentifier());
183 return facade->remove(domainObject).template then<void>([facade](){}, [](int errorCode, const QString &error) {
184 Warning() << "Failed to remove";
185 });
186 }
187 92
188 /** 93 /**
189 * Shutdown resource. 94 * Shutdown resource.