summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorChristian Mollekopf <chrigi_1@fastmail.fm>2017-04-28 11:48:06 +0200
committerChristian Mollekopf <chrigi_1@fastmail.fm>2017-04-28 11:48:06 +0200
commit6b8432a5c0647d8fbe3cda549574ae13e07bb2f4 (patch)
tree12b86a325be07013b3f1a926174d554afc35830a
parent4db017949e6528867963c5495aa557dcfb6f4765 (diff)
downloadsink-6b8432a5c0647d8fbe3cda549574ae13e07bb2f4.tar.gz
sink-6b8432a5c0647d8fbe3cda549574ae13e07bb2f4.zip
Simplified propertymapper
-rw-r--r--common/domain/typeimplementations.cpp10
-rw-r--r--common/domain/typeimplementations.h11
-rw-r--r--common/domainadaptor.h12
-rw-r--r--common/propertymapper.h109
4 files changed, 29 insertions, 113 deletions
diff --git a/common/domain/typeimplementations.cpp b/common/domain/typeimplementations.cpp
index eb3851e..5db35b7 100644
--- a/common/domain/typeimplementations.cpp
+++ b/common/domain/typeimplementations.cpp
@@ -61,7 +61,7 @@ void TypeImplementation<Mail>::configure(IndexPropertyMapper &indexPropertyMappe
61 }); 61 });
62} 62}
63 63
64void TypeImplementation<Mail>::configure(ReadPropertyMapper<Buffer> &propertyMapper) 64void TypeImplementation<Mail>::configure(ReadPropertyMapper &propertyMapper)
65{ 65{
66 propertyMapper.addMapping<Mail::Sender, Buffer>(&Buffer::sender); 66 propertyMapper.addMapping<Mail::Sender, Buffer>(&Buffer::sender);
67 propertyMapper.addMapping<Mail::To, Buffer>(&Buffer::to); 67 propertyMapper.addMapping<Mail::To, Buffer>(&Buffer::to);
@@ -108,7 +108,7 @@ void TypeImplementation<Folder>::configure(TypeIndex &index)
108 index.addProperty<QString>(Folder::Name::name); 108 index.addProperty<QString>(Folder::Name::name);
109} 109}
110 110
111void TypeImplementation<Folder>::configure(ReadPropertyMapper<Buffer> &propertyMapper) 111void TypeImplementation<Folder>::configure(ReadPropertyMapper &propertyMapper)
112{ 112{
113 propertyMapper.addMapping<Folder::Parent, Buffer>(&Buffer::parent); 113 propertyMapper.addMapping<Folder::Parent, Buffer>(&Buffer::parent);
114 propertyMapper.addMapping<Folder::Name, Buffer>(&Buffer::name); 114 propertyMapper.addMapping<Folder::Name, Buffer>(&Buffer::name);
@@ -137,7 +137,7 @@ void TypeImplementation<Contact>::configure(TypeIndex &index)
137 index.addProperty<QByteArray>(Contact::Uid::name); 137 index.addProperty<QByteArray>(Contact::Uid::name);
138} 138}
139 139
140void TypeImplementation<Contact>::configure(ReadPropertyMapper<Buffer> &propertyMapper) 140void TypeImplementation<Contact>::configure(ReadPropertyMapper &propertyMapper)
141{ 141{
142 propertyMapper.addMapping<Contact::Uid, Buffer>(&Buffer::uid); 142 propertyMapper.addMapping<Contact::Uid, Buffer>(&Buffer::uid);
143 propertyMapper.addMapping<Contact::Fn, Buffer>(&Buffer::fn); 143 propertyMapper.addMapping<Contact::Fn, Buffer>(&Buffer::fn);
@@ -171,7 +171,7 @@ void TypeImplementation<Addressbook>::configure(TypeIndex &index)
171 index.addProperty<QString>(Addressbook::Name::name); 171 index.addProperty<QString>(Addressbook::Name::name);
172} 172}
173 173
174void TypeImplementation<Addressbook>::configure(ReadPropertyMapper<Buffer> &propertyMapper) 174void TypeImplementation<Addressbook>::configure(ReadPropertyMapper &propertyMapper)
175{ 175{
176 propertyMapper.addMapping<Addressbook::Parent, Buffer>(&Buffer::parent); 176 propertyMapper.addMapping<Addressbook::Parent, Buffer>(&Buffer::parent);
177 propertyMapper.addMapping<Addressbook::Name, Buffer>(&Buffer::name); 177 propertyMapper.addMapping<Addressbook::Name, Buffer>(&Buffer::name);
@@ -194,7 +194,7 @@ void TypeImplementation<Event>::configure(TypeIndex &index)
194 index.addProperty<QByteArray>(Event::Uid::name); 194 index.addProperty<QByteArray>(Event::Uid::name);
195} 195}
196 196
197void TypeImplementation<Event>::configure(ReadPropertyMapper<Buffer> &propertyMapper) 197void TypeImplementation<Event>::configure(ReadPropertyMapper &propertyMapper)
198{ 198{
199 propertyMapper.addMapping<Event::Summary, Buffer>(&Buffer::summary); 199 propertyMapper.addMapping<Event::Summary, Buffer>(&Buffer::summary);
200 propertyMapper.addMapping<Event::Description, Buffer>(&Buffer::description); 200 propertyMapper.addMapping<Event::Description, Buffer>(&Buffer::description);
diff --git a/common/domain/typeimplementations.h b/common/domain/typeimplementations.h
index 37d6ca9..705b059 100644
--- a/common/domain/typeimplementations.h
+++ b/common/domain/typeimplementations.h
@@ -26,7 +26,6 @@
26#include "contact_generated.h" 26#include "contact_generated.h"
27#include "addressbook_generated.h" 27#include "addressbook_generated.h"
28 28
29template<typename T>
30class ReadPropertyMapper; 29class ReadPropertyMapper;
31template<typename T> 30template<typename T>
32class WritePropertyMapper; 31class WritePropertyMapper;
@@ -48,7 +47,7 @@ public:
48 typedef Sink::ApplicationDomain::Buffer::Mail Buffer; 47 typedef Sink::ApplicationDomain::Buffer::Mail Buffer;
49 typedef Sink::ApplicationDomain::Buffer::MailBuilder BufferBuilder; 48 typedef Sink::ApplicationDomain::Buffer::MailBuilder BufferBuilder;
50 static void configure(TypeIndex &index); 49 static void configure(TypeIndex &index);
51 static void configure(ReadPropertyMapper<Buffer> &propertyMapper); 50 static void configure(ReadPropertyMapper &propertyMapper);
52 static void configure(WritePropertyMapper<BufferBuilder> &propertyMapper); 51 static void configure(WritePropertyMapper<BufferBuilder> &propertyMapper);
53 static void configure(IndexPropertyMapper &indexPropertyMapper); 52 static void configure(IndexPropertyMapper &indexPropertyMapper);
54}; 53};
@@ -59,7 +58,7 @@ public:
59 typedef Sink::ApplicationDomain::Buffer::Folder Buffer; 58 typedef Sink::ApplicationDomain::Buffer::Folder Buffer;
60 typedef Sink::ApplicationDomain::Buffer::FolderBuilder BufferBuilder; 59 typedef Sink::ApplicationDomain::Buffer::FolderBuilder BufferBuilder;
61 static void configure(TypeIndex &); 60 static void configure(TypeIndex &);
62 static void configure(ReadPropertyMapper<Buffer> &); 61 static void configure(ReadPropertyMapper &);
63 static void configure(WritePropertyMapper<BufferBuilder> &); 62 static void configure(WritePropertyMapper<BufferBuilder> &);
64 static void configure(IndexPropertyMapper &indexPropertyMapper); 63 static void configure(IndexPropertyMapper &indexPropertyMapper);
65}; 64};
@@ -70,7 +69,7 @@ public:
70 typedef Sink::ApplicationDomain::Buffer::Contact Buffer; 69 typedef Sink::ApplicationDomain::Buffer::Contact Buffer;
71 typedef Sink::ApplicationDomain::Buffer::ContactBuilder BufferBuilder; 70 typedef Sink::ApplicationDomain::Buffer::ContactBuilder BufferBuilder;
72 static void configure(TypeIndex &); 71 static void configure(TypeIndex &);
73 static void configure(ReadPropertyMapper<Buffer> &); 72 static void configure(ReadPropertyMapper &);
74 static void configure(WritePropertyMapper<BufferBuilder> &); 73 static void configure(WritePropertyMapper<BufferBuilder> &);
75 static void configure(IndexPropertyMapper &indexPropertyMapper); 74 static void configure(IndexPropertyMapper &indexPropertyMapper);
76}; 75};
@@ -81,7 +80,7 @@ public:
81 typedef Sink::ApplicationDomain::Buffer::Addressbook Buffer; 80 typedef Sink::ApplicationDomain::Buffer::Addressbook Buffer;
82 typedef Sink::ApplicationDomain::Buffer::AddressbookBuilder BufferBuilder; 81 typedef Sink::ApplicationDomain::Buffer::AddressbookBuilder BufferBuilder;
83 static void configure(TypeIndex &); 82 static void configure(TypeIndex &);
84 static void configure(ReadPropertyMapper<Buffer> &); 83 static void configure(ReadPropertyMapper &);
85 static void configure(WritePropertyMapper<BufferBuilder> &); 84 static void configure(WritePropertyMapper<BufferBuilder> &);
86 static void configure(IndexPropertyMapper &indexPropertyMapper); 85 static void configure(IndexPropertyMapper &indexPropertyMapper);
87}; 86};
@@ -92,7 +91,7 @@ public:
92 typedef Sink::ApplicationDomain::Buffer::Event Buffer; 91 typedef Sink::ApplicationDomain::Buffer::Event Buffer;
93 typedef Sink::ApplicationDomain::Buffer::EventBuilder BufferBuilder; 92 typedef Sink::ApplicationDomain::Buffer::EventBuilder BufferBuilder;
94 static void configure(TypeIndex &); 93 static void configure(TypeIndex &);
95 static void configure(ReadPropertyMapper<Buffer> &); 94 static void configure(ReadPropertyMapper &);
96 static void configure(WritePropertyMapper<BufferBuilder> &); 95 static void configure(WritePropertyMapper<BufferBuilder> &);
97 static void configure(IndexPropertyMapper &indexPropertyMapper); 96 static void configure(IndexPropertyMapper &indexPropertyMapper);
98}; 97};
diff --git a/common/domainadaptor.h b/common/domainadaptor.h
index af5d5fc..9182001 100644
--- a/common/domainadaptor.h
+++ b/common/domainadaptor.h
@@ -157,8 +157,8 @@ public:
157 157
158 LocalBuffer const *mLocalBuffer; 158 LocalBuffer const *mLocalBuffer;
159 ResourceBuffer const *mResourceBuffer; 159 ResourceBuffer const *mResourceBuffer;
160 QSharedPointer<ReadPropertyMapper<LocalBuffer>> mLocalMapper; 160 QSharedPointer<ReadPropertyMapper> mLocalMapper;
161 QSharedPointer<ReadPropertyMapper<ResourceBuffer>> mResourceMapper; 161 QSharedPointer<ReadPropertyMapper> mResourceMapper;
162 QSharedPointer<IndexPropertyMapper> mIndexMapper; 162 QSharedPointer<IndexPropertyMapper> mIndexMapper;
163 TypeIndex *mIndex; 163 TypeIndex *mIndex;
164}; 164};
@@ -176,8 +176,8 @@ class SINK_EXPORT DomainTypeAdaptorFactory : public DomainTypeAdaptorFactoryInte
176 176
177public: 177public:
178 DomainTypeAdaptorFactory() 178 DomainTypeAdaptorFactory()
179 : mLocalMapper(QSharedPointer<ReadPropertyMapper<LocalBuffer>>::create()), 179 : mLocalMapper(QSharedPointer<ReadPropertyMapper>::create()),
180 mResourceMapper(QSharedPointer<ReadPropertyMapper<ResourceBuffer>>::create()), 180 mResourceMapper(QSharedPointer<ReadPropertyMapper>::create()),
181 mLocalWriteMapper(QSharedPointer<WritePropertyMapper<LocalBuilder>>::create()), 181 mLocalWriteMapper(QSharedPointer<WritePropertyMapper<LocalBuilder>>::create()),
182 mResourceWriteMapper(QSharedPointer<WritePropertyMapper<ResourceBuilder>>::create()), 182 mResourceWriteMapper(QSharedPointer<WritePropertyMapper<ResourceBuilder>>::create()),
183 mIndexMapper(QSharedPointer<IndexPropertyMapper>::create()) 183 mIndexMapper(QSharedPointer<IndexPropertyMapper>::create())
@@ -236,8 +236,8 @@ public:
236 236
237 237
238protected: 238protected:
239 QSharedPointer<ReadPropertyMapper<LocalBuffer>> mLocalMapper; 239 QSharedPointer<ReadPropertyMapper> mLocalMapper;
240 QSharedPointer<ReadPropertyMapper<ResourceBuffer>> mResourceMapper; 240 QSharedPointer<ReadPropertyMapper> mResourceMapper;
241 QSharedPointer<WritePropertyMapper<LocalBuilder>> mLocalWriteMapper; 241 QSharedPointer<WritePropertyMapper<LocalBuilder>> mLocalWriteMapper;
242 QSharedPointer<WritePropertyMapper<ResourceBuilder>> mResourceWriteMapper; 242 QSharedPointer<WritePropertyMapper<ResourceBuilder>> mResourceWriteMapper;
243 QSharedPointer<IndexPropertyMapper> mIndexMapper; 243 QSharedPointer<IndexPropertyMapper> mIndexMapper;
diff --git a/common/propertymapper.h b/common/propertymapper.h
index 9ea0b73..bca45aa 100644
--- a/common/propertymapper.h
+++ b/common/propertymapper.h
@@ -65,13 +65,12 @@ QVariant SINK_EXPORT propertyToVariant(const flatbuffers::Vector<flatbuffers::Of
65 * a virtual method per property, the property mapper can be filled with accessors 65 * a virtual method per property, the property mapper can be filled with accessors
66 * that extract the properties from resource types. 66 * that extract the properties from resource types.
67 */ 67 */
68template <typename BufferType>
69class ReadPropertyMapper 68class ReadPropertyMapper
70{ 69{
71public: 70public:
72 virtual ~ReadPropertyMapper(){}; 71 virtual ~ReadPropertyMapper(){};
73 72
74 virtual QVariant getProperty(const QByteArray &key, BufferType const *buffer) const 73 virtual QVariant getProperty(const QByteArray &key, void const *buffer) const
75 { 74 {
76 if (mReadAccessors.contains(key)) { 75 if (mReadAccessors.contains(key)) {
77 auto accessor = mReadAccessors.value(key); 76 auto accessor = mReadAccessors.value(key);
@@ -79,69 +78,30 @@ public:
79 } 78 }
80 return QVariant(); 79 return QVariant();
81 } 80 }
81
82 bool hasMapping(const QByteArray &key) const 82 bool hasMapping(const QByteArray &key) const
83 { 83 {
84 return mReadAccessors.contains(key); 84 return mReadAccessors.contains(key);
85 } 85 }
86
86 QList<QByteArray> availableProperties() const 87 QList<QByteArray> availableProperties() const
87 { 88 {
88 return mReadAccessors.keys(); 89 return mReadAccessors.keys();
89 } 90 }
90 void addMapping(const QByteArray &property, const std::function<QVariant(BufferType const *)> &mapping)
91 {
92 mReadAccessors.insert(property, mapping);
93 }
94
95 template <typename T, typename Buffer>
96 void addMapping(const flatbuffers::String *(Buffer::*f)() const)
97 {
98 addMapping(T::name, [f](Buffer const *buffer) -> QVariant { return propertyToVariant<typename T::Type>((buffer->*f)()); });
99 }
100
101 template <typename T, typename Buffer>
102 void addMapping(uint8_t (Buffer::*f)() const)
103 {
104 addMapping(T::name, [f](Buffer const *buffer) -> QVariant { return propertyToVariant<typename T::Type>((buffer->*f)()); });
105 }
106
107 template <typename T, typename Buffer>
108 void addMapping(bool (Buffer::*f)() const)
109 {
110 addMapping(T::name, [f](Buffer const *buffer) -> QVariant { return propertyToVariant<typename T::Type>((buffer->*f)()); });
111 }
112
113 template <typename T, typename Buffer>
114 void addMapping(const flatbuffers::Vector<uint8_t> *(Buffer::*f)() const)
115 {
116 addMapping(T::name, [f](Buffer const *buffer) -> QVariant { return propertyToVariant<typename T::Type>((buffer->*f)()); });
117 }
118 91
119 template <typename T, typename Buffer> 92 void addMapping(const QByteArray &property, const std::function<QVariant(void const *)> &mapping)
120 void addMapping(const flatbuffers::Vector<flatbuffers::Offset<flatbuffers::String>> *(Buffer::*f)() const)
121 { 93 {
122 addMapping(T::name, [f](Buffer const *buffer) -> QVariant { return propertyToVariant<typename T::Type>((buffer->*f)()); }); 94 mReadAccessors.insert(property, mapping);
123 }
124
125 template <typename T, typename Buffer>
126 void addMapping(const Sink::ApplicationDomain::Buffer::MailContact *(Buffer::*f)() const)
127 {
128 addMapping(T::name, [f](Buffer const *buffer) -> QVariant { return propertyToVariant<typename T::Type>((buffer->*f)()); });
129 }
130
131 template <typename T, typename Buffer>
132 void addMapping(const flatbuffers::Vector<flatbuffers::Offset<Sink::ApplicationDomain::Buffer::MailContact>> *(Buffer::*f)() const)
133 {
134 addMapping(T::name, [f](Buffer const *buffer) -> QVariant { return propertyToVariant<typename T::Type>((buffer->*f)()); });
135 } 95 }
136 96
137 template <typename T, typename Buffer> 97 template <typename T, typename Buffer, typename FunctionReturnValue>
138 void addMapping(const flatbuffers::Vector<flatbuffers::Offset<Sink::ApplicationDomain::Buffer::ContactEmail>> *(Buffer::*f)() const) 98 void addMapping(FunctionReturnValue (Buffer::*f)() const)
139 { 99 {
140 addMapping(T::name, [f](Buffer const *buffer) -> QVariant { return propertyToVariant<typename T::Type>((buffer->*f)()); }); 100 addMapping(T::name, [f](void const *buffer) -> QVariant { return propertyToVariant<typename T::Type>((static_cast<const Buffer*>(buffer)->*f)()); });
141 } 101 }
142 102
143private: 103private:
144 QHash<QByteArray, std::function<QVariant(BufferType const *)>> mReadAccessors; 104 QHash<QByteArray, std::function<QVariant(void const *)>> mReadAccessors;
145}; 105};
146 106
147template <typename BufferBuilder> 107template <typename BufferBuilder>
@@ -157,10 +117,12 @@ public:
157 builderCalls << accessor(value, fbb); 117 builderCalls << accessor(value, fbb);
158 } 118 }
159 } 119 }
120
160 bool hasMapping(const QByteArray &key) const 121 bool hasMapping(const QByteArray &key) const
161 { 122 {
162 return mWriteAccessors.contains(key); 123 return mWriteAccessors.contains(key);
163 } 124 }
125
164 void addMapping(const QByteArray &property, const std::function<std::function<void(BufferBuilder &)>(const QVariant &, flatbuffers::FlatBufferBuilder &)> &mapping) 126 void addMapping(const QByteArray &property, const std::function<std::function<void(BufferBuilder &)>(const QVariant &, flatbuffers::FlatBufferBuilder &)> &mapping)
165 { 127 {
166 mWriteAccessors.insert(property, mapping); 128 mWriteAccessors.insert(property, mapping);
@@ -182,53 +144,8 @@ public:
182 }); 144 });
183 } 145 }
184 146
185 template <typename T> 147 template <typename T, typename Arg>
186 void addMapping(void (BufferBuilder::*f)(flatbuffers::Offset<flatbuffers::String>)) 148 void addMapping(void (BufferBuilder::*f)(flatbuffers::Offset<Arg>))
187 {
188 addMapping(T::name, [f](const QVariant &value, flatbuffers::FlatBufferBuilder &fbb) -> std::function<void(BufferBuilder &)> {
189 auto offset = variantToProperty<typename T::Type>(value, fbb);
190 return [offset, f](BufferBuilder &builder) { (builder.*f)(offset); };
191 });
192 }
193
194 template <typename T>
195 void addMapping(void (BufferBuilder::*f)(flatbuffers::Offset<flatbuffers::Vector<uint8_t>>))
196 {
197 addMapping(T::name, [f](const QVariant &value, flatbuffers::FlatBufferBuilder &fbb) -> std::function<void(BufferBuilder &)> {
198 auto offset = variantToProperty<typename T::Type>(value, fbb);
199 return [offset, f](BufferBuilder &builder) { (builder.*f)(offset); };
200 });
201 }
202
203 template <typename T>
204 void addMapping(void (BufferBuilder::*f)(flatbuffers::Offset<flatbuffers::Vector<flatbuffers::Offset<flatbuffers::String>>>))
205 {
206 addMapping(T::name, [f](const QVariant &value, flatbuffers::FlatBufferBuilder &fbb) -> std::function<void(BufferBuilder &)> {
207 auto offset = variantToProperty<typename T::Type>(value, fbb);
208 return [offset, f](BufferBuilder &builder) { (builder.*f)(offset); };
209 });
210 }
211
212 template <typename T>
213 void addMapping(void (BufferBuilder::*f)(flatbuffers::Offset<Sink::ApplicationDomain::Buffer::MailContact>))
214 {
215 addMapping(T::name, [f](const QVariant &value, flatbuffers::FlatBufferBuilder &fbb) -> std::function<void(BufferBuilder &)> {
216 auto offset = variantToProperty<typename T::Type>(value, fbb);
217 return [offset, f](BufferBuilder &builder) { (builder.*f)(offset); };
218 });
219 }
220
221 template <typename T>
222 void addMapping(void (BufferBuilder::*f)(flatbuffers::Offset<flatbuffers::Vector<flatbuffers::Offset<Sink::ApplicationDomain::Buffer::MailContact>>>))
223 {
224 addMapping(T::name, [f](const QVariant &value, flatbuffers::FlatBufferBuilder &fbb) -> std::function<void(BufferBuilder &)> {
225 auto offset = variantToProperty<typename T::Type>(value, fbb);
226 return [offset, f](BufferBuilder &builder) { (builder.*f)(offset); };
227 });
228 }
229
230 template <typename T>
231 void addMapping(void (BufferBuilder::*f)(flatbuffers::Offset<flatbuffers::Vector<flatbuffers::Offset<Sink::ApplicationDomain::Buffer::ContactEmail>>>))
232 { 149 {
233 addMapping(T::name, [f](const QVariant &value, flatbuffers::FlatBufferBuilder &fbb) -> std::function<void(BufferBuilder &)> { 150 addMapping(T::name, [f](const QVariant &value, flatbuffers::FlatBufferBuilder &fbb) -> std::function<void(BufferBuilder &)> {
234 auto offset = variantToProperty<typename T::Type>(value, fbb); 151 auto offset = variantToProperty<typename T::Type>(value, fbb);