ctkAbstractFactory.tpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /*=========================================================================
  2. Library: CTK
  3. Copyright (c) Kitware Inc.
  4. All rights reserved.
  5. Distributed under a BSD License. See LICENSE.txt file.
  6. This software is distributed "AS IS" WITHOUT ANY WARRANTY; without even
  7. the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  8. See the above copyright notice for more information.
  9. =========================================================================*/
  10. #ifndef __ctkAbstractFactory_tpp
  11. #define __ctkAbstractFactory_tpp
  12. // QT includes
  13. #include <QDebug>
  14. // CTK includes
  15. #include "ctkAbstractFactory.h"
  16. //----------------------------------------------------------------------------
  17. template<typename BaseClassType>
  18. ctkAbstractFactoryItem<BaseClassType>::ctkAbstractFactoryItem(const QString& _key)
  19. :Instance()
  20. ,Key(_key)
  21. {
  22. }
  23. //----------------------------------------------------------------------------
  24. template<typename BaseClassType>
  25. QString ctkAbstractFactoryItem<BaseClassType>::loadErrorString()const
  26. {
  27. return QString();
  28. }
  29. //----------------------------------------------------------------------------
  30. template<typename BaseClassType>
  31. BaseClassType* ctkAbstractFactoryItem<BaseClassType>::instantiate()
  32. {
  33. if (this->Instance)
  34. {
  35. return this->Instance;
  36. }
  37. this->Instance = this->instanciator();
  38. return this->Instance;
  39. }
  40. //----------------------------------------------------------------------------
  41. template<typename BaseClassType>
  42. bool ctkAbstractFactoryItem<BaseClassType>::instantiated()
  43. {
  44. return (this->Instance != 0);
  45. }
  46. //----------------------------------------------------------------------------
  47. template<typename BaseClassType>
  48. QString ctkAbstractFactoryItem<BaseClassType>::key()
  49. {
  50. return this->Key;
  51. }
  52. //----------------------------------------------------------------------------
  53. template<typename BaseClassType>
  54. void ctkAbstractFactoryItem<BaseClassType>::uninstantiate()
  55. {
  56. if (!this->Instance)
  57. {
  58. return;
  59. }
  60. delete this->Instance;
  61. // Make sure the pointer is set to 0. Doing so, Will prevent attempt to
  62. // delete unextising object if uninstantiate() methods is called multiple times.
  63. this->Instance = 0;
  64. }
  65. //----------------------------------------------------------------------------
  66. template<typename BaseClassType>
  67. ctkAbstractFactory<BaseClassType>::ctkAbstractFactory()
  68. {
  69. }
  70. //----------------------------------------------------------------------------
  71. template<typename BaseClassType>
  72. ctkAbstractFactory<BaseClassType>::~ctkAbstractFactory()
  73. {
  74. }
  75. //----------------------------------------------------------------------------
  76. template<typename BaseClassType>
  77. void ctkAbstractFactory<BaseClassType>::printAdditionalInfo()
  78. {
  79. qDebug() << "ctkAbstractFactory<BaseClassType> (" << this << ")";
  80. // TODO
  81. }
  82. //----------------------------------------------------------------------------
  83. template<typename BaseClassType>
  84. BaseClassType* ctkAbstractFactory<BaseClassType>::instantiate(const QString& itemKey)
  85. {
  86. ctkAbstractFactoryItem<BaseClassType>* _item = this->item(itemKey);
  87. return (_item ? _item->instantiate() : 0);
  88. }
  89. //----------------------------------------------------------------------------
  90. template<typename BaseClassType>
  91. void ctkAbstractFactory<BaseClassType>::uninstantiate(const QString& itemKey)
  92. {
  93. ctkAbstractFactoryItem<BaseClassType> * _item = this->item(itemKey);
  94. if (!_item)
  95. {
  96. return;
  97. }
  98. _item->uninstantiate();
  99. }
  100. //----------------------------------------------------------------------------
  101. template<typename BaseClassType>
  102. QStringList ctkAbstractFactory<BaseClassType>::names() const
  103. {
  104. // Since by construction, we checked if a name was already in the QHash,
  105. // there is no need to call 'uniqueKeys'
  106. return this->RegisteredItemMap.keys();
  107. }
  108. //----------------------------------------------------------------------------
  109. template<typename BaseClassType>
  110. bool ctkAbstractFactory<BaseClassType>::registerItem(
  111. const QSharedPointer<ctkAbstractFactoryItem<BaseClassType> > & _item)
  112. {
  113. // Sanity checks
  114. if (!_item || _item->key().isEmpty() || this->item(_item->key()))
  115. {
  116. return false;
  117. }
  118. // Attempt to load it
  119. if (!_item->load())
  120. {
  121. QString errorStr;
  122. if (!_item->loadErrorString().isEmpty())
  123. {
  124. errorStr = " - " + _item->loadErrorString();
  125. }
  126. qCritical() << "Failed to load object:" << _item->key() << errorStr ;
  127. return false;
  128. }
  129. // Store its reference using a QSharedPointer
  130. this->RegisteredItemMap[_item->key()] = _item;
  131. return true;
  132. }
  133. //----------------------------------------------------------------------------
  134. template<typename BaseClassType>
  135. ctkAbstractFactoryItem<BaseClassType> * ctkAbstractFactory<BaseClassType>::item(const QString& itemKey)const
  136. {
  137. ConstIterator iter = this->RegisteredItemMap.find(itemKey);
  138. if ( iter == this->RegisteredItemMap.constEnd())
  139. {
  140. return 0;
  141. }
  142. return iter.value().data();
  143. }
  144. #endif