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 <ktypelist.h>
00024 #include <kinstance.h>
00025 #include <kgenericfactory.tcc>
00026 #include <kglobal.h>
00027 #include <klocale.h>
00028
00029
00030 template <class T>
00031 class KGenericFactoryBase
00032 {
00033 public:
00034 KGenericFactoryBase( const char *instanceName )
00035 : m_instanceName( instanceName )
00036 {
00037 s_self = this;
00038 }
00039 virtual ~KGenericFactoryBase()
00040 {
00041 if ( s_instance )
00042 KGlobal::locale()->removeCatalogue( s_instance->instanceName() );
00043 delete s_instance;
00044 s_instance = 0;
00045 s_self = 0;
00046 }
00047
00048 static KInstance *instance();
00049
00050 protected:
00051 virtual KInstance *createInstance()
00052 {
00053 if ( !m_instanceName )
00054 return 0;
00055 return new KInstance( m_instanceName );
00056 }
00057
00058 virtual void setupTranslations( void )
00059 {
00060 if ( instance() )
00061 KGlobal::locale()->insertCatalogue( instance()->instanceName() );
00062 }
00063
00064 void initializeMessageCatalogue()
00065 {
00066 static bool catalogueInitialized = false;
00067 if ( !catalogueInitialized )
00068 {
00069 catalogueInitialized = true;
00070 setupTranslations();
00071 }
00072 }
00073
00074 private:
00075 QCString m_instanceName;
00076
00077 static KInstance *s_instance;
00078 static KGenericFactoryBase<T> *s_self;
00079 };
00080
00081
00082 template <class T>
00083 KInstance *KGenericFactoryBase<T>::s_instance = 0;
00084
00085
00086 template <class T>
00087 KGenericFactoryBase<T> *KGenericFactoryBase<T>::s_self = 0;
00088
00089
00090 template <class T>
00091 KInstance *KGenericFactoryBase<T>::instance()
00092 {
00093 if ( !s_instance && s_self )
00094 s_instance = s_self->createInstance();
00095 return s_instance;
00096 }
00097
00157 template <class Product, class ParentType = QObject>
00158 class KGenericFactory : public KLibFactory, public KGenericFactoryBase<Product>
00159 {
00160 public:
00161 KGenericFactory( const char *instanceName = 0 )
00162 : KGenericFactoryBase<Product>( instanceName )
00163 {}
00164
00165 protected:
00166 virtual QObject *createObject( QObject *parent, const char *name,
00167 const char *className, const QStringList &args )
00168 {
00169 initializeMessageCatalogue();
00170 return KDEPrivate::ConcreteFactory<Product, ParentType>
00171 ::create( 0, 0, parent, name, className, args );
00172 }
00173 };
00174
00242 template <class Product, class ProductListTail>
00243 class KGenericFactory< KTypeList<Product, ProductListTail>, QObject >
00244 : public KLibFactory,
00245 public KGenericFactoryBase< KTypeList<Product, ProductListTail> >
00246 {
00247 public:
00248 KGenericFactory( const char *instanceName = 0 )
00249 : KGenericFactoryBase< KTypeList<Product, ProductListTail> >( instanceName )
00250 {}
00251
00252 protected:
00253 virtual QObject *createObject( QObject *parent, const char *name,
00254 const char *className, const QStringList &args )
00255 {
00256 initializeMessageCatalogue();
00257 return KDEPrivate::MultiFactory< KTypeList< Product, ProductListTail > >
00258 ::create( 0, 0, parent, name, className, args );
00259 }
00260 };
00261
00329 template <class Product, class ProductListTail,
00330 class ParentType, class ParentTypeListTail>
00331 class KGenericFactory< KTypeList<Product, ProductListTail>,
00332 KTypeList<ParentType, ParentTypeListTail> >
00333 : public KLibFactory,
00334 public KGenericFactoryBase< KTypeList<Product, ProductListTail> >
00335 {
00336 public:
00337 KGenericFactory( const char *instanceName = 0 )
00338 : KGenericFactoryBase< KTypeList<Product, ProductListTail> >( instanceName )
00339 {}
00340
00341 protected:
00342 virtual QObject *createObject( QObject *parent, const char *name,
00343 const char *className, const QStringList &args )
00344 {
00345 initializeMessageCatalogue();
00346 return KDEPrivate::MultiFactory< KTypeList< Product, ProductListTail >,
00347 KTypeList< ParentType, ParentTypeListTail > >
00348 ::create( 0, 0, parent, name,
00349 className, args );
00350 }
00351 };
00352
00353
00354
00355
00356
00357
00358 #endif
00359