ctkAbstractFactory.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. template<typename BaseClassType>
  28. class ctkAbstractFactoryItem
  29. {
  30. public:
  31. explicit ctkAbstractFactoryItem(const QString& key);
  32. virtual ~ctkAbstractFactoryItem(){}
  33. virtual QString loadErrorString()const;
  34. virtual bool load() = 0;
  35. BaseClassType* instantiate();
  36. bool instantiated();
  37. QString key();
  38. virtual void uninstantiate();
  39. void setVerbose(bool value);
  40. bool verbose();
  41. protected:
  42. virtual BaseClassType* instanciator() = 0;
  43. BaseClassType* Instance;
  44. private:
  45. QString Key;
  46. bool Verbose;
  47. };
  48. //----------------------------------------------------------------------------
  49. template<typename BaseClassType>
  50. class ctkAbstractFactory
  51. {
  52. protected:
  53. typedef typename QHash<QString, QSharedPointer<ctkAbstractFactoryItem<BaseClassType> > >::const_iterator ConstIterator;
  54. typedef typename QHash<QString, QSharedPointer<ctkAbstractFactoryItem<BaseClassType> > >::iterator Iterator;
  55. public:
  56. ///
  57. /// Constructor/Desctructor
  58. explicit ctkAbstractFactory();
  59. virtual ~ctkAbstractFactory();
  60. virtual void printAdditionalInfo();
  61. ///
  62. /// Create an instance of the object
  63. virtual BaseClassType * instantiate(const QString& itemKey);
  64. ///
  65. /// Uninstanciate the object
  66. void uninstantiate(const QString& itemKey);
  67. ///
  68. /// Get list of all registered item names
  69. QStringList names() const;
  70. ///
  71. /// Register items with the factory
  72. /// Method provided for convenience - Should be overloaded in subclasse
  73. virtual void registerItems(){}
  74. /// Enabled verbose output
  75. /// Warning and error message will be printed to standard outputs
  76. void setVerbose(bool value);
  77. bool verbose();
  78. protected:
  79. ///
  80. /// Call the load method associated with the item.
  81. /// If succesfully loaded, add it to the internal map.
  82. bool registerItem(const QSharedPointer<ctkAbstractFactoryItem<BaseClassType> > & item);
  83. ///
  84. /// Get a Factory item given its itemKey. Return 0 if any.
  85. ctkAbstractFactoryItem<BaseClassType> * item(const QString& itemKey)const;
  86. private:
  87. ctkAbstractFactory(const ctkAbstractFactory &); /// Not implemented
  88. void operator=(const ctkAbstractFactory&); /// Not implemented
  89. QHash<QString, QSharedPointer<ctkAbstractFactoryItem<BaseClassType> > > RegisteredItemMap;
  90. bool Verbose;
  91. };
  92. #include "ctkAbstractFactory.tpp"
  93. #endif