ctkAbstractFactory.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /*=========================================================================
  2. Library: CTK
  3. Copyright (c) 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 QString loadErrorString()const;
  36. virtual bool load() = 0;
  37. BaseClassType* instantiate();
  38. bool instantiated()const;
  39. virtual void uninstantiate();
  40. void setVerbose(bool value);
  41. bool verbose()const;
  42. protected:
  43. /// Must be reimplemented in subclasses to instanciate a BaseClassType*
  44. virtual BaseClassType* instanciator() = 0;
  45. BaseClassType* Instance;
  46. private:
  47. bool Verbose;
  48. };
  49. //----------------------------------------------------------------------------
  50. /// \brief ctkAbstractFactory is the base class of all the factory where items need
  51. /// to be registered before being instantiated.
  52. /// \paragraph ctkAbstractFactory contains a collection of ctkAbstractFactoryItems that
  53. /// are uniquely identifyed by a key. Subclasses of ctkAbstractFactory are
  54. /// responsible for populating the list of ctkAbstractFactoryItems.
  55. /// BaseClassType could be any type (most probably a QObject)
  56. template<typename BaseClassType>
  57. class ctkAbstractFactory
  58. {
  59. public:
  60. /// Constructor/Desctructor
  61. explicit ctkAbstractFactory();
  62. virtual ~ctkAbstractFactory();
  63. virtual void printAdditionalInfo();
  64. /// \brief Create an instance of the object.
  65. /// The item corresponding to the key should have been registered before.
  66. virtual BaseClassType * instantiate(const QString& itemKey);
  67. /// \brief Uninstanciate the object.
  68. /// Do nothing if the item given by the key has not be instantiated nor registered.
  69. void uninstantiate(const QString& itemKey);
  70. /// \brief Get path associated with the item identified by \a itemKey
  71. /// Should be overloaded in subclasse
  72. virtual QString path(const QString& itemKey){ Q_UNUSED(itemKey); return QString(); }
  73. /// Get list of all registered item keys.
  74. QStringList keys() const;
  75. /// \brief Register items with the factory
  76. /// Method provided for convenience - Should be overloaded in subclasse
  77. virtual void registerItems(){}
  78. /// \brief Enabled verbose output
  79. /// Warning and error message will be printed to standard outputs
  80. void setVerbose(bool value);
  81. bool verbose()const;
  82. protected:
  83. /// \brief Call the load method associated with the item.
  84. /// If succesfully loaded, add it to the internal map.
  85. bool registerItem(const QString& key, const QSharedPointer<ctkAbstractFactoryItem<BaseClassType> > & item);
  86. /// Get a Factory item given its itemKey. Return 0 if any.
  87. ctkAbstractFactoryItem<BaseClassType> * item(const QString& itemKey)const;
  88. typedef typename QHash<QString, QSharedPointer<ctkAbstractFactoryItem<BaseClassType> > >::const_iterator ConstIterator;
  89. typedef typename QHash<QString, QSharedPointer<ctkAbstractFactoryItem<BaseClassType> > >::iterator Iterator;
  90. private:
  91. ctkAbstractFactory(const ctkAbstractFactory &); /// Not implemented
  92. void operator=(const ctkAbstractFactory&); /// Not implemented
  93. QHash<QString, QSharedPointer<ctkAbstractFactoryItem<BaseClassType> > > RegisteredItemMap;
  94. bool Verbose;
  95. };
  96. #include "ctkAbstractFactory.tpp"
  97. #endif