[prev in list] [next in list] [prev in thread] [next in thread] 

List:       kde-commits
Subject:    [sink/develop] common: TypeHelper so we can centrally define the type dispatch
From:       Christian Mollekopf <chrigi_1 () fastmail ! fm>
Date:       2016-12-02 10:08:34
Message-ID: E1cCkle-0004b8-T1 () code ! kde ! org
[Download RAW message or body]

Git commit e88ffd5209d58118fcc947a060c35504316da855 by Christian Mollekopf.
Committed on 30/11/2016 at 15:08.
Pushed by cmollekopf into branch 'develop'.

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).

A  +45   -0    common/domain/applicationdomaintype_p.h     [License: LGPL]
M  +7    -14   common/storage/entitystore.cpp

https://commits.kde.org/sink/e88ffd5209d58118fcc947a060c35504316da855

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 <chrigi_1@fastmail.fm>
+ *
+ * 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 <http://www.gnu.org/licenses/>.
+ */
+#pragma once
+
+#include "applicationdomaintype.h"
+
+template <template<typename> class Func>
+struct TypeHelper {
+    const QByteArray type;
+    TypeHelper(const QByteArray &type_)
+        : type(type_)
+    {
+
+    }
+
+    template <typename R, typename ...Args>
+    R operator()(Args && ... args) const {
+        if (type == \
Sink::ApplicationDomain::getTypeName<Sink::ApplicationDomain::Folder>()) { +          \
return Func<Sink::ApplicationDomain::Folder>{}(std::forward<Args...>(args...));  +    \
} else if (type == Sink::ApplicationDomain::getTypeName<Sink::ApplicationDomain::Mail>()) \
{ +            return \
Func<Sink::ApplicationDomain::Mail>{}(std::forward<Args...>(args...));  +        } \
else if (type == Sink::ApplicationDomain::getTypeName<Sink::ApplicationDomain::Event>()) \
{ +            return \
Func<Sink::ApplicationDomain::Event>{}(std::forward<Args...>(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<typename ...Args>
-    void configure(const QByteArray &type, Args && ... args)
-    {
-        if (type == ApplicationDomain::getTypeName<ApplicationDomain::Folder>()) {
-            ApplicationDomain::TypeImplementation<ApplicationDomain::Folder>::configure(std::forward<Args...>(args...));
                
-        } else if (type == \
                ApplicationDomain::getTypeName<ApplicationDomain::Mail>()) {
-            ApplicationDomain::TypeImplementation<ApplicationDomain::Mail>::configure(std::forward<Args...>(args...));
                
-        } else if (type == \
                ApplicationDomain::getTypeName<ApplicationDomain::Event>()) {
-            ApplicationDomain::TypeImplementation<ApplicationDomain::Event>::configure(std::forward<Args...>(args...));
                
-        } else {
-            Q_ASSERT(false);
-            SinkError() << "Unkonwn type " << type;
+    template <class T>
+    struct ConfigureHelper {
+        void operator()(TypeIndex &arg) const {
+            ApplicationDomain::TypeImplementation<T>::configure(arg);
         }
-    }
+    };
 
     TypeIndex &cachedIndex(const QByteArray &type)
     {
@@ -78,7 +71,7 @@ public:
             return *indexByType.value(type);
         }
         auto index = QSharedPointer<TypeIndex>::create(type);
-        configure(type, *index);
+        TypeHelper<ConfigureHelper>{type}.template operator()<void>(*index);
         indexByType.insert(type, index);
         return *index;
 


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic