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 #include <kdebug.h>
00029
00030
00031 template <class T>
00032 class KGenericFactoryBase
00033 {
00034 public:
00035 KGenericFactoryBase( const char *instanceName )
00036 : m_instanceName( instanceName )
00037 {
00038 s_self = this;
00039 m_catalogueInitialized = false;
00040 }
00041 virtual ~KGenericFactoryBase()
00042 {
00043 if ( s_instance )
00044 KGlobal::locale()->removeCatalogue( s_instance->instanceName() );
00045 delete s_instance;
00046 s_instance = 0;
00047 s_self = 0;
00048 }
00049
00050 static KInstance *instance();
00051
00052 protected:
00053 virtual KInstance *createInstance()
00054 {
00055 if ( !m_instanceName ) {
00056 kdWarning() << "KGenericFactory: instance requested but no instance name passed to the constructor!" << endl;
00057 return 0;
00058 }
00059 return new KInstance( m_instanceName );
00060 }
00061
00062 virtual void setupTranslations( void )
00063 {
00064 if ( instance() )
00065 KGlobal::locale()->insertCatalogue( instance()->instanceName() );
00066 }
00067
00068 void initializeMessageCatalogue()
00069 {
00070 if ( !m_catalogueInitialized )
00071 {
00072 m_catalogueInitialized = true;
00073 setupTranslations();
00074 }
00075 }
00076
00077 private:
00078 QCString m_instanceName;
00079 bool m_catalogueInitialized;
00080
00081 static KInstance *s_instance;
00082 static KGenericFactoryBase<T> *s_self;
00083 };
00084
00085
00086 template <class T>
00087 KInstance *KGenericFactoryBase<T>::s_instance = 0;
00088
00089
00090 template <class T>
00091 KGenericFactoryBase<T> *KGenericFactoryBase<T>::s_self = 0;
00092
00093
00094 template <class T>
00095 KInstance *KGenericFactoryBase<T>::instance()
00096 {
00097 if ( !s_instance && s_self )
00098 s_instance = s_self->createInstance();
00099 return s_instance;
00100 }
00101
00161 template <class Product, class ParentType = QObject>
00162 class KGenericFactory : public KLibFactory, public KGenericFactoryBase<Product>
00163 {
00164 public:
00165 KGenericFactory( const char *instanceName = 0 )
00166 : KGenericFactoryBase<Product>( instanceName )
00167 {}
00168
00169 protected:
00170 virtual QObject *createObject( QObject *parent, const char *name,
00171 const char *className, const QStringList &args )
00172 {
00173 KGenericFactoryBase<Product>::initializeMessageCatalogue();
00174 return KDEPrivate::ConcreteFactory<Product, ParentType>
00175 ::create( 0, 0, parent, name, className, args );
00176 }
00177 };
00178
00246 template <class Product, class ProductListTail>
00247 class KGenericFactory< KTypeList<Product, ProductListTail>, QObject >
00248 : public KLibFactory,
00249 public KGenericFactoryBase< KTypeList<Product, ProductListTail> >
00250 {
00251 public:
00252 KGenericFactory( const char *instanceName = 0 )
00253 : KGenericFactoryBase< KTypeList<Product, ProductListTail> >( instanceName )
00254 {}
00255
00256 protected:
00257 virtual QObject *createObject( QObject *parent, const char *name,
00258 const char *className, const QStringList &args )
00259 {
00260 this->initializeMessageCatalogue();
00261 return KDEPrivate::MultiFactory< KTypeList< Product, ProductListTail > >
00262 ::create( 0, 0, parent, name, className, args );
00263 }
00264 };
00265
00333 template <class Product, class ProductListTail,
00334 class ParentType, class ParentTypeListTail>
00335 class KGenericFactory< KTypeList<Product, ProductListTail>,
00336 KTypeList<ParentType, ParentTypeListTail> >
00337 : public KLibFactory,
00338 public KGenericFactoryBase< KTypeList<Product, ProductListTail> >
00339 {
00340 public:
00341 KGenericFactory( const char *instanceName = 0 )
00342 : KGenericFactoryBase< KTypeList<Product, ProductListTail> >( instanceName )
00343 {}
00344
00345 protected:
00346 virtual QObject *createObject( QObject *parent, const char *name,
00347 const char *className, const QStringList &args )
00348 {
00349 this->initializeMessageCatalogue();
00350 return KDEPrivate::MultiFactory< KTypeList< Product, ProductListTail >,
00351 KTypeList< ParentType, ParentTypeListTail > >
00352 ::create( 0, 0, parent, name,
00353 className, args );
00354 }
00355 };
00356
00357
00358
00359
00360
00361
00362 #endif
00363