• Skip to content
  • Skip to link menu
  • KDE API Reference
  • kdelibs-4.14.38 API Reference
  • KDE Home
  • Contact Us
 

KDECore

  • kdecore
  • util
klibloader.h
Go to the documentation of this file.
1/* This file is part of the KDE libraries
2 Copyright (C) 1999 Torben Weis <weis@kde.org>
3
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public
6 License version 2 as published by the Free Software Foundation.
7
8 This library is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 Library General Public License for more details.
12
13 You should have received a copy of the GNU Library General Public License
14 along with this library; see the file COPYING.LIB. If not, write to
15 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
16 Boston, MA 02110-1301, USA.
17*/
18#ifndef KLIBLOADER_H
19#define KLIBLOADER_H
20
21#include <kglobal.h>
22
23#include <QtCore/QObject>
24#include <QtCore/QStringList>
25#include <QtCore/QHash>
26#include <QtCore/QLibrary>
27#include <QtCore/QtPlugin>
28
29#include "kpluginfactory.h"
30#include "kpluginloader.h"
31#include "klibrary.h"
32
33#ifndef KDE_NO_DEPRECATED
34
35# define K_EXPORT_COMPONENT_FACTORY( libname, factory ) \
36 extern "C" { KDE_EXPORT KPluginFactory *init_##libname() { return new factory; } }
37
55class KDECORE_EXPORT KLibLoader : public QObject //krazy:exclude=dpointer (private class is kept as a global static)
56{
57 friend class KLibrary;
58 friend class KLibraryPrivate;
59 friend class KLibLoaderPrivate;
60
61 Q_OBJECT
62public:
84 KPluginFactory* factory( const QString &libname, QLibrary::LoadHints loadHint = 0);
85
107 KLibrary* library( const QString &libname, QLibrary::LoadHints loadHint = 0 );
108
116 QString lastErrorMessage() const;
117
132 void unloadLibrary( const QString &libname );
133
144 static KDE_DEPRECATED KLibLoader* self();
145
157 static QString findLibrary(const QString &libname, const KComponentData &cData = KGlobal::mainComponent());
158
165 enum ComponentLoadingError {
166 ErrNoLibrary = 1, /*< the specified library could not be loaded. Use KLibLoader::lastErrorMessage for details*/
167 ErrNoFactory, /*< the library does not export a factory */
168 ErrNoComponent, /*< the factory does not support creating components of the specified type */
169 ErrServiceProvidesNoLibrary, /*< the specified service provides no shared library (when using KService) */
170 ErrNoServiceFound /*< no service implementing the given servicetype and fullfilling the given constraint expression can be found (when using KServiceTypeTrader) */
171 };
172
183 static QString errorString( int componentLoadingError );
184
185
201 template <typename T>
202 static KDE_DEPRECATED T *createInstance(const QString &keyword, const QString &libname, QObject *parent = 0,
203 const QVariantList &args = QVariantList(),
204 int *error = 0 )
205 {
206 KLibrary *library = KLibLoader::self()->library( libname );
207 if ( !library )
208 {
209 if ( error )
210 *error = ErrNoLibrary;
211 return 0;
212 }
213 KPluginFactory *factory = library->factory();
214 if ( !factory )
215 {
216 library->unload();
217 if ( error )
218 *error = ErrNoFactory;
219 return 0;
220 }
221 QObject *object = factory->template create<T>(keyword, parent, args);
222 T *res = qobject_cast<T *>( object );
223 if ( !res )
224 {
225 delete object;
226 library->unload();
227 if ( error )
228 *error = ErrNoComponent;
229 }
230 return res;
231 }
232
247 template <typename T>
248 static KDE_DEPRECATED T *createInstance( const QString &libname, QObject *parent = 0,
249 const QVariantList &args = QVariantList(),
250 int *error = 0 )
251 {
252 return createInstance<T>(QString(), libname, parent, args, error);
253 }
254
259 template <typename T>
260 static KDE_DEPRECATED T *createInstance( const QString &libname, QObject *parent,
261 const QStringList &args,
262 int *error = 0 )
263 {
264 KLibrary *library = KLibLoader::self()->library( libname );
265 if ( !library )
266 {
267 if ( error )
268 *error = ErrNoLibrary;
269 return 0;
270 }
271 KPluginFactory *factory = library->factory();
272 if ( !factory )
273 {
274 library->unload();
275 if ( error )
276 *error = ErrNoFactory;
277 return 0;
278 }
279 QObject *object = factory->template create<T>(parent, args);
280 T *res = qobject_cast<T *>( object );
281 if ( !res )
282 {
283 delete object;
284 library->unload();
285 if ( error )
286 *error = ErrNoComponent;
287 }
288 return res;
289 }
290
291private:
292 ~KLibLoader();
293
294 KLibLoader();
295};
296
297#endif
298#endif
KComponentData
Per component data.
Definition: kcomponentdata.h:47
KLibLoader
The KLibLoader allows you to load libraries dynamically at runtime.
Definition: klibloader.h:56
KLibLoader::self
static KLibLoader * self()
Returns a pointer to the factory.
Definition: klibloader.cpp:46
KLibLoader::createInstance
static T * createInstance(const QString &libname, QObject *parent, const QStringList &args, int *error=0)
Definition: klibloader.h:260
KLibLoader::createInstance
static T * createInstance(const QString &keyword, const QString &libname, QObject *parent=0, const QVariantList &args=QVariantList(), int *error=0)
This template allows to load the specified library and ask the factory to create an instance of the g...
Definition: klibloader.h:202
KLibLoader::ComponentLoadingError
ComponentLoadingError
This enum type defines the possible error cases that can happen when loading a component.
Definition: klibloader.h:165
KLibLoader::ErrServiceProvidesNoLibrary
@ ErrServiceProvidesNoLibrary
Definition: klibloader.h:169
KLibLoader::ErrNoComponent
@ ErrNoComponent
Definition: klibloader.h:168
KLibLoader::ErrNoFactory
@ ErrNoFactory
Definition: klibloader.h:167
KLibLoader::createInstance
static T * createInstance(const QString &libname, QObject *parent=0, const QVariantList &args=QVariantList(), int *error=0)
This template allows to load the specified library and ask the factory to create an instance of the g...
Definition: klibloader.h:248
KLibLoader::library
KLibrary * library(const QString &libname, QLibrary::LoadHints loadHint=0)
Loads and initializes a library.
Definition: klibloader.cpp:89
KLibrary
Thin wrapper around QLibrary; you should rarely use this directly, see KPluginLoader for higher-level...
Definition: klibrary.h:39
KLibrary::unload
bool unload()
Definition: klibrary.h:79
KLibrary::factory
KPluginFactory * factory(const char *factoryname=0)
Returns the factory of the library.
Definition: klibrary.cpp:163
KPluginFactory
If you develop a library that is to be loaded dynamically at runtime, then you should return a pointe...
Definition: kpluginfactory.h:233
QObject
QStringList
QString
kglobal.h
findLibrary
QString findLibrary(const QString &name, const KComponentData &cData)
Definition: klibrary.cpp:39
klibrary.h
kpluginfactory.h
kpluginloader.h
T
#define T
KGlobal::mainComponent
const KComponentData & mainComponent()
Returns the global component data.
Definition: kglobal.cpp:145
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Mon Feb 20 2023 00:00:00 by doxygen 1.9.6 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

KDECore

Skip menu "KDECore"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Modules
  • Related Pages

kdelibs-4.14.38 API Reference

Skip menu "kdelibs-4.14.38 API Reference"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDEWebKit
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUnitConversion
  • KUtils
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver
Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal