ctkAbstractFactory.tpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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_tpp
  15. #define __ctkAbstractFactory_tpp
  16. // QT includes
  17. #include <QDebug>
  18. // CTK includes
  19. #include "ctkAbstractFactory.h"
  20. //----------------------------------------------------------------------------
  21. // ctkAbstractFactoryItem methods
  22. //----------------------------------------------------------------------------
  23. template<typename BaseClassType>
  24. ctkAbstractFactoryItem<BaseClassType>::ctkAbstractFactoryItem(const QString& _key)
  25. :Instance()
  26. ,Key(_key)
  27. {
  28. this->Verbose = false;
  29. }
  30. //----------------------------------------------------------------------------
  31. template<typename BaseClassType>
  32. QString ctkAbstractFactoryItem<BaseClassType>::loadErrorString()const
  33. {
  34. return QString();
  35. }
  36. //----------------------------------------------------------------------------
  37. template<typename BaseClassType>
  38. BaseClassType* ctkAbstractFactoryItem<BaseClassType>::instantiate()
  39. {
  40. if (this->Instance)
  41. {
  42. return this->Instance;
  43. }
  44. this->Instance = this->instanciator();
  45. return this->Instance;
  46. }
  47. //----------------------------------------------------------------------------
  48. template<typename BaseClassType>
  49. bool ctkAbstractFactoryItem<BaseClassType>::instantiated()
  50. {
  51. return (this->Instance != 0);
  52. }
  53. //----------------------------------------------------------------------------
  54. template<typename BaseClassType>
  55. QString ctkAbstractFactoryItem<BaseClassType>::key()
  56. {
  57. return this->Key;
  58. }
  59. //----------------------------------------------------------------------------
  60. template<typename BaseClassType>
  61. void ctkAbstractFactoryItem<BaseClassType>::uninstantiate()
  62. {
  63. if (!this->Instance)
  64. {
  65. return;
  66. }
  67. delete this->Instance;
  68. // Make sure the pointer is set to 0. Doing so, Will prevent attempt to
  69. // delete unextising object if uninstantiate() methods is called multiple times.
  70. this->Instance = 0;
  71. }
  72. //----------------------------------------------------------------------------
  73. template<typename BaseClassType>
  74. void ctkAbstractFactoryItem<BaseClassType>::setVerbose(bool value)
  75. {
  76. this->Verbose = value;
  77. }
  78. //----------------------------------------------------------------------------
  79. template<typename BaseClassType>
  80. bool ctkAbstractFactoryItem<BaseClassType>::verbose()
  81. {
  82. return this->Verbose;
  83. }
  84. //----------------------------------------------------------------------------
  85. // ctkAbstractFactory methods
  86. //----------------------------------------------------------------------------
  87. template<typename BaseClassType>
  88. ctkAbstractFactory<BaseClassType>::ctkAbstractFactory()
  89. {
  90. this->Verbose = false;
  91. }
  92. //----------------------------------------------------------------------------
  93. template<typename BaseClassType>
  94. ctkAbstractFactory<BaseClassType>::~ctkAbstractFactory()
  95. {
  96. }
  97. //----------------------------------------------------------------------------
  98. template<typename BaseClassType>
  99. void ctkAbstractFactory<BaseClassType>::printAdditionalInfo()
  100. {
  101. qDebug() << "ctkAbstractFactory<BaseClassType> (" << this << ")";
  102. // TODO
  103. }
  104. //----------------------------------------------------------------------------
  105. template<typename BaseClassType>
  106. BaseClassType* ctkAbstractFactory<BaseClassType>::instantiate(const QString& itemKey)
  107. {
  108. ctkAbstractFactoryItem<BaseClassType>* _item = this->item(itemKey);
  109. return (_item ? _item->instantiate() : 0);
  110. }
  111. //----------------------------------------------------------------------------
  112. template<typename BaseClassType>
  113. void ctkAbstractFactory<BaseClassType>::uninstantiate(const QString& itemKey)
  114. {
  115. ctkAbstractFactoryItem<BaseClassType> * _item = this->item(itemKey);
  116. if (!_item)
  117. {
  118. return;
  119. }
  120. _item->uninstantiate();
  121. }
  122. //----------------------------------------------------------------------------
  123. template<typename BaseClassType>
  124. QStringList ctkAbstractFactory<BaseClassType>::names() const
  125. {
  126. // Since by construction, we checked if a name was already in the QHash,
  127. // there is no need to call 'uniqueKeys'
  128. return this->RegisteredItemMap.keys();
  129. }
  130. //----------------------------------------------------------------------------
  131. template<typename BaseClassType>
  132. bool ctkAbstractFactory<BaseClassType>::registerItem(
  133. const QSharedPointer<ctkAbstractFactoryItem<BaseClassType> > & _item)
  134. {
  135. // Sanity checks
  136. if (!_item || _item->key().isEmpty() || this->item(_item->key()))
  137. {
  138. return false;
  139. }
  140. // Attempt to load it
  141. if (!_item->load())
  142. {
  143. QString errorStr;
  144. if (!_item->loadErrorString().isEmpty())
  145. {
  146. errorStr = " - " + _item->loadErrorString();
  147. }
  148. if (this->verbose())
  149. {
  150. qCritical() << "Failed to load object:" << _item->key() << errorStr ;
  151. }
  152. return false;
  153. }
  154. // Store its reference using a QSharedPointer
  155. this->RegisteredItemMap[_item->key()] = _item;
  156. return true;
  157. }
  158. //----------------------------------------------------------------------------
  159. template<typename BaseClassType>
  160. ctkAbstractFactoryItem<BaseClassType> * ctkAbstractFactory<BaseClassType>::item(const QString& itemKey)const
  161. {
  162. ConstIterator iter = this->RegisteredItemMap.find(itemKey);
  163. if ( iter == this->RegisteredItemMap.constEnd())
  164. {
  165. return 0;
  166. }
  167. return iter.value().data();
  168. }
  169. //----------------------------------------------------------------------------
  170. template<typename BaseClassType>
  171. void ctkAbstractFactory<BaseClassType>::setVerbose(bool value)
  172. {
  173. this->Verbose = value;
  174. }
  175. //----------------------------------------------------------------------------
  176. template<typename BaseClassType>
  177. bool ctkAbstractFactory<BaseClassType>::verbose()
  178. {
  179. return this->Verbose;
  180. }
  181. #endif