ctkAbstractFactory.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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 ~ctkAbstractFactoryItem();
  38. virtual bool load() = 0;
  39. QStringList instantiateErrorStrings()const;
  40. QStringList instantiateWarningStrings()const;
  41. QStringList loadErrorStrings()const;
  42. QStringList loadWarningStrings()const;
  43. BaseClassType* instantiate();
  44. bool isInstantiated()const;
  45. BaseClassType* instance()const;
  46. virtual void uninstantiate();
  47. void setVerbose(bool value);
  48. bool verbose()const;
  49. protected:
  50. void appendInstantiateErrorString(const QString& msg);
  51. void clearInstantiateErrorStrings();
  52. void appendInstantiateWarningString(const QString& msg);
  53. void clearInstantiateWarningStrings();
  54. void appendLoadErrorString(const QString& msg);
  55. void clearLoadErrorStrings();
  56. void appendLoadWarningString(const QString& msg);
  57. void clearLoadWarningStrings();
  58. /// Must be reimplemented in subclasses to instanciate a BaseClassType*
  59. virtual BaseClassType* instanciator() = 0;
  60. BaseClassType* Instance;
  61. private:
  62. QStringList InstantiateErrorStrings;
  63. QStringList InstantiateWarningStrings;
  64. QStringList LoadErrorStrings;
  65. QStringList LoadWarningStrings;
  66. bool Verbose;
  67. };
  68. //----------------------------------------------------------------------------
  69. /// \ingroup Core
  70. /// \brief ctkAbstractFactory is the base class of all the factory where items need
  71. /// to be registered before being instantiated.
  72. /// <p> ctkAbstractFactory contains a collection of ctkAbstractFactoryItems that
  73. /// are uniquely identifyed by a key. Subclasses of ctkAbstractFactory are
  74. /// responsible for populating the list of ctkAbstractFactoryItems.
  75. /// BaseClassType could be any type (most probably a QObject)
  76. template<typename BaseClassType>
  77. class ctkAbstractFactory
  78. {
  79. public:
  80. typedef QHash<QString, QSharedPointer<ctkAbstractFactoryItem<BaseClassType> > > HashType;
  81. /// Constructor/Desctructor
  82. ctkAbstractFactory();
  83. virtual ~ctkAbstractFactory();
  84. virtual void printAdditionalInfo();
  85. /// \brief Create an instance of the object.
  86. /// The item corresponding to the key should have been registered before.
  87. virtual BaseClassType * instantiate(const QString& itemKey);
  88. /// \brief Return the instance associated with \a itemKey if any, otherwise
  89. /// return 0.
  90. virtual BaseClassType * instance(const QString& itemKey);
  91. /// \brief Uninstanciate the object.
  92. /// Do nothing if the item given by the key has not be instantiated nor registered.
  93. void uninstantiate(const QString& itemKey);
  94. /// \brief Get path associated with the item identified by \a itemKey
  95. /// Should be overloaded in subclasse
  96. virtual QString path(const QString& itemKey){ Q_UNUSED(itemKey); return QString(); }
  97. void setSharedItems(const QSharedPointer<HashType>& items);
  98. QSharedPointer<HashType> sharedItems();
  99. /// Get list of all registered item keys.
  100. QStringList itemKeys() const;
  101. /// \brief Register items with the factory
  102. /// Method provided for convenience - Should be overloaded in subclasse
  103. virtual void registerItems(){}
  104. /// \brief Enabled verbose output
  105. /// Warning and error message will be printed to standard outputs
  106. void setVerbose(bool value);
  107. bool verbose()const;
  108. protected:
  109. void displayStatusMessage(const QtMsgType& type, const QString& description,
  110. const QString& status, bool display);
  111. /// \brief Call the load method associated with the item.
  112. /// If succesfully loaded, add it to the internal map.
  113. bool registerItem(const QString& key, const QSharedPointer<ctkAbstractFactoryItem<BaseClassType> > & item);
  114. /// Get a Factory item given its itemKey. Return 0 if any.
  115. ctkAbstractFactoryItem<BaseClassType> * item(const QString& itemKey)const;
  116. ctkAbstractFactoryItem<BaseClassType> * sharedItem(const QString& itemKey)const;
  117. typedef typename HashType::const_iterator ConstIterator;
  118. typedef typename HashType::iterator Iterator;
  119. private:
  120. /*
  121. ctkAbstractFactory(const ctkAbstractFactory &); /// Not implemented
  122. void operator=(const ctkAbstractFactory&); /// Not implemented
  123. */
  124. HashType RegisteredItemMap;
  125. QSharedPointer<HashType> SharedRegisteredItemMap;
  126. bool Verbose;
  127. };
  128. #include "ctkAbstractFactory.tpp"
  129. #endif