ctkAbstractFactory.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /*=========================================================================
  2. Library: CTK
  3. Copyright (c) Kitware Inc.
  4. All rights reserved.
  5. Distributed under a BSD License. See LICENSE.txt file.
  6. This software is distributed "AS IS" WITHOUT ANY WARRANTY; without even
  7. the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  8. See the above copyright notice for more information.
  9. =========================================================================*/
  10. #ifndef __ctkAbstractFactory_h
  11. #define __ctkAbstractFactory_h
  12. // Qt includes
  13. #include <QString>
  14. #include <QHash>
  15. #include <QSharedPointer>
  16. #include <QStringList>
  17. #ifdef _MSC_VER
  18. /// 4505: 'ctkAbstractFactoryItem<BaseClassType>::loadErrorString' :
  19. /// unreferenced local function has been removed
  20. # pragma warning(disable: 4505)
  21. #endif
  22. //----------------------------------------------------------------------------
  23. template<typename BaseClassType>
  24. class ctkAbstractFactoryItem
  25. {
  26. public:
  27. explicit ctkAbstractFactoryItem(const QString& key);
  28. virtual QString loadErrorString()const;
  29. virtual bool load() = 0;
  30. BaseClassType* instantiate();
  31. bool instantiated();
  32. QString key();
  33. virtual void uninstantiate();
  34. protected:
  35. virtual BaseClassType* instanciator() = 0;
  36. BaseClassType* Instance;
  37. private:
  38. QString Key;
  39. };
  40. //----------------------------------------------------------------------------
  41. template<typename BaseClassType>
  42. class ctkAbstractFactory
  43. {
  44. protected:
  45. typedef typename QHash<QString, QSharedPointer<ctkAbstractFactoryItem<BaseClassType> > >::const_iterator ConstIterator;
  46. typedef typename QHash<QString, QSharedPointer<ctkAbstractFactoryItem<BaseClassType> > >::iterator Iterator;
  47. public:
  48. ///
  49. /// Constructor/Desctructor
  50. explicit ctkAbstractFactory();
  51. virtual ~ctkAbstractFactory();
  52. virtual void printAdditionalInfo();
  53. ///
  54. /// Create an instance of the object
  55. virtual BaseClassType * instantiate(const QString& itemKey);
  56. ///
  57. /// Uninstanciate the object
  58. void uninstantiate(const QString& itemKey);
  59. ///
  60. /// Get list of all registered item names
  61. QStringList names() const;
  62. ///
  63. /// Register items with the factory
  64. /// Method provided for convenience - Should be overloaded in subclasse
  65. virtual void registerItems(){}
  66. protected:
  67. ///
  68. /// Call the load method associated with the item.
  69. /// If succesfully loaded, add it to the internal map.
  70. bool registerItem(const QSharedPointer<ctkAbstractFactoryItem<BaseClassType> > & item);
  71. ///
  72. /// Get a Factory item given its itemKey. Return 0 if any.
  73. ctkAbstractFactoryItem<BaseClassType> * item(const QString& itemKey)const;
  74. private:
  75. ctkAbstractFactory(const ctkAbstractFactory &); /// Not implemented
  76. void operator=(const ctkAbstractFactory&); /// Not implemented
  77. QHash<QString, QSharedPointer<ctkAbstractFactoryItem<BaseClassType> > > RegisteredItemMap;
  78. };
  79. #include "ctkAbstractFactory.tpp"
  80. #endif