summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorChristian Mollekopf <chrigi_1@fastmail.fm>2015-04-09 18:16:39 +0200
committerChristian Mollekopf <chrigi_1@fastmail.fm>2015-04-09 18:16:39 +0200
commit4f1afa39bec38ea38820115e1bbabf4506c2d45c (patch)
treefbcf157e01151a19fe3f35539f62aeeef9fd6c44
parentcc3fed3668a80616ec78bc872e21b7ac06bcde38 (diff)
downloadsink-4f1afa39bec38ea38820115e1bbabf4506c2d45c.tar.gz
sink-4f1afa39bec38ea38820115e1bbabf4506c2d45c.zip
Moved generic parts of the domain adaptor to common
-rw-r--r--common/CMakeLists.txt1
-rw-r--r--common/domainadaptor.cpp40
-rw-r--r--common/domainadaptor.h102
-rw-r--r--dummyresource/domainadaptor.cpp74
-rw-r--r--dummyresource/domainadaptor.h1
5 files changed, 132 insertions, 86 deletions
diff --git a/common/CMakeLists.txt b/common/CMakeLists.txt
index 650691c..b06718f 100644
--- a/common/CMakeLists.txt
+++ b/common/CMakeLists.txt
@@ -30,6 +30,7 @@ set(command_SRCS
30 commands.cpp 30 commands.cpp
31 console.cpp 31 console.cpp
32 pipeline.cpp 32 pipeline.cpp
33 domainadaptor.cpp
33 resource.cpp 34 resource.cpp
34 resourceaccess.cpp 35 resourceaccess.cpp
35 storage_common.cpp 36 storage_common.cpp
diff --git a/common/domainadaptor.cpp b/common/domainadaptor.cpp
new file mode 100644
index 0000000..5b7c427
--- /dev/null
+++ b/common/domainadaptor.cpp
@@ -0,0 +1,40 @@
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 "domainadaptor.h"
21
22template <>
23QSharedPointer<PropertyMapper<Akonadi2::ApplicationDomain::Buffer::Event> > initializePropertyMapper<Akonadi2::ApplicationDomain::Buffer::Event>()
24{
25 auto propertyMapper = QSharedPointer<PropertyMapper<Akonadi2::ApplicationDomain::Buffer::Event> >::create();
26 propertyMapper->mReadAccessors.insert("summary", [](Akonadi2::ApplicationDomain::Buffer::Event const *buffer) -> QVariant {
27 if (buffer->summary()) {
28 return QString::fromStdString(buffer->summary()->c_str());
29 }
30 return QVariant();
31 });
32 propertyMapper->mReadAccessors.insert("uid", [](Akonadi2::ApplicationDomain::Buffer::Event const *buffer) -> QVariant {
33 if (buffer->uid()) {
34 return QString::fromStdString(buffer->uid()->c_str());
35 }
36 return QVariant();
37 });
38 return propertyMapper;
39}
40
diff --git a/common/domainadaptor.h b/common/domainadaptor.h
index df3f8e5..15e3067 100644
--- a/common/domainadaptor.h
+++ b/common/domainadaptor.h
@@ -25,11 +25,17 @@
25#include <functional> 25#include <functional>
26#include "clientapi.h" //for domain parts 26#include "clientapi.h" //for domain parts
27 27
28#include "event_generated.h"
29#include "entity_generated.h"
30#include "metadata_generated.h"
31#include "entitybuffer.h"
32
28/** 33/**
29 * The property mapper holds accessor functions for all properties. 34 * The property mapper is a non-typesafe virtual dispatch.
30 * 35 *
31 * It is by default initialized with accessors that access the local-only buffer, 36 * Instead of using an interface and requring each implementation to override
32 * and resource simply have to overwrite those accessors. 37 * a virtual method per property, the property mapper can be filled with accessors
38 * that extract the properties from resource types.
33 */ 39 */
34template<typename BufferType> 40template<typename BufferType>
35class PropertyMapper 41class PropertyMapper
@@ -55,20 +61,92 @@ public:
55 QHash<QByteArray, std::function<void(const QVariant &, BufferType*)> > mWriteAccessors; 61 QHash<QByteArray, std::function<void(const QVariant &, BufferType*)> > mWriteAccessors;
56}; 62};
57 63
58//The factory should define how to go from an entitybuffer (local + resource buffer), to a domain type adapter. 64/**
59//It defines how values are split accross local and resource buffer. 65 * A generic adaptor implementation that uses a property mapper to read/write values.
60//This is required by the facade the read the value, and by the pipeline preprocessors to access the domain values in a generic way. 66 */
61// template<typename DomainType, typename LocalBuffer, typename ResourceBuffer> 67template <class LocalBuffer, class ResourceBuffer>
62// class DomainTypeAdaptorFactory 68class GenericBufferAdaptor : public Akonadi2::ApplicationDomain::BufferAdaptor
63// { 69{
64// }; 70public:
71 GenericBufferAdaptor()
72 : BufferAdaptor()
73 {
74
75 }
76
77 void setProperty(const QByteArray &key, const QVariant &value)
78 {
79 if (mResourceMapper && mResourceMapper->mWriteAccessors.contains(key)) {
80 // mResourceMapper->setProperty(key, value, mResourceBuffer);
81 } else {
82 // mLocalMapper.;
83 }
84 }
85
86 virtual QVariant getProperty(const QByteArray &key) const
87 {
88 if (mResourceBuffer && mResourceMapper->mReadAccessors.contains(key)) {
89 return mResourceMapper->getProperty(key, mResourceBuffer);
90 } else if (mLocalBuffer && mLocalMapper->mReadAccessors.contains(key)) {
91 return mLocalMapper->getProperty(key, mLocalBuffer);
92 }
93 qWarning() << "no mapping available for key " << key;
94 return QVariant();
95 }
96
97 virtual QList<QByteArray> availableProperties() const
98 {
99 QList<QByteArray> props;
100 props << mResourceMapper->mReadAccessors.keys();
101 props << mLocalMapper->mReadAccessors.keys();
102 return props;
103 }
104
105 LocalBuffer const *mLocalBuffer;
106 ResourceBuffer const *mResourceBuffer;
107 QSharedPointer<PropertyMapper<LocalBuffer> > mLocalMapper;
108 QSharedPointer<PropertyMapper<ResourceBuffer> > mResourceMapper;
109};
110
111/**
112 * Initializes the local property mapper.
113 *
114 * Provide an implementation for each application domain type.
115 */
116template <class T>
117QSharedPointer<PropertyMapper<T> > initializePropertyMapper();
65 118
119/**
120 * The factory should define how to go from an entitybuffer (local + resource buffer), to a domain type adapter.
121 * It defines how values are split accross local and resource buffer.
122 * This is required by the facade the read the value, and by the pipeline preprocessors to access the domain values in a generic way.
123 */
66template<typename DomainType, typename LocalBuffer, typename ResourceBuffer> 124template<typename DomainType, typename LocalBuffer, typename ResourceBuffer>
67class DomainTypeAdaptorFactory/* <typename DomainType, LocalBuffer, ResourceBuffer> */ 125class DomainTypeAdaptorFactory
68{ 126{
69public: 127public:
128 DomainTypeAdaptorFactory() : mLocalMapper(initializePropertyMapper<LocalBuffer>()) {};
70 virtual ~DomainTypeAdaptorFactory() {}; 129 virtual ~DomainTypeAdaptorFactory() {};
71 virtual QSharedPointer<Akonadi2::ApplicationDomain::BufferAdaptor> createAdaptor(const Akonadi2::Entity &entity) = 0; 130
131 /**
132 * Creates an adaptor for the given domain and resource types.
133 *
134 * This returns by default a GenericBufferAdaptor initialized with the corresponding property mappers.
135 */
136 virtual QSharedPointer<Akonadi2::ApplicationDomain::BufferAdaptor> createAdaptor(const Akonadi2::Entity &entity)
137 {
138 const auto resourceBuffer = Akonadi2::EntityBuffer::readBuffer<ResourceBuffer>(entity.resource());
139 const auto localBuffer = Akonadi2::EntityBuffer::readBuffer<LocalBuffer>(entity.local());
140 // const auto metadataBuffer = Akonadi2::EntityBuffer::readBuffer<Akonadi2::Metadata>(entity.metadata());
141
142 auto adaptor = QSharedPointer<GenericBufferAdaptor<LocalBuffer, ResourceBuffer> >::create();
143 adaptor->mLocalBuffer = localBuffer;
144 adaptor->mLocalMapper = mLocalMapper;
145 adaptor->mResourceBuffer = resourceBuffer;
146 adaptor->mResourceMapper = mResourceMapper;
147 return adaptor;
148 }
149
72 virtual void createBuffer(const Akonadi2::ApplicationDomain::Event &event, flatbuffers::FlatBufferBuilder &fbb) {}; 150 virtual void createBuffer(const Akonadi2::ApplicationDomain::Event &event, flatbuffers::FlatBufferBuilder &fbb) {};
73 151
74protected: 152protected:
diff --git a/dummyresource/domainadaptor.cpp b/dummyresource/domainadaptor.cpp
index b133614..74a8dd6 100644
--- a/dummyresource/domainadaptor.cpp
+++ b/dummyresource/domainadaptor.cpp
@@ -14,55 +14,11 @@
14using namespace DummyCalendar; 14using namespace DummyCalendar;
15using namespace flatbuffers; 15using namespace flatbuffers;
16 16
17//This will become a generic implementation that simply takes the resource buffer and local buffer pointer
18class DummyEventAdaptor : public Akonadi2::ApplicationDomain::BufferAdaptor
19{
20public:
21 DummyEventAdaptor()
22 : BufferAdaptor()
23 {
24
25 }
26
27 void setProperty(const QByteArray &key, const QVariant &value)
28 {
29 if (mResourceMapper && mResourceMapper->mWriteAccessors.contains(key)) {
30 // mResourceMapper->setProperty(key, value, mResourceBuffer);
31 } else {
32 // mLocalMapper.;
33 }
34 }
35
36 virtual QVariant getProperty(const QByteArray &key) const
37 {
38 if (mResourceBuffer && mResourceMapper->mReadAccessors.contains(key)) {
39 return mResourceMapper->getProperty(key, mResourceBuffer);
40 } else if (mLocalBuffer && mLocalMapper->mReadAccessors.contains(key)) {
41 return mLocalMapper->getProperty(key, mLocalBuffer);
42 }
43 qWarning() << "no mapping available for key " << key;
44 return QVariant();
45 }
46
47 virtual QList<QByteArray> availableProperties() const
48 {
49 QList<QByteArray> props;
50 props << mResourceMapper->mReadAccessors.keys();
51 props << mLocalMapper->mReadAccessors.keys();
52 return props;
53 }
54
55 Akonadi2::ApplicationDomain::Buffer::Event const *mLocalBuffer;
56 DummyEvent const *mResourceBuffer;
57
58 QSharedPointer<PropertyMapper<Akonadi2::ApplicationDomain::Buffer::Event> > mLocalMapper;
59 QSharedPointer<PropertyMapper<DummyEvent> > mResourceMapper;
60};
61
62 17
63DummyEventAdaptorFactory::DummyEventAdaptorFactory() 18DummyEventAdaptorFactory::DummyEventAdaptorFactory()
64 : DomainTypeAdaptorFactory() 19 : DomainTypeAdaptorFactory()
65{ 20{
21 //TODO turn this into initializePropertyMapper as well?
66 mResourceMapper = QSharedPointer<PropertyMapper<DummyEvent> >::create(); 22 mResourceMapper = QSharedPointer<PropertyMapper<DummyEvent> >::create();
67 mResourceMapper->mReadAccessors.insert("summary", [](DummyEvent const *buffer) -> QVariant { 23 mResourceMapper->mReadAccessors.insert("summary", [](DummyEvent const *buffer) -> QVariant {
68 if (buffer->summary()) { 24 if (buffer->summary()) {
@@ -70,36 +26,8 @@ DummyEventAdaptorFactory::DummyEventAdaptorFactory()
70 } 26 }
71 return QVariant(); 27 return QVariant();
72 }); 28 });
73 mLocalMapper = QSharedPointer<PropertyMapper<Akonadi2::ApplicationDomain::Buffer::Event> >::create();
74 mLocalMapper->mReadAccessors.insert("summary", [](Akonadi2::ApplicationDomain::Buffer::Event const *buffer) -> QVariant {
75 if (buffer->summary()) {
76 return QString::fromStdString(buffer->summary()->c_str());
77 }
78 return QVariant();
79 });
80 mLocalMapper->mReadAccessors.insert("uid", [](Akonadi2::ApplicationDomain::Buffer::Event const *buffer) -> QVariant {
81 if (buffer->uid()) {
82 return QString::fromStdString(buffer->uid()->c_str());
83 }
84 return QVariant();
85 });
86
87} 29}
88 30
89//TODO pass EntityBuffer instead?
90QSharedPointer<Akonadi2::ApplicationDomain::BufferAdaptor> DummyEventAdaptorFactory::createAdaptor(const Akonadi2::Entity &entity)
91{
92 const auto resourceBuffer = Akonadi2::EntityBuffer::readBuffer<DummyEvent>(entity.resource());
93 const auto localBuffer = Akonadi2::EntityBuffer::readBuffer<Akonadi2::ApplicationDomain::Buffer::Event>(entity.local());
94 // const auto metadataBuffer = Akonadi2::EntityBuffer::readBuffer<Akonadi2::Metadata>(entity.metadata());
95
96 auto adaptor = QSharedPointer<DummyEventAdaptor>::create();
97 adaptor->mLocalBuffer = localBuffer;
98 adaptor->mLocalMapper = mLocalMapper;
99 adaptor->mResourceBuffer = resourceBuffer;
100 adaptor->mResourceMapper = mResourceMapper;
101 return adaptor;
102}
103 31
104void DummyEventAdaptorFactory::createBuffer(const Akonadi2::ApplicationDomain::Event &event, flatbuffers::FlatBufferBuilder &fbb) 32void DummyEventAdaptorFactory::createBuffer(const Akonadi2::ApplicationDomain::Event &event, flatbuffers::FlatBufferBuilder &fbb)
105{ 33{
diff --git a/dummyresource/domainadaptor.h b/dummyresource/domainadaptor.h
index 143121a..0e2b883 100644
--- a/dummyresource/domainadaptor.h
+++ b/dummyresource/domainadaptor.h
@@ -10,6 +10,5 @@ class DummyEventAdaptorFactory : public DomainTypeAdaptorFactory<Akonadi2::Appli
10public: 10public:
11 DummyEventAdaptorFactory(); 11 DummyEventAdaptorFactory();
12 virtual ~DummyEventAdaptorFactory() {}; 12 virtual ~DummyEventAdaptorFactory() {};
13 virtual QSharedPointer<Akonadi2::ApplicationDomain::BufferAdaptor> createAdaptor(const Akonadi2::Entity &entity);
14 virtual void createBuffer(const Akonadi2::ApplicationDomain::Event &event, flatbuffers::FlatBufferBuilder &fbb); 13 virtual void createBuffer(const Akonadi2::ApplicationDomain::Event &event, flatbuffers::FlatBufferBuilder &fbb);
15}; 14};