# kdecore Library API Documentation

Overview | Class Hierarchy | Classes | Classes (annotated) | Members | Namespaces | Source Files

# 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         m_aboutData=0L;
    00039         s_self = this;
    00040         m_catalogueInitialized = false;
    00041     }
    00042     KGenericFactoryBase( const KAboutData *data )
    00043         : m_aboutData(data)
    00044     {
    00045         s_self = this;
    00046         m_catalogueInitialized = false;
    00047     }
    00048
    00049     virtual ~KGenericFactoryBase()
    00050     {
    00051         if ( s_instance )
    00052             KGlobal::locale()->removeCatalogue( QString::fromAscii( s_instance->instanceName() ) );
    00053         delete s_instance;
    00054         s_instance = 0;
    00055         s_self = 0;
    00056     }
    00057
    00058     static KInstance *instance();
    00059
    00060 protected:
    00061     virtual KInstance *createInstance()
    00062     {
    00063         if ( m_aboutData )
    00064             return new KInstance( m_aboutData );
    00065         if ( !m_instanceName ) {
    00066             kdWarning() << "KGenericFactory: instance requested but no instance name or about data passed to the constructor!" << endl;
    00067             return 0;
    00068         }
    00069         return new KInstance( m_instanceName );
    00070     }
    00071
    00072     virtual void setupTranslations( void )
    00073     {
    00074         if ( instance() )
    00075             KGlobal::locale()->insertCatalogue( QString::fromAscii( instance()->instanceName() ) );
    00076     }
    00077
    00078     void initializeMessageCatalogue()
    00079     {
    00080         if ( !m_catalogueInitialized )
    00081         {
    00082             m_catalogueInitialized = true;
    00083             setupTranslations();
    00084         }
    00085     }
    00086
    00087 private:
    00088     QCString m_instanceName;
    00089     const KAboutData *m_aboutData;
    00090     bool m_catalogueInitialized;
    00091
    00092     static KInstance *s_instance;
    00093     static KGenericFactoryBase<T> *s_self;
    00094 };
    00095
    00096 /* @internal */
    00097 template <class T>
    00098 KInstance *KGenericFactoryBase<T>::s_instance = 0;
    00099
    00100 /* @internal */
    00101 template <class T>
    00102 KGenericFactoryBase<T> *KGenericFactoryBase<T>::s_self = 0;
    00103
    00104 /* @internal */
    00105 template <class T>
    00106 KInstance *KGenericFactoryBase<T>::instance()
    00107 {
    00108     if ( !s_instance && s_self )
    00109         s_instance = s_self->createInstance();
    00110     return s_instance;
    00111 }
    00112
    00172 template <class Product, class ParentType = QObject>
    00173 class KGenericFactory : public KLibFactory, public KGenericFactoryBase<Product>
    00174 {
    00175 public:
    00176     KGenericFactory( const char *instanceName = 0 )
    00177         : KGenericFactoryBase<Product>( instanceName )
    00178     {}
    00179
    00183     KGenericFactory( const KAboutData *data )
    00184         : KGenericFactoryBase<Product>( data )
    00185     {}
    00186
    00187
    00188 protected:
    00189     virtual QObject *createObject( QObject *parent, const char *name,
    00190                                   const char *className, const QStringList &args )
    00191     {
    00192         KGenericFactoryBase<Product>::initializeMessageCatalogue();
    00193         return KDEPrivate::ConcreteFactory<Product, ParentType>
    00194             ::create( 0, 0, parent, name, className, args );
    00195     }
    00196 };
    00197
    00265 template <class Product, class ProductListTail>
    00266 class KGenericFactory< KTypeList<Product, ProductListTail>, QObject >
    00267     : public KLibFactory,
    00268       public KGenericFactoryBase< KTypeList<Product, ProductListTail> >
    00269 {
    00270 public:
    00271     KGenericFactory( const char *instanceName  = 0 )
    00272         : KGenericFactoryBase< KTypeList<Product, ProductListTail> >( instanceName )
    00273     {}
    00274
    00278     KGenericFactory( const KAboutData *data )
    00279         : KGenericFactoryBase< KTypeList<Product, ProductListTail> >( data )
    00280     {}
    00281
    00282
    00283 protected:
    00284     virtual QObject *createObject( QObject *parent, const char *name,
    00285                                    const char *className, const QStringList &args )
    00286     {
    00287         this->initializeMessageCatalogue();
    00288         return KDEPrivate::MultiFactory< KTypeList< Product, ProductListTail > >
    00289             ::create( 0, 0, parent, name, className, args );
    00290     }
    00291 };
    00292
    00360 template <class Product, class ProductListTail,
    00361           class ParentType, class ParentTypeListTail>
    00362 class KGenericFactory< KTypeList<Product, ProductListTail>,
    00363                        KTypeList<ParentType, ParentTypeListTail> >
    00364     : public KLibFactory,
    00365       public KGenericFactoryBase< KTypeList<Product, ProductListTail> >
    00366 {
    00367 public:
    00368     KGenericFactory( const char *instanceName  = 0 )
    00369         : KGenericFactoryBase< KTypeList<Product, ProductListTail> >( instanceName )
    00370     {}
    00374     KGenericFactory( const KAboutData *data )
    00375         : KGenericFactoryBase< KTypeList<Product, ProductListTail> >( data )
    00376     {}
    00377
    00378
    00379 protected:
    00380     virtual QObject *createObject( QObject *parent, const char *name,
    00381                                    const char *className, const QStringList &args )
    00382     {
    00383         this->initializeMessageCatalogue();
    00384         return KDEPrivate::MultiFactory< KTypeList< Product, ProductListTail >,
    00385                                          KTypeList< ParentType, ParentTypeListTail > >
    00386                                        ::create( 0, 0, parent, name,
    00387                                                  className, args );
    00388     }
    00389 };
    00390
    00391
    00392 /*
    00393  * vim: et sw=4
    00394  */
    00395
    00396 #endif
    00397

This file is part of the documentation for kdecore Library Version 3.4.1.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Sat Jun 11 20:18:04 2005 by doxygen 1.4.1 written by Dimitri van Heesch, © 1997-2003