1 /* 2 This file is part of GammaRay, the Qt application inspection and 3 manipulation tool. 4 5 Copyright (C) 2011-2021 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com 6 Author: Volker Krause <volker.krause@kdab.com> 7 8 Licensees holding valid commercial KDAB GammaRay licenses may use this file in 9 accordance with GammaRay Commercial License Agreement provided with the Software. 10 11 Contact info@kdab.com if any conditions of this licensing are not clear to you. 12 13 This program is free software; you can redistribute it and/or modify 14 it under the terms of the GNU General Public License as published by 15 the Free Software Foundation, either version 2 of the License, or 16 (at your option) any later version. 17 18 This program is distributed in the hope that it will be useful, 19 but WITHOUT ANY WARRANTY; without even the implied warranty of 20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 21 GNU General Public License for more details. 22 23 You should have received a copy of the GNU General Public License 24 along with this program. If not, see <http://www.gnu.org/licenses/>. 25 */ 26 27 #ifndef GAMMARAY_PROXYFACTORYBASE_H 28 #define GAMMARAY_PROXYFACTORYBASE_H 29 30 #include "plugininfo.h" 31 32 #include <QCoreApplication> 33 #include <QObject> 34 #include <QString> 35 36 #include <iostream> 37 38 namespace GammaRay { 39 /** Base class for wrappers for potentially not yet loaded plugins. */ 40 class ProxyFactoryBase : public QObject 41 { 42 Q_OBJECT 43 public: 44 explicit ProxyFactoryBase(const PluginInfo &pluginInfo, QObject *parent = nullptr); 45 ~ProxyFactoryBase() override; 46 47 PluginInfo pluginInfo() const; 48 QString errorString() const; 49 50 protected: 51 void loadPlugin(); 52 53 QObject *m_factory; 54 QString m_errorString; 55 56 private: 57 PluginInfo m_pluginInfo; 58 }; 59 60 template<typename IFace> 61 class ProxyFactory : public ProxyFactoryBase, public IFace 62 { 63 public: 64 explicit inline ProxyFactory(const PluginInfo &pluginInfo, QObject *parent = nullptr) ProxyFactoryBase(pluginInfo,parent)65 : ProxyFactoryBase(pluginInfo, parent) {} 66 inline ~ProxyFactory() override = default; 67 id()68 QString id() const override 69 { 70 return pluginInfo().id(); 71 } 72 73 protected: factory()74 IFace *factory() 75 { 76 loadPlugin(); 77 IFace *iface = qobject_cast<IFace *>(m_factory); 78 if (!iface) { 79 m_errorString = qApp->translate("GammaRay::ProxyFactory", 80 "Plugin does not provide an instance of %1.") 81 .arg(qobject_interface_iid<IFace *>()); 82 std::cerr << "Failed to cast object from " << qPrintable(pluginInfo().path()) 83 << " to " << qobject_interface_iid<IFace *>() << std::endl; 84 } 85 return iface; 86 } 87 }; 88 } 89 90 #endif // GAMMARAY_PROXYFACTORYBASE_H 91