ctkAbstractFactory.tpp 4.8 KB

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