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

KDECore

  • sources
  • kde-4.14
  • kdelibs
  • 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 
34 class KPluginFactoryPrivate;
35 namespace KParts { class Part; }
36 
37 #define K_PLUGIN_FACTORY_DECLARATION_WITH_BASEFACTORY(name, baseFactory) \
38 class 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) \
50 K_GLOBAL_STATIC(KComponentData, name##factorycomponentdata) \
51 name::name(const char *componentName, const char *catalogName, QObject *parent) \
52  : baseFactory(componentName, catalogName, parent) { init(); } \
53 name::name(const KAboutData &aboutData, QObject *parent) \
54  : baseFactory(aboutData, parent) { init(); } \
55 void name::init() \
56 { \
57  if (name##factorycomponentdata->isValid()) \
58  setComponentData(*name##factorycomponentdata); \
59  else \
60  *name##factorycomponentdata = KPluginFactory::componentData(); \
61  pluginRegistrations \
62 } \
63 name::~name() {} \
64 KComponentData 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 
232 class KDECORE_EXPORT KPluginFactory : public QObject
233 {
234  Q_OBJECT
235  Q_DECLARE_PRIVATE(KPluginFactory)
236 public:
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 
359 Q_SIGNALS:
360  void objectCreated(QObject *object);
361 
362 protected:
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 
498 private:
499  void registerPlugin(const QString &keyword, const QMetaObject *metaObject, CreateInstanceFunction instanceFunction);
500 };
501 
502 typedef KPluginFactory KLibFactory;
503 
504 template<typename T>
505 inline 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 
516 template<typename T>
517 inline 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 
528 template<typename T>
529 inline 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
QObject::className
const char * className() const
KPluginFactory::createInstance
static QObject * createInstance(QWidget *parentWidget, QObject *parent, const QVariantList &args)
Definition: kpluginfactory.h:469
QWidget
KPluginFactory::InheritanceChecker::createInstanceFunction
CreateInstanceFunction createInstanceFunction(...)
Definition: kpluginfactory.h:495
KPluginFactory::create
T * create(QObject *parent=0, const QVariantList &args=QVariantList())
Use this method to create an object.
Definition: kpluginfactory.h:505
T
#define T
kdecore_export.h
KPluginFactory::create
T * create(QObject *parent, const QStringList &args)
Definition: kpluginfactory.h:343
KDE_CONSTRUCTOR_DEPRECATED
#define KDE_CONSTRUCTOR_DEPRECATED
The KDE_CONSTRUCTOR_DEPRECATED macro can be used to trigger compile-time warnings with newer compiler...
Definition: kdemacros.h.cmake:207
kglobal.h
KPluginFactory::InheritanceChecker
This is used to detect the arguments need for the constructor of plugin classes.
Definition: kpluginfactory.h:491
QMetaObject
KLibFactory
KPluginFactory KLibFactory
Definition: kpluginfactory.h:502
QObject
KAboutData
This class is used to store information about a program.
Definition: kaboutdata.h:192
QString
QStringList
KPluginFactory::InheritanceChecker::createInstanceFunction
CreateInstanceFunction createInstanceFunction(QWidget *)
Definition: kpluginfactory.h:494
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
KPluginFactoryPrivate
Definition: kpluginfactory_p.h:33
KPluginFactory::InheritanceChecker::createInstanceFunction
CreateInstanceFunction createInstanceFunction(KParts::Part *)
Definition: kpluginfactory.h:493
kexportplugin.h
KPluginFactory::create
QObject * create(QObject *parent=0, const char *classname="QObject", const QStringList &args=QStringList())
Definition: kpluginfactory.h:353
KPluginFactory::createPartInstance
static QObject * createPartInstance(QWidget *parentWidget, QObject *parent, const QVariantList &args)
Definition: kpluginfactory.h:481
KPluginFactory::d_ptr
KPluginFactoryPrivate *const d_ptr
Definition: kpluginfactory.h:422
QObject::isWidgetType
bool isWidgetType() const
kcomponentdata.h
QObject::parent
QObject * parent() const
KPluginFactory
If you develop a library that is to be loaded dynamically at runtime, then you should return a pointe...
Definition: kpluginfactory.h:232
KComponentData
Per component data.
Definition: kcomponentdata.h:46
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:22:11 by doxygen 1.8.7 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 API Reference

Skip menu "kdelibs 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

Search



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