ctkAbstractPluginFactory.tpp 3.4 KB

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