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

KDECore

  • kdecore
  • util
kpluginfactory.h
Go to the documentation of this file.
1/* This file is part of the KDE project
2 Copyright (C) 2007 Matthias Kretz <kretz@kde.org>
3 Copyright (C) 2007 Bernhard Loos <nhuh.put@web.de>
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public
7 License as published by the Free Software Foundation; either
8 version 2 of the License, or (at your option) any later version.
9
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Library General Public License for more details.
14
15 You should have received a copy of the GNU Library General Public License
16 along with this library; see the file COPYING.LIB. If not, write to
17 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
19
20*/
21
22#ifndef KDECORE_KPLUGINFACTORY_H
23#define KDECORE_KPLUGINFACTORY_H
24
25#include "kdecore_export.h"
26
27#include <QtCore/QObject>
28#include <QtCore/QVariant>
29#include <QtCore/QStringList>
30#include <kcomponentdata.h>
31#include <kexportplugin.h>
32#include <kglobal.h>
33
34class KPluginFactoryPrivate;
35namespace KParts { class Part; }
36
37#define K_PLUGIN_FACTORY_DECLARATION_WITH_BASEFACTORY(name, baseFactory) \
38class name : public baseFactory \
39{ \
40 public: \
41 explicit name(const char * = 0, const char * = 0, QObject * = 0); \
42 explicit name(const KAboutData &, QObject * = 0); \
43 ~name(); \
44 static KComponentData componentData(); \
45 private: \
46 void init(); \
47};
48
49#define K_PLUGIN_FACTORY_DEFINITION_WITH_BASEFACTORY(name, baseFactory, pluginRegistrations) \
50K_GLOBAL_STATIC(KComponentData, name##factorycomponentdata) \
51name::name(const char *componentName, const char *catalogName, QObject *parent) \
52 : baseFactory(componentName, catalogName, parent) { init(); } \
53name::name(const KAboutData &aboutData, QObject *parent) \
54 : baseFactory(aboutData, parent) { init(); } \
55void name::init() \
56{ \
57 if (name##factorycomponentdata->isValid()) \
58 setComponentData(*name##factorycomponentdata); \
59 else \
60 *name##factorycomponentdata = KPluginFactory::componentData(); \
61 pluginRegistrations \
62} \
63name::~name() {} \
64KComponentData name::componentData() \
65{ \
66 return *name##factorycomponentdata; \
67}
68
69#define K_PLUGIN_FACTORY_WITH_BASEFACTORY(name, baseFactory, pluginRegistrations) \
70 K_PLUGIN_FACTORY_DECLARATION_WITH_BASEFACTORY(name, baseFactory) \
71 K_PLUGIN_FACTORY_DEFINITION_WITH_BASEFACTORY(name, baseFactory, pluginRegistrations)
72
127#define K_PLUGIN_FACTORY(name, pluginRegistrations) K_PLUGIN_FACTORY_WITH_BASEFACTORY(name, KPluginFactory, pluginRegistrations)
128
140#define K_PLUGIN_FACTORY_DECLARATION(name) K_PLUGIN_FACTORY_DECLARATION_WITH_BASEFACTORY(name, KPluginFactory)
141
156#define K_PLUGIN_FACTORY_DEFINITION(name, pluginRegistrations) K_PLUGIN_FACTORY_DEFINITION_WITH_BASEFACTORY(name, KPluginFactory, pluginRegistrations)
157
232class KDECORE_EXPORT KPluginFactory : public QObject
233{
234 Q_OBJECT
235 Q_DECLARE_PRIVATE(KPluginFactory)
236public:
247 explicit KPluginFactory(const char *componentName = 0, const char *catalogName = 0, QObject *parent = 0);
248
258 explicit KPluginFactory(const KAboutData &aboutData, QObject *parent = 0);
262#ifndef KDE_NO_DEPRECATED
263 KDE_CONSTRUCTOR_DEPRECATED explicit KPluginFactory(const KAboutData *aboutData, QObject *parent = 0);
264#endif
265
269#ifndef KDE_NO_DEPRECATED
270 explicit KDE_CONSTRUCTOR_DEPRECATED KPluginFactory(QObject *parent);
271#endif
272
277 virtual ~KPluginFactory();
278
291 KComponentData componentData() const;
292
304 template<typename T>
305 T *create(QObject *parent = 0, const QVariantList &args = QVariantList());
306
318 template<typename T>
319 T *create(const QString &keyword, QObject *parent = 0, const QVariantList &args = QVariantList());
320
334 template<typename T>
335 T *create(QWidget *parentWidget, QObject *parent, const QString &keyword = QString(), const QVariantList &args = QVariantList());
336
340#ifndef KDE_NO_DEPRECATED
341 template<typename T>
342 KDE_DEPRECATED
343 T *create(QObject *parent, const QStringList &args)
344 {
345 return create<T>(parent, stringListToVariantList(args));
346 }
347#endif
348
352#ifndef KDE_NO_DEPRECATED
353 KDE_DEPRECATED QObject *create(QObject *parent = 0, const char *classname = "QObject", const QStringList &args = QStringList())
354 {
355 return create(classname, 0, parent, stringListToVariantList(args), QString());
356 }
357#endif
358
359Q_SIGNALS:
360 void objectCreated(QObject *object);
361
362protected:
366 typedef QObject *(*CreateInstanceFunction)(QWidget *, QObject *, const QVariantList &);
367
368 explicit KPluginFactory(KPluginFactoryPrivate &dd, QObject *parent = 0);
369
401 template<class T>
402 void registerPlugin(const QString &keyword = QString(), CreateInstanceFunction instanceFunction
403 = InheritanceChecker<T>().createInstanceFunction(reinterpret_cast<T *>(0)))
404 {
405 registerPlugin(keyword, &T::staticMetaObject, instanceFunction);
406 }
407
412 QVariantList stringListToVariantList(const QStringList &list);
413
418 QStringList variantListToStringList(const QVariantList &list);
419
420 virtual void setupTranslations();
421
422 KPluginFactoryPrivate *const d_ptr;
423
427#ifndef KDE_NO_DEPRECATED
428 virtual KDE_DEPRECATED QObject *createObject(QObject *parent, const char *className, const QStringList &args);
429#endif
430
434#ifndef KDE_NO_DEPRECATED
435 virtual KDE_DEPRECATED KParts::Part *createPartObject(QWidget *parentWidget, QObject *parent, const char *classname, const QStringList &args);
436#endif
437
438
450 void setComponentData(const KComponentData &componentData);
451
466 virtual QObject *create(const char *iface, QWidget *parentWidget, QObject *parent, const QVariantList &args, const QString &keyword);
467
468 template<class impl, class ParentType>
469 static QObject *createInstance(QWidget *parentWidget, QObject *parent, const QVariantList &args)
470 {
471 Q_UNUSED(parentWidget);
472 ParentType *p = 0;
473 if (parent) {
474 p = qobject_cast<ParentType *>(parent);
475 Q_ASSERT(p);
476 }
477 return new impl(p, args);
478 }
479
480 template<class impl>
481 static QObject *createPartInstance(QWidget *parentWidget, QObject *parent, const QVariantList &args)
482 {
483 return new impl(parentWidget, parent, args);
484 }
485
490 template<class impl>
491 struct InheritanceChecker
492 {
493 CreateInstanceFunction createInstanceFunction(KParts::Part *) { return &createPartInstance<impl>; }
494 CreateInstanceFunction createInstanceFunction(QWidget *) { return &createInstance<impl, QWidget>; }
495 CreateInstanceFunction createInstanceFunction(...) { return &createInstance<impl, QObject>; }
496 };
497
498private:
499 void registerPlugin(const QString &keyword, const QMetaObject *metaObject, CreateInstanceFunction instanceFunction);
500};
501
502typedef KPluginFactory KLibFactory;
503
504template<typename T>
505inline T *KPluginFactory::create(QObject *parent, const QVariantList &args)
506{
507 QObject *o = create(T::staticMetaObject.className(), parent && parent->isWidgetType() ? reinterpret_cast<QWidget *>(parent): 0, parent, args, QString());
508
509 T *t = qobject_cast<T *>(o);
510 if (!t) {
511 delete o;
512 }
513 return t;
514}
515
516template<typename T>
517inline T *KPluginFactory::create(const QString &keyword, QObject *parent, const QVariantList &args)
518{
519 QObject *o = create(T::staticMetaObject.className(), parent && parent->isWidgetType() ? reinterpret_cast<QWidget *>(parent): 0, parent, args, keyword);
520
521 T *t = qobject_cast<T *>(o);
522 if (!t) {
523 delete o;
524 }
525 return t;
526}
527
528template<typename T>
529inline T *KPluginFactory::create(QWidget *parentWidget, QObject *parent, const QString &keyword, const QVariantList &args)
530{
531 QObject *o = create(T::staticMetaObject.className(), parentWidget, parent, args, keyword);
532
533 T *t = qobject_cast<T *>(o);
534 if (!t) {
535 delete o;
536 }
537 return t;
538}
539
540#endif // KDECORE_KPLUGINFACTORY_H
KAboutData
This class is used to store information about a program.
Definition: kaboutdata.h:193
KComponentData
Per component data.
Definition: kcomponentdata.h:47
KPluginFactoryPrivate
Definition: kpluginfactory_p.h:34
KPluginFactory
If you develop a library that is to be loaded dynamically at runtime, then you should return a pointe...
Definition: kpluginfactory.h:233
KPluginFactory::d_ptr
KPluginFactoryPrivate *const d_ptr
Definition: kpluginfactory.h:422
KPluginFactory::create
T * create(QObject *parent, const QStringList &args)
Definition: kpluginfactory.h:343
KPluginFactory::createInstance
static QObject * createInstance(QWidget *parentWidget, QObject *parent, const QVariantList &args)
Definition: kpluginfactory.h:469
KPluginFactory::createPartInstance
static QObject * createPartInstance(QWidget *parentWidget, QObject *parent, const QVariantList &args)
Definition: kpluginfactory.h:481
KPluginFactory::objectCreated
void objectCreated(QObject *object)
KPluginFactory::registerPlugin
void registerPlugin(const QString &keyword=QString(), CreateInstanceFunction instanceFunction=InheritanceChecker< T >().createInstanceFunction(reinterpret_cast< T * >(0)))
Registers a plugin with the factory.
Definition: kpluginfactory.h:402
KPluginFactory::create
T * create(QObject *parent=0, const QVariantList &args=QVariantList())
Use this method to create an object.
Definition: kpluginfactory.h:505
KPluginFactory::create
QObject * create(QObject *parent=0, const char *classname="QObject", const QStringList &args=QStringList())
Definition: kpluginfactory.h:353
QObject
QStringList
QString
kcomponentdata.h
kdecore_export.h
kexportplugin.h
kglobal.h
KLibFactory
KPluginFactory KLibFactory
Definition: kpluginfactory.h:502
T
#define T
KParts
Definition: kpluginfactory.h:35
KPluginFactory::InheritanceChecker
This is used to detect the arguments need for the constructor of plugin classes.
Definition: kpluginfactory.h:492
KPluginFactory::InheritanceChecker::createInstanceFunction
CreateInstanceFunction createInstanceFunction(...)
Definition: kpluginfactory.h:495
KPluginFactory::InheritanceChecker::createInstanceFunction
CreateInstanceFunction createInstanceFunction(QWidget *)
Definition: kpluginfactory.h:494
KPluginFactory::InheritanceChecker::createInstanceFunction
CreateInstanceFunction createInstanceFunction(KParts::Part *)
Definition: kpluginfactory.h:493
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