TDE core libraries
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

genericfactory.h 6.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. #ifndef __tdeparts__genericfactory_h__
  2. #define __tdeparts__genericfactory_h__
  3. #include <tdeparts/factory.h>
  4. #include <tdeparts/part.h>
  5. #include <kgenericfactory.h>
  6. #include <tdeaboutdata.h>
  7. #include <kdebug.h>
  8. namespace KParts
  9. {
  10. /**
  11. * @internal
  12. */
  13. template <class T>
  14. class GenericFactoryBase : public KParts::Factory
  15. {
  16. public:
  17. GenericFactoryBase()
  18. {
  19. if ( s_self )
  20. kdWarning() << "KParts::GenericFactory instantiated more than once!" << endl;
  21. s_self = this;
  22. }
  23. virtual ~GenericFactoryBase()
  24. {
  25. delete s_aboutData;
  26. delete s_instance;
  27. s_aboutData = 0;
  28. s_instance = 0;
  29. s_self = 0;
  30. }
  31. static TDEInstance *instance();
  32. static TDEAboutData *aboutData();
  33. protected:
  34. virtual TDEInstance *createInstance()
  35. {
  36. return new TDEInstance( aboutData() );
  37. }
  38. virtual void virtual_hook( int id, void *data )
  39. {
  40. if ( id != VIRTUAL_QUERY_INSTANCE_PARAMS ) {
  41. KParts::Factory::virtual_hook( id, data );
  42. return;
  43. }
  44. QueryInstanceParams *params = reinterpret_cast<QueryInstanceParams *>( data );
  45. params->instance = instance();
  46. }
  47. private:
  48. static GenericFactoryBase<T> *s_self;
  49. static TDEInstance *s_instance;
  50. static TDEAboutData *s_aboutData;
  51. };
  52. /**
  53. * A template for a KParts::Factory implementation. It implements the pure virtual
  54. * createPartObject method by instantiating the template argument when requested
  55. * through the className field. In addition it is a container for a part's TDEInstance
  56. * object, by providing a static TDEInstance *instance() method.
  57. *
  58. * The template argument has to inherit from KParts::Part and has to implement two methods:
  59. * 1) There needs to be a public constructor with the following signature:
  60. * MyPart( TQWidget *parentWidget, const char *widgetName, TQObject *parent, const char *name, const TQStringList& args )
  61. *
  62. * 2) It needs to provide one static method to create a TDEAboutData object per
  63. * request, holding information about the component's name, its authors, license, etc.
  64. * The signature of that static method has to be
  65. * TDEAboutData *createAboutData()
  66. *
  67. * The template will take care of memory management of the TDEInstance and the TDEAboutData object,
  68. * meaning ownership of what createAboutData returns is passed to the caller (this template) .
  69. *
  70. * For advanced use you can also inherit from the template and re-implement additionally the
  71. * virtual TDEInstance *createInstance() method, for example in case you want to extend the
  72. * paths of your instance's TDEStandardDirs object.
  73. *
  74. * If a KParts::ReadOnlyPart is requested through this factory and the template argument
  75. * implements a KParts::ReadWritePart then setReadWrite( false ) will automatically be
  76. * called in createPartObject.
  77. *
  78. * Use the factory through the K_EXPORT_COMPONENT_FACTORY macro, like that:
  79. * \code
  80. * typedef KParts::GenericFactory&lt;YourKPart&gt; YourKPartFactory;
  81. * K_EXPORT_COMPONENT_FACTORY( yourlibrary, YourKPartFactory )
  82. * \endcode
  83. * yourlibrary is the library name that you compiled your KPart into.
  84. */
  85. template <class T>
  86. class GenericFactory : public GenericFactoryBase<T>
  87. {
  88. public:
  89. GenericFactory() { }
  90. virtual KParts::Part *createPartObject( TQWidget *parentWidget, const char *widgetName,
  91. TQObject *parent, const char *name,
  92. const char *className,
  93. const TQStringList &args )
  94. {
  95. T *part = KDEPrivate::ConcreteFactory<T>::create( parentWidget,
  96. widgetName,
  97. parent,
  98. name,
  99. className,
  100. args );
  101. if ( part && !qstrcmp( className, "KParts::ReadOnlyPart" ) )
  102. {
  103. KParts::ReadWritePart *rwp = tqt_dynamic_cast<KParts::ReadWritePart *>( part );
  104. if ( rwp )
  105. rwp->setReadWrite( false );
  106. }
  107. return part;
  108. }
  109. };
  110. template <class T1, class T2>
  111. class GenericFactory< KTypeList<T1, T2> > : public GenericFactoryBase<T1>
  112. {
  113. public:
  114. GenericFactory() { }
  115. virtual KParts::Part *createPartObject( TQWidget *parentWidget, const char *widgetName,
  116. TQObject *parent, const char *name,
  117. const char *className,
  118. const TQStringList &args )
  119. {
  120. TQObject *object = KDEPrivate::MultiFactory< KTypeList<T1, T2> >::create( parentWidget,
  121. widgetName,
  122. parent, name,
  123. className,
  124. args );
  125. // (this cast is guaranteed to work...)
  126. KParts::Part *part = tqt_dynamic_cast<KParts::Part *>( object );
  127. if ( part && !qstrcmp( className, "KParts::ReadOnlyPart" ) )
  128. {
  129. KParts::ReadWritePart *rwp = tqt_dynamic_cast<KParts::ReadWritePart *>( part );
  130. if ( rwp )
  131. rwp->setReadWrite( false );
  132. }
  133. return part;
  134. }
  135. };
  136. /**
  137. * @internal
  138. */
  139. template <class T>
  140. GenericFactoryBase<T> *GenericFactoryBase<T>::s_self = 0;
  141. /**
  142. * @internal
  143. */
  144. template <class T>
  145. TDEInstance *GenericFactoryBase<T>::s_instance = 0;
  146. /**
  147. * @internal
  148. */
  149. template <class T>
  150. TDEAboutData *GenericFactoryBase<T>::s_aboutData = 0;
  151. /**
  152. * @internal
  153. */
  154. template <class T>
  155. TDEInstance *GenericFactoryBase<T>::instance()
  156. {
  157. if ( !s_instance )
  158. {
  159. if ( s_self )
  160. s_instance = s_self->createInstance();
  161. else
  162. s_instance = new TDEInstance( aboutData() );
  163. }
  164. return s_instance;
  165. }
  166. /**
  167. * @internal
  168. */
  169. template <class T>
  170. TDEAboutData *GenericFactoryBase<T>::aboutData()
  171. {
  172. if ( !s_aboutData )
  173. s_aboutData = T::createAboutData();
  174. return s_aboutData;
  175. }
  176. }
  177. #endif
  178. /**
  179. * vim: et sw=4
  180. */