File indexing completed on 2024-05-19 05:26:10
0001 /* 0002 * Copyright (C) 2014 Christian Mollekopf <chrigi_1@fastmail.fm> 0003 * 0004 * This library is free software; you can redistribute it and/or 0005 * modify it under the terms of the GNU Lesser General Public 0006 * License as published by the Free Software Foundation; either 0007 * version 2.1 of the License, or (at your option) version 3, or any 0008 * later version accepted by the membership of KDE e.V. (or its 0009 * successor approved by the membership of KDE e.V.), which shall 0010 * act as a proxy defined in Section 6 of version 3 of the license. 0011 * 0012 * This library is distributed in the hope that it will be useful, 0013 * but WITHOUT ANY WARRANTY; without even the implied warranty of 0014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 0015 * Lesser General Public License for more details. 0016 * 0017 * You should have received a copy of the GNU Lesser General Public 0018 * License along with this library. If not, see <http://www.gnu.org/licenses/>. 0019 */ 0020 0021 #pragma once 0022 0023 #include "sink_export.h" 0024 #include <QByteArray> 0025 #include <QDebug> 0026 #include <QMutex> 0027 #include <functional> 0028 #include <memory> 0029 0030 #include "facadeinterface.h" 0031 #include "applicationdomaintype.h" 0032 #include "resourcecontext.h" 0033 #include "log.h" 0034 0035 namespace Sink { 0036 0037 /** 0038 * Facade factory that returns a store facade implementation, by loading a plugin and providing the relevant implementation. 0039 * 0040 * If we were to provide default implementations for certain capabilities. Here would be the place to do so. 0041 */ 0042 class SINK_EXPORT FacadeFactory 0043 { 0044 public: 0045 typedef std::function<std::shared_ptr<void>(const ResourceContext &)> FactoryFunction; 0046 0047 void registerStaticFacades(); 0048 0049 static FacadeFactory &instance(); 0050 0051 static QByteArray key(const QByteArray &resource, const QByteArray &type); 0052 0053 template <class DomainType, class Facade> 0054 void registerFacade(const QByteArray &resource) 0055 { 0056 registerFacade(resource, [](const ResourceContext &context) { return std::make_shared<Facade>(context); }, ApplicationDomain::getTypeName<DomainType>()); 0057 } 0058 0059 template <class DomainType, class Facade> 0060 void registerFacade() 0061 { 0062 registerFacade(QByteArray(), [](const ResourceContext &) { return std::make_shared<Facade>(); }, ApplicationDomain::getTypeName<DomainType>()); 0063 } 0064 0065 /* 0066 * Allows the registrar to register a specific instance. 0067 * 0068 * Primarily for testing. 0069 */ 0070 template <class DomainType, class Facade> 0071 void registerFacade(const QByteArray &resource, const FactoryFunction &customFactoryFunction) 0072 { 0073 registerFacade(resource, customFactoryFunction, ApplicationDomain::getTypeName<DomainType>()); 0074 } 0075 0076 /* 0077 * Can be used to clear the factory. 0078 * 0079 * Primarily for testing. 0080 */ 0081 void resetFactory(); 0082 0083 template <class DomainType> 0084 std::shared_ptr<StoreFacade<DomainType>> getFacade(const QByteArray &resource, const QByteArray &instanceIdentifier) 0085 { 0086 const QByteArray typeName = ApplicationDomain::getTypeName<DomainType>(); 0087 const auto ptr = getFacade(resource, instanceIdentifier, typeName); 0088 //We have to check the pointer before the cast, otherwise a check would return true also for invalid instances. 0089 if (!ptr) { 0090 return std::shared_ptr<StoreFacade<DomainType>>(); 0091 } 0092 return std::static_pointer_cast<StoreFacade<DomainType>>(ptr); 0093 } 0094 0095 template <class DomainType> 0096 std::shared_ptr<StoreFacade<DomainType>> getFacade() 0097 { 0098 return getFacade<DomainType>(QByteArray(), QByteArray()); 0099 } 0100 0101 private: 0102 FacadeFactory(); 0103 std::shared_ptr<void> getFacade(const QByteArray &resource, const QByteArray &instanceIdentifier, const QByteArray &typeName); 0104 void registerFacade(const QByteArray &resource, const FactoryFunction &customFactoryFunction, const QByteArray typeName); 0105 0106 QHash<QByteArray, FactoryFunction> mFacadeRegistry; 0107 static QMutex sMutex; 0108 }; 0109 }