summaryrefslogtreecommitdiffstats
path: root/common/domainadaptor.h
diff options
context:
space:
mode:
authorChristian Mollekopf <chrigi_1@fastmail.fm>2015-04-09 18:16:39 +0200
committerChristian Mollekopf <chrigi_1@fastmail.fm>2015-04-09 18:16:39 +0200
commit4f1afa39bec38ea38820115e1bbabf4506c2d45c (patch)
treefbcf157e01151a19fe3f35539f62aeeef9fd6c44 /common/domainadaptor.h
parentcc3fed3668a80616ec78bc872e21b7ac06bcde38 (diff)
downloadsink-4f1afa39bec38ea38820115e1bbabf4506c2d45c.tar.gz
sink-4f1afa39bec38ea38820115e1bbabf4506c2d45c.zip
Moved generic parts of the domain adaptor to common
Diffstat (limited to 'common/domainadaptor.h')
-rw-r--r--common/domainadaptor.h102
1 files changed, 90 insertions, 12 deletions
diff --git a/common/domainadaptor.h b/common/domainadaptor.h
index df3f8e5..15e3067 100644
--- a/common/domainadaptor.h
+++ b/common/domainadaptor.h
@@ -25,11 +25,17 @@
25#include <functional> 25#include <functional>
26#include "clientapi.h" //for domain parts 26#include "clientapi.h" //for domain parts
27 27
28#include "event_generated.h"
29#include "entity_generated.h"
30#include "metadata_generated.h"
31#include "entitybuffer.h"
32
28/** 33/**
29 * The property mapper holds accessor functions for all properties. 34 * The property mapper is a non-typesafe virtual dispatch.
30 * 35 *
31 * It is by default initialized with accessors that access the local-only buffer, 36 * Instead of using an interface and requring each implementation to override
32 * and resource simply have to overwrite those accessors. 37 * a virtual method per property, the property mapper can be filled with accessors
38 * that extract the properties from resource types.
33 */ 39 */
34template<typename BufferType> 40template<typename BufferType>
35class PropertyMapper 41class PropertyMapper
@@ -55,20 +61,92 @@ public:
55 QHash<QByteArray, std::function<void(const QVariant &, BufferType*)> > mWriteAccessors; 61 QHash<QByteArray, std::function<void(const QVariant &, BufferType*)> > mWriteAccessors;
56}; 62};
57 63
58//The factory should define how to go from an entitybuffer (local + resource buffer), to a domain type adapter. 64/**
59//It defines how values are split accross local and resource buffer. 65 * A generic adaptor implementation that uses a property mapper to read/write values.
60//This is required by the facade the read the value, and by the pipeline preprocessors to access the domain values in a generic way. 66 */
61// template<typename DomainType, typename LocalBuffer, typename ResourceBuffer> 67template <class LocalBuffer, class ResourceBuffer>
62// class DomainTypeAdaptorFactory 68class GenericBufferAdaptor : public Akonadi2::ApplicationDomain::BufferAdaptor
63// { 69{
64// }; 70public:
71 GenericBufferAdaptor()
72 : BufferAdaptor()
73 {
74
75 }
76
77 void setProperty(const QByteArray &key, const QVariant &value)
78 {
79 if (mResourceMapper && mResourceMapper->mWriteAccessors.contains(key)) {
80 // mResourceMapper->setProperty(key, value, mResourceBuffer);
81 } else {
82 // mLocalMapper.;
83 }
84 }
85
86 virtual QVariant getProperty(const QByteArray &key) const
87 {
88 if (mResourceBuffer && mResourceMapper->mReadAccessors.contains(key)) {
89 return mResourceMapper->getProperty(key, mResourceBuffer);
90 } else if (mLocalBuffer && mLocalMapper->mReadAccessors.contains(key)) {
91 return mLocalMapper->getProperty(key, mLocalBuffer);
92 }
93 qWarning() << "no mapping available for key " << key;
94 return QVariant();
95 }
96
97 virtual QList<QByteArray> availableProperties() const
98 {
99 QList<QByteArray> props;
100 props << mResourceMapper->mReadAccessors.keys();
101 props << mLocalMapper->mReadAccessors.keys();
102 return props;
103 }
104
105 LocalBuffer const *mLocalBuffer;
106 ResourceBuffer const *mResourceBuffer;
107 QSharedPointer<PropertyMapper<LocalBuffer> > mLocalMapper;
108 QSharedPointer<PropertyMapper<ResourceBuffer> > mResourceMapper;
109};
110
111/**
112 * Initializes the local property mapper.
113 *
114 * Provide an implementation for each application domain type.
115 */
116template <class T>
117QSharedPointer<PropertyMapper<T> > initializePropertyMapper();
65 118
119/**
120 * The factory should define how to go from an entitybuffer (local + resource buffer), to a domain type adapter.
121 * It defines how values are split accross local and resource buffer.
122 * This is required by the facade the read the value, and by the pipeline preprocessors to access the domain values in a generic way.
123 */
66template<typename DomainType, typename LocalBuffer, typename ResourceBuffer> 124template<typename DomainType, typename LocalBuffer, typename ResourceBuffer>
67class DomainTypeAdaptorFactory/* <typename DomainType, LocalBuffer, ResourceBuffer> */ 125class DomainTypeAdaptorFactory
68{ 126{
69public: 127public:
128 DomainTypeAdaptorFactory() : mLocalMapper(initializePropertyMapper<LocalBuffer>()) {};
70 virtual ~DomainTypeAdaptorFactory() {}; 129 virtual ~DomainTypeAdaptorFactory() {};
71 virtual QSharedPointer<Akonadi2::ApplicationDomain::BufferAdaptor> createAdaptor(const Akonadi2::Entity &entity) = 0; 130
131 /**
132 * Creates an adaptor for the given domain and resource types.
133 *
134 * This returns by default a GenericBufferAdaptor initialized with the corresponding property mappers.
135 */
136 virtual QSharedPointer<Akonadi2::ApplicationDomain::BufferAdaptor> createAdaptor(const Akonadi2::Entity &entity)
137 {
138 const auto resourceBuffer = Akonadi2::EntityBuffer::readBuffer<ResourceBuffer>(entity.resource());
139 const auto localBuffer = Akonadi2::EntityBuffer::readBuffer<LocalBuffer>(entity.local());
140 // const auto metadataBuffer = Akonadi2::EntityBuffer::readBuffer<Akonadi2::Metadata>(entity.metadata());
141
142 auto adaptor = QSharedPointer<GenericBufferAdaptor<LocalBuffer, ResourceBuffer> >::create();
143 adaptor->mLocalBuffer = localBuffer;
144 adaptor->mLocalMapper = mLocalMapper;
145 adaptor->mResourceBuffer = resourceBuffer;
146 adaptor->mResourceMapper = mResourceMapper;
147 return adaptor;
148 }
149
72 virtual void createBuffer(const Akonadi2::ApplicationDomain::Event &event, flatbuffers::FlatBufferBuilder &fbb) {}; 150 virtual void createBuffer(const Akonadi2::ApplicationDomain::Event &event, flatbuffers::FlatBufferBuilder &fbb) {};
73 151
74protected: 152protected: