From e297fb92c2c0e344d36e0aef57921f6b9b921a61 Mon Sep 17 00:00:00 2001 From: Christian Mollekopf Date: Sun, 31 May 2015 20:16:23 +0200 Subject: Moved default read/write property mapper to TypeImplementation There is always exactly one default buffer that we can centralize in TypeImplementation. --- common/domain/applicationdomaintype.h | 9 +++++++++ common/domain/event.cpp | 34 ++++++++++++++++++++++++++++++++-- common/domain/event.h | 19 +++++++++++++++---- 3 files changed, 56 insertions(+), 6 deletions(-) (limited to 'common/domain') diff --git a/common/domain/applicationdomaintype.h b/common/domain/applicationdomaintype.h index 2de1460..f2a4ad6 100644 --- a/common/domain/applicationdomaintype.h +++ b/common/domain/applicationdomaintype.h @@ -130,5 +130,14 @@ QByteArray getTypeName(); template<> QByteArray getTypeName(); +/** + * Type implementation. + * + * Needs to be implemented for every application domain type. + * Contains all non-resource specific, but type-specific code. + */ +template +class TypeImplementation; + } } diff --git a/common/domain/event.cpp b/common/domain/event.cpp index 86100b7..ea0931c 100644 --- a/common/domain/event.cpp +++ b/common/domain/event.cpp @@ -20,15 +20,19 @@ #include #include +#include #include "../resultset.h" #include "../index.h" #include "../storage.h" #include "../log.h" +#include "../propertymapper.h" + +#include "event_generated.h" using namespace Akonadi2::ApplicationDomain; -ResultSet EventImplementation::queryIndexes(const Akonadi2::Query &query, const QByteArray &resourceInstanceIdentifier) +ResultSet TypeImplementation::queryIndexes(const Akonadi2::Query &query, const QByteArray &resourceInstanceIdentifier) { QVector keys; if (query.propertyFilter.contains("uid")) { @@ -43,7 +47,7 @@ ResultSet EventImplementation::queryIndexes(const Akonadi2::Query &query, const return ResultSet(keys); } -void EventImplementation::index(const Event &type) +void TypeImplementation::index(const Event &type) { Index uidIndex(Akonadi2::Store::storageLocation(), type.resourceInstanceIdentifier() + "index.uid", Akonadi2::Storage::ReadWrite); const auto uid = type.getProperty("uid"); @@ -51,3 +55,29 @@ void EventImplementation::index(const Event &type) uidIndex.add(uid.toByteArray(), type.identifier()); } } + +QSharedPointer > TypeImplementation::initializeReadPropertyMapper() +{ + auto propertyMapper = QSharedPointer >::create(); + propertyMapper->addMapping("summary", [](Buffer::Event const *buffer) -> QVariant { + return propertyToVariant(buffer->summary()); + }); + propertyMapper->addMapping("uid", [](Buffer::Event const *buffer) -> QVariant { + return propertyToVariant(buffer->uid()); + }); + return propertyMapper; +} + +QSharedPointer > TypeImplementation::initializeWritePropertyMapper() +{ + auto propertyMapper = QSharedPointer >::create(); + propertyMapper->addMapping("summary", [](const QVariant &value, flatbuffers::FlatBufferBuilder &fbb) -> std::function { + auto offset = variantToProperty(value, fbb); + return [offset](Buffer::EventBuilder &builder) { builder.add_summary(offset); }; + }); + propertyMapper->addMapping("uid", [](const QVariant &value, flatbuffers::FlatBufferBuilder &fbb) -> std::function { + auto offset = variantToProperty(value, fbb); + return [offset](Buffer::EventBuilder &builder) { builder.add_uid(offset); }; + }); + return propertyMapper; +} diff --git a/common/domain/event.h b/common/domain/event.h index 4cb0d34..d4ce20e 100644 --- a/common/domain/event.h +++ b/common/domain/event.h @@ -23,23 +23,34 @@ class ResultSet; class QByteArray; +template +class ReadPropertyMapper; +template +class WritePropertyMapper; + namespace Akonadi2 { class Query; namespace ApplicationDomain { + namespace Buffer { + class Event; + class EventBuilder; + } /** * Implements all type-specific code such as updating and querying indexes. */ -namespace EventImplementation { - typedef Event DomainType; +template<> +struct TypeImplementation { /** * Returns the potential result set based on the indexes. * * An empty result set indicates that a full scan is required. */ - ResultSet queryIndexes(const Akonadi2::Query &query, const QByteArray &resourceInstanceIdentifier); - void index(const Event &type); + static ResultSet queryIndexes(const Akonadi2::Query &query, const QByteArray &resourceInstanceIdentifier); + static void index(const Event &type); + static QSharedPointer > initializeReadPropertyMapper(); + static QSharedPointer > initializeWritePropertyMapper(); }; } -- cgit v1.2.3