File indexing completed on 2024-05-12 05:26:03
0001 /* 0002 * Copyright (C) 2016 Christian Mollekopf <mollekopf@kolabsys.com> 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 #pragma once 0021 0022 #include "domaintypeadaptorfactoryinterface.h" 0023 #include "applicationdomaintype.h" 0024 #include "resourceaccess.h" 0025 #include <QByteArray> 0026 #include <QMap> 0027 0028 namespace Sink { 0029 0030 /* 0031 * A context object that can be passed around so each part of the system knows in what context it works. 0032 * 0033 * This is necessary because we can't rely on a singleton or thread-local storage since multiple resources can be accessed from the same thread/process. 0034 */ 0035 struct ResourceContext { 0036 const QByteArray resourceInstanceIdentifier; 0037 const QByteArray resourceType; 0038 QMap<QByteArray, DomainTypeAdaptorFactoryInterface::Ptr> adaptorFactories; 0039 //TODO prehaps use a weak pointer to not mess up lifetime management 0040 ResourceAccessInterface::Ptr mResourceAccess; 0041 0042 0043 ResourceContext(const QByteArray &identifier, const QByteArray &resourceType_, const QMap<QByteArray, DomainTypeAdaptorFactoryInterface::Ptr> &factories = QMap<QByteArray, DomainTypeAdaptorFactoryInterface::Ptr>()) 0044 : resourceInstanceIdentifier(identifier), 0045 resourceType(resourceType_), 0046 adaptorFactories(factories) 0047 { 0048 } 0049 0050 QByteArray instanceId() const 0051 { 0052 return resourceInstanceIdentifier; 0053 } 0054 0055 DomainTypeAdaptorFactoryInterface &adaptorFactory(const QByteArray &type) const 0056 { 0057 auto factory = adaptorFactories.value(type); 0058 if (!factory) { 0059 qFatal("Failed to find a factory for %s", type.constData()); 0060 } 0061 return *factory; 0062 } 0063 0064 template<typename DomainType> 0065 DomainTypeAdaptorFactoryInterface &adaptorFactory() 0066 { 0067 return adaptorFactory(ApplicationDomain::getTypeName<DomainType>()); 0068 } 0069 0070 ResourceAccessInterface::Ptr resourceAccess() 0071 { 0072 if (!mResourceAccess) { 0073 mResourceAccess = ResourceAccessFactory::instance().getAccess(resourceInstanceIdentifier, resourceType); 0074 } 0075 return mResourceAccess; 0076 } 0077 }; 0078 0079 }