summaryrefslogtreecommitdiffstats
path: root/common
diff options
context:
space:
mode:
Diffstat (limited to 'common')
-rw-r--r--common/domainadaptor.cpp23
-rw-r--r--common/domainadaptor.h95
2 files changed, 68 insertions, 50 deletions
diff --git a/common/domainadaptor.cpp b/common/domainadaptor.cpp
index 5e6f062..aa8c3d9 100644
--- a/common/domainadaptor.cpp
+++ b/common/domainadaptor.cpp
@@ -20,6 +20,15 @@
20#include "domainadaptor.h" 20#include "domainadaptor.h"
21 21
22template <> 22template <>
23flatbuffers::uoffset_t extractProperty<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
31template <>
23QSharedPointer<ReadPropertyMapper<Akonadi2::ApplicationDomain::Buffer::Event> > initializeReadPropertyMapper<Akonadi2::ApplicationDomain::Buffer::Event>() 32QSharedPointer<ReadPropertyMapper<Akonadi2::ApplicationDomain::Buffer::Event> > initializeReadPropertyMapper<Akonadi2::ApplicationDomain::Buffer::Event>()
24{ 33{
25 auto propertyMapper = QSharedPointer<ReadPropertyMapper<Akonadi2::ApplicationDomain::Buffer::Event> >::create(); 34 auto propertyMapper = QSharedPointer<ReadPropertyMapper<Akonadi2::ApplicationDomain::Buffer::Event> >::create();
@@ -38,3 +47,17 @@ QSharedPointer<ReadPropertyMapper<Akonadi2::ApplicationDomain::Buffer::Event> >
38 return propertyMapper; 47 return propertyMapper;
39} 48}
40 49
50template <>
51QSharedPointer<WritePropertyMapper<Akonadi2::ApplicationDomain::Buffer::EventBuilder> > initializeWritePropertyMapper<Akonadi2::ApplicationDomain::Buffer::EventBuilder>()
52{
53 auto propertyMapper = QSharedPointer<WritePropertyMapper<Akonadi2::ApplicationDomain::Buffer::EventBuilder> >::create();
54 propertyMapper->addMapping("summary", [](const QVariant &value, flatbuffers::FlatBufferBuilder &fbb) -> std::function<void(Akonadi2::ApplicationDomain::Buffer::EventBuilder &)> {
55 auto offset = extractProperty<QString>(value, fbb);
56 return [offset](Akonadi2::ApplicationDomain::Buffer::EventBuilder &builder) { builder.add_summary(offset); };
57 });
58 propertyMapper->addMapping("uid", [](const QVariant &value, flatbuffers::FlatBufferBuilder &fbb) -> std::function<void(Akonadi2::ApplicationDomain::Buffer::EventBuilder &)> {
59 auto offset = extractProperty<QString>(value, fbb);
60 return [offset](Akonadi2::ApplicationDomain::Buffer::EventBuilder &builder) { builder.add_uid(offset); };
61 });
62 return propertyMapper;
63}
diff --git a/common/domainadaptor.h b/common/domainadaptor.h
index 5d9574b..e356692 100644
--- a/common/domainadaptor.h
+++ b/common/domainadaptor.h
@@ -78,6 +78,39 @@ private:
78}; 78};
79 79
80/** 80/**
81 * Defines how to convert qt primitives to flatbuffer ones
82 * TODO: rename to createProperty or so?
83 */
84template <class T>
85flatbuffers::uoffset_t extractProperty(const QVariant &, flatbuffers::FlatBufferBuilder &fbb);
86
87/**
88 * Create a buffer from a domain object using the provided mappings
89 */
90template <class Builder, class Buffer>
91flatbuffers::Offset<Buffer> createBufferPart(const Akonadi2::ApplicationDomain::ApplicationDomainType &domainObject, flatbuffers::FlatBufferBuilder &fbb, const WritePropertyMapper<Builder> &mapper)
92{
93 //First create a primitives such as strings using the mappings
94 QList<std::function<void(Builder &)> > propertiesToAddToResource;
95 for (const auto &property : domainObject.changedProperties()) {
96 qWarning() << "copying property " << property;
97 const auto value = domainObject.getProperty(property);
98 if (mapper.hasMapping(property)) {
99 mapper.setProperty(property, domainObject.getProperty(property), propertiesToAddToResource, fbb);
100 } else {
101 qWarning() << "no mapping for property available " << property;
102 }
103 }
104
105 //Then create all porperties using the above generated builderCalls
106 Builder builder(fbb);
107 for (auto propertyBuilder : propertiesToAddToResource) {
108 propertyBuilder(builder);
109 }
110 return builder.Finish();
111}
112
113/**
81 * A generic adaptor implementation that uses a property mapper to read/write values. 114 * A generic adaptor implementation that uses a property mapper to read/write values.
82 * 115 *
83 * TODO: this is the read-only part. Create a write only equivalent 116 * TODO: this is the read-only part. Create a write only equivalent
@@ -95,11 +128,6 @@ public:
95 //TODO remove 128 //TODO remove
96 void setProperty(const QByteArray &key, const QVariant &value) 129 void setProperty(const QByteArray &key, const QVariant &value)
97 { 130 {
98 // if (mResourceMapper && mResourceMapper->hasMapping(key)) {
99 // // mResourceMapper->setProperty(key, value, mResourceBuffer);
100 // } else {
101 // // mLocalMapper.;
102 // }
103 } 131 }
104 132
105 virtual QVariant getProperty(const QByteArray &key) const 133 virtual QVariant getProperty(const QByteArray &key) const
@@ -128,49 +156,6 @@ public:
128}; 156};
129 157
130/** 158/**
131 * A generic adaptor implementation that uses a property mapper to read/write values.
132 */
133template <class LocalBuilder, class ResourceBuilder>
134class GenericWriteBufferAdaptor : public Akonadi2::ApplicationDomain::BufferAdaptor
135{
136public:
137 GenericWriteBufferAdaptor(const BufferAdaptor &buffer)
138 : BufferAdaptor()
139 {
140 for(const auto &property : buffer.availableProperties()) {
141 setProperty(property, buffer.getProperty(property));
142 }
143 }
144
145 void setProperty(const QByteArray &key, const QVariant &value)
146 {
147 // if (mResourceMapper && mResourceMapper->hasMapping(key)) {
148 // // mResourceMapper->setProperty(key, value, mResourceBuffer);
149 // } else {
150 // // mLocalMapper.;
151 // }
152 }
153
154 //TODO remove
155 virtual QVariant getProperty(const QByteArray &key) const
156 {
157 Q_ASSERT(false);
158 }
159
160 virtual QList<QByteArray> availableProperties() const
161 {
162 Q_ASSERT(false);
163 QList<QByteArray> props;
164 return props;
165 }
166
167 // LocalBuffer const *mLocalBuffer;
168 // ResourceBuffer const *mResourceBuffer;
169 QSharedPointer<WritePropertyMapper<LocalBuilder> > mLocalMapper;
170 QSharedPointer<WritePropertyMapper<ResourceBuilder> > mResourceMapper;
171};
172
173/**
174 * Initializes the local property mapper. 159 * Initializes the local property mapper.
175 * 160 *
176 * Provide an implementation for each application domain type. 161 * Provide an implementation for each application domain type.
@@ -178,16 +163,24 @@ public:
178template <class T> 163template <class T>
179QSharedPointer<ReadPropertyMapper<T> > initializeReadPropertyMapper(); 164QSharedPointer<ReadPropertyMapper<T> > initializeReadPropertyMapper();
180 165
166template <class T>
167QSharedPointer<WritePropertyMapper<T> > initializeWritePropertyMapper();
168
181/** 169/**
182 * The factory should define how to go from an entitybuffer (local + resource buffer), to a domain type adapter. 170 * The factory should define how to go from an entitybuffer (local + resource buffer), to a domain type adapter.
183 * It defines how values are split accross local and resource buffer. 171 * It defines how values are split accross local and resource buffer.
184 * This is required by the facade the read the value, and by the pipeline preprocessors to access the domain values in a generic way. 172 * This is required by the facade the read the value, and by the pipeline preprocessors to access the domain values in a generic way.
185 */ 173 */
186template<typename DomainType, typename LocalBuffer, typename ResourceBuffer> 174template<typename DomainType, typename LocalBuffer, typename ResourceBuffer, typename LocalBuilder, typename ResourceBuilder>
187class DomainTypeAdaptorFactory 175class DomainTypeAdaptorFactory
188{ 176{
189public: 177public:
190 DomainTypeAdaptorFactory() : mLocalMapper(initializeReadPropertyMapper<LocalBuffer>()) {}; 178 DomainTypeAdaptorFactory() :
179 mLocalMapper(initializeReadPropertyMapper<LocalBuffer>()),
180 mResourceMapper(QSharedPointer<ReadPropertyMapper<ResourceBuffer> >::create()),
181 mLocalWriteMapper(initializeWritePropertyMapper<LocalBuilder>()),
182 mResourceWriteMapper(QSharedPointer<WritePropertyMapper<ResourceBuilder> >::create())
183 {};
191 virtual ~DomainTypeAdaptorFactory() {}; 184 virtual ~DomainTypeAdaptorFactory() {};
192 185
193 /** 186 /**
@@ -214,6 +207,8 @@ public:
214protected: 207protected:
215 QSharedPointer<ReadPropertyMapper<LocalBuffer> > mLocalMapper; 208 QSharedPointer<ReadPropertyMapper<LocalBuffer> > mLocalMapper;
216 QSharedPointer<ReadPropertyMapper<ResourceBuffer> > mResourceMapper; 209 QSharedPointer<ReadPropertyMapper<ResourceBuffer> > mResourceMapper;
210 QSharedPointer<WritePropertyMapper<LocalBuilder> > mLocalWriteMapper;
211 QSharedPointer<WritePropertyMapper<ResourceBuilder> > mResourceWriteMapper;
217}; 212};
218 213
219 214