KDELibs4Support

kgenericfactory.h
1 /* This file is part of the KDE project
2  * Copyright (C) 2001 Simon Hausmann <[email protected]>
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Library General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public License
15  * along with this library; see the file COPYING.LIB. If not, write to
16  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17  * Boston, MA 02110-1301, USA.
18  */
19 #ifndef kgenericfactory_h
20 #define kgenericfactory_h
21 
22 #include <klibloader.h>
23 #include <kpluginfactory.h>
24 #include <kpluginloader.h>
25 #include <ktypelist.h>
26 #include <kcomponentdata.h>
27 #include <kgenericfactory.tcc>
28 #include <klocalizedstring.h>
29 #include <kdebug.h>
30 
31 #ifndef KDELIBS4SUPPORT_NO_DEPRECATED
32 
33 /* @internal */
34 template <class T>
35 class KGenericFactoryBase : public KPluginFactory
36 {
37 public:
38  KDELIBS4SUPPORT_DEPRECATED explicit KGenericFactoryBase(const char *componentName)
39  : KPluginFactory()
40  {
41  s_self = this;
42  s_createComponentDataCalled = false;
43  }
44 
45  KDELIBS4SUPPORT_DEPRECATED explicit KGenericFactoryBase(const KAboutData *data)
46  : KPluginFactory()
47  {
49  s_self = this;
50  s_createComponentDataCalled = false;
51  }
52 
53  ~KGenericFactoryBase() override
54  {
55  s_self = nullptr;
56  }
57 
58 #if 0 // not available anymore. Port away from KGenericFactory!
59  static KComponentData componentData()
60  {
61  Q_ASSERT(s_self);
62  if (!s_createComponentDataCalled) {
63  s_createComponentDataCalled = true;
64 
65  KComponentData *kcd = s_self->createComponentData();
66  Q_ASSERT(kcd);
67  s_self->setComponentData(*kcd);
68  delete kcd;
69  }
70  return static_cast<KPluginFactory *>(s_self)->componentData();
71  }
72 
73 protected:
74  virtual KComponentData *createComponentData()
75  {
76  return new KComponentData(componentData());
77  }
78 #endif
79 private:
80  static bool s_createComponentDataCalled;
81  static KGenericFactoryBase<T> *s_self;
82 };
83 
84 /* @internal */
85 template <class T>
86 KGenericFactoryBase<T> *KGenericFactoryBase<T>::s_self = nullptr;
87 
88 /* @internal */
89 template <class T>
90 bool KGenericFactoryBase<T>::s_createComponentDataCalled = false;
91 
92 /**
93  * This template provides a generic implementation of a KLibFactory ,
94  * for use with shared library components. It implements the pure virtual
95  * createObject method of KLibFactory and instantiates objects of the
96  * specified class (template argument) when the class name argument of
97  * createObject matches a class name in the given hierarchy.
98  *
99  * In case you are developing a KParts component, skip this file and
100  * go directly to KParts::GenericFactory .
101  *
102  * Note that the class specified as template argument needs to provide
103  * a certain constructor:
104  * <ul>
105  * <li>If the class is derived from QObject then it needs to have
106  * a constructor like:
107  * <code>MyClass( QObject *parent,
108  * const QStringList &args );</code>
109  * <li>If the class is derived from QWidget then it needs to have
110  * a constructor like:
111  * <code>MyWidget( QWidget *parent,
112  * const QStringList &args);</code>
113  * <li>If the class is derived from KParts::Part then it needs to have
114  * a constructor like:
115  * <code>MyPart( QWidget *parentWidget,
116  * QObject *parent,
117  * const QStringList &args );</code>
118  * </ul>
119  * The args QStringList passed to the constructor is the args string list
120  * that the caller passed to KLibFactory's create method.
121  *
122  * In addition upon instantiation this template provides a central
123  * KComponentData object for your component, accessible through the
124  * static componentData() method. The componentName argument
125  * of the KGenericFactory constructor is passed to the KComponentData object.
126  *
127  * The creation of the KComponentData object can be customized by inheriting
128  * from this template class and re-implementing the virtual createComponentData
129  * method. For example it could look like this:
130  * \code
131  * KComponentData *MyFactory::createComponentData()
132  * {
133  * return new KComponentData( myAboutData );
134  * }
135  * \endcode
136  *
137  * Example of usage of the whole template:
138  * \code
139  * class MyPlugin : public KParts::Plugin
140  * {
141  * Q_ OBJECT
142  * public:
143  * MyPlugin( QObject *parent, const QStringList &args );
144  * ...
145  * };
146  *
147  * K_EXPORT_COMPONENT_FACTORY( libmyplugin, KGenericFactory<MyPlugin> )
148  * \endcode
149  *
150  * @deprecated use KPluginFactory
151  */
152 template <class Product, class ParentType = QObject>
153 class KDELIBS4SUPPORT_DEPRECATED KGenericFactory : public KGenericFactoryBase<Product>
154 {
155 public:
156  KDELIBS4SUPPORT_DEPRECATED explicit KGenericFactory(const char *componentName = nullptr)
157  : KGenericFactoryBase<Product>(componentName)
158  {}
159 
160  KDELIBS4SUPPORT_DEPRECATED explicit KGenericFactory(const KAboutData *data)
161  : KGenericFactoryBase<Product>(data)
162  {}
163 
164 protected:
165  virtual QObject *createObject(QObject *parent,
166  const char *className, const QStringList &args)
167  {
168  return KDEPrivate::ConcreteFactory<Product, ParentType>
169  ::create(nullptr, parent, className, args);
170  }
171 };
172 
173 /**
174  * \class KGenericFactory kgenericfactory.h <KGenericFactory>
175  *
176  * This template provides a generic implementation of a KLibFactory ,
177  * for use with shared library components. It implements the pure virtual
178  * createObject method of KLibFactory and instantiates objects of the
179  * specified classes in the given typelist template argument when the class
180  * name argument of createObject matches a class names in the given hierarchy
181  * of classes.
182  *
183  * Note that each class in the specified in the typelist template argument
184  * needs to provide a certain constructor:
185  * <ul>
186  * <li>If the class is derived from QObject then it needs to have
187  * a constructor like:
188  * <code>MyClass( QObject *parent,
189  * const QStringList &args );</code>
190  * <li>If the class is derived from QWidget then it needs to have
191  * a constructor like:
192  * <code>MyWidget( QWidget *parent,
193  * const QStringList &args);</code>
194  * <li>If the class is derived from KParts::Part then it needs to have
195  * a constructor like:
196  * <code>MyPart( QWidget *parentWidget,
197  * QObject *parent,
198  * const QStringList &args );</code>
199  * </ul>
200  * The args QStringList passed to the constructor is the args string list
201  * that the caller passed to KLibFactory's create method.
202  *
203  * In addition upon instantiation this template provides a central
204  * KComponentData object for your component, accessible through the
205  * static componentData() method. The componentName argument
206  * of the KGenericFactory constructor is passed to the KComponentData object.
207  *
208  * The creation of the KComponentData object can be customized by inheriting
209  * from this template class and re-implementing the virtual createComponentData
210  * method. For example it could look like this:
211  * \code
212  * KComponentData *MyFactory::createComponentData()
213  * {
214  * return new KComponentData( myAboutData );
215  * }
216  * \endcode
217  *
218  * Example of usage of the whole template:
219  * \code
220  * class MyPlugin : public KParts::Plugin
221  * {
222  * Q_ OBJECT
223  * public:
224  * MyPlugin( QObject *parent,
225  * const QStringList &args );
226  * ...
227  * };
228  *
229  * class MyDialogComponent : public KDialog
230  * {
231  * Q_ OBJECT
232  * public:
233  * MyDialogComponent( QWidget *parentWidget,
234  * const QStringList &args );
235  * ...
236  * };
237  *
238  * typedef K_TYPELIST_2( MyPlugin, MyDialogComponent ) Products;
239  * K_EXPORT_COMPONENT_FACTORY( libmyplugin, KGenericFactory<Products> )
240  * \endcode
241  */
242 template <class Product, class ProductListTail>
243 class KGenericFactory< KTypeList<Product, ProductListTail>, QObject >
244  : public KGenericFactoryBase<KTypeList<Product, ProductListTail> >
245 {
246 public:
247  KDELIBS4SUPPORT_DEPRECATED explicit KGenericFactory(const char *componentName = nullptr)
248  : KGenericFactoryBase<KTypeList<Product, ProductListTail> >(componentName)
249  {}
250 
251  KDELIBS4SUPPORT_DEPRECATED explicit KGenericFactory(const KAboutData *data)
252  : KGenericFactoryBase<KTypeList<Product, ProductListTail> >(data)
253  {}
254 
255 protected:
256  virtual QObject *createObject(QObject *parent,
257  const char *className, const QStringList &args)
258  {
259  return KDEPrivate::MultiFactory< KTypeList< Product, ProductListTail > >
260  ::create(nullptr, parent, className, args);
261  }
262 };
263 
264 /**
265  * \class KGenericFactory kgenericfactory.h <KGenericFactory>
266  *
267  * This template provides a generic implementation of a KLibFactory ,
268  * for use with shared library components. It implements the pure virtual
269  * createObject method of KLibFactory and instantiates objects of the
270  * specified classes in the given typelist template argument when the class
271  * name argument of createObject matches a class names in the given hierarchy
272  * of classes.
273  *
274  * Note that each class in the specified in the typelist template argument
275  * needs to provide a certain constructor:
276  * <ul>
277  * <li>If the class is derived from QObject then it needs to have
278  * a constructor like:
279  * <code>MyClass( QObject *parent,
280  * const QStringList &args );</code>
281  * <li>If the class is derived from QWidget then it needs to have
282  * a constructor like:
283  * <code>MyWidget( QWidget *parent,
284  * const QStringList &args);</code>
285  * <li>If the class is derived from KParts::Part then it needs to have
286  * a constructor like:
287  * <code>MyPart( QWidget *parentWidget,
288  * QObject *parent,
289  * const QStringList &args );</code>
290  * </ul>
291  * The args QStringList passed to the constructor is the args string list
292  * that the caller passed to KLibFactory's create method.
293  *
294  * In addition upon instantiation this template provides a central
295  * KComponentData object for your component, accessible through the
296  * static componentData() method. The componentName argument
297  * of the KGenericFactory constructor is passed to the KComponentData object.
298  *
299  * The creation of the KComponentData object can be customized by inheriting
300  * from this template class and re-implementing the virtual createComponentData
301  * method. For example it could look like this:
302  * \code
303  * KComponentData *MyFactory::createComponentData()
304  * {
305  * return new KComponentData( myAboutData );
306  * }
307  * \endcode
308  *
309  * Example of usage of the whole template:
310  * \code
311  * class MyPlugin : public KParts::Plugin
312  * {
313  * Q_ OBJECT
314  * public:
315  * MyPlugin( QObject *parent,
316  * const QStringList &args );
317  * ...
318  * };
319  *
320  * class MyDialogComponent : public KDialog
321  * {
322  * Q_ OBJECT
323  * public:
324  * MyDialogComponent( QWidget *parentWidget,
325  * const QStringList &args );
326  * ...
327  * };
328  *
329  * typedef K_TYPELIST_2( MyPlugin, MyDialogComponent ) Products;
330  * K_EXPORT_COMPONENT_FACTORY( libmyplugin, KGenericFactory<Products> )
331  * \endcode
332  */
333 template <class Product, class ProductListTail,
334  class ParentType, class ParentTypeListTail>
335 class KGenericFactory< KTypeList<Product, ProductListTail>,
336  KTypeList<ParentType, ParentTypeListTail> >
337  : public KGenericFactoryBase<KTypeList<Product, ProductListTail> >
338 {
339 public:
340  KDELIBS4SUPPORT_DEPRECATED explicit KGenericFactory(const char *componentName = nullptr)
341  : KGenericFactoryBase<KTypeList<Product, ProductListTail> >(componentName)
342  {}
343  KDELIBS4SUPPORT_DEPRECATED explicit KGenericFactory(const KAboutData *data)
344  : KGenericFactoryBase<KTypeList<Product, ProductListTail> >(data)
345  {}
346 
347 protected:
348  virtual QObject *createObject(QObject *parent,
349  const char *className, const QStringList &args)
350  {
351  return KDEPrivate::MultiFactory< KTypeList< Product, ProductListTail >,
353  ::create(nullptr, nullptr, parent,
354  className, args);
355  }
356 };
357 
358 #endif
359 #endif
360 
This file defines typelist structures as well as convenience macros to create typelists.
This template provides a generic implementation of a KLibFactory , for use with shared library compon...
static void registerPluginData(const KAboutData &aboutData)
The building block of typelists of any length.
Definition: ktypelist.h:453
Per component data.
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Thu Jun 17 2021 22:58:16 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.