1 /* This file is part of the KDE project
2 * Copyright (C) 2001 Simon Hausmann <hausmann@kde.org>
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.
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.
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.
19 #ifndef kgenericfactory_h
20 #define kgenericfactory_h
22 #include <klibloader.h>
23 #include <kpluginfactory.h>
24 #include <kpluginloader.h>
25 #include <ktypelist.h>
26 #include <kcomponentdata.h>
27 #include <kgenericfactory.tcc>
34 class KGenericFactoryBase
: public KPluginFactory
37 explicit KGenericFactoryBase(const char *componentName
, const char *catalogName
)
38 : KPluginFactory(componentName
, catalogName
)
41 s_createComponentDataCalled
= false;
44 explicit KGenericFactoryBase( const KAboutData
*data
)
45 : KPluginFactory(data
)
48 s_createComponentDataCalled
= false;
51 virtual ~KGenericFactoryBase()
56 static KComponentData
componentData()
59 if (!s_createComponentDataCalled
) {
60 s_createComponentDataCalled
= true;
62 KComponentData
*kcd
= s_self
->createComponentData();
64 s_self
->setComponentData(*kcd
);
67 return static_cast<KPluginFactory
*>(s_self
)->componentData();
71 virtual KComponentData
*createComponentData()
73 return new KComponentData(componentData());
77 static bool s_createComponentDataCalled
;
78 static KGenericFactoryBase
<T
> *s_self
;
83 KGenericFactoryBase
<T
> *KGenericFactoryBase
<T
>::s_self
= 0;
87 bool KGenericFactoryBase
<T
>::s_createComponentDataCalled
= false;
90 * This template provides a generic implementation of a KLibFactory ,
91 * for use with shared library components. It implements the pure virtual
92 * createObject method of KLibFactory and instantiates objects of the
93 * specified class (template argument) when the class name argument of
94 * createObject matches a class name in the given hierarchy.
96 * In case you are developing a KParts component, skip this file and
97 * go directly to KParts::GenericFactory .
99 * Note that the class specified as template argument needs to provide
100 * a certain constructor:
102 * <li>If the class is derived from QObject then it needs to have
103 * a constructor like:
104 * <code>MyClass( QObject *parent,
105 * const QStringList &args );</code>
106 * <li>If the class is derived from QWidget then it needs to have
107 * a constructor like:
108 * <code>MyWidget( QWidget *parent,
109 * const QStringList &args);</code>
110 * <li>If the class is derived from KParts::Part then it needs to have
111 * a constructor like:
112 * <code>MyPart( QWidget *parentWidget,
114 * const QStringList &args );</code>
116 * The args QStringList passed to the constructor is the args string list
117 * that the caller passed to KLibFactory's create method.
119 * In addition upon instantiation this template provides a central
120 * KComponentData object for your component, accessible through the
121 * static componentData() method. The componentName and catalogName arguments
122 * of the KGenericFactory constructor are passed to the KComponentData object.
124 * The creation of the KComponentData object can be customized by inheriting
125 * from this template class and re-implementing the virtual createComponentData
126 * method. For example it could look like this:
128 * KComponentData *MyFactory::createComponentData()
130 * return new KComponentData( myAboutData );
134 * Example of usage of the whole template:
136 * class MyPlugin : public KParts::Plugin
140 * MyPlugin( QObject *parent, const QStringList &args );
144 * K_EXPORT_COMPONENT_FACTORY( libmyplugin, KGenericFactory<MyPlugin> )
147 template <class Product
, class ParentType
= QObject
>
148 class KDE_DEPRECATED KGenericFactory
: public KGenericFactoryBase
<Product
>
151 explicit KGenericFactory( const char *componentName
= 0, const char *catalogName
= 0 )
152 : KGenericFactoryBase
<Product
>(componentName
, catalogName
)
155 explicit KGenericFactory( const KAboutData
*data
)
156 : KGenericFactoryBase
<Product
>(data
)
160 virtual QObject
*createObject( QObject
*parent
,
161 const char *className
, const QStringList
&args
)
163 return KDEPrivate::ConcreteFactory
<Product
, ParentType
>
164 ::create( 0, parent
, className
, args
);
169 * \class KGenericFactory kgenericfactory.h <KGenericFactory>
171 * This template provides a generic implementation of a KLibFactory ,
172 * for use with shared library components. It implements the pure virtual
173 * createObject method of KLibFactory and instantiates objects of the
174 * specified classes in the given typelist template argument when the class
175 * name argument of createObject matches a class names in the given hierarchy
178 * Note that each class in the specified in the typelist template argument
179 * needs to provide a certain constructor:
181 * <li>If the class is derived from QObject then it needs to have
182 * a constructor like:
183 * <code>MyClass( QObject *parent,
184 * const QStringList &args );</code>
185 * <li>If the class is derived from QWidget then it needs to have
186 * a constructor like:
187 * <code>MyWidget( QWidget *parent,
188 * const QStringList &args);</code>
189 * <li>If the class is derived from KParts::Part then it needs to have
190 * a constructor like:
191 * <code>MyPart( QWidget *parentWidget,
193 * const QStringList &args );</code>
195 * The args QStringList passed to the constructor is the args string list
196 * that the caller passed to KLibFactory's create method.
198 * In addition upon instantiation this template provides a central
199 * KComponentData object for your component, accessible through the
200 * static componentData() method. The componentName and catalogName arguments
201 * of the KGenericFactory constructor are passed to the KComponentData object.
203 * The creation of the KComponentData object can be customized by inheriting
204 * from this template class and re-implementing the virtual createComponentData
205 * method. For example it could look like this:
207 * KComponentData *MyFactory::createComponentData()
209 * return new KComponentData( myAboutData );
213 * Example of usage of the whole template:
215 * class MyPlugin : public KParts::Plugin
219 * MyPlugin( QObject *parent,
220 * const QStringList &args );
224 * class MyDialogComponent : public KDialog
228 * MyDialogComponent( QWidget *parentWidget,
229 * const QStringList &args );
233 * typedef K_TYPELIST_2( MyPlugin, MyDialogComponent ) Products;
234 * K_EXPORT_COMPONENT_FACTORY( libmyplugin, KGenericFactory<Products> )
237 template <class Product
, class ProductListTail
>
238 class KGenericFactory
< KTypeList
<Product
, ProductListTail
>, QObject
>
239 : public KGenericFactoryBase
<KTypeList
<Product
, ProductListTail
> >
242 explicit KGenericFactory( const char *componentName
= 0, const char *catalogName
= 0 )
243 : KGenericFactoryBase
<KTypeList
<Product
, ProductListTail
> >(componentName
, catalogName
)
246 explicit KGenericFactory( const KAboutData
*data
)
247 : KGenericFactoryBase
<KTypeList
<Product
, ProductListTail
> >(data
)
252 virtual QObject
*createObject( QObject
*parent
,
253 const char *className
, const QStringList
&args
)
255 return KDEPrivate::MultiFactory
< KTypeList
< Product
, ProductListTail
> >
256 ::create( 0, parent
, className
, args
);
261 * \class KGenericFactory kgenericfactory.h <KGenericFactory>
263 * This template provides a generic implementation of a KLibFactory ,
264 * for use with shared library components. It implements the pure virtual
265 * createObject method of KLibFactory and instantiates objects of the
266 * specified classes in the given typelist template argument when the class
267 * name argument of createObject matches a class names in the given hierarchy
270 * Note that each class in the specified in the typelist template argument
271 * needs to provide a certain constructor:
273 * <li>If the class is derived from QObject then it needs to have
274 * a constructor like:
275 * <code>MyClass( QObject *parent,
276 * const QStringList &args );</code>
277 * <li>If the class is derived from QWidget then it needs to have
278 * a constructor like:
279 * <code>MyWidget( QWidget *parent,
280 * const QStringList &args);</code>
281 * <li>If the class is derived from KParts::Part then it needs to have
282 * a constructor like:
283 * <code>MyPart( QWidget *parentWidget,
285 * const QStringList &args );</code>
287 * The args QStringList passed to the constructor is the args string list
288 * that the caller passed to KLibFactory's create method.
290 * In addition upon instantiation this template provides a central
291 * KComponentData object for your component, accessible through the
292 * static componentData() method. The componentName and catalogNames arguments
293 * of the KGenericFactory constructor are passed to the KComponentData object.
295 * The creation of the KComponentData object can be customized by inheriting
296 * from this template class and re-implementing the virtual createComponentData
297 * method. For example it could look like this:
299 * KComponentData *MyFactory::createComponentData()
301 * return new KComponentData( myAboutData );
305 * Example of usage of the whole template:
307 * class MyPlugin : public KParts::Plugin
311 * MyPlugin( QObject *parent,
312 * const QStringList &args );
316 * class MyDialogComponent : public KDialog
320 * MyDialogComponent( QWidget *parentWidget,
321 * const QStringList &args );
325 * typedef K_TYPELIST_2( MyPlugin, MyDialogComponent ) Products;
326 * K_EXPORT_COMPONENT_FACTORY( libmyplugin, KGenericFactory<Products> )
329 template <class Product
, class ProductListTail
,
330 class ParentType
, class ParentTypeListTail
>
331 class KGenericFactory
< KTypeList
<Product
, ProductListTail
>,
332 KTypeList
<ParentType
, ParentTypeListTail
> >
333 : public KGenericFactoryBase
<KTypeList
<Product
, ProductListTail
> >
336 explicit KGenericFactory( const char *componentName
= 0, const char *catalogName
= 0 )
337 : KGenericFactoryBase
<KTypeList
<Product
, ProductListTail
> >(componentName
, catalogName
)
339 explicit KGenericFactory( const KAboutData
*data
)
340 : KGenericFactoryBase
<KTypeList
<Product
, ProductListTail
> >(data
)
345 virtual QObject
*createObject( QObject
*parent
,
346 const char *className
, const QStringList
&args
)
348 return KDEPrivate::MultiFactory
< KTypeList
< Product
, ProductListTail
>,
349 KTypeList
< ParentType
, ParentTypeListTail
> >
350 ::create( 0, 0, parent
,