KDECore
kgenericfactory.h
Go to the documentation of this file.
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., 51 Franklin Street, Fifth Floor, 00017 * Boston, MA 02110-1301, USA. 00018 */ 00019 #ifndef kgenericfactory_h 00020 #define kgenericfactory_h 00021 00022 #include <klibloader.h> 00023 #include <kpluginfactory.h> 00024 #include <kpluginloader.h> 00025 #include <ktypelist.h> 00026 #include <kcomponentdata.h> 00027 #include <kgenericfactory.tcc> 00028 #include <kglobal.h> 00029 #include <klocale.h> 00030 #include <kdebug.h> 00031 00032 #ifndef KDE_NO_DEPRECATED 00033 00034 /* @internal */ 00035 template <class T> 00036 class KGenericFactoryBase : public KPluginFactory 00037 { 00038 public: 00039 explicit KGenericFactoryBase(const char *componentName, const char *catalogName) 00040 : KPluginFactory(componentName, catalogName) 00041 { 00042 s_self = this; 00043 s_createComponentDataCalled = false; 00044 } 00045 00046 explicit KGenericFactoryBase( const KAboutData *data ) 00047 : KPluginFactory(data) 00048 { 00049 s_self = this; 00050 s_createComponentDataCalled = false; 00051 } 00052 00053 virtual ~KGenericFactoryBase() 00054 { 00055 s_self = 0; 00056 } 00057 00058 static KComponentData componentData() 00059 { 00060 Q_ASSERT(s_self); 00061 if (!s_createComponentDataCalled) { 00062 s_createComponentDataCalled = true; 00063 00064 KComponentData *kcd = s_self->createComponentData(); 00065 Q_ASSERT(kcd); 00066 s_self->setComponentData(*kcd); 00067 delete kcd; 00068 } 00069 return static_cast<KPluginFactory *>(s_self)->componentData(); 00070 } 00071 00072 protected: 00073 virtual KComponentData *createComponentData() 00074 { 00075 return new KComponentData(componentData()); 00076 } 00077 00078 private: 00079 static bool s_createComponentDataCalled; 00080 static KGenericFactoryBase<T> *s_self; 00081 }; 00082 00083 /* @internal */ 00084 template <class T> 00085 KGenericFactoryBase<T> *KGenericFactoryBase<T>::s_self = 0; 00086 00087 /* @internal */ 00088 template <class T> 00089 bool KGenericFactoryBase<T>::s_createComponentDataCalled = false; 00090 00149 template <class Product, class ParentType = QObject> 00150 class KDE_DEPRECATED KGenericFactory : public KGenericFactoryBase<Product> 00151 { 00152 public: 00153 explicit KGenericFactory( const char *componentName = 0, const char *catalogName = 0 ) 00154 : KGenericFactoryBase<Product>(componentName, catalogName) 00155 {} 00156 00157 explicit KGenericFactory( const KAboutData *data ) 00158 : KGenericFactoryBase<Product>(data) 00159 {} 00160 00161 protected: 00162 virtual QObject *createObject( QObject *parent, 00163 const char *className, const QStringList &args ) 00164 { 00165 return KDEPrivate::ConcreteFactory<Product, ParentType> 00166 ::create( 0, parent, className, args ); 00167 } 00168 }; 00169 00239 template <class Product, class ProductListTail> 00240 class KGenericFactory< KTypeList<Product, ProductListTail>, QObject > 00241 : public KGenericFactoryBase<KTypeList<Product, ProductListTail> > 00242 { 00243 public: 00244 explicit KGenericFactory( const char *componentName = 0, const char *catalogName = 0 ) 00245 : KGenericFactoryBase<KTypeList<Product, ProductListTail> >(componentName, catalogName) 00246 {} 00247 00248 explicit KGenericFactory( const KAboutData *data ) 00249 : KGenericFactoryBase<KTypeList<Product, ProductListTail> >(data) 00250 {} 00251 00252 00253 protected: 00254 virtual QObject *createObject( QObject *parent, 00255 const char *className, const QStringList &args ) 00256 { 00257 return KDEPrivate::MultiFactory< KTypeList< Product, ProductListTail > > 00258 ::create( 0, parent, className, args ); 00259 } 00260 }; 00261 00331 template <class Product, class ProductListTail, 00332 class ParentType, class ParentTypeListTail> 00333 class KGenericFactory< KTypeList<Product, ProductListTail>, 00334 KTypeList<ParentType, ParentTypeListTail> > 00335 : public KGenericFactoryBase<KTypeList<Product, ProductListTail> > 00336 { 00337 public: 00338 explicit KGenericFactory( const char *componentName = 0, const char *catalogName = 0 ) 00339 : KGenericFactoryBase<KTypeList<Product, ProductListTail> >(componentName, catalogName) 00340 {} 00341 explicit KGenericFactory( const KAboutData *data ) 00342 : KGenericFactoryBase<KTypeList<Product, ProductListTail> >(data) 00343 {} 00344 00345 00346 protected: 00347 virtual QObject *createObject( QObject *parent, 00348 const char *className, const QStringList &args ) 00349 { 00350 return KDEPrivate::MultiFactory< KTypeList< Product, ProductListTail >, 00351 KTypeList< ParentType, ParentTypeListTail > > 00352 ::create( 0, 0, parent, 00353 className, args ); 00354 } 00355 }; 00356 00357 #endif 00358 #endif 00359 00360
KDE 4.6 API Reference