diff options
Diffstat (limited to 'dummyresource/domainadaptor.cpp')
-rw-r--r-- | dummyresource/domainadaptor.cpp | 91 |
1 files changed, 13 insertions, 78 deletions
diff --git a/dummyresource/domainadaptor.cpp b/dummyresource/domainadaptor.cpp index 00af3fe..fa00bbc 100644 --- a/dummyresource/domainadaptor.cpp +++ b/dummyresource/domainadaptor.cpp | |||
@@ -9,49 +9,12 @@ | |||
9 | #include "entity_generated.h" | 9 | #include "entity_generated.h" |
10 | #include "metadata_generated.h" | 10 | #include "metadata_generated.h" |
11 | #include "domainadaptor.h" | 11 | #include "domainadaptor.h" |
12 | #include "log.h" | ||
12 | #include <common/entitybuffer.h> | 13 | #include <common/entitybuffer.h> |
13 | 14 | ||
14 | using namespace DummyCalendar; | 15 | using namespace DummyCalendar; |
15 | using namespace flatbuffers; | 16 | using namespace flatbuffers; |
16 | 17 | ||
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 | 18 | ||
56 | 19 | ||
57 | 20 | ||
@@ -59,7 +22,6 @@ DummyEventAdaptorFactory::DummyEventAdaptorFactory() | |||
59 | : DomainTypeAdaptorFactory() | 22 | : DomainTypeAdaptorFactory() |
60 | { | 23 | { |
61 | //TODO turn this into initializeReadPropertyMapper as well? | 24 | //TODO turn this into initializeReadPropertyMapper as well? |
62 | mResourceMapper = QSharedPointer<ReadPropertyMapper<DummyEvent> >::create(); | ||
63 | mResourceMapper->addMapping("summary", [](DummyEvent const *buffer) -> QVariant { | 25 | mResourceMapper->addMapping("summary", [](DummyEvent const *buffer) -> QVariant { |
64 | if (buffer->summary()) { | 26 | if (buffer->summary()) { |
65 | return QString::fromStdString(buffer->summary()->c_str()); | 27 | return QString::fromStdString(buffer->summary()->c_str()); |
@@ -76,51 +38,24 @@ DummyEventAdaptorFactory::DummyEventAdaptorFactory() | |||
76 | 38 | ||
77 | void DummyEventAdaptorFactory::createBuffer(const Akonadi2::ApplicationDomain::Event &event, flatbuffers::FlatBufferBuilder &fbb) | 39 | void DummyEventAdaptorFactory::createBuffer(const Akonadi2::ApplicationDomain::Event &event, flatbuffers::FlatBufferBuilder &fbb) |
78 | { | 40 | { |
79 | // flatbuffers::FlatBufferBuilder resFbb; | ||
80 | // flatbuffers::FlatBufferBuilder localFbb; | ||
81 | |||
82 | // QList<std::function<void(DummyEventBuilder &)> > propertiesToAddToResource; | ||
83 | // QList<std::function<void(Akonadi2::ApplicationDomain::Buffer::EventBuilder &)> > propertiesToAddToLocal; | ||
84 | // for (const auto &property : event.changedProperties()) { | ||
85 | // const auto value = event.getProperty(property); | ||
86 | // if (mResourceWriteMapper && mResourceWriteMapper->hasMapping(property)) { | ||
87 | // mResourceWriteMapper->setProperty(property, value, propertiesToAddToResource, resFbb); | ||
88 | // } if (mLocalWriteMapper && mLocalWriteMapper->hasMapping(property)) { | ||
89 | // mLocalWriteMapper->setProperty(property, value, propertiesToAddToLocal, localFbb); | ||
90 | // } | ||
91 | // } | ||
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 | ||
116 | flatbuffers::FlatBufferBuilder localFbb; | 41 | flatbuffers::FlatBufferBuilder localFbb; |
117 | if (mLocalWriteMapper) { | 42 | if (mLocalWriteMapper) { |
118 | createBufferPart<Akonadi2::ApplicationDomain::Buffer::EventBuilder>(event, localFbb, *mLocalWriteMapper); | 43 | auto pos = createBufferPart<Akonadi2::ApplicationDomain::Buffer::EventBuilder, Akonadi2::ApplicationDomain::Buffer::Event>(event, localFbb, *mLocalWriteMapper); |
44 | Akonadi2::ApplicationDomain::Buffer::FinishEventBuffer(localFbb, pos); | ||
45 | flatbuffers::Verifier verifier(localFbb.GetBufferPointer(), localFbb.GetSize()); | ||
46 | if (!verifier.VerifyBuffer<Akonadi2::ApplicationDomain::Buffer::Event>()) { | ||
47 | Warning() << "Created invalid local buffer"; | ||
48 | } | ||
119 | } | 49 | } |
120 | 50 | ||
121 | flatbuffers::FlatBufferBuilder resFbb; | 51 | flatbuffers::FlatBufferBuilder resFbb; |
122 | if (mResourceWriteMapper) { | 52 | if (mResourceWriteMapper) { |
123 | createBufferPart<DummyEventBuilder>(event, resFbb, *mResourceWriteMapper); | 53 | auto pos = createBufferPart<DummyEventBuilder, DummyEvent>(event, resFbb, *mResourceWriteMapper); |
54 | DummyCalendar::FinishDummyEventBuffer(resFbb, pos); | ||
55 | flatbuffers::Verifier verifier(resFbb.GetBufferPointer(), resFbb.GetSize()); | ||
56 | if (!verifier.VerifyBuffer<DummyEvent>()) { | ||
57 | Warning() << "Created invalid resource buffer"; | ||
58 | } | ||
124 | } | 59 | } |
125 | 60 | ||
126 | Akonadi2::EntityBuffer::assembleEntityBuffer(fbb, 0, 0, resFbb.GetBufferPointer(), resFbb.GetSize(), localFbb.GetBufferPointer(), localFbb.GetSize()); | 61 | Akonadi2::EntityBuffer::assembleEntityBuffer(fbb, 0, 0, resFbb.GetBufferPointer(), resFbb.GetSize(), localFbb.GetBufferPointer(), localFbb.GetSize()); |