File indexing completed on 2024-05-12 05:25:57
0001 /* 0002 * Copyright (C) 2016 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 #include "adaptorfactoryregistry.h" 0021 0022 #include <QByteArray> 0023 #include <QDebug> 0024 #include <QMutex> 0025 #include <functional> 0026 #include <memory> 0027 0028 #include "domaintypeadaptorfactoryinterface.h" 0029 #include "applicationdomaintype.h" 0030 #include "log.h" 0031 0032 using namespace Sink; 0033 0034 AdaptorFactoryRegistry &AdaptorFactoryRegistry::instance() 0035 { 0036 // QMutexLocker locker(&sMutex); 0037 static AdaptorFactoryRegistry *instance = nullptr; 0038 if (!instance) { 0039 instance = new AdaptorFactoryRegistry; 0040 } 0041 return *instance; 0042 } 0043 0044 static QByteArray key(const QByteArray &resource, const QByteArray &type) 0045 { 0046 return resource + type; 0047 } 0048 0049 AdaptorFactoryRegistry::AdaptorFactoryRegistry() 0050 { 0051 0052 } 0053 0054 std::shared_ptr<DomainTypeAdaptorFactoryInterface> AdaptorFactoryRegistry::getFactory(const QByteArray &resource, const QByteArray &typeName) 0055 { 0056 const auto ptr = mRegistry.value(key(resource, typeName)); 0057 //We have to check the pointer before the cast, otherwise a check would return true also for invalid instances. 0058 if (!ptr) { 0059 return std::shared_ptr<DomainTypeAdaptorFactoryInterface>(); 0060 } 0061 return std::static_pointer_cast<DomainTypeAdaptorFactoryInterface>(ptr); 0062 } 0063 0064 QMap<QByteArray, DomainTypeAdaptorFactoryInterface::Ptr> AdaptorFactoryRegistry::getFactories(const QByteArray &resource) 0065 { 0066 QMap<QByteArray, DomainTypeAdaptorFactoryInterface::Ptr> map; 0067 for (const auto &type : mTypes.values(resource)) { 0068 auto f = getFactory(resource, type); 0069 //Convert the std::shared_ptr to a QSharedPointer 0070 map.insert(type, DomainTypeAdaptorFactoryInterface::Ptr(f.get(), [](DomainTypeAdaptorFactoryInterface *) {})); 0071 } 0072 return map; 0073 } 0074 0075 void AdaptorFactoryRegistry::registerFactory(const QByteArray &resource, const std::shared_ptr<void> &instance, const QByteArray typeName) 0076 { 0077 mTypes.insert(resource, typeName); 0078 mRegistry.insert(key(resource, typeName), instance); 0079 } 0080