From 21138cfb7a4537626e11bdf084fcf9d672361059 Mon Sep 17 00:00:00 2001 From: Christian Mollekopf Date: Wed, 3 Dec 2014 20:32:29 +0100 Subject: Return QSharedPointer wrapped and heap allocated results. --- client/clientapi.h | 149 ++++++++++++++++++++++++------------------ client/test/clientapitest.cpp | 10 +-- 2 files changed, 89 insertions(+), 70 deletions(-) diff --git a/client/clientapi.h b/client/clientapi.h index 4ec90e2..44fc98b 100644 --- a/client/clientapi.h +++ b/client/clientapi.h @@ -3,6 +3,8 @@ #include #include #include +#include +#include #include namespace async { @@ -100,28 +102,45 @@ namespace Domain { class AkonadiDomainType { public: + AkonadiDomainType(const QString &resource, const QString &identifier, qint64 revision) + : mResource(resource), + mIdentifier(identifier), + mRevision(revision) + { + } + + virtual QVariant getProperty(const QString &key){ return QVariant(); } + +private: /* * Each domain object needs to store the resource, identifier, revision triple so we can link back to the storage location. */ - QString identifier; - QString resource; - qint64 revision; + QString mResource; + QString mIdentifier; + qint64 mRevision; }; class Event : public AkonadiDomainType { +public: + typedef QSharedPointer Ptr; + Event(const QString &resource, const QString &identifier, qint64 revision):AkonadiDomainType(resource, identifier, revision){}; }; -class Todo : public AkonadiDomainType { +class Todo : public AkonadiDomainType { +public: + typedef QSharedPointer Ptr; }; -class Calendar : public AkonadiDomainType { +class Calendar : public AkonadiDomainType { +public: + typedef QSharedPointer Ptr; }; -class Mail : public AkonadiDomainType { +class Mail : public AkonadiDomainType { }; -class Folder : public AkonadiDomainType { +class Folder : public AkonadiDomainType { }; /** @@ -185,7 +204,7 @@ public: virtual void create(const DomainType &domainObject) = 0; virtual void modify(const DomainType &domainObject) = 0; virtual void remove(const DomainType &domainObject) = 0; - virtual void load(const Query &query, const std::function &resultCallback) = 0; + virtual void load(const Query &query, const std::function &resultCallback) = 0; }; @@ -262,9 +281,9 @@ public: * Asynchronusly load a dataset */ template - static QSharedPointer > load(Query query) + static QSharedPointer > load(Query query) { - QSharedPointer > resultSet(new ResultProvider); + QSharedPointer > resultSet(new ResultProvider); //Execute the search in a thread. //We must guarantee that the emitter is returned before the first result is emitted. @@ -275,7 +294,7 @@ public: for(const QString &resource : query.resources) { auto facade = FacadeFactory::instance().getFacade(resource); //We have to bind an instance to the function callback. Since we use a shared pointer this keeps the result provider instance (and thus also the emitter) alive. - std::function addCallback = std::bind(&ResultProvider::add, resultSet, std::placeholders::_1); + std::function addCallback = std::bind(&ResultProvider::add, resultSet, std::placeholders::_1); facade->load(query, addCallback); } resultSet->complete(); @@ -363,58 +382,58 @@ class EventDomainAdapter : public Akonadi2::Domain::Event { * * TODO: perhaps we should also allow async access and leave the thread/non-thread decision up to the implementation? */ -template -class StoreFacadeImpl : public Akonadi2::StoreFacade { -}; - -template<> -class StoreFacadeImpl : public Akonadi2::StoreFacade { -public: - StoreFacadeImpl():StoreFacade() {}; - - void create(const Akonadi2::Domain::Event &domainObject) { - //FIXME here we would need to cast to DomainAdapter - //Do actual work - //transformFromDomainType(domainObject); - //Ideally we have an adapter - //getAdater(domainObject).buffer(); - //domainObject.key(); => The domain object needs to provide the id - //writeToDb(); - } - - void modify(const Akonadi2::Domain::Event &domainObject) { - //Do actual work - } - - void remove(const Akonadi2::Domain::Event &domainObject) { - //Do actual work - } - - class EventBuffer { - QString value; - }; - - static Akonadi2::Domain::Event transformToDomainType(const EventBuffer &buffer) { - //We may want to avoid copies here - Akonadi2::Domain::Event event; - // //Ideally we don't have to copy and can use an adaptor instead - // return DomainAdaptor - return event; - }; - - void load(const Akonadi2::Query &query, const std::function &resultCallback) { - //retrieve buffers from storage - QList queryresult; - for(const EventBuffer &buffer : queryresult) { - resultCallback(transformToDomainType(buffer)); - } - } - -private: - //Dummy implementation - class ResourceImpl {}; - ResourceImpl resource; - class DatabaseImpl {}; - DatabaseImpl mDb; -}; +// template +// class StoreFacadeImpl : public Akonadi2::StoreFacade { +// }; +// +// template<> +// class StoreFacadeImpl : public Akonadi2::StoreFacade { +// public: +// StoreFacadeImpl():StoreFacade() {}; +// +// void create(const Akonadi2::Domain::Event &domainObject) { +// //FIXME here we would need to cast to DomainAdapter +// //Do actual work +// //transformFromDomainType(domainObject); +// //Ideally we have an adapter +// //getAdater(domainObject).buffer(); +// //domainObject.key(); => The domain object needs to provide the id +// //writeToDb(); +// } +// +// void modify(const Akonadi2::Domain::Event &domainObject) { +// //Do actual work +// } +// +// void remove(const Akonadi2::Domain::Event &domainObject) { +// //Do actual work +// } +// +// class EventBuffer { +// QString value; +// }; +// +// static Akonadi2::Domain::Event transformToDomainType(const EventBuffer &buffer) { +// //We may want to avoid copies here +// Akonadi2::Domain::Event event; +// // //Ideally we don't have to copy and can use an adaptor instead +// // return DomainAdaptor +// return event; +// }; +// +// void load(const Akonadi2::Query &query, const std::function &resultCallback) { +// //retrieve buffers from storage +// QList queryresult; +// for(const EventBuffer &buffer : queryresult) { +// resultCallback(transformToDomainType(buffer)); +// } +// } +// +// private: +// //Dummy implementation +// class ResourceImpl {}; +// ResourceImpl resource; +// class DatabaseImpl {}; +// DatabaseImpl mDb; +// }; diff --git a/client/test/clientapitest.cpp b/client/test/clientapitest.cpp index 8d8f552..bff910b 100644 --- a/client/test/clientapitest.cpp +++ b/client/test/clientapitest.cpp @@ -11,7 +11,7 @@ public: virtual void create(const Akonadi2::Domain::Event &domainObject){}; virtual void modify(const Akonadi2::Domain::Event &domainObject){}; virtual void remove(const Akonadi2::Domain::Event &domainObject){}; - virtual void load(const Akonadi2::Query &query, const std::function &resultCallback) + virtual void load(const Akonadi2::Query &query, const std::function &resultCallback) { qDebug() << "load called"; for(const auto &result : results) { @@ -19,7 +19,7 @@ public: } } - QList results; + QList results; }; class ClientAPITest : public QObject @@ -30,7 +30,7 @@ private Q_SLOTS: void testLoad() { DummyResourceFacade facade; - facade.results << Akonadi2::Domain::Event(); + facade.results << QSharedPointer::create("resource", "id", 0); Akonadi2::FacadeFactory::instance().registerFacade("dummyresource", [facade](){ return new DummyResourceFacade(facade); }); @@ -39,8 +39,8 @@ private Q_SLOTS: auto result = Akonadi2::Store::load(query); - QList resultSet; - result->onAdded([&resultSet](const Akonadi2::Domain::Event &event){ resultSet << event; qDebug() << "result added";}); + QList resultSet; + result->onAdded([&resultSet](const Akonadi2::Domain::Event::Ptr &event){ resultSet << event; qDebug() << "result added";}); bool complete; result->onComplete([&complete]{ complete = true; qDebug() << "complete";}); -- cgit v1.2.3