ctkAbstractPluginFactory.tpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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 __ctkAbstractPluginFactory_tpp
  15. #define __ctkAbstractPluginFactory_tpp
  16. // CTK includes
  17. #include "ctkAbstractPluginFactory.h"
  18. // QT includes
  19. #include <QPluginLoader>
  20. #include <QDebug>
  21. //----------------------------------------------------------------------------
  22. // ctkFactoryPluginItem methods
  23. //----------------------------------------------------------------------------
  24. template<typename BaseClassType>
  25. ctkFactoryPluginItem<BaseClassType>::ctkFactoryPluginItem(const QString& _key,
  26. const QString& _path)
  27. :ctkAbstractFactoryItem<BaseClassType>(_key),Path(_path)
  28. {
  29. }
  30. //----------------------------------------------------------------------------
  31. template<typename BaseClassType>
  32. bool ctkFactoryPluginItem<BaseClassType>::load()
  33. {
  34. this->Loader.setFileName(this->path());
  35. return this->Loader.load();
  36. }
  37. //----------------------------------------------------------------------------
  38. template<typename BaseClassType>
  39. QString ctkFactoryPluginItem<BaseClassType>::path()const
  40. {
  41. return this->Path;
  42. }
  43. //----------------------------------------------------------------------------
  44. template<typename BaseClassType>
  45. QString ctkFactoryPluginItem<BaseClassType>::loadErrorString()const
  46. {
  47. return this->Loader.errorString();
  48. }
  49. //----------------------------------------------------------------------------
  50. template<typename BaseClassType>
  51. BaseClassType* ctkFactoryPluginItem<BaseClassType>::instanciator()
  52. {
  53. //qDebug() << "PluginItem::instantiate - name:" << this->path();
  54. QObject * object = this->Loader.instance();
  55. if (!object)
  56. {
  57. if (this->verbose())
  58. {
  59. qWarning() << "Failed to instantiate plugin:" << this->path();
  60. }
  61. return 0;
  62. }
  63. BaseClassType* castedObject = qobject_cast<BaseClassType*>(object);
  64. if (!castedObject)
  65. {
  66. if (this->verbose())
  67. {
  68. qWarning() << "Failed to access interface [" << BaseClassType::staticMetaObject.className()
  69. << "] in plugin:" << this->path();
  70. }
  71. delete object; // Clean memory
  72. return 0;
  73. }
  74. return castedObject;
  75. }
  76. //----------------------------------------------------------------------------
  77. // ctkAbstractPluginFactory methods
  78. //----------------------------------------------------------------------------
  79. template<typename BaseClassType, typename FactoryItemType>
  80. ctkAbstractPluginFactory<BaseClassType, FactoryItemType>::ctkAbstractPluginFactory():ctkAbstractFactory<BaseClassType>()
  81. {
  82. }
  83. //----------------------------------------------------------------------------
  84. template<typename BaseClassType, typename FactoryItemType>
  85. ctkAbstractPluginFactory<BaseClassType, FactoryItemType>::~ctkAbstractPluginFactory()
  86. {
  87. }
  88. //-----------------------------------------------------------------------------
  89. template<typename BaseClassType, typename FactoryItemType>
  90. QString ctkAbstractPluginFactory<BaseClassType, FactoryItemType>::fileNameToKey(
  91. const QString& fileName)
  92. {
  93. return fileName;
  94. }
  95. //----------------------------------------------------------------------------
  96. template<typename BaseClassType, typename FactoryItemType>
  97. bool ctkAbstractPluginFactory<BaseClassType, FactoryItemType>::registerLibrary(const QFileInfo& file, QString& key)
  98. {
  99. key = this->fileNameToKey(file.fileName());
  100. // Check if already registered
  101. if (this->item(key))
  102. {
  103. return false;
  104. }
  105. QSharedPointer<FactoryItemType> _item =
  106. QSharedPointer<FactoryItemType>(new FactoryItemType(key, file.filePath()));
  107. _item->setVerbose(this->verbose());
  108. return this->registerItem(_item);
  109. }
  110. #endif