diff options
Diffstat (limited to 'dummyresource/domainadaptor.cpp')
-rw-r--r-- | dummyresource/domainadaptor.cpp | 111 |
1 files changed, 92 insertions, 19 deletions
diff --git a/dummyresource/domainadaptor.cpp b/dummyresource/domainadaptor.cpp index 74a8dd6..00af3fe 100644 --- a/dummyresource/domainadaptor.cpp +++ b/dummyresource/domainadaptor.cpp | |||
@@ -14,42 +14,115 @@ | |||
14 | using namespace DummyCalendar; | 14 | using namespace DummyCalendar; |
15 | using namespace flatbuffers; | 15 | using namespace flatbuffers; |
16 | 16 | ||
17 | /** | ||
18 | * Defines how to convert qt primitives to flatbuffer ones | ||
19 | * TODO: rename to createProperty or so? | ||
20 | */ | ||
21 | template <class T> | ||
22 | uoffset_t extractProperty(const QVariant &, flatbuffers::FlatBufferBuilder &fbb); | ||
23 | |||
24 | template <> | ||
25 | uoffset_t extractProperty<QString>(const QVariant &property, flatbuffers::FlatBufferBuilder &fbb) | ||
26 | { | ||
27 | if (property.isValid()) { | ||
28 | return fbb.CreateString(property.toString().toStdString()).o; | ||
29 | } | ||
30 | return 0; | ||
31 | } | ||
32 | |||
33 | /** | ||
34 | * Create a buffer from a domain object using the provided mappings | ||
35 | */ | ||
36 | template <class Builder> | ||
37 | void createBufferPart(const Akonadi2::ApplicationDomain::ApplicationDomainType &domainObject, flatbuffers::FlatBufferBuilder &fbb, const WritePropertyMapper<Builder> &mapper) | ||
38 | { | ||
39 | //First create a primitives such as strings using the mappings | ||
40 | QList<std::function<void(Builder &)> > propertiesToAddToResource; | ||
41 | for (const auto &property : domainObject.changedProperties()) { | ||
42 | const auto value = domainObject.getProperty(property); | ||
43 | if (mapper.hasMapping(property)) { | ||
44 | mapper.setProperty(property, domainObject.getProperty(property), propertiesToAddToResource, fbb); | ||
45 | } | ||
46 | } | ||
47 | |||
48 | //Then create all porperties using the above generated builderCalls | ||
49 | Builder builder(fbb); | ||
50 | for (auto propertyBuilder : propertiesToAddToResource) { | ||
51 | propertyBuilder(builder); | ||
52 | } | ||
53 | builder.Finish(); | ||
54 | } | ||
55 | |||
56 | |||
17 | 57 | ||
18 | DummyEventAdaptorFactory::DummyEventAdaptorFactory() | 58 | DummyEventAdaptorFactory::DummyEventAdaptorFactory() |
19 | : DomainTypeAdaptorFactory() | 59 | : DomainTypeAdaptorFactory() |
20 | { | 60 | { |
21 | //TODO turn this into initializePropertyMapper as well? | 61 | //TODO turn this into initializeReadPropertyMapper as well? |
22 | mResourceMapper = QSharedPointer<PropertyMapper<DummyEvent> >::create(); | 62 | mResourceMapper = QSharedPointer<ReadPropertyMapper<DummyEvent> >::create(); |
23 | mResourceMapper->mReadAccessors.insert("summary", [](DummyEvent const *buffer) -> QVariant { | 63 | mResourceMapper->addMapping("summary", [](DummyEvent const *buffer) -> QVariant { |
24 | if (buffer->summary()) { | 64 | if (buffer->summary()) { |
25 | return QString::fromStdString(buffer->summary()->c_str()); | 65 | return QString::fromStdString(buffer->summary()->c_str()); |
26 | } | 66 | } |
27 | return QVariant(); | 67 | return QVariant(); |
28 | }); | 68 | }); |
69 | |||
70 | mResourceWriteMapper->addMapping("summary", [](const QVariant &value, flatbuffers::FlatBufferBuilder &fbb) -> std::function<void(DummyEventBuilder &)> { | ||
71 | auto offset = extractProperty<QString>(value, fbb); | ||
72 | return [offset](DummyEventBuilder &builder) { builder.add_summary(offset); }; | ||
73 | }); | ||
29 | } | 74 | } |
30 | 75 | ||
31 | 76 | ||
32 | void DummyEventAdaptorFactory::createBuffer(const Akonadi2::ApplicationDomain::Event &event, flatbuffers::FlatBufferBuilder &fbb) | 77 | void DummyEventAdaptorFactory::createBuffer(const Akonadi2::ApplicationDomain::Event &event, flatbuffers::FlatBufferBuilder &fbb) |
33 | { | 78 | { |
34 | flatbuffers::FlatBufferBuilder eventFbb; | 79 | // flatbuffers::FlatBufferBuilder resFbb; |
35 | eventFbb.Clear(); | 80 | // flatbuffers::FlatBufferBuilder localFbb; |
36 | { | 81 | |
37 | auto summary = eventFbb.CreateString(event.getProperty("summary").toString().toStdString()); | 82 | // QList<std::function<void(DummyEventBuilder &)> > propertiesToAddToResource; |
38 | DummyCalendar::DummyEventBuilder eventBuilder(eventFbb); | 83 | // QList<std::function<void(Akonadi2::ApplicationDomain::Buffer::EventBuilder &)> > propertiesToAddToLocal; |
39 | eventBuilder.add_summary(summary); | 84 | // for (const auto &property : event.changedProperties()) { |
40 | auto eventLocation = eventBuilder.Finish(); | 85 | // const auto value = event.getProperty(property); |
41 | DummyCalendar::FinishDummyEventBuffer(eventFbb, eventLocation); | 86 | // if (mResourceWriteMapper && mResourceWriteMapper->hasMapping(property)) { |
42 | } | 87 | // mResourceWriteMapper->setProperty(property, value, propertiesToAddToResource, resFbb); |
88 | // } if (mLocalWriteMapper && mLocalWriteMapper->hasMapping(property)) { | ||
89 | // mLocalWriteMapper->setProperty(property, value, propertiesToAddToLocal, localFbb); | ||
90 | // } | ||
91 | // } | ||
43 | 92 | ||
93 | // DummyEventBuilder resBuilder(resFbb); | ||
94 | // for (auto propertyBuilder : propertiesToAddToResource) { | ||
95 | // propertyBuilder(resBuilder); | ||
96 | // } | ||
97 | // resBuilder.Finish(); | ||
98 | |||
99 | // DummyEventBuilder localBuilder(localFbb); | ||
100 | // for (auto propertyBuilder : propertiesToAddToResource) { | ||
101 | // propertyBuilder(localBuilder); | ||
102 | // } | ||
103 | // localBuilder.Finish(); | ||
104 | |||
105 | // TODO: how does a resource specify what goes to a local buffer and what it stores separately? | ||
106 | // flatbuffers::FlatBufferBuilder eventFbb; | ||
107 | // { | ||
108 | // auto summary = extractProperty<QString>(event.getProperty("summary"), fbb); | ||
109 | // DummyCalendar::DummyEventBuilder eventBuilder(eventFbb); | ||
110 | // eventBuilder.add_summary(summary); | ||
111 | // auto eventLocation = eventBuilder.Finish(); | ||
112 | // DummyCalendar::FinishDummyEventBuffer(eventFbb, eventLocation); | ||
113 | // } | ||
114 | |||
115 | //TODO we should only copy values into the local buffer that haven't already been copied by the resource buffer | ||
44 | flatbuffers::FlatBufferBuilder localFbb; | 116 | flatbuffers::FlatBufferBuilder localFbb; |
45 | { | 117 | if (mLocalWriteMapper) { |
46 | auto uid = localFbb.CreateString(event.getProperty("uid").toString().toStdString()); | 118 | createBufferPart<Akonadi2::ApplicationDomain::Buffer::EventBuilder>(event, localFbb, *mLocalWriteMapper); |
47 | auto localBuilder = Akonadi2::ApplicationDomain::Buffer::EventBuilder(localFbb); | 119 | } |
48 | localBuilder.add_uid(uid); | 120 | |
49 | auto location = localBuilder.Finish(); | 121 | flatbuffers::FlatBufferBuilder resFbb; |
50 | Akonadi2::ApplicationDomain::Buffer::FinishEventBuffer(localFbb, location); | 122 | if (mResourceWriteMapper) { |
123 | createBufferPart<DummyEventBuilder>(event, resFbb, *mResourceWriteMapper); | ||
51 | } | 124 | } |
52 | 125 | ||
53 | Akonadi2::EntityBuffer::assembleEntityBuffer(fbb, 0, 0, eventFbb.GetBufferPointer(), eventFbb.GetSize(), localFbb.GetBufferPointer(), localFbb.GetSize()); | 126 | Akonadi2::EntityBuffer::assembleEntityBuffer(fbb, 0, 0, resFbb.GetBufferPointer(), resFbb.GetSize(), localFbb.GetBufferPointer(), localFbb.GetSize()); |
54 | } | 127 | } |
55 | 128 | ||