ctkAbstractPluginFactory.tpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. bool loaded = this->Loader.load();
  39. if (!loaded)
  40. {
  41. this->appendLoadErrorString(this->loadErrorString());
  42. }
  43. return loaded;
  44. }
  45. //----------------------------------------------------------------------------
  46. template<typename BaseClassType>
  47. QString ctkFactoryPluginItem<BaseClassType>::loadErrorString()const
  48. {
  49. return this->Loader.errorString();
  50. }
  51. //----------------------------------------------------------------------------
  52. template<typename BaseClassType>
  53. BaseClassType* ctkFactoryPluginItem<BaseClassType>::instanciator()
  54. {
  55. //qDebug() << "PluginItem::instantiate - name:" << this->path();
  56. QObject * object = this->Loader.instance();
  57. if (!object)
  58. {
  59. if (this->verbose())
  60. {
  61. this->appendLoadErrorString(QString("Failed to instantiate plugin: %1").arg(this->path()));
  62. }
  63. return 0;
  64. }
  65. BaseClassType* castedObject = qobject_cast<BaseClassType*>(object);
  66. if (!castedObject)
  67. {
  68. if (this->verbose())
  69. {
  70. this->appendLoadErrorString(
  71. QString("Plugin %1 - Failed to access expected interface [%2] - Instead got object of type [%3]")
  72. .arg(this->path())
  73. .arg(BaseClassType::staticMetaObject.className())
  74. .arg(object->metaObject()->className()));
  75. }
  76. delete object; // Clean memory
  77. return 0;
  78. }
  79. return castedObject;
  80. }
  81. //----------------------------------------------------------------------------
  82. // ctkAbstractPluginFactory methods
  83. //-----------------------------------------------------------------------------
  84. template<typename BaseClassType>
  85. ctkAbstractFactoryItem<BaseClassType>* ctkAbstractPluginFactory<BaseClassType>
  86. ::createFactoryFileBasedItem()
  87. {
  88. return new ctkFactoryPluginItem<BaseClassType>();
  89. }
  90. //-----------------------------------------------------------------------------
  91. template<typename BaseClassType>
  92. bool ctkAbstractPluginFactory<BaseClassType>
  93. ::isValidFile(const QFileInfo& fileInfo)const
  94. {
  95. return this->ctkAbstractFileBasedFactory<BaseClassType>::isValidFile(fileInfo) &&
  96. QLibrary::isLibrary(fileInfo.fileName());
  97. }
  98. #endif