KParts
genericfactory.h
Go to the documentation of this file.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 virtual void virtual_hook( int id, void *data )
00045 {
00046 if ( id != VIRTUAL_QUERY_INSTANCE_PARAMS ) {
00047 KParts::Factory::virtual_hook( id, data );
00048 return;
00049 }
00050
00051 QueryInstanceParams *params = reinterpret_cast<QueryInstanceParams *>( data );
00052 params->instance = instance();
00053 }
00054
00055 private:
00056 static GenericFactoryBase<T> *s_self;
00057 static KInstance *s_instance;
00058 static KAboutData *s_aboutData;
00059 };
00060
00094 template <class T>
00095 class GenericFactory : public GenericFactoryBase<T>
00096 {
00097 public:
00098 GenericFactory() { }
00099
00100 virtual KParts::Part *createPartObject( QWidget *parentWidget, const char *widgetName,
00101 QObject *parent, const char *name,
00102 const char *className,
00103 const QStringList &args )
00104 {
00105 T *part = KDEPrivate::ConcreteFactory<T>::create( parentWidget,
00106 widgetName,
00107 parent,
00108 name,
00109 className,
00110 args );
00111
00112 if ( part && !qstrcmp( className, "KParts::ReadOnlyPart" ) )
00113 {
00114 KParts::ReadWritePart *rwp = dynamic_cast<KParts::ReadWritePart *>( part );
00115 if ( rwp )
00116 rwp->setReadWrite( false );
00117 }
00118 return part;
00119 }
00120 };
00121
00122 template <class T1, class T2>
00123 class GenericFactory< KTypeList<T1, T2> > : public GenericFactoryBase<T1>
00124 {
00125 public:
00126 GenericFactory() { }
00127
00128 virtual KParts::Part *createPartObject( QWidget *parentWidget, const char *widgetName,
00129 QObject *parent, const char *name,
00130 const char *className,
00131 const QStringList &args )
00132 {
00133 QObject *object = KDEPrivate::MultiFactory< KTypeList<T1, T2> >::create( parentWidget,
00134 widgetName,
00135 parent, name,
00136 className,
00137 args );
00138
00139
00140 KParts::Part *part = dynamic_cast<KParts::Part *>( object );
00141
00142 if ( part && !qstrcmp( className, "KParts::ReadOnlyPart" ) )
00143 {
00144 KParts::ReadWritePart *rwp = dynamic_cast<KParts::ReadWritePart *>( part );
00145 if ( rwp )
00146 rwp->setReadWrite( false );
00147 }
00148 return part;
00149 }
00150 };
00151
00155 template <class T>
00156 GenericFactoryBase<T> *GenericFactoryBase<T>::s_self = 0;
00157
00161 template <class T>
00162 KInstance *GenericFactoryBase<T>::s_instance = 0;
00163
00167 template <class T>
00168 KAboutData *GenericFactoryBase<T>::s_aboutData = 0;
00169
00173 template <class T>
00174 KInstance *GenericFactoryBase<T>::instance()
00175 {
00176 if ( !s_instance )
00177 {
00178 if ( s_self )
00179 s_instance = s_self->createInstance();
00180 else
00181 s_instance = new KInstance( aboutData() );
00182 }
00183 return s_instance;
00184 }
00185
00189 template <class T>
00190 KAboutData *GenericFactoryBase<T>::aboutData()
00191 {
00192 if ( !s_aboutData )
00193 s_aboutData = T::createAboutData();
00194 return s_aboutData;
00195 }
00196
00197 }
00198
00199 #endif
00200