ctkAbstractPluginFactory.tpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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.txt
  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. #include "ctkScopedCurrentDir.h"
  19. // QT includes
  20. #include <QPluginLoader>
  21. #include <QDebug>
  22. //----------------------------------------------------------------------------
  23. // ctkFactoryPluginItem methods
  24. /*
  25. //----------------------------------------------------------------------------
  26. template<typename BaseClassType>
  27. ctkFactoryPluginItem<BaseClassType>::ctkFactoryPluginItem(const QString& _path)
  28. :ctkAbstractFactoryFileBasedItem<BaseClassType>(_path)
  29. {
  30. }
  31. */
  32. //----------------------------------------------------------------------------
  33. template<typename BaseClassType>
  34. bool ctkFactoryPluginItem<BaseClassType>::load()
  35. {
  36. ctkScopedCurrentDir scopedCurrentDir(QFileInfo(this->path()).path());
  37. this->Loader.setFileName(this->path());
  38. return this->Loader.load();
  39. }
  40. //----------------------------------------------------------------------------
  41. template<typename BaseClassType>
  42. QString ctkFactoryPluginItem<BaseClassType>::loadErrorString()const
  43. {
  44. return this->Loader.errorString();
  45. }
  46. //----------------------------------------------------------------------------
  47. template<typename BaseClassType>
  48. BaseClassType* ctkFactoryPluginItem<BaseClassType>::instanciator()
  49. {
  50. //qDebug() << "PluginItem::instantiate - name:" << this->path();
  51. QObject * object = this->Loader.instance();
  52. if (!object)
  53. {
  54. if (this->verbose())
  55. {
  56. qWarning() << "Failed to instantiate plugin:" << this->path();
  57. }
  58. return 0;
  59. }
  60. BaseClassType* castedObject = qobject_cast<BaseClassType*>(object);
  61. if (!castedObject)
  62. {
  63. if (this->verbose())
  64. {
  65. qWarning() << "Failed to access interface [" << BaseClassType::staticMetaObject.className()
  66. << "] in plugin:" << this->path() << "\n instead, got object of type:" << object->metaObject()->className();
  67. }
  68. delete object; // Clean memory
  69. return 0;
  70. }
  71. return castedObject;
  72. }
  73. //----------------------------------------------------------------------------
  74. // ctkAbstractPluginFactory methods
  75. //-----------------------------------------------------------------------------
  76. template<typename BaseClassType>
  77. ctkAbstractFactoryItem<BaseClassType>* ctkAbstractPluginFactory<BaseClassType>
  78. ::createFactoryFileBasedItem()
  79. {
  80. return new ctkFactoryPluginItem<BaseClassType>();
  81. }
  82. //-----------------------------------------------------------------------------
  83. template<typename BaseClassType>
  84. bool ctkAbstractPluginFactory<BaseClassType>
  85. ::isValidFile(const QFileInfo& fileInfo)const
  86. {
  87. return this->ctkAbstractFileBasedFactory<BaseClassType>::isValidFile(fileInfo) &&
  88. QLibrary::isLibrary(fileInfo.fileName());
  89. }
  90. #endif