kparts Library API Documentation

genericfactory.h

00001 #ifndef __kparts__genericfactory_h__
00002 #define __kparts__genericfactory_h__
00003 
00004 #include <kparts/factory.h>
00005 #include <kparts/part.h>
00006 #include <kgenericfactory.h>
00007 #include <kaboutdata.h>
00008 #include <kdebug.h>
00009 
00010 namespace KParts
00011 {
00012 
00016     template <class T>
00017     class GenericFactoryBase : public KParts::Factory
00018     {
00019     public:
00020         GenericFactoryBase()
00021         {
00022             if ( s_self )
00023                 kdWarning() << "KParts::GenericFactory instantiated more than once!" << endl;
00024             s_self = this;
00025         }
00026         virtual ~GenericFactoryBase()
00027         {
00028             delete s_aboutData;
00029             delete s_instance;
00030             s_aboutData = 0;
00031             s_instance = 0;
00032             s_self = 0;
00033         }
00034 
00035         static KInstance *instance();
00036         static KAboutData *aboutData();
00037 
00038     protected:
00039         virtual KInstance *createInstance()
00040         {
00041             return new KInstance( aboutData() );
00042         }
00043 
00044     private:
00045         static GenericFactoryBase<T> *s_self;
00046         static KInstance *s_instance;
00047         static KAboutData *s_aboutData;
00048     };
00049 
00082     template <class T>
00083     class GenericFactory : public GenericFactoryBase<T>
00084     {
00085     public:
00086         GenericFactory() { }
00087 
00088         virtual KParts::Part *createPartObject( QWidget *parentWidget, const char *widgetName,
00089                                                 QObject *parent, const char *name,
00090                                                 const char *className,
00091                                                 const QStringList &args )
00092         {
00093             T *part = KDEPrivate::ConcreteFactory<T>::create( parentWidget,
00094                                                               widgetName,
00095                                                               parent,
00096                                                               name,
00097                                                               className,
00098                                                               args );
00099 
00100             if ( part && !qstrcmp( className, "KParts::ReadOnlyPart" ) )
00101             {
00102                 KParts::ReadWritePart *rwp = dynamic_cast<KParts::ReadWritePart *>( part );
00103                 if ( rwp )
00104                     rwp->setReadWrite( false );
00105             }
00106             return part;
00107         }
00108     };
00109 
00110     template <class T1, class T2>
00111     class GenericFactory< KTypeList<T1, T2> > : public GenericFactoryBase<T1>
00112     {
00113     public:
00114         GenericFactory() { }
00115 
00116         virtual KParts::Part *createPartObject( QWidget *parentWidget, const char *widgetName,
00117                                                 QObject *parent, const char *name,
00118                                                 const char *className,
00119                                                 const QStringList &args )
00120         {
00121             QObject *object = KDEPrivate::MultiFactory< KTypeList<T1, T2> >::create( parentWidget,
00122                                                                                      widgetName,
00123                                                                                      parent, name,
00124                                                                                      className,
00125                                                                                      args );
00126 
00127             // (this cast is guaranteed to work...)
00128             KParts::Part *part = dynamic_cast<KParts::Part *>( object );
00129 
00130             if ( part && !qstrcmp( className, "KParts::ReadOnlyPart" ) )
00131             {
00132                 KParts::ReadWritePart *rwp = dynamic_cast<KParts::ReadWritePart *>( part );
00133                 if ( rwp )
00134                     rwp->setReadWrite( false );
00135             }
00136             return part;
00137         }
00138     };
00139 
00143     template <class T>
00144     GenericFactoryBase<T> *GenericFactoryBase<T>::s_self = 0;
00145 
00149     template <class T>
00150     KInstance *GenericFactoryBase<T>::s_instance = 0;
00151 
00155     template <class T>
00156     KAboutData *GenericFactoryBase<T>::s_aboutData = 0;
00157 
00161     template <class T>
00162     KInstance *GenericFactoryBase<T>::instance()
00163     {
00164         if ( !s_instance )
00165         {
00166             if ( s_self )
00167                 s_instance = s_self->createInstance();
00168             else
00169                 s_instance = new KInstance( aboutData() );
00170         }
00171         return s_instance;
00172     }
00173 
00177     template <class T>
00178     KAboutData *GenericFactoryBase<T>::aboutData()
00179     {
00180         if ( !s_aboutData )
00181             s_aboutData = T::createAboutData();
00182         return s_aboutData;
00183     }
00184 
00185 };
00186 
00187 #endif
00188 
KDE Logo
This file is part of the documentation for kdelibs Version 3.1.0.
Documentation copyright © 1996-2002 the KDE developers.
Generated on Wed Oct 8 12:21:45 2003 by doxygen 1.2.18 written by Dimitri van Heesch, © 1997-2001