summaryrefslogtreecommitdiffstats
path: root/common/domainadaptor.h
diff options
context:
space:
mode:
authorChristian Mollekopf <chrigi_1@fastmail.fm>2015-04-10 12:32:03 +0200
committerChristian Mollekopf <chrigi_1@fastmail.fm>2015-04-10 12:32:03 +0200
commitea348c62fdebe1d9c6531fc4491d3316a1e941df (patch)
treea5984008eacf60154e3454544cb6d68969611fed /common/domainadaptor.h
parent2e13ef36cabbe1c995e77b92c06d6350719bf952 (diff)
downloadsink-ea348c62fdebe1d9c6531fc4491d3316a1e941df.tar.gz
sink-ea348c62fdebe1d9c6531fc4491d3316a1e941df.zip
checkpoint
Diffstat (limited to 'common/domainadaptor.h')
-rw-r--r--common/domainadaptor.h112
1 files changed, 87 insertions, 25 deletions
diff --git a/common/domainadaptor.h b/common/domainadaptor.h
index 15e3067..5d9574b 100644
--- a/common/domainadaptor.h
+++ b/common/domainadaptor.h
@@ -38,17 +38,9 @@
38 * that extract the properties from resource types. 38 * that extract the properties from resource types.
39 */ 39 */
40template<typename BufferType> 40template<typename BufferType>
41class PropertyMapper 41class ReadPropertyMapper
42{ 42{
43public: 43public:
44 void setProperty(const QByteArray &key, const QVariant &value, BufferType *buffer)
45 {
46 if (mWriteAccessors.contains(key)) {
47 auto accessor = mWriteAccessors.value(key);
48 return accessor(value, buffer);
49 }
50 }
51
52 virtual QVariant getProperty(const QByteArray &key, BufferType const *buffer) const 44 virtual QVariant getProperty(const QByteArray &key, BufferType const *buffer) const
53 { 45 {
54 if (mReadAccessors.contains(key)) { 46 if (mReadAccessors.contains(key)) {
@@ -57,12 +49,38 @@ public:
57 } 49 }
58 return QVariant(); 50 return QVariant();
59 } 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 }
57private:
60 QHash<QByteArray, std::function<QVariant(BufferType const *)> > mReadAccessors; 58 QHash<QByteArray, std::function<QVariant(BufferType const *)> > mReadAccessors;
61 QHash<QByteArray, std::function<void(const QVariant &, BufferType*)> > mWriteAccessors; 59};
60
61template<typename BufferBuilder>
62class WritePropertyMapper
63{
64public:
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 }
76private:
77 QHash<QByteArray, std::function<std::function<void(BufferBuilder &)>(const QVariant &, flatbuffers::FlatBufferBuilder &)> > mWriteAccessors;
62}; 78};
63 79
64/** 80/**
65 * A generic adaptor implementation that uses a property mapper to read/write values. 81 * A generic adaptor implementation that uses a property mapper to read/write values.
82 *
83 * TODO: this is the read-only part. Create a write only equivalent
66 */ 84 */
67template <class LocalBuffer, class ResourceBuffer> 85template <class LocalBuffer, class ResourceBuffer>
68class GenericBufferAdaptor : public Akonadi2::ApplicationDomain::BufferAdaptor 86class GenericBufferAdaptor : public Akonadi2::ApplicationDomain::BufferAdaptor
@@ -74,20 +92,21 @@ public:
74 92
75 } 93 }
76 94
95 //TODO remove
77 void setProperty(const QByteArray &key, const QVariant &value) 96 void setProperty(const QByteArray &key, const QVariant &value)
78 { 97 {
79 if (mResourceMapper && mResourceMapper->mWriteAccessors.contains(key)) { 98 // if (mResourceMapper && mResourceMapper->hasMapping(key)) {
80 // mResourceMapper->setProperty(key, value, mResourceBuffer); 99 // // mResourceMapper->setProperty(key, value, mResourceBuffer);
81 } else { 100 // } else {
82 // mLocalMapper.; 101 // // mLocalMapper.;
83 } 102 // }
84 } 103 }
85 104
86 virtual QVariant getProperty(const QByteArray &key) const 105 virtual QVariant getProperty(const QByteArray &key) const
87 { 106 {
88 if (mResourceBuffer && mResourceMapper->mReadAccessors.contains(key)) { 107 if (mResourceBuffer && mResourceMapper->hasMapping(key)) {
89 return mResourceMapper->getProperty(key, mResourceBuffer); 108 return mResourceMapper->getProperty(key, mResourceBuffer);
90 } else if (mLocalBuffer && mLocalMapper->mReadAccessors.contains(key)) { 109 } else if (mLocalBuffer && mLocalMapper->hasMapping(key)) {
91 return mLocalMapper->getProperty(key, mLocalBuffer); 110 return mLocalMapper->getProperty(key, mLocalBuffer);
92 } 111 }
93 qWarning() << "no mapping available for key " << key; 112 qWarning() << "no mapping available for key " << key;
@@ -97,15 +116,58 @@ public:
97 virtual QList<QByteArray> availableProperties() const 116 virtual QList<QByteArray> availableProperties() const
98 { 117 {
99 QList<QByteArray> props; 118 QList<QByteArray> props;
100 props << mResourceMapper->mReadAccessors.keys(); 119 props << mResourceMapper->availableProperties();
101 props << mLocalMapper->mReadAccessors.keys(); 120 props << mLocalMapper->availableProperties();
102 return props; 121 return props;
103 } 122 }
104 123
105 LocalBuffer const *mLocalBuffer; 124 LocalBuffer const *mLocalBuffer;
106 ResourceBuffer const *mResourceBuffer; 125 ResourceBuffer const *mResourceBuffer;
107 QSharedPointer<PropertyMapper<LocalBuffer> > mLocalMapper; 126 QSharedPointer<ReadPropertyMapper<LocalBuffer> > mLocalMapper;
108 QSharedPointer<PropertyMapper<ResourceBuffer> > mResourceMapper; 127 QSharedPointer<ReadPropertyMapper<ResourceBuffer> > mResourceMapper;
128};
129
130/**
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;
109}; 171};
110 172
111/** 173/**
@@ -114,7 +176,7 @@ public:
114 * Provide an implementation for each application domain type. 176 * Provide an implementation for each application domain type.
115 */ 177 */
116template <class T> 178template <class T>
117QSharedPointer<PropertyMapper<T> > initializePropertyMapper(); 179QSharedPointer<ReadPropertyMapper<T> > initializeReadPropertyMapper();
118 180
119/** 181/**
120 * The factory should define how to go from an entitybuffer (local + resource buffer), to a domain type adapter. 182 * The factory should define how to go from an entitybuffer (local + resource buffer), to a domain type adapter.
@@ -125,7 +187,7 @@ template<typename DomainType, typename LocalBuffer, typename ResourceBuffer>
125class DomainTypeAdaptorFactory 187class DomainTypeAdaptorFactory
126{ 188{
127public: 189public:
128 DomainTypeAdaptorFactory() : mLocalMapper(initializePropertyMapper<LocalBuffer>()) {}; 190 DomainTypeAdaptorFactory() : mLocalMapper(initializeReadPropertyMapper<LocalBuffer>()) {};
129 virtual ~DomainTypeAdaptorFactory() {}; 191 virtual ~DomainTypeAdaptorFactory() {};
130 192
131 /** 193 /**
@@ -150,8 +212,8 @@ public:
150 virtual void createBuffer(const Akonadi2::ApplicationDomain::Event &event, flatbuffers::FlatBufferBuilder &fbb) {}; 212 virtual void createBuffer(const Akonadi2::ApplicationDomain::Event &event, flatbuffers::FlatBufferBuilder &fbb) {};
151 213
152protected: 214protected:
153 QSharedPointer<PropertyMapper<LocalBuffer> > mLocalMapper; 215 QSharedPointer<ReadPropertyMapper<LocalBuffer> > mLocalMapper;
154 QSharedPointer<PropertyMapper<ResourceBuffer> > mResourceMapper; 216 QSharedPointer<ReadPropertyMapper<ResourceBuffer> > mResourceMapper;
155}; 217};
156 218
157 219