kdecore Library API Documentation

kgenericfactory.h

00001 /* This file is part of the KDE project
00002  * Copyright (C) 2001 Simon Hausmann <hausmann@kde.org>
00003  *
00004  * This library is free software; you can redistribute it and/or
00005  * modify it under the terms of the GNU Library General Public
00006  * License as published by the Free Software Foundation; either
00007  * version 2 of the License, or (at your option) any later version.
00008  *
00009  * This library is distributed in the hope that it will be useful,
00010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012  * Library General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU Library General Public License
00015  * along with this library; see the file COPYING.LIB.  If not, write to
00016  * the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
00017  * Boston, MA 02111-1307, USA.
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 /* @internal */
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 /* @internal */
00086 template <class T>
00087 KInstance *KGenericFactoryBase<T>::s_instance = 0;
00088 
00089 /* @internal */
00090 template <class T>
00091 KGenericFactoryBase<T> *KGenericFactoryBase<T>::s_self = 0;
00092 
00093 /* @internal */
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  * vim: et sw=4
00360  */
00361 
00362 #endif
00363 
KDE Logo
This file is part of the documentation for kdecore Library Version 3.2.2.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Sun May 16 22:01:19 2004 by doxygen 1.2.15 written by Dimitri van Heesch, © 1997-2003