ctkAbstractFactory.tpp 5.1 KB

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