kgenericfactory.h Source File (kdecore Library)

| # kdecore Library API Documentation
Overview | Class Hierarchy | Classes | Classes (annotated) | Members | 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
    00029 /* @internal */
    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; // ### assert
    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 /* @internal */
    00082 template <class T>
    00083 KInstance *KGenericFactoryBase<T>::s_instance = 0;
    00084
    00085 /* @internal */
    00086 template <class T>
    00087 KGenericFactoryBase<T> *KGenericFactoryBase<T>::s_self = 0;
    00088
    00089 /* @internal */
    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         this->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         this->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         this->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  * vim: et sw=4
    00356  */
    00357
    00358 #endif
    00359

This file is part of the documentation for kdelibs Version 3.1.5.
Documentation copyright © 1996-2002 the KDE developers.
Generated on Wed Jan 28 12:46:30 2004 by doxygen 1.3.4 written by Dimitri van Heesch, © 1997-2001