ctkAbstractPluginFactory.tpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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.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& _path)
  26. :Path(_path)
  27. {
  28. }
  29. //----------------------------------------------------------------------------
  30. template<typename BaseClassType>
  31. bool ctkFactoryPluginItem<BaseClassType>::load()
  32. {
  33. this->Loader.setFileName(this->path());
  34. return this->Loader.load();
  35. }
  36. //----------------------------------------------------------------------------
  37. template<typename BaseClassType>
  38. QString ctkFactoryPluginItem<BaseClassType>::path()const
  39. {
  40. return this->Path;
  41. }
  42. //----------------------------------------------------------------------------
  43. template<typename BaseClassType>
  44. QString ctkFactoryPluginItem<BaseClassType>::loadErrorString()const
  45. {
  46. return this->Loader.errorString();
  47. }
  48. //----------------------------------------------------------------------------
  49. template<typename BaseClassType>
  50. BaseClassType* ctkFactoryPluginItem<BaseClassType>::instanciator()
  51. {
  52. //qDebug() << "PluginItem::instantiate - name:" << this->path();
  53. QObject * object = this->Loader.instance();
  54. if (!object)
  55. {
  56. if (this->verbose())
  57. {
  58. qWarning() << "Failed to instantiate plugin:" << this->path();
  59. }
  60. return 0;
  61. }
  62. BaseClassType* castedObject = qobject_cast<BaseClassType*>(object);
  63. if (!castedObject)
  64. {
  65. if (this->verbose())
  66. {
  67. qWarning() << "Failed to access interface [" << BaseClassType::staticMetaObject.className()
  68. << "] in plugin:" << this->path() << "\n instead, got object of type:" << object->metaObject()->className();
  69. }
  70. delete object; // Clean memory
  71. return 0;
  72. }
  73. return castedObject;
  74. }
  75. //----------------------------------------------------------------------------
  76. // ctkAbstractPluginFactory methods
  77. //----------------------------------------------------------------------------
  78. template<typename BaseClassType>
  79. ctkAbstractPluginFactory<BaseClassType>::ctkAbstractPluginFactory()
  80. :ctkAbstractFactory<BaseClassType>()
  81. {
  82. }
  83. //----------------------------------------------------------------------------
  84. template<typename BaseClassType>
  85. bool ctkAbstractPluginFactory<BaseClassType>::registerLibrary(const QString& key, const QFileInfo& file)
  86. {
  87. // Check if already registered
  88. if (this->item(key))
  89. {
  90. return false;
  91. }
  92. QSharedPointer<ctkAbstractFactoryItem<BaseClassType> > _item =
  93. QSharedPointer<ctkAbstractFactoryItem<BaseClassType> >(
  94. this->createFactoryPluginItem(file));
  95. if (_item.isNull())
  96. {
  97. return false;
  98. }
  99. _item->setVerbose(this->verbose());
  100. return this->registerItem(key, _item);
  101. }
  102. //----------------------------------------------------------------------------
  103. template<typename BaseClassType>
  104. QString ctkAbstractPluginFactory<BaseClassType>::path(const QString& key)
  105. {
  106. ctkFactoryPluginItem<BaseClassType>* _item =
  107. dynamic_cast<ctkFactoryPluginItem<BaseClassType>*>(this->item(key));
  108. Q_ASSERT(_item);
  109. return _item->path();
  110. }
  111. //----------------------------------------------------------------------------
  112. template<typename BaseClassType>
  113. ctkAbstractFactoryItem<BaseClassType>* ctkAbstractPluginFactory<BaseClassType>::createFactoryPluginItem(const QFileInfo& file)
  114. {
  115. return new ctkFactoryPluginItem<BaseClassType>(file.filePath());
  116. }
  117. #endif