diff options
author | Christian Mollekopf <chrigi_1@fastmail.fm> | 2015-05-31 20:16:23 +0200 |
---|---|---|
committer | Christian Mollekopf <chrigi_1@fastmail.fm> | 2015-05-31 20:16:23 +0200 |
commit | e297fb92c2c0e344d36e0aef57921f6b9b921a61 (patch) | |
tree | ab36c3a121f3f132235ed3eafb5eb5d767c77b3b | |
parent | 0815156ef93cb9f48073a777b888ed47b579d695 (diff) | |
download | sink-e297fb92c2c0e344d36e0aef57921f6b9b921a61.tar.gz sink-e297fb92c2c0e344d36e0aef57921f6b9b921a61.zip |
Moved default read/write property mapper to TypeImplementation
There is always exactly one default buffer that we can centralize
in TypeImplementation.
-rw-r--r-- | common/CMakeLists.txt | 1 | ||||
-rw-r--r-- | common/domain/applicationdomaintype.h | 9 | ||||
-rw-r--r-- | common/domain/event.cpp | 34 | ||||
-rw-r--r-- | common/domain/event.h | 19 | ||||
-rw-r--r-- | common/domainadaptor.cpp | 45 | ||||
-rw-r--r-- | common/domainadaptor.h | 69 | ||||
-rw-r--r-- | common/propertymapper.cpp | 39 | ||||
-rw-r--r-- | common/propertymapper.h | 86 | ||||
-rw-r--r-- | examples/dummyresource/facade.cpp | 4 | ||||
-rw-r--r-- | examples/dummyresource/resourcefactory.cpp | 2 | ||||
-rw-r--r-- | tests/domainadaptortest.cpp | 4 |
11 files changed, 193 insertions, 119 deletions
diff --git a/common/CMakeLists.txt b/common/CMakeLists.txt index 6a048ea..9e30752 100644 --- a/common/CMakeLists.txt +++ b/common/CMakeLists.txt | |||
@@ -17,6 +17,7 @@ set(command_SRCS | |||
17 | commands.cpp | 17 | commands.cpp |
18 | facade.cpp | 18 | facade.cpp |
19 | pipeline.cpp | 19 | pipeline.cpp |
20 | propertymapper.cpp | ||
20 | domainadaptor.cpp | 21 | domainadaptor.cpp |
21 | resource.cpp | 22 | resource.cpp |
22 | genericresource.cpp | 23 | genericresource.cpp |
diff --git a/common/domain/applicationdomaintype.h b/common/domain/applicationdomaintype.h index 2de1460..f2a4ad6 100644 --- a/common/domain/applicationdomaintype.h +++ b/common/domain/applicationdomaintype.h | |||
@@ -130,5 +130,14 @@ QByteArray getTypeName<Todo>(); | |||
130 | template<> | 130 | template<> |
131 | QByteArray getTypeName<AkonadiResource>(); | 131 | QByteArray getTypeName<AkonadiResource>(); |
132 | 132 | ||
133 | /** | ||
134 | * Type implementation. | ||
135 | * | ||
136 | * Needs to be implemented for every application domain type. | ||
137 | * Contains all non-resource specific, but type-specific code. | ||
138 | */ | ||
139 | template<typename DomainType> | ||
140 | class TypeImplementation; | ||
141 | |||
133 | } | 142 | } |
134 | } | 143 | } |
diff --git a/common/domain/event.cpp b/common/domain/event.cpp index 86100b7..ea0931c 100644 --- a/common/domain/event.cpp +++ b/common/domain/event.cpp | |||
@@ -20,15 +20,19 @@ | |||
20 | 20 | ||
21 | #include <QVector> | 21 | #include <QVector> |
22 | #include <QByteArray> | 22 | #include <QByteArray> |
23 | #include <QString> | ||
23 | 24 | ||
24 | #include "../resultset.h" | 25 | #include "../resultset.h" |
25 | #include "../index.h" | 26 | #include "../index.h" |
26 | #include "../storage.h" | 27 | #include "../storage.h" |
27 | #include "../log.h" | 28 | #include "../log.h" |
29 | #include "../propertymapper.h" | ||
30 | |||
31 | #include "event_generated.h" | ||
28 | 32 | ||
29 | using namespace Akonadi2::ApplicationDomain; | 33 | using namespace Akonadi2::ApplicationDomain; |
30 | 34 | ||
31 | ResultSet EventImplementation::queryIndexes(const Akonadi2::Query &query, const QByteArray &resourceInstanceIdentifier) | 35 | ResultSet TypeImplementation<Event>::queryIndexes(const Akonadi2::Query &query, const QByteArray &resourceInstanceIdentifier) |
32 | { | 36 | { |
33 | QVector<QByteArray> keys; | 37 | QVector<QByteArray> keys; |
34 | if (query.propertyFilter.contains("uid")) { | 38 | if (query.propertyFilter.contains("uid")) { |
@@ -43,7 +47,7 @@ ResultSet EventImplementation::queryIndexes(const Akonadi2::Query &query, const | |||
43 | return ResultSet(keys); | 47 | return ResultSet(keys); |
44 | } | 48 | } |
45 | 49 | ||
46 | void EventImplementation::index(const Event &type) | 50 | void TypeImplementation<Event>::index(const Event &type) |
47 | { | 51 | { |
48 | Index uidIndex(Akonadi2::Store::storageLocation(), type.resourceInstanceIdentifier() + "index.uid", Akonadi2::Storage::ReadWrite); | 52 | Index uidIndex(Akonadi2::Store::storageLocation(), type.resourceInstanceIdentifier() + "index.uid", Akonadi2::Storage::ReadWrite); |
49 | const auto uid = type.getProperty("uid"); | 53 | const auto uid = type.getProperty("uid"); |
@@ -51,3 +55,29 @@ void EventImplementation::index(const Event &type) | |||
51 | uidIndex.add(uid.toByteArray(), type.identifier()); | 55 | uidIndex.add(uid.toByteArray(), type.identifier()); |
52 | } | 56 | } |
53 | } | 57 | } |
58 | |||
59 | QSharedPointer<ReadPropertyMapper<Buffer::Event> > TypeImplementation<Event>::initializeReadPropertyMapper() | ||
60 | { | ||
61 | auto propertyMapper = QSharedPointer<ReadPropertyMapper<Buffer::Event> >::create(); | ||
62 | propertyMapper->addMapping("summary", [](Buffer::Event const *buffer) -> QVariant { | ||
63 | return propertyToVariant<QString>(buffer->summary()); | ||
64 | }); | ||
65 | propertyMapper->addMapping("uid", [](Buffer::Event const *buffer) -> QVariant { | ||
66 | return propertyToVariant<QString>(buffer->uid()); | ||
67 | }); | ||
68 | return propertyMapper; | ||
69 | } | ||
70 | |||
71 | QSharedPointer<WritePropertyMapper<Buffer::EventBuilder> > TypeImplementation<Event>::initializeWritePropertyMapper() | ||
72 | { | ||
73 | auto propertyMapper = QSharedPointer<WritePropertyMapper<Buffer::EventBuilder> >::create(); | ||
74 | propertyMapper->addMapping("summary", [](const QVariant &value, flatbuffers::FlatBufferBuilder &fbb) -> std::function<void(Buffer::EventBuilder &)> { | ||
75 | auto offset = variantToProperty<QString>(value, fbb); | ||
76 | return [offset](Buffer::EventBuilder &builder) { builder.add_summary(offset); }; | ||
77 | }); | ||
78 | propertyMapper->addMapping("uid", [](const QVariant &value, flatbuffers::FlatBufferBuilder &fbb) -> std::function<void(Buffer::EventBuilder &)> { | ||
79 | auto offset = variantToProperty<QString>(value, fbb); | ||
80 | return [offset](Buffer::EventBuilder &builder) { builder.add_uid(offset); }; | ||
81 | }); | ||
82 | return propertyMapper; | ||
83 | } | ||
diff --git a/common/domain/event.h b/common/domain/event.h index 4cb0d34..d4ce20e 100644 --- a/common/domain/event.h +++ b/common/domain/event.h | |||
@@ -23,23 +23,34 @@ | |||
23 | class ResultSet; | 23 | class ResultSet; |
24 | class QByteArray; | 24 | class QByteArray; |
25 | 25 | ||
26 | template<typename T> | ||
27 | class ReadPropertyMapper; | ||
28 | template<typename T> | ||
29 | class WritePropertyMapper; | ||
30 | |||
26 | namespace Akonadi2 { | 31 | namespace Akonadi2 { |
27 | class Query; | 32 | class Query; |
28 | 33 | ||
29 | namespace ApplicationDomain { | 34 | namespace ApplicationDomain { |
35 | namespace Buffer { | ||
36 | class Event; | ||
37 | class EventBuilder; | ||
38 | } | ||
30 | 39 | ||
31 | /** | 40 | /** |
32 | * Implements all type-specific code such as updating and querying indexes. | 41 | * Implements all type-specific code such as updating and querying indexes. |
33 | */ | 42 | */ |
34 | namespace EventImplementation { | 43 | template<> |
35 | typedef Event DomainType; | 44 | struct TypeImplementation<Akonadi2::ApplicationDomain::Event> { |
36 | /** | 45 | /** |
37 | * Returns the potential result set based on the indexes. | 46 | * Returns the potential result set based on the indexes. |
38 | * | 47 | * |
39 | * An empty result set indicates that a full scan is required. | 48 | * An empty result set indicates that a full scan is required. |
40 | */ | 49 | */ |
41 | ResultSet queryIndexes(const Akonadi2::Query &query, const QByteArray &resourceInstanceIdentifier); | 50 | static ResultSet queryIndexes(const Akonadi2::Query &query, const QByteArray &resourceInstanceIdentifier); |
42 | void index(const Event &type); | 51 | static void index(const Event &type); |
52 | static QSharedPointer<ReadPropertyMapper<Akonadi2::ApplicationDomain::Buffer::Event> > initializeReadPropertyMapper(); | ||
53 | static QSharedPointer<WritePropertyMapper<Akonadi2::ApplicationDomain::Buffer::EventBuilder> > initializeWritePropertyMapper(); | ||
43 | }; | 54 | }; |
44 | 55 | ||
45 | } | 56 | } |
diff --git a/common/domainadaptor.cpp b/common/domainadaptor.cpp index 8a4b5ed..2955cab 100644 --- a/common/domainadaptor.cpp +++ b/common/domainadaptor.cpp | |||
@@ -19,48 +19,3 @@ | |||
19 | 19 | ||
20 | #include "domainadaptor.h" | 20 | #include "domainadaptor.h" |
21 | 21 | ||
22 | template <> | ||
23 | flatbuffers::uoffset_t variantToProperty<QString>(const QVariant &property, flatbuffers::FlatBufferBuilder &fbb) | ||
24 | { | ||
25 | if (property.isValid()) { | ||
26 | return fbb.CreateString(property.toString().toStdString()).o; | ||
27 | } | ||
28 | return 0; | ||
29 | } | ||
30 | |||
31 | template <> | ||
32 | QVariant propertyToVariant<QString>(const flatbuffers::String *property) | ||
33 | { | ||
34 | if (property) { | ||
35 | return QString::fromStdString(property->c_str()); | ||
36 | } | ||
37 | return QVariant(); | ||
38 | } | ||
39 | |||
40 | template <> | ||
41 | QSharedPointer<ReadPropertyMapper<Akonadi2::ApplicationDomain::Buffer::Event> > initializeReadPropertyMapper<Akonadi2::ApplicationDomain::Buffer::Event>() | ||
42 | { | ||
43 | auto propertyMapper = QSharedPointer<ReadPropertyMapper<Akonadi2::ApplicationDomain::Buffer::Event> >::create(); | ||
44 | propertyMapper->addMapping("summary", [](Akonadi2::ApplicationDomain::Buffer::Event const *buffer) -> QVariant { | ||
45 | return propertyToVariant<QString>(buffer->summary()); | ||
46 | }); | ||
47 | propertyMapper->addMapping("uid", [](Akonadi2::ApplicationDomain::Buffer::Event const *buffer) -> QVariant { | ||
48 | return propertyToVariant<QString>(buffer->uid()); | ||
49 | }); | ||
50 | return propertyMapper; | ||
51 | } | ||
52 | |||
53 | template <> | ||
54 | QSharedPointer<WritePropertyMapper<Akonadi2::ApplicationDomain::Buffer::EventBuilder> > initializeWritePropertyMapper<Akonadi2::ApplicationDomain::Buffer::EventBuilder>() | ||
55 | { | ||
56 | auto propertyMapper = QSharedPointer<WritePropertyMapper<Akonadi2::ApplicationDomain::Buffer::EventBuilder> >::create(); | ||
57 | propertyMapper->addMapping("summary", [](const QVariant &value, flatbuffers::FlatBufferBuilder &fbb) -> std::function<void(Akonadi2::ApplicationDomain::Buffer::EventBuilder &)> { | ||
58 | auto offset = variantToProperty<QString>(value, fbb); | ||
59 | return [offset](Akonadi2::ApplicationDomain::Buffer::EventBuilder &builder) { builder.add_summary(offset); }; | ||
60 | }); | ||
61 | propertyMapper->addMapping("uid", [](const QVariant &value, flatbuffers::FlatBufferBuilder &fbb) -> std::function<void(Akonadi2::ApplicationDomain::Buffer::EventBuilder &)> { | ||
62 | auto offset = variantToProperty<QString>(value, fbb); | ||
63 | return [offset](Akonadi2::ApplicationDomain::Buffer::EventBuilder &builder) { builder.add_uid(offset); }; | ||
64 | }); | ||
65 | return propertyMapper; | ||
66 | } | ||
diff --git a/common/domainadaptor.h b/common/domainadaptor.h index fd015b5..70c3a42 100644 --- a/common/domainadaptor.h +++ b/common/domainadaptor.h | |||
@@ -29,65 +29,8 @@ | |||
29 | #include "entity_generated.h" | 29 | #include "entity_generated.h" |
30 | #include "metadata_generated.h" | 30 | #include "metadata_generated.h" |
31 | #include "entitybuffer.h" | 31 | #include "entitybuffer.h" |
32 | 32 | #include "propertymapper.h" | |
33 | /** | 33 | #include "domain/event.h" |
34 | * The property mapper is a non-typesafe virtual dispatch. | ||
35 | * | ||
36 | * Instead of using an interface and requring each implementation to override | ||
37 | * a virtual method per property, the property mapper can be filled with accessors | ||
38 | * that extract the properties from resource types. | ||
39 | */ | ||
40 | template<typename BufferType> | ||
41 | class ReadPropertyMapper | ||
42 | { | ||
43 | public: | ||
44 | virtual QVariant getProperty(const QByteArray &key, BufferType const *buffer) const | ||
45 | { | ||
46 | if (mReadAccessors.contains(key)) { | ||
47 | auto accessor = mReadAccessors.value(key); | ||
48 | return accessor(buffer); | ||
49 | } | ||
50 | return QVariant(); | ||
51 | } | ||
52 | bool hasMapping(const QByteArray &key) const { return mReadAccessors.contains(key); } | ||
53 | QList<QByteArray> availableProperties() const { return mReadAccessors.keys(); } | ||
54 | void addMapping(const QByteArray &property, const std::function<QVariant(BufferType const *)> &mapping) { | ||
55 | mReadAccessors.insert(property, mapping); | ||
56 | } | ||
57 | private: | ||
58 | QHash<QByteArray, std::function<QVariant(BufferType const *)> > mReadAccessors; | ||
59 | }; | ||
60 | |||
61 | template<typename BufferBuilder> | ||
62 | class WritePropertyMapper | ||
63 | { | ||
64 | public: | ||
65 | virtual void setProperty(const QByteArray &key, const QVariant &value, QList<std::function<void(BufferBuilder &)> > &builderCalls, flatbuffers::FlatBufferBuilder &fbb) const | ||
66 | { | ||
67 | if (mWriteAccessors.contains(key)) { | ||
68 | auto accessor = mWriteAccessors.value(key); | ||
69 | builderCalls << accessor(value, fbb); | ||
70 | } | ||
71 | } | ||
72 | bool hasMapping(const QByteArray &key) const { return mWriteAccessors.contains(key); } | ||
73 | void addMapping(const QByteArray &property, const std::function<std::function<void(BufferBuilder &)>(const QVariant &, flatbuffers::FlatBufferBuilder &)> &mapping) { | ||
74 | mWriteAccessors.insert(property, mapping); | ||
75 | } | ||
76 | private: | ||
77 | QHash<QByteArray, std::function<std::function<void(BufferBuilder &)>(const QVariant &, flatbuffers::FlatBufferBuilder &)> > mWriteAccessors; | ||
78 | }; | ||
79 | |||
80 | /** | ||
81 | * Defines how to convert qt primitives to flatbuffer ones | ||
82 | */ | ||
83 | template <class T> | ||
84 | flatbuffers::uoffset_t variantToProperty(const QVariant &, flatbuffers::FlatBufferBuilder &fbb); | ||
85 | |||
86 | /** | ||
87 | * Defines how to convert flatbuffer primitives to qt ones | ||
88 | */ | ||
89 | template <typename T> | ||
90 | QVariant propertyToVariant(const flatbuffers::String *); | ||
91 | 34 | ||
92 | /** | 35 | /** |
93 | * Create a buffer from a domain object using the provided mappings | 36 | * Create a buffer from a domain object using the provided mappings |
@@ -177,7 +120,7 @@ class DomainTypeAdaptorFactoryInterface | |||
177 | public: | 120 | public: |
178 | virtual ~DomainTypeAdaptorFactoryInterface() {}; | 121 | virtual ~DomainTypeAdaptorFactoryInterface() {}; |
179 | virtual QSharedPointer<Akonadi2::ApplicationDomain::BufferAdaptor> createAdaptor(const Akonadi2::Entity &entity) = 0; | 122 | virtual QSharedPointer<Akonadi2::ApplicationDomain::BufferAdaptor> createAdaptor(const Akonadi2::Entity &entity) = 0; |
180 | virtual void createBuffer(const DomainType &event, flatbuffers::FlatBufferBuilder &fbb) = 0; | 123 | virtual void createBuffer(const DomainType &domainType, flatbuffers::FlatBufferBuilder &fbb) = 0; |
181 | }; | 124 | }; |
182 | 125 | ||
183 | /** | 126 | /** |
@@ -190,9 +133,9 @@ class DomainTypeAdaptorFactory : public DomainTypeAdaptorFactoryInterface<Domain | |||
190 | { | 133 | { |
191 | public: | 134 | public: |
192 | DomainTypeAdaptorFactory() : | 135 | DomainTypeAdaptorFactory() : |
193 | mLocalMapper(initializeReadPropertyMapper<LocalBuffer>()), | 136 | mLocalMapper(Akonadi2::ApplicationDomain::TypeImplementation<DomainType>::initializeReadPropertyMapper()), |
194 | mResourceMapper(QSharedPointer<ReadPropertyMapper<ResourceBuffer> >::create()), | 137 | mResourceMapper(QSharedPointer<ReadPropertyMapper<ResourceBuffer> >::create()), |
195 | mLocalWriteMapper(initializeWritePropertyMapper<LocalBuilder>()), | 138 | mLocalWriteMapper(Akonadi2::ApplicationDomain::TypeImplementation<DomainType>::initializeWritePropertyMapper()), |
196 | mResourceWriteMapper(QSharedPointer<WritePropertyMapper<ResourceBuilder> >::create()) | 139 | mResourceWriteMapper(QSharedPointer<WritePropertyMapper<ResourceBuilder> >::create()) |
197 | {}; | 140 | {}; |
198 | virtual ~DomainTypeAdaptorFactory() {}; | 141 | virtual ~DomainTypeAdaptorFactory() {}; |
@@ -216,7 +159,7 @@ public: | |||
216 | return adaptor; | 159 | return adaptor; |
217 | } | 160 | } |
218 | 161 | ||
219 | virtual void createBuffer(const Akonadi2::ApplicationDomain::Event &event, flatbuffers::FlatBufferBuilder &fbb) Q_DECL_OVERRIDE {}; | 162 | virtual void createBuffer(const DomainType &domainType, flatbuffers::FlatBufferBuilder &fbb) Q_DECL_OVERRIDE {}; |
220 | 163 | ||
221 | protected: | 164 | protected: |
222 | QSharedPointer<ReadPropertyMapper<LocalBuffer> > mLocalMapper; | 165 | QSharedPointer<ReadPropertyMapper<LocalBuffer> > mLocalMapper; |
diff --git a/common/propertymapper.cpp b/common/propertymapper.cpp new file mode 100644 index 0000000..0896aa6 --- /dev/null +++ b/common/propertymapper.cpp | |||
@@ -0,0 +1,39 @@ | |||
1 | /* | ||
2 | * Copyright (C) 2015 Christian Mollekopf <chrigi_1@fastmail.fm> | ||
3 | * | ||
4 | * This program is free software; you can redistribute it and/or modify | ||
5 | * it under the terms of the GNU General Public License as published by | ||
6 | * the Free Software Foundation; either version 2 of the License, or | ||
7 | * (at your option) any later version. | ||
8 | * | ||
9 | * This program is distributed in the hope that it will be useful, | ||
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
12 | * GNU General Public License for more details. | ||
13 | * | ||
14 | * You should have received a copy of the GNU General Public License | ||
15 | * along with this program; if not, write to the | ||
16 | * Free Software Foundation, Inc., | ||
17 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | ||
18 | */ | ||
19 | |||
20 | #include "propertymapper.h" | ||
21 | |||
22 | template <> | ||
23 | flatbuffers::uoffset_t variantToProperty<QString>(const QVariant &property, flatbuffers::FlatBufferBuilder &fbb) | ||
24 | { | ||
25 | if (property.isValid()) { | ||
26 | return fbb.CreateString(property.toString().toStdString()).o; | ||
27 | } | ||
28 | return 0; | ||
29 | } | ||
30 | |||
31 | template <> | ||
32 | QVariant propertyToVariant<QString>(const flatbuffers::String *property) | ||
33 | { | ||
34 | if (property) { | ||
35 | return QString::fromStdString(property->c_str()); | ||
36 | } | ||
37 | return QVariant(); | ||
38 | } | ||
39 | |||
diff --git a/common/propertymapper.h b/common/propertymapper.h new file mode 100644 index 0000000..0c6c16f --- /dev/null +++ b/common/propertymapper.h | |||
@@ -0,0 +1,86 @@ | |||
1 | /* | ||
2 | * Copyright (C) 2014 Christian Mollekopf <chrigi_1@fastmail.fm> | ||
3 | * | ||
4 | * This program is free software; you can redistribute it and/or modify | ||
5 | * it under the terms of the GNU General Public License as published by | ||
6 | * the Free Software Foundation; either version 2 of the License, or | ||
7 | * (at your option) any later version. | ||
8 | * | ||
9 | * This program is distributed in the hope that it will be useful, | ||
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
12 | * GNU General Public License for more details. | ||
13 | * | ||
14 | * You should have received a copy of the GNU General Public License | ||
15 | * along with this program; if not, write to the | ||
16 | * Free Software Foundation, Inc., | ||
17 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | ||
18 | */ | ||
19 | |||
20 | #pragma once | ||
21 | |||
22 | #include <QVariant> | ||
23 | #include <QByteArray> | ||
24 | #include <functional> | ||
25 | #include <flatbuffers/flatbuffers.h> | ||
26 | |||
27 | /** | ||
28 | * Defines how to convert qt primitives to flatbuffer ones | ||
29 | */ | ||
30 | template <class T> | ||
31 | flatbuffers::uoffset_t variantToProperty(const QVariant &, flatbuffers::FlatBufferBuilder &fbb); | ||
32 | |||
33 | /** | ||
34 | * Defines how to convert flatbuffer primitives to qt ones | ||
35 | */ | ||
36 | template <typename T> | ||
37 | QVariant propertyToVariant(const flatbuffers::String *); | ||
38 | |||
39 | |||
40 | /** | ||
41 | * The property mapper is a non-typesafe virtual dispatch. | ||
42 | * | ||
43 | * Instead of using an interface and requring each implementation to override | ||
44 | * a virtual method per property, the property mapper can be filled with accessors | ||
45 | * that extract the properties from resource types. | ||
46 | */ | ||
47 | template<typename BufferType> | ||
48 | class ReadPropertyMapper | ||
49 | { | ||
50 | public: | ||
51 | virtual QVariant getProperty(const QByteArray &key, BufferType const *buffer) const | ||
52 | { | ||
53 | if (mReadAccessors.contains(key)) { | ||
54 | auto accessor = mReadAccessors.value(key); | ||
55 | return accessor(buffer); | ||
56 | } | ||
57 | return QVariant(); | ||
58 | } | ||
59 | bool hasMapping(const QByteArray &key) const { return mReadAccessors.contains(key); } | ||
60 | QList<QByteArray> availableProperties() const { return mReadAccessors.keys(); } | ||
61 | void addMapping(const QByteArray &property, const std::function<QVariant(BufferType const *)> &mapping) { | ||
62 | mReadAccessors.insert(property, mapping); | ||
63 | } | ||
64 | private: | ||
65 | QHash<QByteArray, std::function<QVariant(BufferType const *)> > mReadAccessors; | ||
66 | }; | ||
67 | |||
68 | template<typename BufferBuilder> | ||
69 | class WritePropertyMapper | ||
70 | { | ||
71 | public: | ||
72 | virtual void setProperty(const QByteArray &key, const QVariant &value, QList<std::function<void(BufferBuilder &)> > &builderCalls, flatbuffers::FlatBufferBuilder &fbb) const | ||
73 | { | ||
74 | if (mWriteAccessors.contains(key)) { | ||
75 | auto accessor = mWriteAccessors.value(key); | ||
76 | builderCalls << accessor(value, fbb); | ||
77 | } | ||
78 | } | ||
79 | bool hasMapping(const QByteArray &key) const { return mWriteAccessors.contains(key); } | ||
80 | void addMapping(const QByteArray &property, const std::function<std::function<void(BufferBuilder &)>(const QVariant &, flatbuffers::FlatBufferBuilder &)> &mapping) { | ||
81 | mWriteAccessors.insert(property, mapping); | ||
82 | } | ||
83 | private: | ||
84 | QHash<QByteArray, std::function<std::function<void(BufferBuilder &)>(const QVariant &, flatbuffers::FlatBufferBuilder &)> > mWriteAccessors; | ||
85 | }; | ||
86 | |||
diff --git a/examples/dummyresource/facade.cpp b/examples/dummyresource/facade.cpp index 611217f..e678214 100644 --- a/examples/dummyresource/facade.cpp +++ b/examples/dummyresource/facade.cpp | |||
@@ -130,11 +130,11 @@ void DummyResourceFacade::readValue(const QSharedPointer<Akonadi2::Storage> &sto | |||
130 | 130 | ||
131 | static ResultSet getResultSet(const Akonadi2::Query &query, const QSharedPointer<Akonadi2::Storage> &storage) | 131 | static ResultSet getResultSet(const Akonadi2::Query &query, const QSharedPointer<Akonadi2::Storage> &storage) |
132 | { | 132 | { |
133 | auto resultSet = Akonadi2::ApplicationDomain::EventImplementation::queryIndexes(query, "org.kde.dummy"); | 133 | auto resultSet = Akonadi2::ApplicationDomain::TypeImplementation<Akonadi2::ApplicationDomain::Event>::queryIndexes(query, "org.kde.dummy"); |
134 | 134 | ||
135 | //Scan for where we don't have an index | 135 | //Scan for where we don't have an index |
136 | //TODO: we may want a way for queryIndexes to indicate that the resultSet is not final, and that a scan over the remaining set is required | 136 | //TODO: we may want a way for queryIndexes to indicate that the resultSet is not final, and that a scan over the remaining set is required |
137 | //TODO: the prepared query should be generalized in EventImplementation on top of domain adaptors | 137 | //TODO: the prepared query should be generalized in TypeImplementation on top of domain adaptors |
138 | if (resultSet.isEmpty()) { | 138 | if (resultSet.isEmpty()) { |
139 | QVector<QByteArray> keys; | 139 | QVector<QByteArray> keys; |
140 | const auto preparedQuery = prepareQuery(query); | 140 | const auto preparedQuery = prepareQuery(query); |
diff --git a/examples/dummyresource/resourcefactory.cpp b/examples/dummyresource/resourcefactory.cpp index 71a4ac5..31ec972 100644 --- a/examples/dummyresource/resourcefactory.cpp +++ b/examples/dummyresource/resourcefactory.cpp | |||
@@ -122,7 +122,7 @@ void DummyResource::configurePipeline(Akonadi2::Pipeline *pipeline) | |||
122 | auto adaptor = eventFactory->createAdaptor(entity); | 122 | auto adaptor = eventFactory->createAdaptor(entity); |
123 | //FIXME set revision? | 123 | //FIXME set revision? |
124 | Akonadi2::ApplicationDomain::Event event(resourceIdentifier, state.key(), -1, adaptor); | 124 | Akonadi2::ApplicationDomain::Event event(resourceIdentifier, state.key(), -1, adaptor); |
125 | Akonadi2::ApplicationDomain::EventImplementation::index(event); | 125 | Akonadi2::ApplicationDomain::TypeImplementation<Akonadi2::ApplicationDomain::Event>::index(event); |
126 | }); | 126 | }); |
127 | 127 | ||
128 | //event is the entitytype and not the domain type | 128 | //event is the entitytype and not the domain type |
diff --git a/tests/domainadaptortest.cpp b/tests/domainadaptortest.cpp index 1e285dc..97a78c2 100644 --- a/tests/domainadaptortest.cpp +++ b/tests/domainadaptortest.cpp | |||
@@ -18,7 +18,7 @@ class TestFactory : public DomainTypeAdaptorFactory<Akonadi2::ApplicationDomain: | |||
18 | public: | 18 | public: |
19 | TestFactory() | 19 | TestFactory() |
20 | { | 20 | { |
21 | mResourceWriteMapper = initializeWritePropertyMapper<Akonadi2::ApplicationDomain::Buffer::EventBuilder>(); | 21 | mResourceWriteMapper = Akonadi2::ApplicationDomain::TypeImplementation<Akonadi2::ApplicationDomain::Event>::initializeWritePropertyMapper(); |
22 | } | 22 | } |
23 | }; | 23 | }; |
24 | 24 | ||
@@ -36,7 +36,7 @@ private Q_SLOTS: | |||
36 | 36 | ||
37 | void testCreateBufferPart() | 37 | void testCreateBufferPart() |
38 | { | 38 | { |
39 | auto writeMapper = initializeWritePropertyMapper<Akonadi2::ApplicationDomain::Buffer::EventBuilder>(); | 39 | auto writeMapper = Akonadi2::ApplicationDomain::TypeImplementation<Akonadi2::ApplicationDomain::Event>::initializeWritePropertyMapper(); |
40 | 40 | ||
41 | Akonadi2::ApplicationDomain::Event event; | 41 | Akonadi2::ApplicationDomain::Event event; |
42 | event.setProperty("summary", "foo"); | 42 | event.setProperty("summary", "foo"); |