KDECore
klibloader.h
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #ifndef KLIBLOADER_H
00019 #define KLIBLOADER_H
00020
00021 #include <kglobal.h>
00022
00023 #include <QtCore/QObject>
00024 #include <QtCore/QStringList>
00025 #include <QtCore/QHash>
00026 #include <QtCore/QLibrary>
00027 #include <QtCore/QtPlugin>
00028
00029 #include "kpluginfactory.h"
00030 #include "kpluginloader.h"
00031 #include "klibrary.h"
00032
00033 # define K_EXPORT_COMPONENT_FACTORY( libname, factory ) \
00034 extern "C" { KDE_EXPORT KPluginFactory *init_##libname() { return new factory; } }
00035
00053 class KDECORE_EXPORT KLibLoader : public QObject
00054 {
00055 friend class KLibrary;
00056 friend class KLibraryPrivate;
00057 friend class KLibLoaderPrivate;
00058
00059 Q_OBJECT
00060 public:
00081 KPluginFactory* factory( const QString &libname, QLibrary::LoadHints loadHint = 0);
00082
00103 KLibrary* library( const QString &libname, QLibrary::LoadHints loadHint = 0 );
00104
00112 QString lastErrorMessage() const;
00113
00128 void unloadLibrary( const QString &libname );
00129
00138 static KDE_DEPRECATED KLibLoader* self();
00139
00150 static QString findLibrary(const QString &libname, const KComponentData &cData = KGlobal::mainComponent());
00151
00170 enum ComponentLoadingError { ErrNoLibrary = 1,
00171 ErrNoFactory,
00172 ErrNoComponent,
00173 ErrServiceProvidesNoLibrary,
00174 ErrNoServiceFound };
00175
00181 static QString errorString( int componentLoadingError );
00182
00183
00196 template <typename T>
00197 static KDE_DEPRECATED T *createInstance(const QString &keyword, const QString &libname, QObject *parent = 0,
00198 const QVariantList &args = QVariantList(),
00199 int *error = 0 )
00200 {
00201 KLibrary *library = KLibLoader::self()->library( libname );
00202 if ( !library )
00203 {
00204 if ( error )
00205 *error = ErrNoLibrary;
00206 return 0;
00207 }
00208 KPluginFactory *factory = library->factory();
00209 if ( !factory )
00210 {
00211 library->unload();
00212 if ( error )
00213 *error = ErrNoFactory;
00214 return 0;
00215 }
00216 QObject *object = factory->template create<T>(keyword, parent, args);
00217 T *res = qobject_cast<T *>( object );
00218 if ( !res )
00219 {
00220 delete object;
00221 library->unload();
00222 if ( error )
00223 *error = ErrNoComponent;
00224 }
00225 return res;
00226 }
00227
00228 template <typename T>
00229 static KDE_DEPRECATED T *createInstance( const QString &libname, QObject *parent = 0,
00230 const QVariantList &args = QVariantList(),
00231 int *error = 0 )
00232 {
00233 return createInstance<T>(QString(), libname, parent, args, error);
00234 }
00235
00240 template <typename T>
00241 static KDE_DEPRECATED T *createInstance( const QString &libname, QObject *parent,
00242 const QStringList &args,
00243 int *error = 0 )
00244 {
00245 KLibrary *library = KLibLoader::self()->library( libname );
00246 if ( !library )
00247 {
00248 if ( error )
00249 *error = ErrNoLibrary;
00250 return 0;
00251 }
00252 KPluginFactory *factory = library->factory();
00253 if ( !factory )
00254 {
00255 library->unload();
00256 if ( error )
00257 *error = ErrNoFactory;
00258 return 0;
00259 }
00260 QObject *object = factory->template create<T>(parent, args);
00261 T *res = qobject_cast<T *>( object );
00262 if ( !res )
00263 {
00264 delete object;
00265 library->unload();
00266 if ( error )
00267 *error = ErrNoComponent;
00268 }
00269 return res;
00270 }
00271
00272 private:
00273 ~KLibLoader();
00274
00275 KLibLoader();
00276 };
00277
00278 #endif