home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2009 February / PCWFEB09.iso / Software / Linux / SLAX 6.0.8 / slax-6.0.8.iso / slax / base / 006-devel.lzm / usr / include / kgenericfactory.h < prev    next >
Encoding:
C/C++ Source or Header  |  2005-10-10  |  13.5 KB  |  398 lines

  1. /* This file is part of the KDE project
  2.  * Copyright (C) 2001 Simon Hausmann <hausmann@kde.org>
  3.  *
  4.  * This library is free software; you can redistribute it and/or
  5.  * modify it under the terms of the GNU Library General Public
  6.  * License as published by the Free Software Foundation; either
  7.  * version 2 of the License, or (at your option) any later version.
  8.  *
  9.  * This library is distributed in the hope that it will be useful,
  10.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  12.  * Library General Public License for more details.
  13.  *
  14.  * You should have received a copy of the GNU Library General Public License
  15.  * along with this library; see the file COPYING.LIB.  If not, write to
  16.  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  17.  * Boston, MA 02110-1301, USA.
  18.  */
  19. #ifndef __kgenericfactory_h__
  20. #define __kgenericfactory_h__
  21.  
  22. #include <klibloader.h>
  23. #include <ktypelist.h>
  24. #include <kinstance.h>
  25. #include <kgenericfactory.tcc>
  26. #include <kglobal.h>
  27. #include <klocale.h>
  28. #include <kdebug.h>
  29.  
  30. /* @internal */
  31. template <class T>
  32. class KGenericFactoryBase
  33. {
  34. public:
  35.     KGenericFactoryBase( const char *instanceName )
  36.         : m_instanceName( instanceName )
  37.     {
  38.         m_aboutData=0L;
  39.         s_self = this;
  40.         m_catalogueInitialized = false;
  41.     }
  42.     KGenericFactoryBase( const KAboutData *data )
  43.         : m_aboutData(data)
  44.     {
  45.         s_self = this;
  46.         m_catalogueInitialized = false;
  47.     }
  48.  
  49.     virtual ~KGenericFactoryBase()
  50.     {
  51.         if ( s_instance )
  52.             KGlobal::locale()->removeCatalogue( QString::fromAscii( s_instance->instanceName() ) );
  53.         delete s_instance;
  54.         s_instance = 0;
  55.         s_self = 0;
  56.     }
  57.  
  58.     static KInstance *instance();
  59.  
  60. protected:
  61.     virtual KInstance *createInstance()
  62.     {
  63.         if ( m_aboutData )
  64.             return new KInstance( m_aboutData );
  65.         if ( !m_instanceName ) {
  66.             kdWarning() << "KGenericFactory: instance requested but no instance name or about data passed to the constructor!" << endl;
  67.             return 0;
  68.         }
  69.         return new KInstance( m_instanceName );
  70.     }
  71.  
  72.     virtual void setupTranslations( void )
  73.     {
  74.         if ( instance() )
  75.             KGlobal::locale()->insertCatalogue( QString::fromAscii( instance()->instanceName() ) );
  76.     }
  77.  
  78.     void initializeMessageCatalogue()
  79.     {
  80.         if ( !m_catalogueInitialized )
  81.         {
  82.             m_catalogueInitialized = true;
  83.             setupTranslations();
  84.         }
  85.     }
  86.  
  87. private:
  88.     QCString m_instanceName;
  89.     const KAboutData *m_aboutData;
  90.     bool m_catalogueInitialized;
  91.  
  92.     static KInstance *s_instance;
  93.     static KGenericFactoryBase<T> *s_self;
  94. };
  95.  
  96. /* @internal */
  97. template <class T>
  98. KInstance *KGenericFactoryBase<T>::s_instance = 0;
  99.  
  100. /* @internal */
  101. template <class T>
  102. KGenericFactoryBase<T> *KGenericFactoryBase<T>::s_self = 0;
  103.  
  104. /* @internal */
  105. template <class T>
  106. KInstance *KGenericFactoryBase<T>::instance()
  107. {
  108.     if ( !s_instance && s_self )
  109.         s_instance = s_self->createInstance();
  110.     return s_instance;
  111. }
  112.  
  113. /**
  114.  * This template provides a generic implementation of a KLibFactory ,
  115.  * for use with shared library components. It implements the pure virtual
  116.  * createObject method of KLibFactory and instantiates objects of the
  117.  * specified class (template argument) when the class name argument of
  118.  * createObject matches a class name in the given hierarchy.
  119.  *
  120.  * In case you are developing a KParts component, skip this file and
  121.  * go directly to KParts::GenericFactory .
  122.  *
  123.  * Note that the class specified as template argument needs to provide
  124.  * a certain constructor:
  125.  * <ul>
  126.  *     <li>If the class is derived from QObject then it needs to have
  127.  *         a constructor like:
  128.  *         <code>MyClass( QObject *parent, const char *name,
  129.  *                        const QStringList &args );</code>
  130.  *     <li>If the class is derived from QWidget then it needs to have
  131.  *         a constructor like:
  132.  *         <code>MyWidget( QWidget *parent, const char *name,
  133.  *                         const QStringList &args);</code>
  134.  *     <li>If the class is derived from KParts::Part then it needs to have
  135.  *         a constructor like:
  136.  *         <code>MyPart( QWidget *parentWidget, const char *widgetName,
  137.  *                       QObject *parent, const char *name,
  138.  *                       const QStringList &args );</code>
  139.  * </ul>
  140.  * The args QStringList passed to the constructor is the args string list
  141.  * that the caller passed to KLibFactory's create method.
  142.  *
  143.  * In addition upon instantiation this template provides a central
  144.  * KInstance object for your component, accessible through the
  145.  * static instance() method. The instanceName argument of the
  146.  * KGenericFactory constructor is passed to the KInstance object.
  147.  *
  148.  * The creation of the KInstance object can be customized by inheriting
  149.  * from this template class and re-implementing the virtual createInstance
  150.  * method. For example it could look like this:
  151.  * \code
  152.  *     KInstance *MyFactory::createInstance()
  153.  *     {
  154.  *         return new KInstance( myAboutData );
  155.  *     }
  156.  * \endcode
  157.  *
  158.  * Example of usage of the whole template:
  159.  * \code
  160.  *     class MyPlugin : public KParts::Plugin
  161.  *     {
  162.  *         Q_ OBJECT
  163.  *     public:
  164.  *         MyPlugin( QObject *parent, const char *name,
  165.  *                   const QStringList &args );
  166.  *         ...
  167.  *     };
  168.  *
  169.  *     K_EXPORT_COMPONENT_FACTORY( libmyplugin, KGenericFactory<MyPlugin> )
  170.  * \endcode
  171.  */
  172. template <class Product, class ParentType = QObject>
  173. class KGenericFactory : public KLibFactory, public KGenericFactoryBase<Product>
  174. {
  175. public:
  176.     KGenericFactory( const char *instanceName = 0 )
  177.         : KGenericFactoryBase<Product>( instanceName )
  178.     {}
  179.  
  180.     /**
  181.      * @since 3.3
  182.     */
  183.     KGenericFactory( const KAboutData *data )
  184.         : KGenericFactoryBase<Product>( data )
  185.     {}
  186.  
  187.  
  188. protected:
  189.     virtual QObject *createObject( QObject *parent, const char *name,
  190.                                   const char *className, const QStringList &args )
  191.     {
  192.         KGenericFactoryBase<Product>::initializeMessageCatalogue();
  193.         return KDEPrivate::ConcreteFactory<Product, ParentType>
  194.             ::create( 0, 0, parent, name, className, args );
  195.     }
  196. };
  197.  
  198. /**
  199.  * This template provides a generic implementation of a KLibFactory ,
  200.  * for use with shared library components. It implements the pure virtual
  201.  * createObject method of KLibFactory and instantiates objects of the
  202.  * specified classes in the given typelist template argument when the class
  203.  * name argument of createObject matches a class names in the given hierarchy
  204.  * of classes.
  205.  *
  206.  * Note that each class in the specified in the typelist template argument
  207.  * needs to provide a certain constructor:
  208.  * <ul>
  209.  *     <li>If the class is derived from QObject then it needs to have
  210.  *         a constructor like:
  211.  *         <code>MyClass( QObject *parent, const char *name,
  212.  *                        const QStringList &args );</code>
  213.  *     <li>If the class is derived from QWidget then it needs to have
  214.  *         a constructor like:
  215.  *         <code>MyWidget( QWidget *parent, const char *name,
  216.  *                         const QStringList &args);</code>
  217.  *     <li>If the class is derived from KParts::Part then it needs to have
  218.  *         a constructor like:
  219.  *         <code>MyPart( QWidget *parentWidget, const char *widgetName,
  220.  *                       QObject *parent, const char *name,
  221.  *                       const QStringList &args );</code>
  222.  * </ul>
  223.  * The args QStringList passed to the constructor is the args string list
  224.  * that the caller passed to KLibFactory's create method.
  225.  *
  226.  * In addition upon instantiation this template provides a central
  227.  * KInstance object for your component, accessible through the
  228.  * static instance() method. The instanceName argument of the
  229.  * KGenericFactory constructor is passed to the KInstance object.
  230.  *
  231.  * The creation of the KInstance object can be customized by inheriting
  232.  * from this template class and re-implementing the virtual createInstance
  233.  * method. For example it could look like this:
  234.  * \code
  235.  *     KInstance *MyFactory::createInstance()
  236.  *     {
  237.  *         return new KInstance( myAboutData );
  238.  *     }
  239.  * \endcode
  240.  *
  241.  * Example of usage of the whole template:
  242.  * \code
  243.  *     class MyPlugin : public KParts::Plugin
  244.  *     {
  245.  *         Q_ OBJECT
  246.  *     public:
  247.  *         MyPlugin( QObject *parent, const char *name,
  248.  *                   const QStringList &args );
  249.  *         ...
  250.  *     };
  251.  *
  252.  *     class MyDialogComponent : public KDialogBase
  253.  *     {
  254.  *         Q_ OBJECT
  255.  *     public:
  256.  *         MyDialogComponent( QWidget *parentWidget, const char *name,
  257.  *                            const QStringList &args );
  258.  *         ...
  259.  *     };
  260.  *
  261.  *     typedef K_TYPELIST_2( MyPlugin, MyDialogComponent ) Products;
  262.  *     K_EXPORT_COMPONENT_FACTORY( libmyplugin, KGenericFactory<Products> )
  263.  * \endcode
  264.  */
  265. template <class Product, class ProductListTail>
  266. class KGenericFactory< KTypeList<Product, ProductListTail>, QObject >
  267.     : public KLibFactory,
  268.       public KGenericFactoryBase< KTypeList<Product, ProductListTail> >
  269. {
  270. public:
  271.     KGenericFactory( const char *instanceName  = 0 )
  272.         : KGenericFactoryBase< KTypeList<Product, ProductListTail> >( instanceName )
  273.     {}
  274.  
  275.     /**
  276.      * @since 3.3
  277.     */
  278.     KGenericFactory( const KAboutData *data )
  279.         : KGenericFactoryBase< KTypeList<Product, ProductListTail> >( data )
  280.     {}
  281.  
  282.  
  283. protected:
  284.     virtual QObject *createObject( QObject *parent, const char *name,
  285.                                    const char *className, const QStringList &args )
  286.     {
  287.         this->initializeMessageCatalogue();
  288.         return KDEPrivate::MultiFactory< KTypeList< Product, ProductListTail > >
  289.             ::create( 0, 0, parent, name, className, args );
  290.     }
  291. };
  292.  
  293. /**
  294.  * This template provides a generic implementation of a KLibFactory ,
  295.  * for use with shared library components. It implements the pure virtual
  296.  * createObject method of KLibFactory and instantiates objects of the
  297.  * specified classes in the given typelist template argument when the class
  298.  * name argument of createObject matches a class names in the given hierarchy
  299.  * of classes.
  300.  *
  301.  * Note that each class in the specified in the typelist template argument
  302.  * needs to provide a certain constructor:
  303.  * <ul>
  304.  *     <li>If the class is derived from QObject then it needs to have
  305.  *         a constructor like:
  306.  *         <code>MyClass( QObject *parent, const char *name,
  307.  *                        const QStringList &args );</code>
  308.  *     <li>If the class is derived from QWidget then it needs to have
  309.  *         a constructor like:
  310.  *         <code>MyWidget( QWidget *parent, const char *name,
  311.  *                         const QStringList &args);</code>
  312.  *     <li>If the class is derived from KParts::Part then it needs to have
  313.  *         a constructor like:
  314.  *         <code>MyPart( QWidget *parentWidget, const char *widgetName,
  315.  *                       QObject *parent, const char *name,
  316.  *                       const QStringList &args );</code>
  317.  * </ul>
  318.  * The args QStringList passed to the constructor is the args string list
  319.  * that the caller passed to KLibFactory's create method.
  320.  *
  321.  * In addition upon instantiation this template provides a central
  322.  * KInstance object for your component, accessible through the
  323.  * static instance() method. The instanceName argument of the
  324.  * KGenericFactory constructor is passed to the KInstance object.
  325.  *
  326.  * The creation of the KInstance object can be customized by inheriting
  327.  * from this template class and re-implementing the virtual createInstance
  328.  * method. For example it could look like this:
  329.  * \code
  330.  *     KInstance *MyFactory::createInstance()
  331.  *     {
  332.  *         return new KInstance( myAboutData );
  333.  *     }
  334.  * \endcode
  335.  *
  336.  * Example of usage of the whole template:
  337.  * \code
  338.  *     class MyPlugin : public KParts::Plugin
  339.  *     {
  340.  *         Q_ OBJECT
  341.  *     public:
  342.  *         MyPlugin( QObject *parent, const char *name,
  343.  *                   const QStringList &args );
  344.  *         ...
  345.  *     };
  346.  *
  347.  *     class MyDialogComponent : public KDialogBase
  348.  *     {
  349.  *         Q_ OBJECT
  350.  *     public:
  351.  *         MyDialogComponent( QWidget *parentWidget, const char *name,
  352.  *                            const QStringList &args );
  353.  *         ...
  354.  *     };
  355.  *
  356.  *     typedef K_TYPELIST_2( MyPlugin, MyDialogComponent ) Products;
  357.  *     K_EXPORT_COMPONENT_FACTORY( libmyplugin, KGenericFactory<Products> )
  358.  * \endcode
  359.  */
  360. template <class Product, class ProductListTail,
  361.           class ParentType, class ParentTypeListTail>
  362. class KGenericFactory< KTypeList<Product, ProductListTail>,
  363.                        KTypeList<ParentType, ParentTypeListTail> >
  364.     : public KLibFactory,
  365.       public KGenericFactoryBase< KTypeList<Product, ProductListTail> >
  366. {
  367. public:
  368.     KGenericFactory( const char *instanceName  = 0 )
  369.         : KGenericFactoryBase< KTypeList<Product, ProductListTail> >( instanceName )
  370.     {}
  371.     /**
  372.     * @since 3.3
  373.     */
  374.     KGenericFactory( const KAboutData *data )
  375.         : KGenericFactoryBase< KTypeList<Product, ProductListTail> >( data )
  376.     {}
  377.  
  378.  
  379. protected:
  380.     virtual QObject *createObject( QObject *parent, const char *name,
  381.                                    const char *className, const QStringList &args )
  382.     {
  383.         this->initializeMessageCatalogue();
  384.         return KDEPrivate::MultiFactory< KTypeList< Product, ProductListTail >,
  385.                                          KTypeList< ParentType, ParentTypeListTail > >
  386.                                        ::create( 0, 0, parent, name,
  387.                                                  className, args );
  388.     }
  389. };
  390.  
  391.  
  392. /*
  393.  * vim: et sw=4
  394.  */
  395.  
  396. #endif
  397.  
  398.