KCoreAddons

kpluginfactory.h
1 /*
2  This file is part of the KDE project
3 
4  SPDX-FileCopyrightText: 2007 Matthias Kretz <[email protected]>
5  SPDX-FileCopyrightText: 2007 Bernhard Loos <[email protected]>
6 
7  SPDX-License-Identifier: LGPL-2.0-or-later
8 */
9 
10 #ifndef KPLUGINFACTORY_H
11 #define KPLUGINFACTORY_H
12 
13 #include "kcoreaddons_export.h"
14 
15 #include <QObject>
16 #include <QVariant>
17 #include <QStringList>
18 #include <kexportplugin.h> // for source compat
19 
20 class QWidget;
21 
22 class KPluginFactoryPrivate;
23 namespace KParts
24 {
25 class Part;
26 }
27 
28 #define KPluginFactory_iid "org.kde.KPluginFactory"
29 
30 #define K_PLUGIN_FACTORY_DECLARATION_WITH_BASEFACTORY_SKEL(name, baseFactory, ...) \
31  class name : public KPluginFactory \
32  { \
33  Q_OBJECT \
34  Q_INTERFACES(KPluginFactory) \
35  __VA_ARGS__ \
36  public: \
37  explicit name(); \
38  ~name(); \
39  };
40 
41 #define K_PLUGIN_FACTORY_DECLARATION_WITH_BASEFACTORY_JSON(name, baseFactory, json) \
42  K_PLUGIN_FACTORY_DECLARATION_WITH_BASEFACTORY_SKEL(name, baseFactory, Q_PLUGIN_METADATA(IID KPluginFactory_iid FILE json))
43 
44 #define K_PLUGIN_FACTORY_DECLARATION_WITH_BASEFACTORY(name, baseFactory) \
45  K_PLUGIN_FACTORY_DECLARATION_WITH_BASEFACTORY_SKEL(name, baseFactory, Q_PLUGIN_METADATA(IID KPluginFactory_iid))
46 
47 #define K_PLUGIN_FACTORY_DEFINITION_WITH_BASEFACTORY(name, baseFactory, pluginRegistrations) \
48  name::name() \
49  { \
50  pluginRegistrations \
51  } \
52  name::~name() {}
53 
54 #define K_PLUGIN_FACTORY_WITH_BASEFACTORY(name, baseFactory, pluginRegistrations) \
55  K_PLUGIN_FACTORY_DECLARATION_WITH_BASEFACTORY(name, baseFactory) \
56  K_PLUGIN_FACTORY_DEFINITION_WITH_BASEFACTORY(name, baseFactory, pluginRegistrations)
57 
58 #define K_PLUGIN_FACTORY_WITH_BASEFACTORY_JSON(name, baseFactory, jsonFile, pluginRegistrations) \
59  K_PLUGIN_FACTORY_DECLARATION_WITH_BASEFACTORY_JSON(name, baseFactory, jsonFile) \
60  K_PLUGIN_FACTORY_DEFINITION_WITH_BASEFACTORY(name, baseFactory, pluginRegistrations)
61 
107 #define K_PLUGIN_FACTORY(name, pluginRegistrations) K_PLUGIN_FACTORY_WITH_BASEFACTORY(name, KPluginFactory, pluginRegistrations)
108 
161 #define K_PLUGIN_FACTORY_WITH_JSON(name, jsonFile, pluginRegistrations) K_PLUGIN_FACTORY_WITH_BASEFACTORY_JSON(name, KPluginFactory, jsonFile, pluginRegistrations)
162 
199 #define K_PLUGIN_CLASS_WITH_JSON(classname, jsonFile) K_PLUGIN_FACTORY_WITH_JSON(classname ## Factory, jsonFile, registerPlugin<classname >();)
200 
212 #define K_PLUGIN_FACTORY_DECLARATION(name) K_PLUGIN_FACTORY_DECLARATION_WITH_BASEFACTORY(name, KPluginFactory)
213 
227 #define K_PLUGIN_FACTORY_DEFINITION(name, pluginRegistrations) K_PLUGIN_FACTORY_DEFINITION_WITH_BASEFACTORY(name, KPluginFactory, pluginRegistrations)
228 
318 class KCOREADDONS_EXPORT KPluginFactory : public QObject
319 {
320  Q_OBJECT
321  Q_DECLARE_PRIVATE(KPluginFactory)
322 public:
326  explicit KPluginFactory();
327 
331  ~KPluginFactory() override;
332 
344  template<typename T>
345  T *create(QObject *parent = nullptr, const QVariantList &args = QVariantList());
346 
358  template<typename T>
359  T *create(const QString &keyword, QObject *parent = nullptr, const QVariantList &args = QVariantList());
360 
374  template<typename T>
375  T *create(QWidget *parentWidget, QObject *parent, const QString &keyword = QString(), const QVariantList &args = QVariantList());
376 
377 #if KCOREADDONS_ENABLE_DEPRECATED_SINCE(4, 0)
378 
381  template<typename T>
382  KCOREADDONS_DEPRECATED_VERSION(4, 0, "Use KPluginFactory::create<T>(QObject *parent, const QVariantList &args)")
383  T *create(QObject *parent, const QStringList &args)
384  {
385  return create<T>(parent, stringListToVariantList(args));
386  }
387 
391  KCOREADDONS_DEPRECATED_VERSION(4, 0, "Use KPluginFactory::create<T>(QObject *parent, const QVariantList &args)")
392  QObject *create(QObject *parent = nullptr, const char *classname = "QObject", const QStringList &args = QStringList())
393  {
394  return create(classname, nullptr, parent, stringListToVariantList(args), QString());
395  }
396 #endif
397 
402  static QVariantList stringListToVariantList(const QStringList &list);
403 
408  static QStringList variantListToStringList(const QVariantList &list);
409 
410 Q_SIGNALS:
411  void objectCreated(QObject *object);
412 
413 protected:
417  typedef QObject *(*CreateInstanceFunction)(QWidget *, QObject *, const QVariantList &);
418 
423  template<class impl>
425  CreateInstanceFunction createInstanceFunction(KParts::Part *)
426  {
427  return &createPartInstance<impl>;
428  }
429  CreateInstanceFunction createInstanceFunction(QWidget *)
430  {
431  return &createInstance<impl, QWidget>;
432  }
433  CreateInstanceFunction createInstanceFunction(...)
434  {
435  return &createInstance<impl, QObject>;
436  }
437  };
438 
439  explicit KPluginFactory(KPluginFactoryPrivate &dd);
440 
472  template<class T>
473  void registerPlugin(const QString &keyword = QString(), CreateInstanceFunction instanceFunction
474  = InheritanceChecker<T>().createInstanceFunction(static_cast<T *>(nullptr)))
475  {
476  registerPlugin(keyword, &T::staticMetaObject, instanceFunction);
477  }
478 
479  KPluginFactoryPrivate *const d_ptr;
480 
481 #if KCOREADDONS_BUILD_DEPRECATED_SINCE(4, 0)
482 
485  KCOREADDONS_DEPRECATED_VERSION(4, 0, "Use KPluginFactory::create<T>(QObject *parent, const QVariantList &args)")
486  virtual QObject *createObject(QObject *parent, const char *className, const QStringList &args);
487 
491  KCOREADDONS_DEPRECATED_VERSION(4, 0, "Use KPluginFactory::create<T>(QWidget *parentWidget, QObject *parent, const QString &keyword, const QVariantList &args)")
492  virtual KParts::Part *createPartObject(QWidget *parentWidget, QObject *parent, const char *classname, const QStringList &args);
493 #endif
494 
509  virtual QObject *create(const char *iface, QWidget *parentWidget, QObject *parent, const QVariantList &args, const QString &keyword);
510 
511  template<class impl, class ParentType>
512  static QObject *createInstance(QWidget *parentWidget, QObject *parent, const QVariantList &args)
513  {
514  Q_UNUSED(parentWidget)
515  ParentType *p = nullptr;
516  if (parent) {
517  p = qobject_cast<ParentType *>(parent);
518  Q_ASSERT(p);
519  }
520  return new impl(p, args);
521  }
522 
523  template<class impl>
524  static QObject *createPartInstance(QWidget *parentWidget, QObject *parent, const QVariantList &args)
525  {
526  return new impl(parentWidget, parent, args);
527  }
528 
529 private:
530  void registerPlugin(const QString &keyword, const QMetaObject *metaObject, CreateInstanceFunction instanceFunction);
531 };
532 
533 // Deprecation wrapper macro added only for 5.70, while backward typedef added in 4.0
534 #if KCOREADDONS_ENABLE_DEPRECATED_SINCE(5, 70)
535 
540 #endif
541 
542 template<typename T>
543 inline T *KPluginFactory::create(QObject *parent, const QVariantList &args)
544 {
545  QObject *o = create(T::staticMetaObject.className(), parent && parent->isWidgetType() ? reinterpret_cast<QWidget *>(parent) : nullptr, parent, args, QString());
546 
547  T *t = qobject_cast<T *>(o);
548  if (!t) {
549  delete o;
550  }
551  return t;
552 }
553 
554 template<typename T>
555 inline T *KPluginFactory::create(const QString &keyword, QObject *parent, const QVariantList &args)
556 {
557  QObject *o = create(T::staticMetaObject.className(), parent && parent->isWidgetType() ? reinterpret_cast<QWidget *>(parent) : nullptr, parent, args, keyword);
558 
559  T *t = qobject_cast<T *>(o);
560  if (!t) {
561  delete o;
562  }
563  return t;
564 }
565 
566 template<typename T>
567 inline T *KPluginFactory::create(QWidget *parentWidget, QObject *parent, const QString &keyword, const QVariantList &args)
568 {
569  QObject *o = create(T::staticMetaObject.className(), parentWidget, parent, args, keyword);
570 
571  T *t = qobject_cast<T *>(o);
572  if (!t) {
573  delete o;
574  }
575  return t;
576 }
577 
578 Q_DECLARE_INTERFACE(KPluginFactory, KPluginFactory_iid)
579 
580 #endif // KPLUGINFACTORY_H
T * create(QObject *parent=nullptr, const QVariantList &args=QVariantList())
Use this method to create an object.
This is used to detect the arguments need for the constructor of plugin classes.
void registerPlugin(const QString &keyword=QString(), CreateInstanceFunction instanceFunction=InheritanceChecker< T >().createInstanceFunction(static_cast< T * >(nullptr)))
Registers a plugin with the factory.
bool isWidgetType() const const
KPluginFactory provides a convenient way to provide factory-style plugins.
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Wed Sep 30 2020 23:10:49 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.