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:
00082 KPluginFactory* factory( const QString &libname, QLibrary::LoadHints loadHint = 0);
00083
00105 KLibrary* library( const QString &libname, QLibrary::LoadHints loadHint = 0 );
00106
00114 QString lastErrorMessage() const;
00115
00130 void unloadLibrary( const QString &libname );
00131
00142 static KDE_DEPRECATED KLibLoader* self();
00143
00155 static QString findLibrary(const QString &libname, const KComponentData &cData = KGlobal::mainComponent());
00156
00163 enum ComponentLoadingError {
00164 ErrNoLibrary = 1,
00165 ErrNoFactory,
00166 ErrNoComponent,
00167 ErrServiceProvidesNoLibrary,
00168 ErrNoServiceFound
00169 };
00170
00181 static QString errorString( int componentLoadingError );
00182
00183
00199 template <typename T>
00200 static KDE_DEPRECATED T *createInstance(const QString &keyword, const QString &libname, QObject *parent = 0,
00201 const QVariantList &args = QVariantList(),
00202 int *error = 0 )
00203 {
00204 KLibrary *library = KLibLoader::self()->library( libname );
00205 if ( !library )
00206 {
00207 if ( error )
00208 *error = ErrNoLibrary;
00209 return 0;
00210 }
00211 KPluginFactory *factory = library->factory();
00212 if ( !factory )
00213 {
00214 library->unload();
00215 if ( error )
00216 *error = ErrNoFactory;
00217 return 0;
00218 }
00219 QObject *object = factory->template create<T>(keyword, parent, args);
00220 T *res = qobject_cast<T *>( object );
00221 if ( !res )
00222 {
00223 delete object;
00224 library->unload();
00225 if ( error )
00226 *error = ErrNoComponent;
00227 }
00228 return res;
00229 }
00230
00245 template <typename T>
00246 static KDE_DEPRECATED T *createInstance( const QString &libname, QObject *parent = 0,
00247 const QVariantList &args = QVariantList(),
00248 int *error = 0 )
00249 {
00250 return createInstance<T>(QString(), libname, parent, args, error);
00251 }
00252
00257 template <typename T>
00258 static KDE_DEPRECATED T *createInstance( const QString &libname, QObject *parent,
00259 const QStringList &args,
00260 int *error = 0 )
00261 {
00262 KLibrary *library = KLibLoader::self()->library( libname );
00263 if ( !library )
00264 {
00265 if ( error )
00266 *error = ErrNoLibrary;
00267 return 0;
00268 }
00269 KPluginFactory *factory = library->factory();
00270 if ( !factory )
00271 {
00272 library->unload();
00273 if ( error )
00274 *error = ErrNoFactory;
00275 return 0;
00276 }
00277 QObject *object = factory->template create<T>(parent, args);
00278 T *res = qobject_cast<T *>( object );
00279 if ( !res )
00280 {
00281 delete object;
00282 library->unload();
00283 if ( error )
00284 *error = ErrNoComponent;
00285 }
00286 return res;
00287 }
00288
00289 private:
00290 ~KLibLoader();
00291
00292 KLibLoader();
00293 };
00294
00295 #endif