00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #ifndef kgenericfactory_h
00020 #define kgenericfactory_h
00021
00022 #include <klibloader.h>
00023 #include <kpluginfactory.h>
00024 #include <kpluginloader.h>
00025 #include <ktypelist.h>
00026 #include <kcomponentdata.h>
00027 #include <kgenericfactory.tcc>
00028 #include <kglobal.h>
00029 #include <klocale.h>
00030 #include <kdebug.h>
00031
00032
00033 template <class T>
00034 class KGenericFactoryBase : public KPluginFactory
00035 {
00036 public:
00037 explicit KGenericFactoryBase(const char *componentName, const char *catalogName)
00038 : KPluginFactory(componentName, catalogName)
00039 {
00040 s_self = this;
00041 s_createComponentDataCalled = false;
00042 }
00043
00044 explicit KGenericFactoryBase( const KAboutData *data )
00045 : KPluginFactory(data)
00046 {
00047 s_self = this;
00048 s_createComponentDataCalled = false;
00049 }
00050
00051 virtual ~KGenericFactoryBase()
00052 {
00053 s_self = 0;
00054 }
00055
00056 static KComponentData componentData()
00057 {
00058 Q_ASSERT(s_self);
00059 if (!s_createComponentDataCalled) {
00060 s_createComponentDataCalled = true;
00061
00062 KComponentData *kcd = s_self->createComponentData();
00063 Q_ASSERT(kcd);
00064 s_self->setComponentData(*kcd);
00065 delete kcd;
00066 }
00067 return static_cast<KPluginFactory *>(s_self)->componentData();
00068 }
00069
00070 protected:
00071 virtual KComponentData *createComponentData()
00072 {
00073 return new KComponentData(componentData());
00074 }
00075
00076 private:
00077 static bool s_createComponentDataCalled;
00078 static KGenericFactoryBase<T> *s_self;
00079 };
00080
00081
00082 template <class T>
00083 KGenericFactoryBase<T> *KGenericFactoryBase<T>::s_self = 0;
00084
00085
00086 template <class T>
00087 bool KGenericFactoryBase<T>::s_createComponentDataCalled = false;
00088
00147 template <class Product, class ParentType = QObject>
00148 class KDE_DEPRECATED KGenericFactory : public KGenericFactoryBase<Product>
00149 {
00150 public:
00151 explicit KGenericFactory( const char *componentName = 0, const char *catalogName = 0 )
00152 : KGenericFactoryBase<Product>(componentName, catalogName)
00153 {}
00154
00155 explicit KGenericFactory( const KAboutData *data )
00156 : KGenericFactoryBase<Product>(data)
00157 {}
00158
00159 protected:
00160 virtual QObject *createObject( QObject *parent,
00161 const char *className, const QStringList &args )
00162 {
00163 return KDEPrivate::ConcreteFactory<Product, ParentType>
00164 ::create( 0, parent, className, args );
00165 }
00166 };
00167
00237 template <class Product, class ProductListTail>
00238 class KGenericFactory< KTypeList<Product, ProductListTail>, QObject >
00239 : public KGenericFactoryBase<KTypeList<Product, ProductListTail> >
00240 {
00241 public:
00242 explicit KGenericFactory( const char *componentName = 0, const char *catalogName = 0 )
00243 : KGenericFactoryBase<KTypeList<Product, ProductListTail> >(componentName, catalogName)
00244 {}
00245
00246 explicit KGenericFactory( const KAboutData *data )
00247 : KGenericFactoryBase<KTypeList<Product, ProductListTail> >(data)
00248 {}
00249
00250
00251 protected:
00252 virtual QObject *createObject( QObject *parent,
00253 const char *className, const QStringList &args )
00254 {
00255 return KDEPrivate::MultiFactory< KTypeList< Product, ProductListTail > >
00256 ::create( 0, parent, className, args );
00257 }
00258 };
00259
00329 template <class Product, class ProductListTail,
00330 class ParentType, class ParentTypeListTail>
00331 class KGenericFactory< KTypeList<Product, ProductListTail>,
00332 KTypeList<ParentType, ParentTypeListTail> >
00333 : public KGenericFactoryBase<KTypeList<Product, ProductListTail> >
00334 {
00335 public:
00336 explicit KGenericFactory( const char *componentName = 0, const char *catalogName = 0 )
00337 : KGenericFactoryBase<KTypeList<Product, ProductListTail> >(componentName, catalogName)
00338 {}
00339 explicit KGenericFactory( const KAboutData *data )
00340 : KGenericFactoryBase<KTypeList<Product, ProductListTail> >(data)
00341 {}
00342
00343
00344 protected:
00345 virtual QObject *createObject( QObject *parent,
00346 const char *className, const QStringList &args )
00347 {
00348 return KDEPrivate::MultiFactory< KTypeList< Product, ProductListTail >,
00349 KTypeList< ParentType, ParentTypeListTail > >
00350 ::create( 0, 0, parent,
00351 className, args );
00352 }
00353 };
00354
00355
00356
00357
00358
00359 #endif
00360
00361