ctkAbstractFactory.h 3.1 KB

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