• Skip to content
  • Skip to link menu
KDE 4.2 API Reference
  • KDE API Reference
  • kdelibs
  • Sitemap
  • Contact Us
 

KDECore

klibloader.h

Go to the documentation of this file.
00001 /* This file is part of the KDE libraries
00002    Copyright (C) 1999 Torben Weis <weis@kde.org>
00003 
00004    This library is free software; you can redistribute it and/or
00005    modify it under the terms of the GNU Library General Public
00006    License version 2 as published by the Free Software Foundation.
00007 
00008    This library is distributed in the hope that it will be useful,
00009    but WITHOUT ANY WARRANTY; without even the implied warranty of
00010    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00011    Library General Public License for more details.
00012 
00013    You should have received a copy of the GNU Library General Public License
00014    along with this library; see the file COPYING.LIB.  If not, write to
00015    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00016    Boston, MA 02110-1301, USA.
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 //krazy:exclude=dpointer (private class is kept as a global static)
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, /*< the specified library could not be loaded. Use KLibLoader::lastErrorMessage for details*/
00165         ErrNoFactory, /*< the library does not export a factory */
00166         ErrNoComponent, /*< the factory does not support creating components of the specified type */
00167         ErrServiceProvidesNoLibrary, /*< the specified service provides no shared library (when using KService) */
00168         ErrNoServiceFound /*< no service implementing the given servicetype and fullfilling the given constraint expression can be found (when using KServiceTypeTrader) */
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

KDECore

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

kdelibs

Skip menu "kdelibs"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • Kate
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • Kross
  • KUtils
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver
Generated for kdelibs by doxygen 1.5.7
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal