ctkAbstractFactory.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. /// 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(const QString& key);
  35. virtual ~ctkAbstractFactoryItem(){}
  36. virtual QString loadErrorString()const;
  37. virtual bool load() = 0;
  38. BaseClassType* instantiate();
  39. bool instantiated()const;
  40. virtual void uninstantiate();
  41. QString key()const;
  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. QString Key;
  50. bool Verbose;
  51. };
  52. //----------------------------------------------------------------------------
  53. /// ctkAbstractFactory is the base class of all the factory where items need
  54. /// to be registered before being instantiated.
  55. /// 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. ///
  64. /// Constructor/Desctructor
  65. explicit ctkAbstractFactory();
  66. virtual ~ctkAbstractFactory();
  67. virtual void printAdditionalInfo();
  68. ///
  69. /// Create an instance of the object. The item corresponding to the key
  70. /// should have been registered before.
  71. virtual BaseClassType * instantiate(const QString& itemKey);
  72. ///
  73. /// Uninstanciate the object. Do nothing if the item given by the key has
  74. /// not be instantiated nor registered.
  75. void uninstantiate(const QString& itemKey);
  76. ///
  77. /// Get list of all registered item keys.
  78. QStringList keys() const;
  79. ///
  80. /// Register items with the factory
  81. /// Method provided for convenience - Should be overloaded in subclasse
  82. virtual void registerItems(){}
  83. /// Enabled verbose output
  84. /// Warning and error message will be printed to standard outputs
  85. void setVerbose(bool value);
  86. bool verbose()const;
  87. protected:
  88. ///
  89. /// Call the load method associated with the item.
  90. /// If succesfully loaded, add it to the internal map.
  91. bool registerItem(const QSharedPointer<ctkAbstractFactoryItem<BaseClassType> > & item);
  92. ///
  93. /// Get a Factory item given its itemKey. Return 0 if any.
  94. ctkAbstractFactoryItem<BaseClassType> * item(const QString& itemKey)const;
  95. typedef typename QHash<QString, QSharedPointer<ctkAbstractFactoryItem<BaseClassType> > >::const_iterator ConstIterator;
  96. typedef typename QHash<QString, QSharedPointer<ctkAbstractFactoryItem<BaseClassType> > >::iterator Iterator;
  97. private:
  98. ctkAbstractFactory(const ctkAbstractFactory &); /// Not implemented
  99. void operator=(const ctkAbstractFactory&); /// Not implemented
  100. QHash<QString, QSharedPointer<ctkAbstractFactoryItem<BaseClassType> > > RegisteredItemMap;
  101. bool Verbose;
  102. };
  103. #include "ctkAbstractFactory.tpp"
  104. #endif