ctkAbstractFactory.h 4.4 KB

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