From 4f1afa39bec38ea38820115e1bbabf4506c2d45c Mon Sep 17 00:00:00 2001 From: Christian Mollekopf Date: Thu, 9 Apr 2015 18:16:39 +0200 Subject: Moved generic parts of the domain adaptor to common --- common/CMakeLists.txt | 1 + common/domainadaptor.cpp | 40 ++++++++++++++++ common/domainadaptor.h | 102 +++++++++++++++++++++++++++++++++++----- dummyresource/domainadaptor.cpp | 74 +---------------------------- dummyresource/domainadaptor.h | 1 - 5 files changed, 132 insertions(+), 86 deletions(-) create mode 100644 common/domainadaptor.cpp diff --git a/common/CMakeLists.txt b/common/CMakeLists.txt index 650691c..b06718f 100644 --- a/common/CMakeLists.txt +++ b/common/CMakeLists.txt @@ -30,6 +30,7 @@ set(command_SRCS commands.cpp console.cpp pipeline.cpp + domainadaptor.cpp resource.cpp resourceaccess.cpp storage_common.cpp diff --git a/common/domainadaptor.cpp b/common/domainadaptor.cpp new file mode 100644 index 0000000..5b7c427 --- /dev/null +++ b/common/domainadaptor.cpp @@ -0,0 +1,40 @@ +/* + * Copyright (C) 2015 Christian Mollekopf + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the + * Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#include "domainadaptor.h" + +template <> +QSharedPointer > initializePropertyMapper() +{ + auto propertyMapper = QSharedPointer >::create(); + propertyMapper->mReadAccessors.insert("summary", [](Akonadi2::ApplicationDomain::Buffer::Event const *buffer) -> QVariant { + if (buffer->summary()) { + return QString::fromStdString(buffer->summary()->c_str()); + } + return QVariant(); + }); + propertyMapper->mReadAccessors.insert("uid", [](Akonadi2::ApplicationDomain::Buffer::Event const *buffer) -> QVariant { + if (buffer->uid()) { + return QString::fromStdString(buffer->uid()->c_str()); + } + return QVariant(); + }); + return propertyMapper; +} + 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 @@ #include #include "clientapi.h" //for domain parts +#include "event_generated.h" +#include "entity_generated.h" +#include "metadata_generated.h" +#include "entitybuffer.h" + /** - * The property mapper holds accessor functions for all properties. + * The property mapper is a non-typesafe virtual dispatch. * - * It is by default initialized with accessors that access the local-only buffer, - * and resource simply have to overwrite those accessors. + * Instead of using an interface and requring each implementation to override + * a virtual method per property, the property mapper can be filled with accessors + * that extract the properties from resource types. */ template class PropertyMapper @@ -55,20 +61,92 @@ public: QHash > mWriteAccessors; }; -//The factory should define how to go from an entitybuffer (local + resource buffer), to a domain type adapter. -//It defines how values are split accross local and resource buffer. -//This is required by the facade the read the value, and by the pipeline preprocessors to access the domain values in a generic way. -// template -// class DomainTypeAdaptorFactory -// { -// }; +/** + * A generic adaptor implementation that uses a property mapper to read/write values. + */ +template +class GenericBufferAdaptor : public Akonadi2::ApplicationDomain::BufferAdaptor +{ +public: + GenericBufferAdaptor() + : BufferAdaptor() + { + + } + + void setProperty(const QByteArray &key, const QVariant &value) + { + if (mResourceMapper && mResourceMapper->mWriteAccessors.contains(key)) { + // mResourceMapper->setProperty(key, value, mResourceBuffer); + } else { + // mLocalMapper.; + } + } + + virtual QVariant getProperty(const QByteArray &key) const + { + if (mResourceBuffer && mResourceMapper->mReadAccessors.contains(key)) { + return mResourceMapper->getProperty(key, mResourceBuffer); + } else if (mLocalBuffer && mLocalMapper->mReadAccessors.contains(key)) { + return mLocalMapper->getProperty(key, mLocalBuffer); + } + qWarning() << "no mapping available for key " << key; + return QVariant(); + } + + virtual QList availableProperties() const + { + QList props; + props << mResourceMapper->mReadAccessors.keys(); + props << mLocalMapper->mReadAccessors.keys(); + return props; + } + + LocalBuffer const *mLocalBuffer; + ResourceBuffer const *mResourceBuffer; + QSharedPointer > mLocalMapper; + QSharedPointer > mResourceMapper; +}; + +/** + * Initializes the local property mapper. + * + * Provide an implementation for each application domain type. + */ +template +QSharedPointer > initializePropertyMapper(); +/** + * The factory should define how to go from an entitybuffer (local + resource buffer), to a domain type adapter. + * It defines how values are split accross local and resource buffer. + * This is required by the facade the read the value, and by the pipeline preprocessors to access the domain values in a generic way. + */ template -class DomainTypeAdaptorFactory/* */ +class DomainTypeAdaptorFactory { public: + DomainTypeAdaptorFactory() : mLocalMapper(initializePropertyMapper()) {}; virtual ~DomainTypeAdaptorFactory() {}; - virtual QSharedPointer createAdaptor(const Akonadi2::Entity &entity) = 0; + + /** + * Creates an adaptor for the given domain and resource types. + * + * This returns by default a GenericBufferAdaptor initialized with the corresponding property mappers. + */ + virtual QSharedPointer createAdaptor(const Akonadi2::Entity &entity) + { + const auto resourceBuffer = Akonadi2::EntityBuffer::readBuffer(entity.resource()); + const auto localBuffer = Akonadi2::EntityBuffer::readBuffer(entity.local()); + // const auto metadataBuffer = Akonadi2::EntityBuffer::readBuffer(entity.metadata()); + + auto adaptor = QSharedPointer >::create(); + adaptor->mLocalBuffer = localBuffer; + adaptor->mLocalMapper = mLocalMapper; + adaptor->mResourceBuffer = resourceBuffer; + adaptor->mResourceMapper = mResourceMapper; + return adaptor; + } + virtual void createBuffer(const Akonadi2::ApplicationDomain::Event &event, flatbuffers::FlatBufferBuilder &fbb) {}; protected: diff --git a/dummyresource/domainadaptor.cpp b/dummyresource/domainadaptor.cpp index b133614..74a8dd6 100644 --- a/dummyresource/domainadaptor.cpp +++ b/dummyresource/domainadaptor.cpp @@ -14,55 +14,11 @@ using namespace DummyCalendar; using namespace flatbuffers; -//This will become a generic implementation that simply takes the resource buffer and local buffer pointer -class DummyEventAdaptor : public Akonadi2::ApplicationDomain::BufferAdaptor -{ -public: - DummyEventAdaptor() - : BufferAdaptor() - { - - } - - void setProperty(const QByteArray &key, const QVariant &value) - { - if (mResourceMapper && mResourceMapper->mWriteAccessors.contains(key)) { - // mResourceMapper->setProperty(key, value, mResourceBuffer); - } else { - // mLocalMapper.; - } - } - - virtual QVariant getProperty(const QByteArray &key) const - { - if (mResourceBuffer && mResourceMapper->mReadAccessors.contains(key)) { - return mResourceMapper->getProperty(key, mResourceBuffer); - } else if (mLocalBuffer && mLocalMapper->mReadAccessors.contains(key)) { - return mLocalMapper->getProperty(key, mLocalBuffer); - } - qWarning() << "no mapping available for key " << key; - return QVariant(); - } - - virtual QList availableProperties() const - { - QList props; - props << mResourceMapper->mReadAccessors.keys(); - props << mLocalMapper->mReadAccessors.keys(); - return props; - } - - Akonadi2::ApplicationDomain::Buffer::Event const *mLocalBuffer; - DummyEvent const *mResourceBuffer; - - QSharedPointer > mLocalMapper; - QSharedPointer > mResourceMapper; -}; - DummyEventAdaptorFactory::DummyEventAdaptorFactory() : DomainTypeAdaptorFactory() { + //TODO turn this into initializePropertyMapper as well? mResourceMapper = QSharedPointer >::create(); mResourceMapper->mReadAccessors.insert("summary", [](DummyEvent const *buffer) -> QVariant { if (buffer->summary()) { @@ -70,36 +26,8 @@ DummyEventAdaptorFactory::DummyEventAdaptorFactory() } return QVariant(); }); - mLocalMapper = QSharedPointer >::create(); - mLocalMapper->mReadAccessors.insert("summary", [](Akonadi2::ApplicationDomain::Buffer::Event const *buffer) -> QVariant { - if (buffer->summary()) { - return QString::fromStdString(buffer->summary()->c_str()); - } - return QVariant(); - }); - mLocalMapper->mReadAccessors.insert("uid", [](Akonadi2::ApplicationDomain::Buffer::Event const *buffer) -> QVariant { - if (buffer->uid()) { - return QString::fromStdString(buffer->uid()->c_str()); - } - return QVariant(); - }); - } -//TODO pass EntityBuffer instead? -QSharedPointer DummyEventAdaptorFactory::createAdaptor(const Akonadi2::Entity &entity) -{ - const auto resourceBuffer = Akonadi2::EntityBuffer::readBuffer(entity.resource()); - const auto localBuffer = Akonadi2::EntityBuffer::readBuffer(entity.local()); - // const auto metadataBuffer = Akonadi2::EntityBuffer::readBuffer(entity.metadata()); - - auto adaptor = QSharedPointer::create(); - adaptor->mLocalBuffer = localBuffer; - adaptor->mLocalMapper = mLocalMapper; - adaptor->mResourceBuffer = resourceBuffer; - adaptor->mResourceMapper = mResourceMapper; - return adaptor; -} void DummyEventAdaptorFactory::createBuffer(const Akonadi2::ApplicationDomain::Event &event, flatbuffers::FlatBufferBuilder &fbb) { diff --git a/dummyresource/domainadaptor.h b/dummyresource/domainadaptor.h index 143121a..0e2b883 100644 --- a/dummyresource/domainadaptor.h +++ b/dummyresource/domainadaptor.h @@ -10,6 +10,5 @@ class DummyEventAdaptorFactory : public DomainTypeAdaptorFactory createAdaptor(const Akonadi2::Entity &entity); virtual void createBuffer(const Akonadi2::ApplicationDomain::Event &event, flatbuffers::FlatBufferBuilder &fbb); }; -- cgit v1.2.3