From e88ffd5209d58118fcc947a060c35504316da855 Mon Sep 17 00:00:00 2001 From: Christian Mollekopf Date: Wed, 30 Nov 2016 16:08:09 +0100 Subject: TypeHelper so we can centrally define the type dispatch Not pretty, but that at least allows us to centrally define the string to type dispatch (It's somehow very hard to do in C++ without repeating all types over and over in various interfaces). --- common/domain/applicationdomaintype_p.h | 45 +++++++++++++++++++++++++++++++++ common/storage/entitystore.cpp | 21 +++++---------- 2 files changed, 52 insertions(+), 14 deletions(-) create mode 100644 common/domain/applicationdomaintype_p.h (limited to 'common') diff --git a/common/domain/applicationdomaintype_p.h b/common/domain/applicationdomaintype_p.h new file mode 100644 index 0000000..aedf21f --- /dev/null +++ b/common/domain/applicationdomaintype_p.h @@ -0,0 +1,45 @@ +/* + * Copyright (C) 2014 Christian Mollekopf + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) version 3, or any + * later version accepted by the membership of KDE e.V. (or its + * successor approved by the membership of KDE e.V.), which shall + * act as a proxy defined in Section 6 of version 3 of the license. + * + * This library 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 + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library. If not, see . + */ +#pragma once + +#include "applicationdomaintype.h" + +template class Func> +struct TypeHelper { + const QByteArray type; + TypeHelper(const QByteArray &type_) + : type(type_) + { + + } + + template + R operator()(Args && ... args) const { + if (type == Sink::ApplicationDomain::getTypeName()) { + return Func{}(std::forward(args...)); + } else if (type == Sink::ApplicationDomain::getTypeName()) { + return Func{}(std::forward(args...)); + } else if (type == Sink::ApplicationDomain::getTypeName()) { + return Func{}(std::forward(args...)); + } else { + Q_ASSERT(false); + } + } +}; diff --git a/common/storage/entitystore.cpp b/common/storage/entitystore.cpp index f6b8f43..b6251b6 100644 --- a/common/storage/entitystore.cpp +++ b/common/storage/entitystore.cpp @@ -27,6 +27,7 @@ #include "index.h" #include "bufferutils.h" #include "entity_generated.h" +#include "applicationdomaintype_p.h" #include "mail.h" #include "folder.h" @@ -57,20 +58,12 @@ public: return transaction; } - template - void configure(const QByteArray &type, Args && ... args) - { - if (type == ApplicationDomain::getTypeName()) { - ApplicationDomain::TypeImplementation::configure(std::forward(args...)); - } else if (type == ApplicationDomain::getTypeName()) { - ApplicationDomain::TypeImplementation::configure(std::forward(args...)); - } else if (type == ApplicationDomain::getTypeName()) { - ApplicationDomain::TypeImplementation::configure(std::forward(args...)); - } else { - Q_ASSERT(false); - SinkError() << "Unkonwn type " << type; + template + struct ConfigureHelper { + void operator()(TypeIndex &arg) const { + ApplicationDomain::TypeImplementation::configure(arg); } - } + }; TypeIndex &cachedIndex(const QByteArray &type) { @@ -78,7 +71,7 @@ public: return *indexByType.value(type); } auto index = QSharedPointer::create(type); - configure(type, *index); + TypeHelper{type}.template operator()(*index); indexByType.insert(type, index); return *index; -- cgit v1.2.3