ctkAbstractPluginFactory.tpp 3.6 KB

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