ctkAbstractFactory.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /*=========================================================================
  2. Library: CTK
  3. Copyright (c) 2010 Kitware Inc.
  4. Licensed under the Apache License, Version 2.0 (the "License");
  5. you may not use this file except in compliance with the License.
  6. You may obtain a copy of the License at
  7. http://www.commontk.org/LICENSE
  8. Unless required by applicable law or agreed to in writing, software
  9. distributed under the License is distributed on an "AS IS" BASIS,
  10. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. See the License for the specific language governing permissions and
  12. limitations under the License.
  13. =========================================================================*/
  14. #ifndef __ctkAbstractFactory_h
  15. #define __ctkAbstractFactory_h
  16. // Qt includes
  17. #include <QString>
  18. #include <QHash>
  19. #include <QSharedPointer>
  20. #include <QStringList>
  21. #ifdef _MSC_VER
  22. /// 4505: 'ctkAbstractFactoryItem<BaseClassType>::loadErrorString' :
  23. /// unreferenced local function has been removed
  24. # pragma warning(disable: 4505)
  25. #endif
  26. //----------------------------------------------------------------------------
  27. /// ctkAbstractFactoryItem is the base class of factory items. They are
  28. /// uniquely defined by a key and are responsible for creating/holding an
  29. /// instance of a BaseClassType object.
  30. template<typename BaseClassType>
  31. class ctkAbstractFactoryItem
  32. {
  33. public:
  34. explicit ctkAbstractFactoryItem();
  35. virtual ~ctkAbstractFactoryItem(){}
  36. virtual QString loadErrorString()const;
  37. virtual bool load() = 0;
  38. BaseClassType* instantiate();
  39. bool instantiated()const;
  40. virtual void uninstantiate();
  41. void setVerbose(bool value);
  42. bool verbose()const;
  43. protected:
  44. /// Must be reimplemented in subclasses to instanciate a BaseClassType*
  45. virtual BaseClassType* instanciator() = 0;
  46. BaseClassType* Instance;
  47. private:
  48. bool Verbose;
  49. };
  50. //----------------------------------------------------------------------------
  51. /// ctkAbstractFactory is the base class of all the factory where items need
  52. /// to be registered before being instantiated.
  53. /// ctkAbstractFactory contains a collection of ctkAbstractFactoryItems that
  54. /// are uniquely identifyed by a key. Subclasses of ctkAbstractFactory are
  55. /// responsible for populating the list of ctkAbstractFactoryItems.
  56. /// BaseClassType could be any type (most probably a QObject)
  57. template<typename BaseClassType>
  58. class ctkAbstractFactory
  59. {
  60. public:
  61. ///
  62. /// Constructor/Desctructor
  63. explicit ctkAbstractFactory();
  64. virtual ~ctkAbstractFactory();
  65. virtual void printAdditionalInfo();
  66. ///
  67. /// Create an instance of the object. The item corresponding to the key
  68. /// should have been registered before.
  69. virtual BaseClassType * instantiate(const QString& itemKey);
  70. ///
  71. /// Uninstanciate the object. Do nothing if the item given by the key has
  72. /// not be instantiated nor registered.
  73. void uninstantiate(const QString& itemKey);
  74. ///
  75. /// Get list of all registered item keys.
  76. QStringList keys() const;
  77. ///
  78. /// Register items with the factory
  79. /// Method provided for convenience - Should be overloaded in subclasse
  80. virtual void registerItems(){}
  81. /// Enabled verbose output
  82. /// Warning and error message will be printed to standard outputs
  83. void setVerbose(bool value);
  84. bool verbose()const;
  85. protected:
  86. ///
  87. /// Call the load method associated with the item.
  88. /// If succesfully loaded, add it to the internal map.
  89. bool registerItem(const QString& key, const QSharedPointer<ctkAbstractFactoryItem<BaseClassType> > & item);
  90. ///
  91. /// Get a Factory item given its itemKey. Return 0 if any.
  92. ctkAbstractFactoryItem<BaseClassType> * item(const QString& itemKey)const;
  93. typedef typename QHash<QString, QSharedPointer<ctkAbstractFactoryItem<BaseClassType> > >::const_iterator ConstIterator;
  94. typedef typename QHash<QString, QSharedPointer<ctkAbstractFactoryItem<BaseClassType> > >::iterator Iterator;
  95. private:
  96. ctkAbstractFactory(const ctkAbstractFactory &); /// Not implemented
  97. void operator=(const ctkAbstractFactory&); /// Not implemented
  98. QHash<QString, QSharedPointer<ctkAbstractFactoryItem<BaseClassType> > > RegisteredItemMap;
  99. bool Verbose;
  100. };
  101. #include "ctkAbstractFactory.tpp"
  102. #endif