summaryrefslogtreecommitdiffstats
path: root/common/facadefactory.h
diff options
context:
space:
mode:
authorChristian Mollekopf <chrigi_1@fastmail.fm>2015-07-24 17:26:25 +0200
committerChristian Mollekopf <chrigi_1@fastmail.fm>2015-07-27 23:22:10 +0200
commit7ae8c8719497ec7556f532bab061d3758976f792 (patch)
tree8ace2300cebcf6926c7053e3f23a5f68c304ad98 /common/facadefactory.h
parent4430eabdfeddf02c20424508a2a4207b6b4a764e (diff)
downloadsink-7ae8c8719497ec7556f532bab061d3758976f792.tar.gz
sink-7ae8c8719497ec7556f532bab061d3758976f792.zip
Mode FacadeFactory to separate file, mutex protected it, and loaded
resource The factory is potentially used from several queries simultaneously, so it's now mutex protected. Additionally we try to load the plugins directly in the factory.
Diffstat (limited to 'common/facadefactory.h')
-rw-r--r--common/facadefactory.h91
1 files changed, 91 insertions, 0 deletions
diff --git a/common/facadefactory.h b/common/facadefactory.h
new file mode 100644
index 0000000..4a6a698
--- /dev/null
+++ b/common/facadefactory.h
@@ -0,0 +1,91 @@
1/*
2 * Copyright (C) 2014 Christian Mollekopf <chrigi_1@fastmail.fm>
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) version 3, or any
8 * later version accepted by the membership of KDE e.V. (or its
9 * successor approved by the membership of KDE e.V.), which shall
10 * act as a proxy defined in Section 6 of version 3 of the license.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
19 */
20
21#pragma once
22
23#include <QByteArray>
24#include <QDebug>
25#include <QMutex>
26#include <functional>
27#include <memory>
28
29#include "facadeinterface.h"
30#include "domain/applicationdomaintype.h"
31#include "log.h"
32
33namespace Akonadi2 {
34
35/**
36 * Facade factory that returns a store facade implementation, by loading a plugin and providing the relevant implementation.
37 *
38 * If we were to provide default implementations for certain capabilities. Here would be the place to do so.
39 */
40class FacadeFactory {
41public:
42 typedef std::function<std::shared_ptr<void>(const QByteArray &)> FactoryFunction;
43
44 void registerStaticFacades();
45
46 static FacadeFactory &instance();
47
48 static QByteArray key(const QByteArray &resource, const QByteArray &type);
49
50 template<class DomainType, class Facade>
51 void registerFacade(const QByteArray &resource)
52 {
53 registerFacade(resource, [](const QByteArray &instanceIdentifier){ return std::make_shared<Facade>(instanceIdentifier); }, ApplicationDomain::getTypeName<DomainType>());
54 }
55
56 /*
57 * Allows the registrar to register a specific instance.
58 *
59 * Primarily for testing.
60 */
61 template<class DomainType, class Facade>
62 void registerFacade(const QByteArray &resource, const FactoryFunction &customFactoryFunction)
63 {
64 registerFacade(resource, customFactoryFunction, ApplicationDomain::getTypeName<DomainType>());
65 }
66
67 /*
68 * Can be used to clear the factory.
69 *
70 * Primarily for testing.
71 */
72 void resetFactory();
73
74 template<class DomainType>
75 std::shared_ptr<StoreFacade<DomainType> > getFacade(const QByteArray &resource, const QByteArray &instanceIdentifier)
76 {
77 const QByteArray typeName = ApplicationDomain::getTypeName<DomainType>();
78 return std::static_pointer_cast<StoreFacade<DomainType> >(getFacade(resource, instanceIdentifier, typeName));
79 }
80
81private:
82 FacadeFactory();
83 std::shared_ptr<void> getFacade(const QByteArray &resource, const QByteArray &instanceIdentifier, const QByteArray &typeName);
84 void registerFacade(const QByteArray &resource, const FactoryFunction &customFactoryFunction, const QByteArray typeName);
85
86 QHash<QByteArray, FactoryFunction> mFacadeRegistry;
87 static QMutex sMutex;
88};
89
90}
91